mirror of
https://github.com/ghostty-org/ghostty.git
synced 2026-06-15 08:03:56 +00:00
config: use Config to check key binding instead of App
Previously `ghostty_app_key_is_binding` (unlike Surface) is just using `config.keybind` to check whether a KeyEvent is in the set or not. After this, I can add unit tests for keybinding more easily, with dummy configs.
This commit is contained in:
@@ -1078,6 +1078,7 @@ GHOSTTY_API bool ghostty_config_get(ghostty_config_t, void*, const char*, uintpt
|
||||
GHOSTTY_API ghostty_input_trigger_s ghostty_config_trigger(ghostty_config_t,
|
||||
const char*,
|
||||
uintptr_t);
|
||||
GHOSTTY_API bool ghostty_config_key_is_binding(ghostty_config_t, ghostty_input_key_s);
|
||||
GHOSTTY_API uint32_t ghostty_config_diagnostics_count(ghostty_config_t);
|
||||
GHOSTTY_API ghostty_diagnostic_s ghostty_config_get_diagnostic(ghostty_config_t, uint32_t);
|
||||
GHOSTTY_API ghostty_string_s ghostty_config_open_path(void);
|
||||
@@ -1089,7 +1090,6 @@ GHOSTTY_API void ghostty_app_tick(ghostty_app_t);
|
||||
GHOSTTY_API void* ghostty_app_userdata(ghostty_app_t);
|
||||
GHOSTTY_API void ghostty_app_set_focus(ghostty_app_t, bool);
|
||||
GHOSTTY_API bool ghostty_app_key(ghostty_app_t, ghostty_input_key_s);
|
||||
GHOSTTY_API bool ghostty_app_key_is_binding(ghostty_app_t, ghostty_input_key_s);
|
||||
GHOSTTY_API void ghostty_app_keyboard_changed(ghostty_app_t);
|
||||
GHOSTTY_API void ghostty_app_open_config(ghostty_app_t);
|
||||
GHOSTTY_API void ghostty_app_update_config(ghostty_app_t, ghostty_config_t);
|
||||
|
||||
@@ -585,11 +585,11 @@ class AppDelegate: NSObject,
|
||||
guard NSApp.mainWindow == nil else { return event }
|
||||
|
||||
// If this event as-is would result in a key binding then we send it.
|
||||
if let app = ghostty.app {
|
||||
if let app = ghostty.app, let config = ghostty.config.config {
|
||||
var ghosttyEvent = event.ghosttyKeyEvent(GHOSTTY_ACTION_PRESS)
|
||||
let match = (event.characters ?? "").withCString { ptr in
|
||||
ghosttyEvent.text = ptr
|
||||
if !ghostty_app_key_is_binding(app, ghosttyEvent) {
|
||||
if !ghostty_config_key_is_binding(config, ghosttyEvent) {
|
||||
return false
|
||||
}
|
||||
|
||||
|
||||
19
src/App.zig
19
src/App.zig
@@ -315,25 +315,6 @@ pub fn focusEvent(self: *App, focused: bool) void {
|
||||
self.focused = focused;
|
||||
}
|
||||
|
||||
/// Returns true if the given key event would trigger a keybinding
|
||||
/// if it were to be processed. This is useful for determining if
|
||||
/// a key event should be sent to the terminal or not.
|
||||
pub fn keyEventIsBinding(
|
||||
self: *App,
|
||||
rt_app: *apprt.App,
|
||||
event: input.KeyEvent,
|
||||
) bool {
|
||||
_ = self;
|
||||
|
||||
switch (event.action) {
|
||||
.release => return false,
|
||||
.press, .repeat => {},
|
||||
}
|
||||
|
||||
// If we have a keybinding for this event then we return true.
|
||||
return rt_app.config.keybind.set.getEvent(event) != null;
|
||||
}
|
||||
|
||||
/// Handle a key event at the app-scope. If this key event is used,
|
||||
/// this will return true and the caller shouldn't continue processing
|
||||
/// the event. If the event is not used, this will return false.
|
||||
|
||||
@@ -1466,8 +1466,8 @@ pub const CAPI = struct {
|
||||
/// if it were sent to the surface right now. The "right now"
|
||||
/// is important because things like trigger sequences are only
|
||||
/// valid until the next key event.
|
||||
export fn ghostty_app_key_is_binding(
|
||||
app: *App,
|
||||
export fn ghostty_config_key_is_binding(
|
||||
config: *Config,
|
||||
event: KeyEvent,
|
||||
) bool {
|
||||
const core_event = event.keyEvent().core() orelse {
|
||||
@@ -1475,7 +1475,7 @@ pub const CAPI = struct {
|
||||
return false;
|
||||
};
|
||||
|
||||
return app.core_app.keyEventIsBinding(app, core_event);
|
||||
return config.keyEventIsBinding(core_event);
|
||||
}
|
||||
|
||||
/// Notify the app that the keyboard was changed. This causes the
|
||||
|
||||
@@ -6397,6 +6397,22 @@ pub const RepeatableFontVariation = struct {
|
||||
}
|
||||
};
|
||||
|
||||
/// Returns true if the given key event would trigger a keybinding
|
||||
/// if it were to be processed. This is useful for determining if
|
||||
/// a key event should be sent to the terminal or not.
|
||||
pub fn keyEventIsBinding(
|
||||
self: *Config,
|
||||
event: inputpkg.KeyEvent,
|
||||
) bool {
|
||||
switch (event.action) {
|
||||
.release => return false,
|
||||
.press, .repeat => {},
|
||||
}
|
||||
|
||||
// If we have a keybinding for this event then we return true.
|
||||
return self.keybind.set.getEvent(event) != null;
|
||||
}
|
||||
|
||||
/// Stores a set of keybinds.
|
||||
pub const Keybinds = struct {
|
||||
set: inputpkg.Binding.Set = .{},
|
||||
|
||||
Reference in New Issue
Block a user