gpui: Add keybind metadata API (#33316)

Closes #ISSUE

Adds a very simple API to track metadata about keybindings in GPUI,
namely the source of the binding. The motivation for this is displaying
the source of keybindings in the [keymap
UI](https://github.com/zed-industries/zed/pull/32436).

The API is designed to be as simple and flexible as possible, storing
only a `Option<u32>` on the bindings themselves to keep the struct
small. It is intended to be used as an index or key into a table/map
created and managed by the consumer of the API to map from indices to
arbitrary meta-data. I.e. the consumer is responsible for both
generating these indices and giving them meaning.

The current usage in Zed is stateless, just a mapping between constants
and User, Default, Base, and Vim keymap sources, however, this can be
extended in the future to also track _which_ base keymap is being used.

Release Notes:

- N/A *or* Added/Fixed/Improved ...
This commit is contained in:
Ben Kunkle 2025-06-24 11:07:45 -05:00 committed by GitHub
parent 13f134448d
commit deb2564b31
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 119 additions and 13 deletions

View file

@ -47,8 +47,8 @@ use release_channel::{AppCommitSha, ReleaseChannel};
use rope::Rope;
use search::project_search::ProjectSearchBar;
use settings::{
DEFAULT_KEYMAP_PATH, InvalidSettingsError, KeymapFile, KeymapFileLoadResult, Settings,
SettingsStore, VIM_KEYMAP_PATH, initial_local_debug_tasks_content,
DEFAULT_KEYMAP_PATH, InvalidSettingsError, KeybindSource, KeymapFile, KeymapFileLoadResult,
Settings, SettingsStore, VIM_KEYMAP_PATH, initial_local_debug_tasks_content,
initial_project_settings_content, initial_tasks_content, update_settings_file,
};
use std::path::PathBuf;
@ -1403,10 +1403,15 @@ fn show_markdown_app_notification<F>(
.detach();
}
fn reload_keymaps(cx: &mut App, user_key_bindings: Vec<KeyBinding>) {
fn reload_keymaps(cx: &mut App, mut user_key_bindings: Vec<KeyBinding>) {
cx.clear_key_bindings();
load_default_keymap(cx);
for key_binding in &mut user_key_bindings {
key_binding.set_meta(KeybindSource::User.meta());
}
cx.bind_keys(user_key_bindings);
cx.set_menus(app_menus());
// On Windows, this is set in the `update_jump_list` method of the `HistoryManager`.
#[cfg(not(target_os = "windows"))]
@ -1422,14 +1427,18 @@ pub fn load_default_keymap(cx: &mut App) {
return;
}
cx.bind_keys(KeymapFile::load_asset(DEFAULT_KEYMAP_PATH, cx).unwrap());
cx.bind_keys(
KeymapFile::load_asset(DEFAULT_KEYMAP_PATH, Some(KeybindSource::Default), cx).unwrap(),
);
if let Some(asset_path) = base_keymap.asset_path() {
cx.bind_keys(KeymapFile::load_asset(asset_path, cx).unwrap());
cx.bind_keys(KeymapFile::load_asset(asset_path, Some(KeybindSource::Base), cx).unwrap());
}
if VimModeSetting::get_global(cx).0 || vim_mode_setting::HelixModeSetting::get_global(cx).0 {
cx.bind_keys(KeymapFile::load_asset(VIM_KEYMAP_PATH, cx).unwrap());
cx.bind_keys(
KeymapFile::load_asset(VIM_KEYMAP_PATH, Some(KeybindSource::Vim), cx).unwrap(),
);
}
}