refactor: add focus/unfocus to widget vtable

This commit is contained in:
CJ van den Berg 2025-11-06 18:16:16 +01:00
parent 70cc191c4a
commit 3437f4fd20
Signed by: neurocyte
GPG key ID: 8EB1E1BB660E3FB9
3 changed files with 42 additions and 0 deletions

View file

@ -53,6 +53,8 @@ pub const VTable = struct {
unsubscribe: *const fn (ctx: *anyopaque, h: EventHandler) error{NotSupported}!void, unsubscribe: *const fn (ctx: *anyopaque, h: EventHandler) error{NotSupported}!void,
get: *const fn (ctx: *anyopaque, name_: []const u8) ?*Self, get: *const fn (ctx: *anyopaque, name_: []const u8) ?*Self,
walk: *const fn (ctx: *anyopaque, walk_ctx: *anyopaque, f: WalkFn, self_widget: *Self) bool, walk: *const fn (ctx: *anyopaque, walk_ctx: *anyopaque, f: WalkFn, self_widget: *Self) bool,
focus: *const fn (ctx: *anyopaque) void,
unfocus: *const fn (ctx: *anyopaque) void,
hover: *const fn (ctx: *anyopaque) bool, hover: *const fn (ctx: *anyopaque) bool,
type_name: []const u8, type_name: []const u8,
}; };
@ -141,6 +143,16 @@ pub fn to(pimpl: anytype) Self {
return if (comptime @hasDecl(child, "walk")) child.walk(@as(*child, @ptrCast(@alignCast(ctx))), walk_ctx, f, self) else false; return if (comptime @hasDecl(child, "walk")) child.walk(@as(*child, @ptrCast(@alignCast(ctx))), walk_ctx, f, self) else false;
} }
}.walk, }.walk,
.focus = struct {
pub fn focus(ctx: *anyopaque) void {
if (comptime @hasDecl(child, "focus")) @as(*child, @ptrCast(@alignCast(ctx))).focus();
}
}.focus,
.unfocus = struct {
pub fn unfocus(ctx: *anyopaque) void {
if (comptime @hasDecl(child, "unfocus")) @as(*child, @ptrCast(@alignCast(ctx))).unfocus();
}
}.unfocus,
.hover = struct { .hover = struct {
pub fn hover(ctx: *anyopaque) bool { pub fn hover(ctx: *anyopaque) bool {
return if (comptime @hasField(child, "hover")) @as(*child, @ptrCast(@alignCast(ctx))).hover else false; return if (comptime @hasField(child, "hover")) @as(*child, @ptrCast(@alignCast(ctx))).hover else false;
@ -222,6 +234,14 @@ pub fn walk(self: *Self, walk_ctx: *anyopaque, f: WalkFn) bool {
return if (self.vtable.walk(self.ptr, walk_ctx, f, self)) true else f(walk_ctx, self); return if (self.vtable.walk(self.ptr, walk_ctx, f, self)) true else f(walk_ctx, self);
} }
pub fn focus(self: *Self) void {
self.vtable.focus(self.ptr);
}
pub fn unfocus(self: *Self) void {
self.vtable.unfocus(self.ptr);
}
pub fn hover(self: *Self) bool { pub fn hover(self: *Self) bool {
return self.vtable.hover(self.ptr); return self.vtable.hover(self.ptr);
} }
@ -286,6 +306,12 @@ pub fn empty(allocator: Allocator, parent: Plane, layout_: Layout) !Self {
return false; return false;
} }
}.walk, }.walk,
.focus = struct {
pub fn focus(_: *anyopaque) void {}
}.focus,
.unfocus = struct {
pub fn unfocus(_: *anyopaque) void {}
}.unfocus,
.hover = struct { .hover = struct {
pub fn hover(_: *anyopaque) bool { pub fn hover(_: *anyopaque) bool {
return false; return false;

View file

@ -404,6 +404,14 @@ pub fn walk(self: *Self, ctx: *anyopaque, f: Widget.WalkFn, self_w: *Widget) boo
return f(ctx, self_w); return f(ctx, self_w);
} }
pub fn focus(self: *Self) void {
for (self.widgets.items) |*w| w.widget.focus();
}
pub fn unfocus(self: *Self) void {
for (self.widgets.items) |*w| w.widget.unfocus();
}
pub fn hover(self: *Self) bool { pub fn hover(self: *Self) bool {
for (self.widgets.items) |*w| if (w.widget.hover()) return true; for (self.widgets.items) |*w| if (w.widget.hover()) return true;
return false; return false;

View file

@ -100,3 +100,11 @@ pub fn walk(self: *Self, walk_ctx: *anyopaque, f: Widget.WalkFn) bool {
} }
return false; return false;
} }
pub fn focus(self: *Self) void {
for (self.widgets.items) |*w| w.widget.focus();
}
pub fn unfocus(self: *Self) void {
for (self.widgets.items) |*w| w.widget.unfocus();
}