From 2be1a4d149017d3d9eddebb2765f9a7538505beb Mon Sep 17 00:00:00 2001 From: CJ van den Berg Date: Sun, 11 Aug 2024 20:27:08 +0200 Subject: [PATCH] feat: make button widgets forward mouse wheel events --- src/tui/Button.zig | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/src/tui/Button.zig b/src/tui/Button.zig index 792a258..1c17373 100644 --- a/src/tui/Button.zig +++ b/src/tui/Button.zig @@ -18,6 +18,8 @@ pub fn Options(context: type) type { 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, @@ -90,15 +92,24 @@ pub fn State(ctx_type: type) type { pub fn receive(self: *Self, from: tp.pid_ref, m: tp.message) error{Exit}!bool { var btn: u32 = 0; - if (try m.match(.{ "B", event_type.PRESS, tp.extract(&btn), tp.any, tp.any, tp.any, tp.any, tp.any })) { - if (btn == key.BUTTON1) self.active = true; - tui.need_render(); + if (try m.match(.{ "B", event_type.PRESS, tp.extract(&btn), tp.more })) { + switch (btn) { + key.BUTTON1 => { + self.active = true; + tui.need_render(); + }, + key.BUTTON4, key.BUTTON5 => { + self.call_click_handler(btn); + return true; + }, + else => {}, + } return true; - } else if (try m.match(.{ "B", event_type.RELEASE, tp.extract(&btn), tp.any, tp.any, tp.any, tp.any, tp.any })) { + } else if (try m.match(.{ "B", event_type.RELEASE, tp.extract(&btn), tp.more })) { self.call_click_handler(btn); tui.need_render(); return true; - } else if (try m.match(.{ "D", event_type.RELEASE, tp.extract(&btn), tp.any, tp.any, tp.any, tp.any, tp.any })) { + } else if (try m.match(.{ "D", event_type.RELEASE, tp.extract(&btn), tp.more })) { self.call_click_handler(btn); tui.need_render(); return true; @@ -117,6 +128,8 @@ pub fn State(ctx_type: type) type { key.BUTTON1 => self.opts.on_click(&self.opts.ctx, self), key.BUTTON2 => self.opts.on_click2(&self.opts.ctx, self), key.BUTTON3 => self.opts.on_click3(&self.opts.ctx, self), + key.BUTTON4 => self.opts.on_click4(&self.opts.ctx, self), + key.BUTTON5 => self.opts.on_click5(&self.opts.ctx, self), else => {}, } }