refactor: pass relative click position to button click handlers

This a big refactor just to clean-up type definitions used by Button and Menu.
The goals is to pass the click position as a cursor object.
This commit is contained in:
CJ van den Berg 2025-10-09 19:11:25 +02:00
parent 35ccf7f1df
commit ce87dcfa2b
Signed by: neurocyte
GPG key ID: 8EB1E1BB660E3FB9
21 changed files with 148 additions and 118 deletions

View file

@ -4,6 +4,7 @@ const tp = @import("thespian");
const EventHandler = @import("EventHandler");
const Plane = @import("renderer").Plane;
const input = @import("input");
pub const Cursor = @import("Buffer").Cursor;
const Widget = @import("Widget.zig");
const tui = @import("tui.zig");
@ -14,20 +15,23 @@ 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_click2: *const fn (ctx: *context, button: *State(Context)) void = do_nothing,
on_click3: *const fn (ctx: *context, button: *State(Context)) void = do_nothing,
on_click4: *const fn (ctx: *context, button: *State(Context)) void = do_nothing,
on_click5: *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,
on_click: ClickHandler = do_nothing,
on_click2: ClickHandler = do_nothing,
on_click3: ClickHandler = do_nothing,
on_click4: ClickHandler = do_nothing,
on_click5: ClickHandler = do_nothing,
on_render: *const fn (ctx: *context, button: *ButtonType, theme: *const Widget.Theme) bool = on_render_default,
on_layout: *const fn (ctx: *context, button: *ButtonType) Widget.Layout = on_layout_default,
on_receive: *const fn (ctx: *context, button: *ButtonType, from: tp.pid_ref, m: tp.message) error{Exit}!bool = on_receive_default,
on_event: ?EventHandler = null,
pub const ButtonType = State(Context);
pub const Context = context;
pub fn do_nothing(_: *context, _: *State(Context)) void {}
pub const ClickHandler = *const fn (ctx: *context, button: *ButtonType, pos: Cursor) void;
pub fn on_render_default(_: *context, self: *State(Context), theme: *const Widget.Theme) bool {
pub fn do_nothing(_: *context, _: *ButtonType, _: Cursor) void {}
pub fn on_render_default(_: *context, self: *ButtonType, theme: *const Widget.Theme) bool {
self.plane.set_base_style(if (self.active) theme.scrollbar_active else if (self.hover) theme.scrollbar_hover else theme.scrollbar);
self.plane.erase();
self.plane.home();
@ -35,18 +39,18 @@ 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: *ButtonType) 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 {
pub fn on_receive_default(_: *context, _: *ButtonType, _: tp.pid_ref, _: tp.message) error{Exit}!bool {
return false;
}
};
}
pub fn create(ctx_type: type, allocator: std.mem.Allocator, parent: Plane, opts: Options(ctx_type)) error{OutOfMemory}!*State(ctx_type) {
const Self = State(ctx_type);
pub fn create(ctx_type: type, allocator: std.mem.Allocator, parent: Plane, opts: Options(ctx_type)) error{OutOfMemory}!*Options(ctx_type).ButtonType {
const Self = Options(ctx_type).ButtonType;
const self = try allocator.create(Self);
errdefer allocator.destroy(self);
var n = try Plane.init(&opts.pos.opts(@typeName(Self)), parent);
@ -67,7 +71,7 @@ pub fn create_widget(ctx_type: type, allocator: std.mem.Allocator, parent: Plane
return Widget.to(try create(ctx_type, allocator, parent, opts));
}
pub fn State(ctx_type: type) type {
fn State(ctx_type: type) type {
return struct {
allocator: std.mem.Allocator,
parent: Plane,
@ -110,7 +114,9 @@ pub fn State(ctx_type: type) type {
pub fn receive(self: *Self, from: tp.pid_ref, m: tp.message) error{Exit}!bool {
var btn: input.MouseType = 0;
if (try m.match(.{ "B", input.event.press, tp.extract(&btn), tp.more })) {
var x: c_int = undefined;
var y: c_int = undefined;
if (try m.match(.{ "B", input.event.press, tp.extract(&btn), tp.any, tp.extract(&x), tp.extract(&y), tp.any, tp.any })) {
const btn_enum: input.Mouse = @enumFromInt(btn);
switch (btn_enum) {
input.mouse.BUTTON1 => {
@ -118,14 +124,14 @@ pub fn State(ctx_type: type) type {
tui.need_render();
},
input.mouse.BUTTON4, input.mouse.BUTTON5 => {
self.call_click_handler(btn_enum);
self.call_click_handler(btn_enum, self.to_rel_cursor(x, y));
return true;
},
else => {},
}
return true;
} else if (try m.match(.{ "B", input.event.release, tp.extract(&btn), tp.more })) {
self.call_click_handler(@enumFromInt(btn));
} else if (try m.match(.{ "B", input.event.release, tp.extract(&btn), tp.any, tp.extract(&x), tp.extract(&y), tp.any, tp.any })) {
self.call_click_handler(@enumFromInt(btn), self.to_rel_cursor(x, y));
tui.need_render();
return true;
} else if (try m.match(.{ "D", input.event.press, tp.extract(&btn), tp.more })) {
@ -134,12 +140,12 @@ pub fn State(ctx_type: type) type {
h.send(from, m) catch {};
}
return true;
} else if (try m.match(.{ "D", input.event.release, tp.extract(&btn), tp.more })) {
} else if (try m.match(.{ "D", input.event.release, tp.extract(&btn), tp.any, tp.extract(&x), tp.extract(&y), tp.any, tp.any })) {
if (self.opts.on_event) |h| {
self.active = false;
h.send(from, m) catch {};
}
self.call_click_handler(@enumFromInt(btn));
self.call_click_handler(@enumFromInt(btn), self.to_rel_cursor(x, y));
tui.need_render();
return true;
} else if (try m.match(.{ "H", tp.extract(&self.hover) })) {
@ -150,18 +156,23 @@ pub fn State(ctx_type: type) type {
return self.opts.on_receive(&self.opts.ctx, self, from, m);
}
fn call_click_handler(self: *Self, btn: input.Mouse) void {
fn to_rel_cursor(self: *const Self, abs_x: c_int, abs_y: c_int) Cursor {
const rel_y, const rel_x = self.plane.abs_yx_to_rel(abs_y, abs_x);
return .{ .row = @intCast(rel_y), .col = @intCast(rel_x) };
}
fn call_click_handler(self: *Self, btn: input.Mouse, pos: Cursor) void {
if (btn == input.mouse.BUTTON1) {
if (!self.active) return;
self.active = false;
}
if (!self.hover) return;
switch (btn) {
input.mouse.BUTTON1 => self.opts.on_click(&self.opts.ctx, self),
input.mouse.BUTTON2 => self.opts.on_click2(&self.opts.ctx, self),
input.mouse.BUTTON3 => self.opts.on_click3(&self.opts.ctx, self),
input.mouse.BUTTON4 => self.opts.on_click4(&self.opts.ctx, self),
input.mouse.BUTTON5 => self.opts.on_click5(&self.opts.ctx, self),
input.mouse.BUTTON1 => self.opts.on_click(&self.opts.ctx, self, pos),
input.mouse.BUTTON2 => self.opts.on_click2(&self.opts.ctx, self, pos),
input.mouse.BUTTON3 => self.opts.on_click3(&self.opts.ctx, self, pos),
input.mouse.BUTTON4 => self.opts.on_click4(&self.opts.ctx, self, pos),
input.mouse.BUTTON5 => self.opts.on_click5(&self.opts.ctx, self, pos),
else => {},
}
}