fix: store button click locations as signed values

This commit is contained in:
CJ van den Berg 2025-10-10 16:05:50 +02:00
parent e6cc1c35f9
commit 25a719382f
Signed by: neurocyte
GPG key ID: 8EB1E1BB660E3FB9
22 changed files with 36 additions and 36 deletions

View file

@ -4,7 +4,6 @@ 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");
@ -27,9 +26,9 @@ pub fn Options(context: type) type {
pub const ButtonType = State(Context);
pub const Context = context;
pub const ClickHandler = *const fn (ctx: *context, button: *ButtonType, pos: Cursor) void;
pub const ClickHandler = *const fn (ctx: *context, button: *ButtonType, pos: Widget.Pos) void;
pub fn do_nothing(_: *context, _: *ButtonType, _: Cursor) void {}
pub fn do_nothing(_: *context, _: *ButtonType, _: Widget.Pos) 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);
@ -156,12 +155,12 @@ fn State(ctx_type: type) type {
return self.opts.on_receive(&self.opts.ctx, self, from, m);
}
fn to_rel_cursor(self: *const Self, abs_x: c_int, abs_y: c_int) Cursor {
fn to_rel_cursor(self: *const Self, abs_x: c_int, abs_y: c_int) Widget.Pos {
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) };
return .{ .y = @intCast(rel_y), .x = @intCast(rel_x) };
}
fn call_click_handler(self: *Self, btn: input.Mouse, pos: Cursor) void {
fn call_click_handler(self: *Self, btn: input.Mouse, pos: Widget.Pos) void {
if (btn == input.mouse.BUTTON1) {
if (!self.active) return;
self.active = false;

View file

@ -31,7 +31,7 @@ pub fn Options(context: type) type {
pub const ButtonClickHandler = Button.Options(*MenuType).ClickHandler;
pub const ClickHandler = *const fn (ctx: context, button: *ButtonType) void;
pub fn do_nothing(_: context, _: *ButtonType) void {}
pub fn do_nothing_click(_: **MenuType, _: *ButtonType, _: Button.Cursor) void {}
pub fn do_nothing_click(_: **MenuType, _: *ButtonType, _: Widget.Pos) void {}
pub fn on_render_default(_: context, button: *ButtonType, theme: *const Widget.Theme, selected: bool) bool {
const style_base = theme.editor;

View file

@ -7,6 +7,7 @@ const EventHandler = @import("EventHandler");
const tui = @import("tui.zig");
pub const Box = @import("Box.zig");
pub const Pos = struct { y: i32 = 0, x: i32 = 0 };
pub const Theme = @import("theme");
pub const themes = @import("themes").themes;
pub const scopes = @import("themes").scopes;

View file

@ -197,7 +197,7 @@ fn update_scrollbar(self: *Self) void {
scrollbar.set(@intCast(self.entries.items.len), @intCast(self.view_rows), @intCast(self.view_pos));
}
fn mouse_click_button4(menu: **MenuType, _: *ButtonType, _: Button.Cursor) void {
fn mouse_click_button4(menu: **MenuType, _: *ButtonType, _: Widget.Pos) void {
const self = &menu.*.opts.ctx.*;
self.selected = if (self.menu.selected) |sel_| sel_ + self.view_pos else self.selected;
if (self.view_pos < Menu.scroll_lines) {
@ -209,7 +209,7 @@ fn mouse_click_button4(menu: **MenuType, _: *ButtonType, _: Button.Cursor) void
self.update_scrollbar();
}
fn mouse_click_button5(menu: **MenuType, _: *ButtonType, _: Button.Cursor) void {
fn mouse_click_button5(menu: **MenuType, _: *ButtonType, _: Widget.Pos) void {
const self = &menu.*.opts.ctx.*;
self.selected = if (self.menu.selected) |sel_| sel_ + self.view_pos else self.selected;
if (self.view_pos < @max(self.entries.items.len, self.view_rows) - self.view_rows)
@ -228,7 +228,7 @@ fn update_selected(self: *Self) void {
}
}
fn handle_menu_action(menu: **MenuType, button: *ButtonType, _: Button.Cursor) void {
fn handle_menu_action(menu: **MenuType, button: *ButtonType, _: Widget.Pos) void {
const self = menu.*.opts.ctx;
var idx: usize = undefined;
var iter = button.opts.label;

View file

@ -259,7 +259,7 @@ fn menu_on_render(self: *Self, button: *ButtonType, theme: *const Widget.Theme,
return false;
}
fn menu_action(_: **Menu.State(*Self), button: *ButtonType, _: Button.Cursor) void {
fn menu_action(_: **Menu.State(*Self), button: *ButtonType, _: Widget.Pos) void {
var description: []const u8 = undefined;
var hint: []const u8 = undefined;
var command_name: []const u8 = undefined;

View file

@ -58,7 +58,7 @@ pub fn on_render_menu(_: *Type, button: *Type.ButtonType, theme: *const Widget.T
return tui.render_file_item_cbor(&button.plane, button.opts.label, button.active, selected, button.hover, theme);
}
fn select(menu: **Type.MenuType, button: *Type.ButtonType, _: Type.Cursor) void {
fn select(menu: **Type.MenuType, button: *Type.ButtonType, _: Type.Pos) void {
var file_path: []const u8 = undefined;
var iter = button.opts.label;
if (!(cbor.matchString(&iter, &file_path) catch false)) return;

View file

@ -52,7 +52,7 @@ pub fn add_menu_entry(palette: *Type, entry: *Entry, matches: ?[]const usize) !v
palette.items += 1;
}
fn select(menu: **Type.MenuType, button: *Type.ButtonType, _: Type.Cursor) void {
fn select(menu: **Type.MenuType, button: *Type.ButtonType, _: Type.Pos) void {
var unused: []const u8 = undefined;
var command_id: command.ID = undefined;
var iter = button.opts.label;

View file

@ -146,7 +146,7 @@ fn get_replace_selection(replace: Buffer.Selection) ?Buffer.Selection {
replace;
}
fn select(menu: **Type.MenuType, button: *Type.ButtonType, _: Type.Cursor) void {
fn select(menu: **Type.MenuType, button: *Type.ButtonType, _: Type.Pos) void {
const label_, _, _, _, _ = get_values(button.opts.label);
tp.self_pid().send(.{ "cmd", "exit_overlay_mode" }) catch |e| menu.*.opts.ctx.logger.err(module_name, e);
tp.self_pid().send(.{ "cmd", "insert_chars", .{label_} }) catch |e| menu.*.opts.ctx.logger.err(module_name, e);

View file

@ -116,7 +116,7 @@ pub fn Variant(comptime command: []const u8, comptime label_: []const u8, allow_
return false;
}
fn select(menu: **Type.MenuType, button: *Type.ButtonType, _: Type.Cursor) void {
fn select(menu: **Type.MenuType, button: *Type.ButtonType, _: Type.Pos) void {
var description_: []const u8 = undefined;
var icon_: []const u8 = undefined;
var color: u24 = undefined;

View file

@ -56,7 +56,7 @@ pub fn add_menu_entry(palette: *Type, entry: *Entry, matches: ?[]const usize) !v
palette.items += 1;
}
fn select(menu: **Type.MenuType, button: *Type.ButtonType, _: Type.Cursor) void {
fn select(menu: **Type.MenuType, button: *Type.ButtonType, _: Type.Pos) void {
var label_: []const u8 = undefined;
var iter = button.opts.label;
if (!(cbor.matchString(&iter, &label_) catch false)) return;

View file

@ -69,7 +69,7 @@ pub fn add_menu_entry(palette: *Type, entry: *Entry, matches: ?[]const usize) !v
palette.items += 1;
}
fn select(menu: **Type.MenuType, button: *Type.ButtonType, _: Type.Cursor) void {
fn select(menu: **Type.MenuType, button: *Type.ButtonType, _: Type.Pos) void {
var unused: []const u8 = undefined;
var command_id: command.ID = undefined;
var iter = button.opts.label;

View file

@ -125,7 +125,7 @@ fn do_resize(self: *Self) void {
self.menu.resize(self.prepare_resize());
}
fn menu_action_open_file(menu: **MenuType, button: *ButtonType, _: Button.Cursor) void {
fn menu_action_open_file(menu: **MenuType, button: *ButtonType, _: Widget.Pos) void {
var file_path: []const u8 = undefined;
var iter = button.opts.label;
if (!(cbor.matchString(&iter, &file_path) catch false)) return;

View file

@ -63,7 +63,7 @@ pub fn add_menu_entry(palette: *Type, entry: *Entry, matches: ?[]const usize) !v
palette.items += 1;
}
fn select(menu: **Type.MenuType, button: *Type.ButtonType, _: Type.Cursor) void {
fn select(menu: **Type.MenuType, button: *Type.ButtonType, _: Type.Pos) void {
var name_: []const u8 = undefined;
var iter = button.opts.label;
if (!(cbor.matchString(&iter, &name_) catch false)) return;

View file

@ -48,7 +48,7 @@ pub fn Create(options: type) type {
pub const MenuType = Menu.Options(*Self).MenuType;
pub const ButtonType = MenuType.ButtonType;
pub const Cursor = Button.Cursor;
pub const Pos = Widget.Pos;
pub fn create(allocator: std.mem.Allocator) !tui.Mode {
return create_with_args(allocator, .{});
@ -212,7 +212,7 @@ pub fn Create(options: type) type {
scrollbar.set(@intCast(@max(self.total_items, 1) - 1), @intCast(self.view_rows), @intCast(self.view_pos));
}
fn mouse_click_button4(menu: **Menu.State(*Self), _: *ButtonType, _: Button.Cursor) void {
fn mouse_click_button4(menu: **Menu.State(*Self), _: *ButtonType, _: Widget.Pos) void {
const self = &menu.*.opts.ctx.*;
if (self.view_pos < Menu.scroll_lines) {
self.view_pos = 0;
@ -223,7 +223,7 @@ pub fn Create(options: type) type {
self.start_query(0) catch {};
}
fn mouse_click_button5(menu: **Menu.State(*Self), _: *ButtonType, _: Button.Cursor) void {
fn mouse_click_button5(menu: **Menu.State(*Self), _: *ButtonType, _: Widget.Pos) void {
const self = &menu.*.opts.ctx.*;
if (self.view_pos < @max(self.total_items, self.view_rows) - self.view_rows)
self.view_pos += Menu.scroll_lines;

View file

@ -116,7 +116,7 @@ pub fn on_render_menu(palette: *Type, button: *Type.ButtonType, theme: *const Wi
return false;
}
fn select(menu: **Type.MenuType, button: *Type.ButtonType, _: Type.Cursor) void {
fn select(menu: **Type.MenuType, button: *Type.ButtonType, _: Type.Pos) void {
var entry: Entry = undefined;
var iter = button.opts.label;
if (!(cbor.matchValue(&iter, cbor.extract(&entry)) catch false)) return;

View file

@ -53,7 +53,7 @@ pub fn add_menu_entry(palette: *Type, entry: *Entry, matches: ?[]const usize) !v
palette.items += 1;
}
fn select(menu: **Type.MenuType, button: *Type.ButtonType, _: Type.Cursor) void {
fn select(menu: **Type.MenuType, button: *Type.ButtonType, _: Type.Pos) void {
var description_: []const u8 = undefined;
var name_: []const u8 = undefined;
var iter = button.opts.label;

View file

@ -62,7 +62,7 @@ pub fn ctx_deinit(self: *Self) void {
if (self.behind) |p| self.allocator.free(p);
}
fn on_click(self: *Self, _: *ButtonType, _: Button.Cursor) void {
fn on_click(self: *Self, _: *ButtonType, _: Widget.Pos) void {
self.refresh_git_status();
command.executeName("show_git_status", .{}) catch {};
}

View file

@ -31,7 +31,7 @@ pub fn create(allocator: Allocator, parent: Plane, event_handler: ?EventHandler,
});
}
fn on_click(_: *Self, _: *ButtonType, _: Button.Cursor) void {
fn on_click(_: *Self, _: *ButtonType, _: Widget.Pos) void {
command.executeName("show_diagnostics", .{}) catch {};
}

View file

@ -65,15 +65,15 @@ pub fn create(allocator: Allocator, parent: Plane, event_handler: ?EventHandler,
return Widget.to(btn);
}
fn on_click(_: *Self, _: *ButtonType, _: Button.Cursor) void {
fn on_click(_: *Self, _: *ButtonType, _: Widget.Pos) void {
command.executeName("open_recent", .{}) catch {};
}
fn on_click2(_: *Self, _: *ButtonType, _: Button.Cursor) void {
fn on_click2(_: *Self, _: *ButtonType, _: Widget.Pos) void {
command.executeName("close_file", .{}) catch {};
}
fn on_click3(self: *Self, _: *ButtonType, _: Button.Cursor) void {
fn on_click3(self: *Self, _: *ButtonType, _: Widget.Pos) void {
self.detailed = !self.detailed;
}

View file

@ -60,7 +60,7 @@ pub fn create(allocator: Allocator, parent: Plane, event_handler: ?EventHandler,
});
}
fn on_click(_: *Self, _: *ButtonType, _: Button.Cursor) void {
fn on_click(_: *Self, _: *ButtonType, _: Widget.Pos) void {
command.executeName("goto", .{}) catch {};
}

View file

@ -105,7 +105,7 @@ fn render_logo(self: *ButtonType, theme: *const Widget.Theme, style_label: Widge
}
}
fn on_click(_: *Style, _: *ButtonType, _: Button.Cursor) void {
fn on_click(_: *Style, _: *ButtonType, _: Widget.Pos) void {
if (is_mini_mode()) {
command.executeName("exit_mini_mode", .{}) catch {};
} else if (is_overlay_mode()) {
@ -115,6 +115,6 @@ fn on_click(_: *Style, _: *ButtonType, _: Button.Cursor) void {
}
}
fn toggle_panel(_: *Style, _: *ButtonType, _: Button.Cursor) void {
fn toggle_panel(_: *Style, _: *ButtonType, _: Widget.Pos) void {
command.executeName("toggle_panel", .{}) catch {};
}

View file

@ -353,14 +353,14 @@ const Tab = struct {
});
}
fn on_click(self: *@This(), _: *ButtonType, pos: Button.Cursor) void {
fn on_click(self: *@This(), _: *ButtonType, pos: Widget.Pos) void {
const buffer_manager = tui.get_buffer_manager() orelse @panic("tabs no buffer manager");
if (buffer_manager.buffer_from_ref(self.buffer_ref)) |buffer| {
if (self.close_pos) |close_pos| if (pos.col == close_pos) {
if (self.close_pos) |close_pos| if (pos.x == close_pos) {
tp.self_pid().send(.{ "cmd", "close_buffer", .{buffer.get_file_path()} }) catch {};
return;
};
if (self.save_pos) |save_pos| if (pos.col == save_pos) {
if (self.save_pos) |save_pos| if (pos.x == save_pos) {
tp.self_pid().send(.{ "cmd", "save_buffer", .{buffer.get_file_path()} }) catch {};
return;
};
@ -368,7 +368,7 @@ const Tab = struct {
}
}
fn on_click2(self: *@This(), _: *ButtonType, _: Button.Cursor) void {
fn on_click2(self: *@This(), _: *ButtonType, _: Widget.Pos) void {
const buffer_manager = tui.get_buffer_manager() orelse @panic("tabs no buffer manager");
if (buffer_manager.buffer_from_ref(self.buffer_ref)) |buffer|
tp.self_pid().send(.{ "cmd", "close_buffer", .{buffer.get_file_path()} }) catch {};