feat: make linenumstate a button that enters goto mode when clicked

This commit is contained in:
CJ van den Berg 2024-04-10 18:41:04 +02:00
parent 28ab5643d9
commit 30d1ac9e8c

View file

@ -5,10 +5,10 @@ const tp = @import("thespian");
const tracy = @import("tracy"); const tracy = @import("tracy");
const Widget = @import("../Widget.zig"); const Widget = @import("../Widget.zig");
const Button = @import("../Button.zig");
const tui = @import("../tui.zig"); const tui = @import("../tui.zig");
const command = @import("../command.zig");
parent: nc.Plane,
plane: nc.Plane,
line: usize = 0, line: usize = 0,
lines: usize = 0, lines: usize = 0,
column: usize = 0, column: usize = 0,
@ -18,35 +18,29 @@ rendered: [:0]const u8 = "",
const Self = @This(); const Self = @This();
pub fn create(a: Allocator, parent: nc.Plane) !Widget { pub fn create(a: Allocator, parent: nc.Plane) !Widget {
const self: *Self = try a.create(Self); return Button.create_widget(Self, a, parent, .{
self.* = try init(parent); .ctx = .{},
return Widget.to(self); .label = "",
.on_click = on_click,
.on_layout = layout,
.on_render = render,
.on_receive = receive,
});
} }
fn init(parent: nc.Plane) !Self { fn on_click(_: *Self, _: *Button.State(Self)) void {
var n = try nc.Plane.init(&(Widget.Box{}).opts(@typeName(Self)), parent); command.executeName("enter_goto_mode", .{}) catch {};
errdefer n.deinit();
return .{
.parent = parent,
.plane = n,
};
} }
pub fn deinit(self: *Self, a: Allocator) void { pub fn layout(self: *Self, _: *Button.State(Self)) Widget.Layout {
self.plane.deinit();
a.destroy(self);
}
pub fn layout(self: *Self) Widget.Layout {
return .{ .static = self.rendered.len }; return .{ .static = self.rendered.len };
} }
pub fn render(self: *Self, theme: *const Widget.Theme) bool { pub fn render(self: *Self, btn: *Button.State(Self), theme: *const Widget.Theme) bool {
tui.set_base_style(&self.plane, " ", theme.statusbar); tui.set_base_style(&btn.plane, " ", if (btn.active) theme.editor_cursor else if (btn.hover) theme.statusbar_hover else theme.statusbar);
self.plane.erase(); btn.plane.erase();
self.plane.home(); btn.plane.home();
_ = self.plane.putstr(self.rendered) catch {}; _ = btn.plane.putstr(self.rendered) catch {};
return false; return false;
} }
@ -58,7 +52,7 @@ fn format(self: *Self) void {
self.buf[self.rendered.len] = 0; self.buf[self.rendered.len] = 0;
} }
pub fn receive(self: *Self, _: tp.pid_ref, m: tp.message) error{Exit}!bool { pub fn receive(self: *Self, _: *Button.State(Self), _: tp.pid_ref, m: tp.message) error{Exit}!bool {
if (try m.match(.{ "E", "pos", tp.extract(&self.lines), tp.extract(&self.line), tp.extract(&self.column) })) { if (try m.match(.{ "E", "pos", tp.extract(&self.lines), tp.extract(&self.line), tp.extract(&self.column) })) {
self.format(); self.format();
} else if (try m.match(.{ "E", "close" })) { } else if (try m.match(.{ "E", "close" })) {