refactor: split mini modes into input mode and a ui mode
This commit is contained in:
		
							parent
							
								
									d804203760
								
							
						
					
					
						commit
						f41fb97d02
					
				
					 6 changed files with 56 additions and 58 deletions
				
			
		| 
						 | 
				
			
			@ -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;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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",
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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,
 | 
			
		||||
};
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue