feat: add Menu widget and use it on home screen

This commit is contained in:
CJ van den Berg 2024-03-07 20:34:15 +01:00
parent 0e71a25a49
commit a7a27fdc89
3 changed files with 174 additions and 84 deletions

View file

@ -7,26 +7,24 @@ const Widget = @import("Widget.zig");
const command = @import("command.zig");
const tui = @import("tui.zig");
parent: nc.Plane,
plane: nc.Plane,
active: bool = false,
hover: bool = false,
opts: Options,
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 = {},
const Self = @This();
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,
pub const Options = struct {
label: []const u8 = "button",
pos: Widget.Box = .{ .y = 0, .x = 0, .w = 8, .h = 1 },
ctx: ?*anyopaque = null,
pub const Context = context;
pub fn do_nothing(_: context, _: *State(Context)) void {}
};
}
on_click: *const fn (ctx: ?*anyopaque, button: *Self) void = do_nothing,
on_render: ?*const fn (ctx: ?*anyopaque, button: *Self, theme: *const Widget.Theme) bool = null,
on_layout: ?*const fn (ctx: ?*anyopaque, button: *Self) Widget.Layout = null,
};
pub fn create(a: std.mem.Allocator, parent: nc.Plane, opts: Options) !Widget {
const self: *Self = try a.create(Self);
pub fn create(ctx: anytype, a: std.mem.Allocator, parent: nc.Plane, opts: Options(@TypeOf(ctx))) !Widget {
const Self = State(@TypeOf(ctx));
const self = try a.create(Self);
var n = try nc.Plane.init(&opts.pos.opts(@typeName(Self)), parent);
errdefer n.deinit();
self.* = .{
@ -37,45 +35,56 @@ pub fn create(a: std.mem.Allocator, parent: nc.Plane, opts: Options) !Widget {
return Widget.to(self);
}
pub fn deinit(self: *Self, a: std.mem.Allocator) void {
self.plane.deinit();
a.destroy(self);
}
pub fn State(ctx_type: type) type {
return struct {
parent: nc.Plane,
plane: nc.Plane,
active: bool = false,
hover: bool = false,
opts: Options(ctx_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 };
}
const Self = @This();
pub const Context = ctx_type;
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;
}
pub fn deinit(self: *Self, a: std.mem.Allocator) void {
self.plane.deinit();
a.destroy(self);
}
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;
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.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.active = false;
return true;
} else if (try m.match(.{ "H", tp.extract(&self.hover) })) {
tui.current().request_mouse_cursor_pointer(self.hover);
return true;
}
return false;
}
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 };
}
pub fn do_nothing(_: ?*anyopaque, _: *Self) void {}
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;
}
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;
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.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.active = false;
return true;
} else if (try m.match(.{ "H", tp.extract(&self.hover) })) {
tui.current().request_mouse_cursor_pointer(self.hover);
return true;
}
return false;
}
};
}