From f41fb97d02ab8e1231869c60c88b6a51bac39279 Mon Sep 17 00:00:00 2001 From: CJ van den Berg Date: Fri, 25 Oct 2024 18:40:01 +0200 Subject: [PATCH] refactor: split mini modes into input mode and a ui mode --- src/tui/mode/mini/file_browser.zig | 19 +++++++++---------- src/tui/mode/mini/find.zig | 20 ++++++++++---------- src/tui/mode/mini/find_in_files.zig | 20 ++++++++++---------- src/tui/mode/mini/goto.zig | 20 ++++++++++---------- src/tui/mode/mini/move_to_char.zig | 17 ++++++++++------- src/tui/tui.zig | 18 +++++++----------- 6 files changed, 56 insertions(+), 58 deletions(-) diff --git a/src/tui/mode/mini/file_browser.zig b/src/tui/mode/mini/file_browser.zig index dca661c..79818f4 100644 --- a/src/tui/mode/mini/file_browser.zig +++ b/src/tui/mode/mini/file_browser.zig @@ -34,7 +34,7 @@ pub fn Create(options: type) type { type: enum { dir, file, link }, }; - pub fn create(allocator: std.mem.Allocator, _: command.Context) !*Self { + pub fn create(allocator: std.mem.Allocator, _: command.Context) !struct { tui.Mode, tui.MiniMode } { const self: *Self = try allocator.create(Self); self.* = .{ .allocator = allocator, @@ -47,7 +47,14 @@ pub fn Create(options: type) type { try options.load_entries(self); if (@hasDecl(options, "restore_state")) options.restore_state(self) catch {}; - return self; + return .{ + .{ + .handler = EventHandler.to_owned(self), + .name = options.name(self), + .description = options.name(self), + }, + .{}, + }; } pub fn deinit(self: *Self) void { @@ -60,14 +67,6 @@ pub fn Create(options: type) type { self.allocator.destroy(self); } - pub fn handler(self: *Self) EventHandler { - return EventHandler.to_owned(self); - } - - pub fn name(self: *Self) []const u8 { - return options.name(self); - } - pub fn receive(self: *Self, _: tp.pid_ref, m: tp.message) error{Exit}!bool { var evtype: u32 = undefined; var keypress: u32 = undefined; diff --git a/src/tui/mode/mini/find.zig b/src/tui/mode/mini/find.zig index 1a8cfb6..f867862 100644 --- a/src/tui/mode/mini/find.zig +++ b/src/tui/mode/mini/find.zig @@ -16,6 +16,7 @@ const eql = @import("std").mem.eql; const ArrayList = @import("std").ArrayList; const Self = @This(); +const name = "󱎸 find"; allocator: Allocator, input: ArrayList(u8), @@ -25,7 +26,7 @@ start_cursor: ed.Cursor, editor: *ed.Editor, history_pos: ?usize = null, -pub fn create(allocator: Allocator, _: command.Context) !*Self { +pub fn create(allocator: Allocator, _: command.Context) !struct { tui.Mode, tui.MiniMode } { if (tui.current().mainview.dynamic_cast(mainview)) |mv_| if (mv_.get_editor()) |editor| { const self: *Self = try allocator.create(Self); self.* = .{ @@ -41,7 +42,14 @@ pub fn create(allocator: Allocator, _: command.Context) !*Self { defer self.allocator.free(text); try self.input.appendSlice(text); } - return self; + return .{ + .{ + .handler = EventHandler.to_owned(self), + .name = name, + .description = name, + }, + .{}, + }; }; return error.NotFound; } @@ -52,14 +60,6 @@ pub fn deinit(self: *Self) void { self.allocator.destroy(self); } -pub fn handler(self: *Self) EventHandler { - return EventHandler.to_owned(self); -} - -pub fn name(_: *Self) []const u8 { - return "󱎸 find"; -} - pub fn receive(self: *Self, _: tp.pid_ref, m: tp.message) error{Exit}!bool { var evtype: u32 = undefined; var keypress: u32 = undefined; diff --git a/src/tui/mode/mini/find_in_files.zig b/src/tui/mode/mini/find_in_files.zig index 0b7ff8e..8ab14ad 100644 --- a/src/tui/mode/mini/find_in_files.zig +++ b/src/tui/mode/mini/find_in_files.zig @@ -14,6 +14,7 @@ const Allocator = @import("std").mem.Allocator; const eql = @import("std").mem.eql; const Self = @This(); +const name = "󰥨 find"; allocator: Allocator, buf: [1024]u8 = undefined, @@ -22,7 +23,7 @@ last_buf: [1024]u8 = undefined, last_input: []u8 = "", mainview: *mainview, -pub fn create(allocator: Allocator, _: command.Context) !*Self { +pub fn create(allocator: Allocator, _: command.Context) !struct { tui.Mode, tui.MiniMode } { const self: *Self = try allocator.create(Self); if (tui.current().mainview.dynamic_cast(mainview)) |mv| { self.* = .{ @@ -35,7 +36,14 @@ pub fn create(allocator: Allocator, _: command.Context) !*Self { @memcpy(self.buf[0..text.len], text); self.input = self.buf[0..text.len]; }; - return self; + return .{ + .{ + .handler = EventHandler.to_owned(self), + .name = name, + .description = name, + }, + .{}, + }; } return error.NotFound; } @@ -44,14 +52,6 @@ pub fn deinit(self: *Self) void { self.allocator.destroy(self); } -pub fn handler(self: *Self) EventHandler { - return EventHandler.to_owned(self); -} - -pub fn name(_: *Self) []const u8 { - return "󰥨 find"; -} - pub fn receive(self: *Self, _: tp.pid_ref, m: tp.message) error{Exit}!bool { var evtype: u32 = undefined; var keypress: u32 = undefined; diff --git a/src/tui/mode/mini/goto.zig b/src/tui/mode/mini/goto.zig index 93463f9..f7ddf44 100644 --- a/src/tui/mode/mini/goto.zig +++ b/src/tui/mode/mini/goto.zig @@ -13,20 +13,28 @@ const Allocator = @import("std").mem.Allocator; const fmt = @import("std").fmt; const Self = @This(); +const name = "#goto"; allocator: Allocator, buf: [30]u8 = undefined, input: ?usize = null, start: usize, -pub fn create(allocator: Allocator, _: command.Context) !*Self { +pub fn create(allocator: Allocator, _: command.Context) !struct { tui.Mode, tui.MiniMode } { const self: *Self = try allocator.create(Self); if (tui.current().mainview.dynamic_cast(mainview)) |mv_| if (mv_.get_editor()) |editor| { self.* = .{ .allocator = allocator, .start = editor.get_primary().cursor.row + 1, }; - return self; + return .{ + .{ + .handler = EventHandler.to_owned(self), + .name = name, + .description = name, + }, + .{}, + }; }; return error.NotFound; } @@ -35,14 +43,6 @@ pub fn deinit(self: *Self) void { self.allocator.destroy(self); } -pub fn handler(self: *Self) EventHandler { - return EventHandler.to_owned(self); -} - -pub fn name(_: *Self) []const u8 { - return "#goto"; -} - pub fn receive(self: *Self, _: tp.pid_ref, m: tp.message) error{Exit}!bool { var evtype: u32 = undefined; var keypress: u32 = undefined; diff --git a/src/tui/mode/mini/move_to_char.zig b/src/tui/mode/mini/move_to_char.zig index 41ff1fa..da4561a 100644 --- a/src/tui/mode/mini/move_to_char.zig +++ b/src/tui/mode/mini/move_to_char.zig @@ -29,7 +29,7 @@ const Operation = enum { select, }; -pub fn create(allocator: Allocator, ctx: command.Context) !*Self { +pub fn create(allocator: Allocator, ctx: command.Context) !struct { tui.Mode, tui.MiniMode } { var right: bool = true; const select = if (tui.current().mainview.dynamic_cast(mainview)) |mv| if (mv.get_editor()) |editor| if (editor.get_primary().selection) |_| true else false else false else false; _ = ctx.args.match(.{tp.extract(&right)}) catch return error.NotFound; @@ -39,18 +39,21 @@ pub fn create(allocator: Allocator, ctx: command.Context) !*Self { .direction = if (right) .right else .left, .operation = if (select) .select else .move, }; - return self; + return .{ + .{ + .handler = EventHandler.to_owned(self), + .name = self.name(), + .description = self.name(), + }, + .{}, + }; } pub fn deinit(self: *Self) void { self.allocator.destroy(self); } -pub fn handler(self: *Self) EventHandler { - return EventHandler.to_owned(self); -} - -pub fn name(self: *Self) []const u8 { +fn name(self: *Self) []const u8 { return switch (self.operation) { .move => switch (self.direction) { .left => "↶ move", diff --git a/src/tui/tui.zig b/src/tui/tui.zig index 0534693..bcf7a70 100644 --- a/src/tui/tui.zig +++ b/src/tui/tui.zig @@ -31,7 +31,7 @@ input_mode: ?Mode, input_mode_outer: ?Mode = null, input_listeners: EventHandler.List, keyboard_focus: ?Widget = null, -mini_mode: ?MiniModeState = null, +mini_mode: ?MiniMode = null, hover_focus: ?*Widget = null, last_hover_x: c_int = -1, last_hover_y: c_int = -1, @@ -727,8 +727,6 @@ const cmds = struct { } pub const save_as_meta = .{ .description = "Save as" }; - const MiniModeFactory = fn (Allocator, Ctx) error{ NotFound, OutOfMemory }!EventHandler; - fn enter_mini_mode(self: *Self, comptime mode: anytype, ctx: Ctx) Result { if (self.mini_mode) |_| try exit_mini_mode(self, .{}); if (self.input_mode_outer) |_| try exit_overlay_mode(self, .{}); @@ -738,13 +736,9 @@ const cmds = struct { self.input_mode_outer = null; self.mini_mode = null; } - const mode_instance = try mode.create(self.allocator, ctx); - self.input_mode = .{ - .handler = mode_instance.handler(), - .name = mode_instance.name(), - .description = mode_instance.name(), - }; - self.mini_mode = .{}; + const input_mode, const mini_mode = try mode.create(self.allocator, ctx); + self.input_mode = input_mode; + self.mini_mode = mini_mode; } pub fn exit_mini_mode(self: *Self, _: Ctx) Result { @@ -755,6 +749,7 @@ const cmds = struct { 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(); } pub const exit_mini_mode_meta = .{ .interactive = false }; }; @@ -772,7 +767,8 @@ pub const Mode = struct { } }; -pub const MiniModeState = struct { +pub const MiniMode = struct { + event_handler: ?EventHandler = null, text: []const u8 = "", cursor: ?usize = null, };