refactor: make Widget.get method const

This commit is contained in:
CJ van den Berg 2026-01-06 19:22:24 +01:00
parent 902fc0ab75
commit c1200ac5bd
Signed by: neurocyte
GPG key ID: 8EB1E1BB660E3FB9
5 changed files with 12 additions and 13 deletions

View file

@ -51,7 +51,7 @@ pub const VTable = struct {
layout: *const fn (ctx: *anyopaque) Layout, layout: *const fn (ctx: *anyopaque) Layout,
subscribe: *const fn (ctx: *anyopaque, h: EventHandler) error{NotSupported}!void, subscribe: *const fn (ctx: *anyopaque, h: EventHandler) error{NotSupported}!void,
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: *const anyopaque, name_: []const u8) ?*const 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, focus: *const fn (ctx: *anyopaque) void,
unfocus: *const fn (ctx: *anyopaque) void, unfocus: *const fn (ctx: *anyopaque) void,
@ -134,8 +134,8 @@ pub fn to(pimpl: anytype) Self {
} }
}.unsubscribe, }.unsubscribe,
.get = struct { .get = struct {
pub fn get(ctx: *anyopaque, name_: []const u8) ?*Self { pub fn get(ctx: *const anyopaque, name_: []const u8) ?*const Self {
return if (comptime @hasDecl(child, "get")) child.get(@as(*child, @ptrCast(@alignCast(ctx))), name_) else null; return if (comptime @hasDecl(child, "get")) child.get(@as(*const child, @ptrCast(@alignCast(ctx))), name_) else null;
} }
}.get, }.get,
.walk = struct { .walk = struct {
@ -222,7 +222,7 @@ pub fn unsubscribe(self: Self, h: EventHandler) !void {
return self.vtable.unsubscribe(self.ptr, h); 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; var buf: [256]u8 = undefined;
return if (std.mem.eql(u8, self.plane.name(&buf), name_)) return if (std.mem.eql(u8, self.plane.name(&buf), name_))
self self
@ -297,7 +297,7 @@ pub fn empty(allocator: Allocator, parent: Plane, layout_: Layout) !Self {
} }
}.unsubscribe, }.unsubscribe,
.get = struct { .get = struct {
pub fn get(_: *anyopaque, _: []const u8) ?*Self { pub fn get(_: *const anyopaque, _: []const u8) ?*const Self {
return null; return null;
} }
}.get, }.get,

View file

@ -115,7 +115,7 @@ pub fn addP(self: *Self, w_: Widget) !*Widget {
return &w.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| for (self.widgets.items) |*w|
if (w.widget.get(name_)) |p| if (w.widget.get(name_)) |p|
return p; return p;

View file

@ -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(); 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 { const Ctx = struct {
w: *Widget, w: *const Widget,
fn find(ctx_: *anyopaque, w: *Widget) bool { fn find(ctx_: *anyopaque, w: *Widget) bool {
const ctx = @as(*@This(), @ptrCast(@alignCast(ctx_))); const ctx = @as(*@This(), @ptrCast(@alignCast(ctx_)));
return ctx.w == w; return ctx.w == w;
@ -1648,7 +1648,7 @@ pub fn find_view_for_widget(self: *Self, w_: *Widget) ?usize {
return null; 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; const n = self.find_view_for_widget(w) orelse return .notfound;
if (n >= self.views.widgets.items.len) return .notfound; if (n >= self.views.widgets.items.len) return .notfound;
if (n == self.active_view) return .same; if (n == self.active_view) return .same;

View file

@ -217,7 +217,7 @@ pub const TabBar = struct {
self.plane = self.widget_list.plane; 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); return self.widget_list_widget.get(name);
} }

View file

@ -788,9 +788,8 @@ fn is_live_widget_ptr(self: *Self, w_: *Widget) bool {
pub const FocusAction = enum { same, changed, notfound }; pub const FocusAction = enum { same, changed, notfound };
pub fn set_focus_by_widget(w: *Widget) FocusAction { pub fn set_focus_by_widget(w: *const Widget) FocusAction {
const self = current(); const mv = mainview() orelse return .notfound;
const mv = self.mainview_ orelse return .notfound;
return mv.focus_view_by_widget(w); return mv.focus_view_by_widget(w);
} }