1
0
Fork 0

fix: removeChild works properly now

closes #12
This commit is contained in:
LordMZTE 2024-01-30 15:16:53 +01:00
parent ad388687cd
commit f925e5736c
Signed by: LordMZTE
GPG key ID: B64802DC33A64FF6
2 changed files with 11 additions and 6 deletions

View file

@ -135,18 +135,22 @@ fn Prototype(comptime Self: type) type {
}
/// Removes the child at position or the last child if position is null.
/// The function then returns the removed child.
/// The function then unlinks and returns the removed child.
/// The caller must ensure that position is less than children().len and children() is not
/// empty, otherwise, undefined behaviour is invoked.
/// Returns error.Unsupported if the widget does not support such functionality.
pub fn removeChild(self: *Self, position: usize) !*Widget {
return statspatch.implcallOptional(
pub fn removeChild(self: *Self, position: ?usize) !*Widget {
const child = try (statspatch.implcallOptional(
self,
.ptr,
"removeChild",
anyerror!void,
anyerror!*Widget,
.{ self, position },
) orelse error.Unsupported;
) orelse return error.Unsupported);
try child.unlink();
return child;
}
/// Returns true if self is a child widget of other.
@ -169,6 +173,7 @@ fn Prototype(comptime Self: type) type {
/// the widget tree.
/// It is safe to deinitialize the subtree after it has been unlinked.
pub fn unlink(self: *Self) !void {
zenolith.log.debug("child {s}@{x} unlinked", .{ @tagName(self.u), @intFromPtr(self) });
try (statspatch.implcallOptional(self, .ptr, "unlink", anyerror!void, .{self}) orelse {});
self.data.parent = null;

View file

@ -249,7 +249,7 @@ pub fn addChildPositioned(
}
}
pub fn removeChild(self: *Box, selfw: *Widget, position: ?usize) void {
pub fn removeChild(self: *Box, selfw: *Widget, position: ?usize) *Widget {
_ = selfw;
if (position) |pos| {
const old = self.children.get(pos).widget;