refactor: improve button widget api
This commit is contained in:
parent
53554be4b0
commit
0053b80d81
5 changed files with 61 additions and 40 deletions
|
@ -12,14 +12,17 @@ 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 = 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_click2: *const fn (ctx: *context, button: *State(Context)) void = do_nothing,
|
||||
on_click3: *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_receive: *const fn (ctx: *context, button: *State(Context), from: tp.pid_ref, m: tp.message) error{Exit}!bool = on_receive_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();
|
||||
|
@ -27,13 +30,17 @@ 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 on_receive_default(_: *context, _: *State(Context), _: tp.pid_ref, _: tp.message) error{Exit}!bool {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
pub fn create(ctx_type: type, a: std.mem.Allocator, parent: nc.Plane, opts: Options(ctx_type)) !Widget {
|
||||
pub fn create(ctx_type: type, a: std.mem.Allocator, parent: nc.Plane, opts: Options(ctx_type)) !*State(ctx_type) {
|
||||
const Self = State(ctx_type);
|
||||
const self = try a.create(Self);
|
||||
var n = try nc.Plane.init(&opts.pos.opts(@typeName(Self)), parent);
|
||||
|
@ -46,7 +53,11 @@ pub fn create(ctx_type: type, a: std.mem.Allocator, parent: nc.Plane, opts: Opti
|
|||
};
|
||||
try self.label.appendSlice(self.opts.label);
|
||||
self.opts.label = self.label.items;
|
||||
return Widget.to(self);
|
||||
return self;
|
||||
}
|
||||
|
||||
pub fn create_widget(ctx_type: type, a: std.mem.Allocator, parent: nc.Plane, opts: Options(ctx_type)) !Widget {
|
||||
return Widget.to(try create(ctx_type, a, parent, opts));
|
||||
}
|
||||
|
||||
pub fn State(ctx_type: type) type {
|
||||
|
@ -68,25 +79,26 @@ 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 })) {
|
||||
pub fn receive(self: *Self, from: tp.pid_ref, m: tp.message) error{Exit}!bool {
|
||||
var btn: u32 = 0;
|
||||
if (try m.match(.{ "B", nc.event_type.PRESS, tp.extract(&btn), 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);
|
||||
} else if (try m.match(.{ "B", nc.event_type.RELEASE, tp.extract(&btn), tp.any, tp.any, tp.any, tp.any, tp.any })) {
|
||||
self.call_click_handler(btn);
|
||||
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);
|
||||
} else if (try m.match(.{ "D", nc.event_type.RELEASE, tp.extract(&btn), tp.any, tp.any, tp.any, tp.any, tp.any })) {
|
||||
self.call_click_handler(btn);
|
||||
self.active = false;
|
||||
tui.need_render();
|
||||
return true;
|
||||
|
@ -95,7 +107,16 @@ pub fn State(ctx_type: type) type {
|
|||
tui.need_render();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
return self.opts.on_receive(&self.opts.ctx, self, from, m);
|
||||
}
|
||||
|
||||
fn call_click_handler(self: *Self, btn: u32) void {
|
||||
switch (btn) {
|
||||
nc.key.BUTTON1 => self.opts.on_click(&self.opts.ctx, self),
|
||||
nc.key.BUTTON2 => self.opts.on_click2(&self.opts.ctx, self),
|
||||
nc.key.BUTTON3 => self.opts.on_click3(&self.opts.ctx, self),
|
||||
else => {},
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue