diff --git a/src/keybind/static/mini/file_browser.zig b/src/keybind/static/mini/file_browser.zig index 4533738..d9c2e45 100644 --- a/src/keybind/static/mini/file_browser.zig +++ b/src/keybind/static/mini/file_browser.zig @@ -7,10 +7,8 @@ const EventHandler = @import("EventHandler"); const Allocator = @import("std").mem.Allocator; -const Mode = @import("../root.zig").Mode; - -pub fn create(_: Allocator) !Mode { - return .{ .handler = EventHandler.static(@This()) }; +pub fn create(_: Allocator) EventHandler { + return EventHandler.static(@This()); } pub fn receive(_: tp.pid_ref, m: tp.message) error{Exit}!bool { diff --git a/src/keybind/static/mini/find.zig b/src/keybind/static/mini/find.zig index 1f81800..61f5649 100644 --- a/src/keybind/static/mini/find.zig +++ b/src/keybind/static/mini/find.zig @@ -7,10 +7,8 @@ const EventHandler = @import("EventHandler"); const Allocator = @import("std").mem.Allocator; -const Mode = @import("../root.zig").Mode; - -pub fn create(_: Allocator) !Mode { - return .{ .handler = EventHandler.static(@This()) }; +pub fn create(_: Allocator) EventHandler { + return EventHandler.static(@This()); } pub fn receive(_: tp.pid_ref, m: tp.message) error{Exit}!bool { diff --git a/src/keybind/static/mini/find_in_files.zig b/src/keybind/static/mini/find_in_files.zig index 553ff06..9426300 100644 --- a/src/keybind/static/mini/find_in_files.zig +++ b/src/keybind/static/mini/find_in_files.zig @@ -7,10 +7,8 @@ const EventHandler = @import("EventHandler"); const Allocator = @import("std").mem.Allocator; -const Mode = @import("../root.zig").Mode; - -pub fn create(_: Allocator) !Mode { - return .{ .handler = EventHandler.static(@This()) }; +pub fn create(_: Allocator) EventHandler { + return EventHandler.static(@This()); } pub fn receive(_: tp.pid_ref, m: tp.message) error{Exit}!bool { diff --git a/src/keybind/static/mini/goto.zig b/src/keybind/static/mini/goto.zig index 993b1ed..af6f51c 100644 --- a/src/keybind/static/mini/goto.zig +++ b/src/keybind/static/mini/goto.zig @@ -8,10 +8,8 @@ const EventHandler = @import("EventHandler"); const Allocator = @import("std").mem.Allocator; const fmt = @import("std").fmt; -const Mode = @import("../root.zig").Mode; - -pub fn create(_: Allocator) error{OutOfMemory}!Mode { - return .{ .handler = EventHandler.static(@This()) }; +pub fn create(_: Allocator) EventHandler { + return EventHandler.static(@This()); } pub fn receive(_: tp.pid_ref, m: tp.message) error{Exit}!bool { diff --git a/src/keybind/static/mini/move_to_char.zig b/src/keybind/static/mini/move_to_char.zig index 4d6537f..db5c501 100644 --- a/src/keybind/static/mini/move_to_char.zig +++ b/src/keybind/static/mini/move_to_char.zig @@ -7,10 +7,8 @@ const EventHandler = @import("EventHandler"); const Allocator = @import("std").mem.Allocator; -const Mode = @import("../root.zig").Mode; - -pub fn create(_: Allocator) !Mode { - return .{ .handler = EventHandler.static(@This()) }; +pub fn create(_: Allocator) EventHandler { + return EventHandler.static(@This()); } pub fn receive(_: tp.pid_ref, m: tp.message) error{Exit}!bool { diff --git a/src/keybind/static/root.zig b/src/keybind/static/root.zig index 4e289d8..9a75dc5 100644 --- a/src/keybind/static/root.zig +++ b/src/keybind/static/root.zig @@ -9,14 +9,17 @@ pub const mode = struct { }; pub const Mode = struct { - handler: EventHandler, + input_handler: EventHandler, + event_handler: ?EventHandler = null, + name: []const u8 = "", line_numbers: enum { absolute, relative } = .absolute, keybind_hints: ?*const KeybindHints = null, cursor_shape: renderer.CursorShape = .block, pub fn deinit(self: *Mode) void { - self.handler.deinit(); + self.input_handler.deinit(); + if (self.event_handler) |eh| eh.deinit(); } }; diff --git a/src/tui/mode/input/flow.zig b/src/tui/mode/input/flow.zig index f886530..ee32dc2 100644 --- a/src/tui/mode/input/flow.zig +++ b/src/tui/mode/input/flow.zig @@ -29,7 +29,7 @@ pub fn create(allocator: Allocator) !tui.Mode { .input = try ArrayList(u8).initCapacity(allocator, input_buffer_size), }; return .{ - .handler = EventHandler.to_owned(self), + .input_handler = EventHandler.to_owned(self), .name = root.application_name, .keybind_hints = &hints, }; diff --git a/src/tui/mode/input/helix/insert.zig b/src/tui/mode/input/helix/insert.zig index 990981d..3cf6e77 100644 --- a/src/tui/mode/input/helix/insert.zig +++ b/src/tui/mode/input/helix/insert.zig @@ -30,7 +30,7 @@ pub fn create(allocator: Allocator) !tui.Mode { }; try self.commands.init(self); return .{ - .handler = EventHandler.to_owned(self), + .input_handler = EventHandler.to_owned(self), .name = "INS", .line_numbers = if (tui.current().config.vim_insert_gutter_line_numbers_relative) .relative else .absolute, .cursor_shape = .beam, diff --git a/src/tui/mode/input/helix/normal.zig b/src/tui/mode/input/helix/normal.zig index d7ba850..e79f861 100644 --- a/src/tui/mode/input/helix/normal.zig +++ b/src/tui/mode/input/helix/normal.zig @@ -31,7 +31,7 @@ pub fn create(allocator: Allocator) !tui.Mode { }; try self.commands.init(self); return .{ - .handler = EventHandler.to_owned(self), + .input_handler = EventHandler.to_owned(self), .name = "NOR", .line_numbers = if (tui.current().config.vim_normal_gutter_line_numbers_relative) .relative else .absolute, .keybind_hints = &hints, diff --git a/src/tui/mode/input/helix/select.zig b/src/tui/mode/input/helix/select.zig index 30d9618..a350110 100644 --- a/src/tui/mode/input/helix/select.zig +++ b/src/tui/mode/input/helix/select.zig @@ -31,7 +31,7 @@ pub fn create(allocator: Allocator) !tui.Mode { }; try self.commands.init(self); return .{ - .handler = EventHandler.to_owned(self), + .input_handler = EventHandler.to_owned(self), .name = "SEL", .line_numbers = if (tui.current().config.vim_visual_gutter_line_numbers_relative) .relative else .absolute, .keybind_hints = &hints, diff --git a/src/tui/mode/input/home.zig b/src/tui/mode/input/home.zig index 10d5dff..c68a355 100644 --- a/src/tui/mode/input/home.zig +++ b/src/tui/mode/input/home.zig @@ -22,7 +22,7 @@ pub fn create(allocator: std.mem.Allocator) !tui.Mode { .allocator = allocator, }; return .{ - .handler = EventHandler.to_owned(self), + .input_handler = EventHandler.to_owned(self), .name = root.application_name, .keybind_hints = &hints, }; diff --git a/src/tui/mode/input/vim/insert.zig b/src/tui/mode/input/vim/insert.zig index ab89a6b..5b57972 100644 --- a/src/tui/mode/input/vim/insert.zig +++ b/src/tui/mode/input/vim/insert.zig @@ -39,7 +39,7 @@ pub fn create(allocator: Allocator) !tui.Mode { }; try self.commands.init(self); return .{ - .handler = EventHandler.to_owned(self), + .input_handler = EventHandler.to_owned(self), .name = "INSERT", .line_numbers = if (tui.current().config.vim_insert_gutter_line_numbers_relative) .relative else .absolute, .cursor_shape = .beam, diff --git a/src/tui/mode/input/vim/normal.zig b/src/tui/mode/input/vim/normal.zig index b5a7168..a80c814 100644 --- a/src/tui/mode/input/vim/normal.zig +++ b/src/tui/mode/input/vim/normal.zig @@ -31,7 +31,7 @@ pub fn create(allocator: Allocator) !tui.Mode { }; try self.commands.init(self); return .{ - .handler = EventHandler.to_owned(self), + .input_handler = EventHandler.to_owned(self), .name = "NORMAL", .line_numbers = if (tui.current().config.vim_normal_gutter_line_numbers_relative) .relative else .absolute, .keybind_hints = &hints, diff --git a/src/tui/mode/input/vim/visual.zig b/src/tui/mode/input/vim/visual.zig index 306f302..0e2ad97 100644 --- a/src/tui/mode/input/vim/visual.zig +++ b/src/tui/mode/input/vim/visual.zig @@ -31,7 +31,7 @@ pub fn create(allocator: Allocator) !tui.Mode { }; try self.commands.init(self); return .{ - .handler = EventHandler.to_owned(self), + .input_handler = EventHandler.to_owned(self), .name = "VISUAL", .line_numbers = if (tui.current().config.vim_visual_gutter_line_numbers_relative) .relative else .absolute, .keybind_hints = &hints, diff --git a/src/tui/mode/mini/file_browser.zig b/src/tui/mode/mini/file_browser.zig index c344f33..ddf71ef 100644 --- a/src/tui/mode/mini/file_browser.zig +++ b/src/tui/mode/mini/file_browser.zig @@ -52,9 +52,11 @@ pub fn Create(options: type) type { if (@hasDecl(options, "restore_state")) options.restore_state(self) catch {}; return .{ - try keybind.mode.mini.file_browser.create(allocator), .{ + .input_handler = keybind.mode.mini.file_browser.create(allocator), .event_handler = EventHandler.to_owned(self), + }, + .{ .name = options.name(self), }, }; diff --git a/src/tui/mode/mini/find.zig b/src/tui/mode/mini/find.zig index 8ddd1c4..b4a78e7 100644 --- a/src/tui/mode/mini/find.zig +++ b/src/tui/mode/mini/find.zig @@ -48,9 +48,11 @@ pub fn create(allocator: Allocator, _: command.Context) !struct { tui.Mode, tui. try self.input.appendSlice(text); } return .{ - try keybind.mode.mini.find.create(allocator), .{ + .input_handler = keybind.mode.mini.find.create(allocator), .event_handler = EventHandler.to_owned(self), + }, + .{ .name = name, }, }; diff --git a/src/tui/mode/mini/find_in_files.zig b/src/tui/mode/mini/find_in_files.zig index d339e37..29f67ac 100644 --- a/src/tui/mode/mini/find_in_files.zig +++ b/src/tui/mode/mini/find_in_files.zig @@ -42,9 +42,11 @@ pub fn create(allocator: Allocator, _: command.Context) !struct { tui.Mode, tui. self.input = self.buf[0..text.len]; }; return .{ - try keybind.mode.mini.find_in_files.create(allocator), .{ + .input_handler = keybind.mode.mini.find_in_files.create(allocator), .event_handler = EventHandler.to_owned(self), + }, + .{ .name = name, }, }; diff --git a/src/tui/mode/mini/goto.zig b/src/tui/mode/mini/goto.zig index b2424f0..13ab02c 100644 --- a/src/tui/mode/mini/goto.zig +++ b/src/tui/mode/mini/goto.zig @@ -33,9 +33,11 @@ pub fn create(allocator: Allocator, _: command.Context) !struct { tui.Mode, tui. }; try self.commands.init(self); return .{ - try keybind.mode.mini.goto.create(allocator), .{ + .input_handler = keybind.mode.mini.goto.create(allocator), .event_handler = EventHandler.to_owned(self), + }, + .{ .name = name, }, }; diff --git a/src/tui/mode/mini/move_to_char.zig b/src/tui/mode/mini/move_to_char.zig index a57e693..bc7a508 100644 --- a/src/tui/mode/mini/move_to_char.zig +++ b/src/tui/mode/mini/move_to_char.zig @@ -45,9 +45,11 @@ pub fn create(allocator: Allocator, ctx: command.Context) !struct { tui.Mode, tu }; try self.commands.init(self); return .{ - try keybind.mode.mini.move_to_char.create(allocator), .{ + .input_handler = keybind.mode.mini.move_to_char.create(allocator), .event_handler = EventHandler.to_owned(self), + }, + .{ .name = self.name(), }, }; diff --git a/src/tui/mode/overlay/open_recent.zig b/src/tui/mode/overlay/open_recent.zig index 21b2fcb..61b7da0 100644 --- a/src/tui/mode/overlay/open_recent.zig +++ b/src/tui/mode/overlay/open_recent.zig @@ -62,7 +62,7 @@ pub fn create(allocator: std.mem.Allocator) !tui.Mode { try mv.floating_views.add(self.modal.widget()); try mv.floating_views.add(self.menu.container_widget); return .{ - .handler = EventHandler.to_owned(self), + .input_handler = EventHandler.to_owned(self), .name = "󰈞 open recent", }; } diff --git a/src/tui/mode/overlay/palette.zig b/src/tui/mode/overlay/palette.zig index 6edfb74..8be97c5 100644 --- a/src/tui/mode/overlay/palette.zig +++ b/src/tui/mode/overlay/palette.zig @@ -84,7 +84,7 @@ pub fn Create(options: type) type { try mv.floating_views.add(self.modal.widget()); try mv.floating_views.add(self.menu.container_widget); return .{ - .handler = EventHandler.to_owned(self), + .input_handler = EventHandler.to_owned(self), .name = options.name, }; } diff --git a/src/tui/tui.zig b/src/tui/tui.zig index 9f10dbc..b75ffc5 100644 --- a/src/tui/tui.zig +++ b/src/tui/tui.zig @@ -272,8 +272,6 @@ fn receive_safe(self: *Self, from: tp.pid_ref, m: tp.message) !void { } if (try m.match(.{ "system_clipboard", tp.string })) { - if (self.mini_mode) |mode| if (mode.event_handler) |eh| - return eh.send(tp.self_pid(), m) catch |e| self.logger.err("clipboard handler", e); if (self.active_event_handler()) |eh| eh.send(tp.self_pid(), m) catch |e| self.logger.err("clipboard handler", e); return; @@ -394,9 +392,8 @@ fn render(self: *Self) void { } fn active_event_handler(self: *Self) ?EventHandler { - if (self.mini_mode) |mm| if (mm.event_handler) |eh| return eh; - if (self.input_mode) |im| return im.handler; - return null; + const mode = self.input_mode orelse return null; + return mode.event_handler orelse mode.input_handler; } fn dispatch_flush_input_event(self: *Self) !void { @@ -419,7 +416,7 @@ fn dispatch_input(ctx: *anyopaque, cbor_msg: []const u8) void { }) return; if (self.input_mode) |mode| - mode.handler.send(from, m) catch |e| self.logger.err("input handler", e); + mode.input_handler.send(from, m) catch |e| self.logger.err("input handler", e); } fn dispatch_mouse(ctx: *anyopaque, y: c_int, x: c_int, cbor_msg: []const u8) void { @@ -753,26 +750,22 @@ const cmds = struct { pub fn exit_mini_mode(self: *Self, _: Ctx) Result { if (self.mini_mode) |_| {} else return; - defer { - self.input_mode = self.input_mode_outer; - self.input_mode_outer = null; - self.mini_mode = null; - } if (self.input_mode) |*mode| mode.deinit(); - if (self.mini_mode) |*mode| if (mode.event_handler) |*event_handler| event_handler.deinit(); + self.input_mode = self.input_mode_outer; + self.input_mode_outer = null; + self.mini_mode = null; } pub const exit_mini_mode_meta = .{ .interactive = false }; }; pub const MiniMode = struct { - event_handler: ?EventHandler = null, name: []const u8, text: []const u8 = "", cursor: ?usize = null, }; pub const Mode = keybind.Mode; -pub const KeybindHints = std.static_string_map.StaticStringMap([]const u8); +pub const KeybindHints = keybind.KeybindHints; threadlocal var instance_: ?*Self = null;