refactor: make Widget.get method const
This commit is contained in:
parent
902fc0ab75
commit
c1200ac5bd
5 changed files with 12 additions and 13 deletions
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue