mirror of https://github.com/buggins/dlangui.git
Return null instead of throwing of Widget.child does not exist
This commit is contained in:
parent
3c38f32e79
commit
a22f2aabd0
|
@ -248,6 +248,17 @@ struct ObjectList(T) {
|
||||||
assert(index >= 0 && index < _count, "child index out of range");
|
assert(index >= 0 && index < _count, "child index out of range");
|
||||||
return _list[index];
|
return _list[index];
|
||||||
}
|
}
|
||||||
|
/// get item by index. Returns null if item not found
|
||||||
|
inout(T) tryGet(int index) @safe inout nothrow {
|
||||||
|
try {
|
||||||
|
if (index < 0 || index >= _count) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return _list[index];
|
||||||
|
} catch (Exception e) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
/// get item by index
|
/// get item by index
|
||||||
T opIndex(int index) @safe {
|
T opIndex(int index) @safe {
|
||||||
return get(index);
|
return get(index);
|
||||||
|
|
|
@ -279,7 +279,7 @@ class TreeItem {
|
||||||
/// returns number of children of this widget
|
/// returns number of children of this widget
|
||||||
@property int childCount() { return _children.count; }
|
@property int childCount() { return _children.count; }
|
||||||
/// returns child by index
|
/// 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
|
/// adds child, returns added item
|
||||||
TreeItem addChild(TreeItem item, int index = -1) {
|
TreeItem addChild(TreeItem item, int index = -1) {
|
||||||
TreeItem res = _children.insert(item, index).parent(this).level(_level + 1);
|
TreeItem res = _children.insert(item, index).parent(this).level(_level + 1);
|
||||||
|
|
|
@ -1821,7 +1821,7 @@ class WidgetGroup : Widget {
|
||||||
/// returns number of children of this widget
|
/// returns number of children of this widget
|
||||||
@property override int childCount() const { return _children.count; }
|
@property override int childCount() const { return _children.count; }
|
||||||
/// returns child by index
|
/// 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
|
/// adds child, returns added item
|
||||||
override Widget addChild(Widget item) { return _children.add(item).parent(this); }
|
override Widget addChild(Widget item) { return _children.add(item).parent(this); }
|
||||||
/// inserts child at given index, returns inserted item
|
/// inserts child at given index, returns inserted item
|
||||||
|
|
Loading…
Reference in New Issue