refactor: improve flexibility of the Button API
This commit is contained in:
parent
7f08716575
commit
5965431b57
3 changed files with 33 additions and 30 deletions
|
@ -13,12 +13,24 @@ pub fn Options(context: type) type {
|
|||
pos: Widget.Box = .{ .y = 0, .x = 0, .w = 8, .h = 1 },
|
||||
ctx: context = {},
|
||||
|
||||
on_click: *const fn (ctx: context, button: *State(Context)) void = do_nothing,
|
||||
on_render: ?*const fn (ctx: context, button: *State(Context), theme: *const Widget.Theme) bool = null,
|
||||
on_layout: ?*const fn (ctx: context, button: *State(Context)) Widget.Layout = null,
|
||||
on_click: *const fn (ctx: *context, button: *State(Context)) void = do_nothing,
|
||||
on_render: *const fn (ctx: *context, button: *State(Context), theme: *const Widget.Theme) bool = on_render_default,
|
||||
on_layout: *const fn (ctx: *context, button: *State(Context)) Widget.Layout = on_layout_default,
|
||||
|
||||
pub const Context = context;
|
||||
pub fn do_nothing(_: context, _: *State(Context)) void {}
|
||||
pub fn do_nothing(_: *context, _: *State(Context)) void {}
|
||||
|
||||
pub fn on_render_default(_: *context, self: *State(Context), theme: *const Widget.Theme) bool {
|
||||
tui.set_base_style(&self.plane, " ", if (self.active) theme.scrollbar_active else if (self.hover) theme.scrollbar_hover else theme.scrollbar);
|
||||
self.plane.erase();
|
||||
self.plane.home();
|
||||
_ = self.plane.print(" {s} ", .{self.opts.label}) catch {};
|
||||
return false;
|
||||
}
|
||||
|
||||
pub fn on_layout_default(_: *context, self: *State(Context)) Widget.Layout {
|
||||
return .{ .static = self.opts.label.len + 2 };
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
@ -52,20 +64,11 @@ pub fn State(ctx_type: type) type {
|
|||
}
|
||||
|
||||
pub fn layout(self: *Self) Widget.Layout {
|
||||
return if (self.opts.on_layout) |on_layout|
|
||||
on_layout(self.opts.ctx, self)
|
||||
else
|
||||
.{ .static = self.opts.label.len + 2 };
|
||||
return self.opts.on_layout(&self.opts.ctx, self);
|
||||
}
|
||||
|
||||
pub fn render(self: *Self, theme: *const Widget.Theme) bool {
|
||||
if (self.opts.on_render) |on_render|
|
||||
return on_render(self.opts.ctx, self, theme);
|
||||
tui.set_base_style(&self.plane, " ", if (self.active) theme.scrollbar_active else if (self.hover) theme.scrollbar_hover else theme.scrollbar);
|
||||
self.plane.erase();
|
||||
self.plane.home();
|
||||
_ = self.plane.print(" {s} ", .{self.opts.label}) catch {};
|
||||
return false;
|
||||
return self.opts.on_render(&self.opts.ctx, self, theme);
|
||||
}
|
||||
|
||||
pub fn receive(self: *Self, _: tp.pid_ref, m: tp.message) error{Exit}!bool {
|
||||
|
@ -73,11 +76,11 @@ pub fn State(ctx_type: type) type {
|
|||
self.active = true;
|
||||
return true;
|
||||
} else if (try m.match(.{ "B", nc.event_type.RELEASE, nc.key.BUTTON1, tp.any, tp.any, tp.any, tp.any, tp.any })) {
|
||||
self.opts.on_click(self.opts.ctx, self);
|
||||
self.opts.on_click(&self.opts.ctx, self);
|
||||
self.active = false;
|
||||
return true;
|
||||
} else if (try m.match(.{ "D", nc.event_type.RELEASE, nc.key.BUTTON1, tp.any, tp.any, tp.any, tp.any, tp.any })) {
|
||||
self.opts.on_click(self.opts.ctx, self);
|
||||
self.opts.on_click(&self.opts.ctx, self);
|
||||
self.active = false;
|
||||
return true;
|
||||
} else if (try m.match(.{ "H", tp.extract(&self.hover) })) {
|
||||
|
|
|
@ -23,7 +23,7 @@ pub fn create(a: std.mem.Allocator, parent: Widget) !*Self {
|
|||
return self;
|
||||
}
|
||||
|
||||
pub fn add_item(self: *Self, label: []const u8, on_click: *const fn (_: void, _: *Button.State(void)) void) !void {
|
||||
pub fn add_item(self: *Self, label: []const u8, on_click: *const fn (_: *void, _: *Button.State(void)) void) !void {
|
||||
try self.menu.add(try Button.create({}, self.a, self.menu.parent, .{
|
||||
.on_layout = menu_layout,
|
||||
.label = label,
|
||||
|
@ -55,11 +55,11 @@ pub fn walk(self: *Self, walk_ctx: *anyopaque, f: Widget.WalkFn) bool {
|
|||
return self.menu.walk(walk_ctx, f, &self.menu_widget);
|
||||
}
|
||||
|
||||
fn menu_layout(_: void, _: *Button.State(void)) Widget.Layout {
|
||||
fn menu_layout(_: *void, _: *Button.State(void)) Widget.Layout {
|
||||
return .{ .static = 1 };
|
||||
}
|
||||
|
||||
fn render_menu_item(_: void, button: *Button.State(void), theme: *const Widget.Theme) bool {
|
||||
fn render_menu_item(_: *void, button: *Button.State(void), theme: *const Widget.Theme) bool {
|
||||
tui.set_base_style(&button.plane, " ", if (button.active) theme.editor_cursor else if (button.hover) theme.editor_selection else theme.editor);
|
||||
button.plane.erase();
|
||||
button.plane.home();
|
||||
|
|
|
@ -44,7 +44,7 @@ pub fn create(a: std.mem.Allocator, parent: Widget) !Widget {
|
|||
return w;
|
||||
}
|
||||
|
||||
fn menu_item(self: *Self, label: []const u8, on_click: *const fn (_: void, _: *Button.State(void)) void) !void {
|
||||
fn menu_item(self: *Self, label: []const u8, on_click: *const fn (_: *void, _: *Button.State(void)) void) !void {
|
||||
try self.menu.add(try Button.create({}, self.a, self.parent, .{
|
||||
.on_layout = menu_layout,
|
||||
.label = label,
|
||||
|
@ -78,35 +78,35 @@ pub fn receive(_: *Self, _: tp.pid_ref, m: tp.message) error{Exit}!bool {
|
|||
return false;
|
||||
}
|
||||
|
||||
fn menu_layout(_: void, _: *Button.State(void)) Widget.Layout {
|
||||
fn menu_layout(_: *void, _: *Button.State(void)) Widget.Layout {
|
||||
return .{ .static = 1 };
|
||||
}
|
||||
|
||||
fn menu_action_help(_: void, _: *Button.State(void)) void {
|
||||
fn menu_action_help(_: *void, _: *Button.State(void)) void {
|
||||
command.executeName("open_help", .{}) catch {};
|
||||
}
|
||||
|
||||
fn menu_action_open_file(_: void, _: *Button.State(void)) void {
|
||||
fn menu_action_open_file(_: *void, _: *Button.State(void)) void {
|
||||
command.executeName("enter_open_file_mode", .{}) catch {};
|
||||
}
|
||||
|
||||
fn menu_action_open_recent_file(_: void, _: *Button.State(void)) void {
|
||||
fn menu_action_open_recent_file(_: *void, _: *Button.State(void)) void {
|
||||
tp.self_pid().send(.{ "log", "home", "open recent file not implemented" }) catch {};
|
||||
}
|
||||
|
||||
fn menu_action_open_recent_project(_: void, _: *Button.State(void)) void {
|
||||
fn menu_action_open_recent_project(_: *void, _: *Button.State(void)) void {
|
||||
tp.self_pid().send(.{ "log", "home", "open recent project not implemented" }) catch {};
|
||||
}
|
||||
|
||||
fn menu_action_show_commands(_: void, _: *Button.State(void)) void {
|
||||
fn menu_action_show_commands(_: *void, _: *Button.State(void)) void {
|
||||
tp.self_pid().send(.{ "log", "home", "open command palette not implemented" }) catch {};
|
||||
}
|
||||
|
||||
fn menu_action_open_config(_: void, _: *Button.State(void)) void {
|
||||
fn menu_action_open_config(_: *void, _: *Button.State(void)) void {
|
||||
command.executeName("open_config", .{}) catch {};
|
||||
}
|
||||
|
||||
fn menu_action_quit(_: void, _: *Button.State(void)) void {
|
||||
fn menu_action_quit(_: *void, _: *Button.State(void)) void {
|
||||
command.executeName("quit", .{}) catch {};
|
||||
}
|
||||
|
||||
|
@ -158,7 +158,7 @@ pub fn render(self: *Self, theme: *const Widget.Theme) bool {
|
|||
return true;
|
||||
}
|
||||
|
||||
fn render_menu_item(_: void, button: *Button.State(void), theme: *const Widget.Theme) bool {
|
||||
fn render_menu_item(_: *void, button: *Button.State(void), theme: *const Widget.Theme) bool {
|
||||
tui.set_base_style(&button.plane, " ", if (button.active) theme.editor_cursor else if (button.hover) theme.editor_selection else theme.editor);
|
||||
button.plane.erase();
|
||||
button.plane.home();
|
||||
|
|
Loading…
Add table
Reference in a new issue