From c1200ac5bde5a78b12aa504f91a9291ac559705d Mon Sep 17 00:00:00 2001 From: CJ van den Berg Date: Tue, 6 Jan 2026 19:22:24 +0100 Subject: [PATCH] refactor: make Widget.get method const --- src/tui/Widget.zig | 10 +++++----- src/tui/WidgetList.zig | 2 +- src/tui/mainview.zig | 6 +++--- src/tui/status/tabs.zig | 2 +- src/tui/tui.zig | 5 ++--- 5 files changed, 12 insertions(+), 13 deletions(-) diff --git a/src/tui/Widget.zig b/src/tui/Widget.zig index 3e209ce..89a0aef 100644 --- a/src/tui/Widget.zig +++ b/src/tui/Widget.zig @@ -51,7 +51,7 @@ pub const VTable = struct { layout: *const fn (ctx: *anyopaque) Layout, subscribe: *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: *const anyopaque, name_: []const u8) ?*const Self, 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, @@ -134,8 +134,8 @@ pub fn to(pimpl: anytype) Self { } }.unsubscribe, .get = struct { - pub fn get(ctx: *anyopaque, name_: []const u8) ?*Self { - return if (comptime @hasDecl(child, "get")) child.get(@as(*child, @ptrCast(@alignCast(ctx))), name_) else null; + pub fn get(ctx: *const anyopaque, name_: []const u8) ?*const Self { + return if (comptime @hasDecl(child, "get")) child.get(@as(*const child, @ptrCast(@alignCast(ctx))), name_) else null; } }.get, .walk = struct { @@ -222,7 +222,7 @@ pub fn unsubscribe(self: Self, h: EventHandler) !void { return self.vtable.unsubscribe(self.ptr, h); } -pub fn get(self: *Self, name_: []const u8) ?*Self { +pub fn get(self: *const Self, name_: []const u8) ?*const Self { var buf: [256]u8 = undefined; return if (std.mem.eql(u8, self.plane.name(&buf), name_)) self @@ -297,7 +297,7 @@ pub fn empty(allocator: Allocator, parent: Plane, layout_: Layout) !Self { } }.unsubscribe, .get = struct { - pub fn get(_: *anyopaque, _: []const u8) ?*Self { + pub fn get(_: *const anyopaque, _: []const u8) ?*const Self { return null; } }.get, diff --git a/src/tui/WidgetList.zig b/src/tui/WidgetList.zig index ba62123..91c38aa 100644 --- a/src/tui/WidgetList.zig +++ b/src/tui/WidgetList.zig @@ -115,7 +115,7 @@ pub fn addP(self: *Self, w_: Widget) !*Widget { return &w.widget; } -pub fn get(self: *Self, name_: []const u8) ?*Widget { +pub fn get(self: *const Self, name_: []const u8) ?*const Widget { for (self.widgets.items) |*w| if (w.widget.get(name_)) |p| return p; diff --git a/src/tui/mainview.zig b/src/tui/mainview.zig index af1ff90..9e16fb8 100644 --- a/src/tui/mainview.zig +++ b/src/tui/mainview.zig @@ -1634,9 +1634,9 @@ fn add_and_activate_view(self: *Self, widget: Widget) !void { if (self.views.get_at(self.active_view)) |view| view.focus(); } -pub fn find_view_for_widget(self: *Self, w_: *Widget) ?usize { +pub fn find_view_for_widget(self: *Self, w_: *const Widget) ?usize { const Ctx = struct { - w: *Widget, + w: *const Widget, fn find(ctx_: *anyopaque, w: *Widget) bool { const ctx = @as(*@This(), @ptrCast(@alignCast(ctx_))); return ctx.w == w; @@ -1648,7 +1648,7 @@ pub fn find_view_for_widget(self: *Self, w_: *Widget) ?usize { return null; } -pub fn focus_view_by_widget(self: *Self, w: *Widget) tui.FocusAction { +pub fn focus_view_by_widget(self: *Self, w: *const Widget) tui.FocusAction { const n = self.find_view_for_widget(w) orelse return .notfound; if (n >= self.views.widgets.items.len) return .notfound; if (n == self.active_view) return .same; diff --git a/src/tui/status/tabs.zig b/src/tui/status/tabs.zig index f17fcff..5a77307 100644 --- a/src/tui/status/tabs.zig +++ b/src/tui/status/tabs.zig @@ -217,7 +217,7 @@ pub const TabBar = struct { self.plane = self.widget_list.plane; } - pub fn get(self: *Self, name: []const u8) ?*Widget { + pub fn get(self: *const Self, name: []const u8) ?*const Widget { return self.widget_list_widget.get(name); } diff --git a/src/tui/tui.zig b/src/tui/tui.zig index c09bc00..c58df9b 100644 --- a/src/tui/tui.zig +++ b/src/tui/tui.zig @@ -788,9 +788,8 @@ fn is_live_widget_ptr(self: *Self, w_: *Widget) bool { pub const FocusAction = enum { same, changed, notfound }; -pub fn set_focus_by_widget(w: *Widget) FocusAction { - const self = current(); - const mv = self.mainview_ orelse return .notfound; +pub fn set_focus_by_widget(w: *const Widget) FocusAction { + const mv = mainview() orelse return .notfound; return mv.focus_view_by_widget(w); }