Skip to content

A fast file manager for Windows and macOS that never freezes: tabs, dual panes, archives as folders, Quick Look-style previews. A File Explorer and Finder alternative written in Rust.

Shortcuts are bound anew when one is set or reset and when the keyboard layout changes

gelenmedyabb9a44870bc2
diff --git a/crates/rf-app/src/actions.rs b/crates/rf-app/src/actions.rs
index 6e7e7ba4..8dd5cd2d 100644
--- a/crates/rf-app/src/actions.rs
+++ b/crates/rf-app/src/actions.rs
@@ -143,8 +143,34 @@ pub const TREE_CTX: &str = "FolderTree";
/// tree's Tab: the root's Tab binding would take the key first).
pub const DIALOG_CTX: &str = "Dialog";
+/// Where our own bindings (the built-in ones and the user's) lie in the app's list of bindings: [`rebind`] takes exactly these
+/// out before it binds anew, and leaves the component library's alone.
+struct Bound(std::ops::Range<usize>);
+impl Global for Bound {}
+
+/// Binds the built-in shortcuts and the user's own, from the settings and the keyboard layout as they are now: at startup, and
+/// again whenever either changes. The bindings of an earlier call are taken out first.
+///
+/// WHY. Bindings could only be added, so a change was half done until the next start (a Discord report, 2026-12-04: "The hotkey
+/// removed is still active": a shortcut set and then reset in Settings went on working; and a command given new keys kept its
+/// built-in ones too). And the layout forms of Ctrl+Shift+digit ([`bind_layout_shifted`]) were made once, for the layout the
+/// program started in (the same reporter: "Hotkeys like Ctrl+Shift+digit don't work in Russian layout reliably": Shift+2 is `@`
+/// in English and `"` in Russian, so after a switch of layouts the keys were bound to nothing).
+pub fn rebind(cx: &mut App) {
+ let all: Vec<KeyBinding> = cx.key_bindings().borrow().bindings().cloned().collect();
+ let ours = cx.try_global::<Bound>().map(|b| b.0.clone()).filter(|r| r.end <= all.len()).unwrap_or(all.len()..all.len());
+ let others: Vec<KeyBinding> = all.into_iter().enumerate().filter(|(i, _)| !ours.contains(i)).map(|(_, b)| b).collect();
+ cx.clear_key_bindings();
+ let start = others.len();
+ cx.bind_keys(others);
+ bind(cx);
+ bind_user(cx);
+ let end = cx.key_bindings().borrow().bindings().len();
+ cx.set_global(Bound(start..end));
+}
+
/// User overrides from settings.toml `[keybindings]`. Invalid entries are logged and skipped.
-pub fn bind_user(cx: &mut App) {
+fn bind_user(cx: &mut App) {
let map = cx.global::<crate::settings::Settings>().keybindings.clone();
let mut bindings = Vec::new();
for (keys, action) in map {
@@ -191,7 +217,7 @@ pub(crate) fn types_text(keys: &str) -> bool {
/// Binds built-in shortcuts, leaving out those of a command the user gave keys of their own in Settings: a custom shortcut
/// replaces the built-in one, as the shortcut editor shows it, so the old keys are free for another command. (With both bound,
/// the built-in one kept working, and in a nearer context such as the file list's it won over a custom one on the same keys.)
-/// Read at startup: a shortcut set in Settings adds its keys at once, the built-in ones go at the next start.
+/// A shortcut set or reset in Settings binds everything anew ([`rebind`]), so this holds at once.
fn bind_defaults(cx: &mut App, bindings: impl IntoIterator<Item = KeyBinding>) {
let custom: std::collections::HashSet<String> =
cx.global::<crate::settings::Settings>().keybindings.values().map(|a| short_name(a).to_string()).collect();
@@ -207,7 +233,7 @@ fn without_customized(bindings: impl IntoIterator<Item = KeyBinding>, custom: &s
bindings.into_iter().filter(|b| !custom.contains(short_name(b.action().name()))).collect()
}
-pub fn bind(cx: &mut App) {
+fn bind(cx: &mut App) {
let f = Some(FOLDER_CTX);
bind_defaults(cx, [KeyBinding::new("tab", LeaveTree, Some(TREE_CTX))]);
// Tab and Shift+Tab walk the window's panes: the address bar, the folder tree, the file list (issue 38: "TAB does not work
@@ -329,9 +355,14 @@ pub fn bind(cx: &mut App) {
bind_layout_shifted(cx);
}
+thread_local! {
+ /// The layout forms last written to the log (written again only when they change).
+ static LAYOUT_FORMS: std::cell::RefCell<String> = const { std::cell::RefCell::new(String::new()) };
+}
+
/// GPUI reports Shift+digit as the layout's shifted character without Shift (Turkish Q:
/// Ctrl+Shift+2 arrives as `ctrl-'`), so `ctrl-shift-2` alone never fires. Bind the
-/// layout-specific form too.
+/// layout-specific form too. It is the form of the layout in use now: [`rebind`] runs this again when the layout changes.
fn bind_layout_shifted(cx: &mut App) {
// (virtual key, action)
let wanted: [(u16, Box<dyn Fn() -> Box<dyn Action>>); 6] = [
@@ -343,6 +374,7 @@ fn bind_layout_shifted(cx: &mut App) {
(0xC0, Box::new(|| Box::new(OpenTerminal))), // VK_OEM_3 (` on US layouts)
];
let mut bindings = Vec::new();
+ let mut forms = Vec::new();
for (vk, action) in wanted {
let Some(ch) = rf_win::keys::shifted_char(vk) else { continue };
// AZERTY: Shift+& gives "1"; binding ctrl-1 would steal a plain ctrl+digit shortcut
@@ -355,10 +387,19 @@ fn bind_layout_shifted(cx: &mut App) {
}
let keys = format!("ctrl-{ch}");
match KeyBinding::load(&keys, action(), None, false, None, cx) {
- Ok(b) => bindings.push(b),
+ Ok(b) => {
+ bindings.push(b);
+ forms.push(keys);
+ }
Err(e) => log::debug!("layout binding {keys:?}: {e}"),
}
}
+ // (one line a layout: a report of "Ctrl+Shift+2 does nothing" can be read against it)
+ let forms = forms.join(" ");
+ let said = LAYOUT_FORMS.with(|f| f.replace(forms.clone()));
+ if said != forms {
+ log::info!(target: "rustyfiles::start", "shortcuts: Ctrl+Shift+1..5 and the terminal's keys arrive as [{forms}] on this keyboard layout");
+ }
bind_defaults(cx, bindings);
}
diff --git a/crates/rf-app/src/keymap_editor.rs b/crates/rf-app/src/keymap_editor.rs
index 287e36f8..a8c2ab7a 100644
--- a/crates/rf-app/src/keymap_editor.rs
+++ b/crates/rf-app/src/keymap_editor.rs
@@ -2,9 +2,8 @@
//!
//! Capturing uses a keystroke interceptor, so the pressed keys never reach their current action
//! (pressing Ctrl+N while recording does not open a window). Escape cancels. The new binding is
-//! stored in `settings.toml` → `[keybindings]` and bound immediately. It replaces the command's
-//! built-in keys, which are left unbound from the next start (`actions::bind_defaults`); until then,
-//! and after a reset until restart, the previous keys keep working too.
+//! stored in `settings.toml` → `[keybindings]`, and every shortcut is bound anew at once (`actions::rebind`): the new keys
+//! replace the command's built-in ones, and after a reset the built-in ones are back and the custom ones gone, with no restart.
use gpui_kit::*;
@@ -98,14 +97,7 @@ pub fn init(cx: &mut App) {
s.keybindings.retain(|_, a| *a != action);
s.keybindings.insert(keys.clone(), action.clone());
});
- // bind just the new key (re-binding the whole map would stack duplicates)
- match cx.build_action(&format!("rustyfiles::{action}"), None) {
- Ok(a) => match crate::actions::user_binding(&keys, a, cx) {
- Ok(b) => cx.bind_keys([b]),
- Err(e) => log::warn!("keybinding {keys:?}: {e}"),
- },
- Err(e) => log::warn!("keybinding action {action:?}: {e}"),
- }
+ crate::actions::rebind(cx);
window.refresh();
})
.detach();
@@ -210,12 +202,33 @@ pub fn canon(shown: &str) -> String {
/// `entries` is (label, built-in keys as shown, action name); a custom shortcut replaces the
/// built-in one in the comparison.
pub fn conflicts(entries: &[(&'static str, &'static str, String)], custom: &dyn Fn(&str) -> Option<String>) -> std::collections::HashMap<String, &'static str> {
+ conflicts_on(entries, custom, &|digit| rf_win::keys::shifted_char(0x30 + digit as u16))
+}
+
+/// A shortcut with Shift and a digit, in the form the keyboard gives it: "ctrl+shift+2" is "ctrl+@" on a US layout (GPUI reports
+/// Shift+digit as the character the layout makes of it, without Shift), and that is how a recorded shortcut is written. So the
+/// built-in "Ctrl+Shift+2" and a custom "ctrl-@" are the same keys there. `shifted` gives the layout's character for a digit; the
+/// forms `actions::bind_layout_shifted` does not bind (a letter or digit, or a sign of the zoom keys) are left as they are.
+fn typed_form(canon: &str, shifted: &dyn Fn(u8) -> Option<String>) -> String {
+ let parts: Vec<&str> = canon.split('+').collect();
+ let (Some(last), true) = (parts.last(), parts.contains(&"shift")) else { return canon.to_string() };
+ let Some(digit) = (last.len() == 1).then(|| last.as_bytes()[0]).filter(u8::is_ascii_digit) else { return canon.to_string() };
+ match shifted(digit - b'0') {
+ Some(ch) if !ch.chars().any(|c| c.is_alphanumeric()) && !matches!(ch.as_str(), "+" | "-" | "=") => {
+ let mods: Vec<&str> = parts[..parts.len() - 1].iter().copied().filter(|p| *p != "shift").collect();
+ if mods.is_empty() { ch } else { format!("{}+{ch}", mods.join("+")) }
+ }
+ _ => canon.to_string(),
+ }
+}
+
+fn conflicts_on(entries: &[(&'static str, &'static str, String)], custom: &dyn Fn(&str) -> Option<String>, shifted: &dyn Fn(u8) -> Option<String>) -> std::collections::HashMap<String, &'static str> {
let keys: Vec<Option<String>> = entries
.iter()
.map(|(_, built_in, action)| match custom(action) {
- Some(k) => Some(canon(&pretty(&k))),
+ Some(k) => Some(typed_form(&canon(&pretty(&k)), shifted)),
None if built_in.is_empty() => None,
- None => Some(canon(built_in)),
+ None => Some(typed_form(&canon(built_in), shifted)),
})
.collect();
let mut out = std::collections::HashMap::new();
@@ -235,6 +248,7 @@ pub fn reset(action: &str, cx: &mut App) {
crate::settings::update(cx, |s| {
s.keybindings.retain(|_, a| *a != action && a.strip_prefix("rustyfiles::") != Some(action.as_str()))
});
+ crate::actions::rebind(cx);
}
#[cfg(test)]
@@ -273,6 +287,25 @@ mod tests {
assert!(super::conflicts(&entries, &moved).is_empty());
}
+ /// (a Discord report, 2026-12-04: Ctrl+Shift+2 recorded for another command was written "Ctrl+@" and nothing said that the
+ /// cards layout already had those keys)
+ #[test]
+ fn a_recorded_shift_digit_is_the_same_keys_as_the_built_in_shortcut_written_with_shift() {
+ let us = |d: u8| [")", "!", "@", "#", "$", "%", "^", "&", "*", "("].get(d as usize).map(|s| s.to_string());
+ assert_eq!(super::typed_form("ctrl+shift+2", &us), "ctrl+@");
+ assert_eq!(super::typed_form("ctrl+@", &us), "ctrl+@");
+ assert_eq!(super::typed_form("ctrl+2", &us), "ctrl+2", "no Shift: the digit itself");
+ assert_eq!(super::typed_form("ctrl+shift+k", &us), "ctrl+shift+k");
+ // (Turkish Q: Shift+4 is "+", the zoom key: left as written, as it is left unbound)
+ let tr = |d: u8| (d == 4).then(|| "+".to_string());
+ assert_eq!(super::typed_form("ctrl+shift+4", &tr), "ctrl+shift+4");
+ let entries = [("Kart görünümü", "Ctrl+Shift+2", "ViewGrid".to_string()), ("Oturumu kaydet", "", "SaveSession".to_string())];
+ let recorded = |a: &str| (a == "SaveSession").then(|| "ctrl-@".to_string());
+ let c = super::conflicts_on(&entries, &recorded, &us);
+ assert_eq!(c.get("SaveSession"), Some(&"Kart görünümü"));
+ assert_eq!(c.get("ViewGrid"), Some(&"Oturumu kaydet"));
+ }
+
#[test]
fn pretty_keys() {
assert_eq!(pretty("ctrl-shift-k"), "Ctrl+Shift+K");
diff --git a/crates/rf-app/src/lib.rs b/crates/rf-app/src/lib.rs
index 90a5eb59..0edcaed4 100644
--- a/crates/rf-app/src/lib.rs
+++ b/crates/rf-app/src/lib.rs
@@ -1045,8 +1045,9 @@ pub fn main() {
})
.detach();
cx.set_global(status_center::OpsGlobal(ops));
- actions::bind(cx);
- actions::bind_user(cx);
+ actions::rebind(cx);
+ // (the layout forms of Ctrl+Shift+digit follow the keyboard layout in use)
+ cx.on_keyboard_layout_change(actions::rebind).detach();
keymap_editor::init(cx);
if let Some(page) = &settings_arg {
settings_view::open_page(