refactor: move goto mini mode keybindings to keybind module

This commit is contained in:
CJ van den Berg 2024-10-25 22:40:58 +02:00
parent 16c5471126
commit 49319d6207
Signed by: neurocyte
GPG key ID: 8EB1E1BB660E3FB9
5 changed files with 176 additions and 90 deletions

View file

@ -0,0 +1,72 @@
const tp = @import("thespian");
const key = @import("renderer").input.key;
const mod = @import("renderer").input.modifier;
const event_type = @import("renderer").input.event_type;
const command = @import("command");
const EventHandler = @import("EventHandler");
const Allocator = @import("std").mem.Allocator;
const fmt = @import("std").fmt;
const Mode = @import("root.zig").Mode;
const name = "goto";
pub fn create(_: Allocator) error{OutOfMemory}!Mode {
return .{
.handler = EventHandler.static(@This()),
.name = name,
.description = name,
};
}
pub fn receive(_: tp.pid_ref, m: tp.message) error{Exit}!bool {
var evtype: u32 = undefined;
var keypress: u32 = undefined;
var modifiers: u32 = undefined;
if (try m.match(.{ "I", tp.extract(&evtype), tp.extract(&keypress), tp.any, tp.string, tp.extract(&modifiers) }))
try mapEvent(evtype, keypress, modifiers);
return false;
}
fn mapEvent(evtype: u32, keypress: u32, modifiers: u32) tp.result {
switch (evtype) {
event_type.PRESS => try mapPress(keypress, modifiers),
event_type.REPEAT => try mapPress(keypress, modifiers),
event_type.RELEASE => try mapRelease(keypress, modifiers),
else => {},
}
}
fn mapPress(keypress: u32, modifiers: u32) tp.result {
const keynormal = if ('a' <= keypress and keypress <= 'z') keypress - ('a' - 'A') else keypress;
return switch (modifiers) {
mod.CTRL => switch (keynormal) {
'Q' => command.executeName("quit", .{}),
'U' => command.executeName("mini_mode_reset", .{}),
'G' => command.executeName("mini_mode_cancel", .{}),
'C' => command.executeName("mini_mode_cancel", .{}),
'L' => command.executeName("scroll_view_center", .{}),
key.SPACE => command.executeName("mini_mode_cancel", .{}),
else => {},
},
0 => switch (keypress) {
key.LCTRL, key.RCTRL => command.executeName("enable_fast_scroll", .{}),
key.LALT, key.RALT => command.executeName("enable_fast_scroll", .{}),
key.ESC => command.executeName("mini_mode_cancel", .{}),
key.ENTER => command.executeName("exit_mini_mode", .{}),
key.BACKSPACE => command.executeName("mini_mode_delete_backwards", .{}),
'0'...'9' => command.executeName("mini_mode_insert_code_point", command.fmt(.{keypress})),
else => {},
},
else => {},
};
}
fn mapRelease(keypress: u32, _: u32) tp.result {
return switch (keypress) {
key.LCTRL, key.RCTRL => command.executeName("disable_fast_scroll", .{}),
key.LALT, key.RALT => command.executeName("disable_fast_scroll", .{}),
else => {},
};
}

View file

@ -0,0 +1,24 @@
pub const mode = struct {
pub const mini = struct {
pub const goto = @import("goto.zig");
};
};
pub const Mode = struct {
handler: EventHandler,
name: []const u8,
description: []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();
}
};
pub const KeybindHints = std.static_string_map.StaticStringMap([]const u8);
const renderer = @import("renderer");
const EventHandler = @import("EventHandler");
const std = @import("std");