feat: make modestate a clickable button that changes input modes

This commit is contained in:
CJ van den Berg 2024-03-08 22:19:02 +01:00
parent 5965431b57
commit c702f5ea5d

View file

@ -7,37 +7,22 @@ const root = @import("root");
const Buffer = @import("Buffer");
const Widget = @import("../Widget.zig");
const Menu = @import("../Menu.zig");
const Button = @import("../Button.zig");
const command = @import("../command.zig");
const ed = @import("../editor.zig");
const tui = @import("../tui.zig");
parent: nc.Plane,
plane: nc.Plane,
const Self = @This();
pub fn create(a: Allocator, parent: nc.Plane) !Widget {
const self: *Self = try a.create(Self);
self.* = try init(parent);
return Widget.to(self);
return Button.create({}, a, parent, .{
.label = tui.get_mode(),
.on_click = on_click,
.on_layout = layout,
.on_render = render,
});
}
fn init(parent: nc.Plane) !Self {
var n = try nc.Plane.init(&(Widget.Box{}).opts(@typeName(Self)), parent);
errdefer n.deinit();
return .{
.parent = parent,
.plane = n,
};
}
pub fn deinit(self: *Self, a: Allocator) void {
self.plane.deinit();
a.destroy(self);
}
pub fn layout(_: *Self) Widget.Layout {
pub fn layout(_: *void, _: *Button.State(void)) Widget.Layout {
const name = tui.get_mode();
const width = Buffer.egc_chunk_width(name, 0);
const padding: usize = if (is_mini_mode()) 3 else 2;
@ -48,20 +33,24 @@ fn is_mini_mode() bool {
return if (tui.current().mini_mode) |_| true else false;
}
pub fn render(self: *Self, theme: *const Widget.Theme) bool {
tui.set_base_style(&self.plane, " ", theme.statusbar_hover);
pub fn render(state: *void, self: *Button.State(void), theme: *const Widget.Theme) bool {
tui.set_base_style(&self.plane, " ", if (self.active) theme.editor_cursor else if (self.hover) theme.editor_selection else theme.statusbar_hover);
self.plane.on_styles(nc.style.bold);
self.plane.erase();
self.plane.home();
var buf: [31:0]u8 = undefined;
_ = self.plane.putstr(std.fmt.bufPrintZ(&buf, " {s} ", .{tui.get_mode()}) catch return false) catch {};
if (is_mini_mode())
self.render_separator(theme);
render_separator(state, self, theme);
return false;
}
fn render_separator(self: *Self, theme: *const Widget.Theme) void {
fn render_separator(_: *void, self: *Button.State(void), theme: *const Widget.Theme) void {
if (theme.statusbar_hover.bg) |bg| self.plane.set_fg_rgb(bg) catch {};
if (theme.statusbar.bg) |bg| self.plane.set_bg_rgb(bg) catch {};
_ = self.plane.putstr("") catch {};
}
fn on_click(_: *void, _: *Button.State(void)) void {
command.executeName("toggle_input_mode", .{}) catch {};
}