Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return null instead of throwing of Widget.child does not exist #692

Merged
merged 2 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions src/dlangui/core/collections.d
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ module dlangui.core.collections;

import std.algorithm;

/**
/**
Array based collection of items.

Retains item order when during add/remove operations.
Expand All @@ -62,10 +62,10 @@ struct Collection(T, bool ownItems = false) {
@property void size(size_t newSize) {
if (_len > newSize)
length = newSize; // shrink
_items.length = newSize;
_items.length = newSize;
}
/// returns number of items in collection
@property void length(size_t newSize) {
@property void length(size_t newSize) {
if (newSize < _len) {
// shrink
static if (is(T == class) || is(T == struct)) {
Expand Down Expand Up @@ -248,6 +248,13 @@ struct ObjectList(T) {
assert(index >= 0 && index < _count, "child index out of range");
return _list[index];
}
/// get item by index. Returns null if item not found
inout(T) tryGet(int index) @safe inout {
if (index < 0 || index >= _count) {
return null;
}
return _list[index];
}
/// get item by index
T opIndex(int index) @safe {
return get(index);
Expand Down
2 changes: 1 addition & 1 deletion src/dlangui/widgets/tree.d
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ class TreeItem {
/// returns number of children of this widget
@property int childCount() { return _children.count; }
/// returns child by index
TreeItem child(int index) { return _children.get(index); }
TreeItem child(int index) { return _children.tryGet(index); }
/// adds child, returns added item
TreeItem addChild(TreeItem item, int index = -1) {
TreeItem res = _children.insert(item, index).parent(this).level(_level + 1);
Expand Down
2 changes: 1 addition & 1 deletion src/dlangui/widgets/widget.d
Original file line number Diff line number Diff line change
Expand Up @@ -1821,7 +1821,7 @@ class WidgetGroup : Widget {
/// returns number of children of this widget
@property override int childCount() const { return _children.count; }
/// returns child by index
override inout(Widget) child(int index) inout { return _children.get(index); }
override inout(Widget) child(int index) inout { return _children.tryGet(index); }
/// adds child, returns added item
override Widget addChild(Widget item) { return _children.add(item).parent(this); }
/// inserts child at given index, returns inserted item
Expand Down