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. /// 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 /// The caller must ensure that position is less than children().len and children() is not
/// empty, otherwise, undefined behaviour is invoked. /// empty, otherwise, undefined behaviour is invoked.
/// Returns error.Unsupported if the widget does not support such functionality. /// Returns error.Unsupported if the widget does not support such functionality.
pub fn removeChild(self: *Self, position: usize) !*Widget { pub fn removeChild(self: *Self, position: ?usize) !*Widget {
return statspatch.implcallOptional( const child = try (statspatch.implcallOptional(
self, self,
.ptr, .ptr,
"removeChild", "removeChild",
anyerror!void, anyerror!*Widget,
.{ self, position }, .{ self, position },
) orelse error.Unsupported; ) orelse return error.Unsupported);
try child.unlink();
return child;
} }
/// Returns true if self is a child widget of other. /// Returns true if self is a child widget of other.
@ -169,6 +173,7 @@ fn Prototype(comptime Self: type) type {
/// the widget tree. /// the widget tree.
/// It is safe to deinitialize the subtree after it has been unlinked. /// It is safe to deinitialize the subtree after it has been unlinked.
pub fn unlink(self: *Self) !void { 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 {}); try (statspatch.implcallOptional(self, .ptr, "unlink", anyerror!void, .{self}) orelse {});
self.data.parent = null; 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; _ = selfw;
if (position) |pos| { if (position) |pos| {
const old = self.children.get(pos).widget; const old = self.children.get(pos).widget;