refactor: button, menu and widget stack apis
This commit is contained in:
parent
dcd9e119da
commit
2f9a0e2eb0
9 changed files with 348 additions and 130 deletions
|
@ -11,16 +11,16 @@ pub fn Options(context: type) type {
|
|||
return struct {
|
||||
label: []const u8 = "button",
|
||||
pos: Widget.Box = .{ .y = 0, .x = 0, .w = 8, .h = 1 },
|
||||
ctx: context = {},
|
||||
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 = on_render_default,
|
||||
on_layout: *const fn (ctx: *context, button: *State(Context)) Widget.Layout = on_layout_default,
|
||||
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 {
|
||||
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();
|
||||
|
@ -28,14 +28,14 @@ pub fn Options(context: type) type {
|
|||
return false;
|
||||
}
|
||||
|
||||
pub fn on_layout_default(_: *context, self: *State(Context)) Widget.Layout {
|
||||
pub fn on_layout_default(_: context, self: *State(Context)) Widget.Layout {
|
||||
return .{ .static = self.opts.label.len + 2 };
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
pub fn create(ctx: anytype, a: std.mem.Allocator, parent: nc.Plane, opts: Options(@TypeOf(ctx))) !Widget {
|
||||
const Self = State(@TypeOf(ctx));
|
||||
pub fn create(ctx_type: type, a: std.mem.Allocator, parent: nc.Plane, opts: Options(ctx_type)) !Widget {
|
||||
const Self = State(ctx_type);
|
||||
const self = try a.create(Self);
|
||||
var n = try nc.Plane.init(&opts.pos.opts(@typeName(Self)), parent);
|
||||
errdefer n.deinit();
|
||||
|
@ -64,27 +64,31 @@ pub fn State(ctx_type: type) type {
|
|||
}
|
||||
|
||||
pub fn layout(self: *Self) Widget.Layout {
|
||||
return self.opts.on_layout(&self.opts.ctx, self);
|
||||
return self.opts.on_layout(self.opts.ctx, self);
|
||||
}
|
||||
|
||||
pub fn render(self: *Self, theme: *const Widget.Theme) bool {
|
||||
return self.opts.on_render(&self.opts.ctx, self, theme);
|
||||
return self.opts.on_render(self.opts.ctx, self, theme);
|
||||
}
|
||||
|
||||
pub fn receive(self: *Self, _: tp.pid_ref, m: tp.message) error{Exit}!bool {
|
||||
if (try m.match(.{ "B", nc.event_type.PRESS, nc.key.BUTTON1, tp.any, tp.any, tp.any, tp.any, tp.any })) {
|
||||
self.active = true;
|
||||
tui.need_render();
|
||||
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;
|
||||
tui.need_render();
|
||||
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;
|
||||
tui.need_render();
|
||||
return true;
|
||||
} else if (try m.match(.{ "H", tp.extract(&self.hover) })) {
|
||||
tui.current().request_mouse_cursor_pointer(self.hover);
|
||||
tui.need_render();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue