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.

A one-key shortcut no longer fires while typing in a text box (1031); default sizes work with a shared layout (878)

gelenmedyaa3344df403f0
diff --git a/crates/rf-app/src/actions.rs b/crates/rf-app/src/actions.rs
index 281ba8cf..6e7e7ba4 100644
--- a/crates/rf-app/src/actions.rs
+++ b/crates/rf-app/src/actions.rs
@@ -150,7 +150,7 @@ pub fn bind_user(cx: &mut App) {
for (keys, action) in map {
let name = if action.contains("::") { action.clone() } else { format!("rustyfiles::{action}") };
match cx.build_action(&name, None) {
- Ok(a) => match KeyBinding::load(&keys, a, None, false, None, cx) {
+ Ok(a) => match user_binding(&keys, a, cx) {
Ok(b) => bindings.push(b),
Err(e) => log::warn!("keybinding {keys:?}: {e}"),
},
@@ -160,6 +160,34 @@ pub fn bind_user(cx: &mut App) {
cx.bind_keys(bindings);
}
+/// A shortcut of the user's own, bound everywhere. One whose keys type a character ([`types_text`]) is left out while a text box
+/// has the keyboard (the rename box, the address bar, a dialog's field), so the character is typed there.
+///
+/// WHY (issue 1031: "I set F as the shortcut for New folder; typing F in the new folder's name creates another one"). A binding is
+/// matched before the text box is asked, and the box has no binding of its own for a letter, so the shortcut took every F typed.
+pub(crate) fn user_binding(keys: &str, action: Box<dyn Action>, cx: &App) -> Result<KeyBinding, InvalidKeystrokeError> {
+ // ("Input" is the component library's context for every text box; a rule with `!` is false when it is anywhere on the focus path)
+ let outside_text = if types_text(keys) { KeyBindingContextPredicate::parse("!Input").ok().map(std::rc::Rc::new) } else { None };
+ KeyBinding::load(keys, action, outside_text, false, None, cx)
+}
+
+/// Whether pressing these keys types a character into a text box: a single key that is a letter, a digit, a sign or the space bar,
+/// with no Ctrl, Alt or Windows key held (Shift only makes another character). Pure.
+pub(crate) fn types_text(keys: &str) -> bool {
+ let mut strokes = keys.split_whitespace();
+ let (Some(stroke), None) = (strokes.next(), strokes.next()) else { return false };
+ // ("ctrl--" and "-" end with the minus key itself)
+ let (mods, key) = if stroke == "-" {
+ ("", "-")
+ } else if let Some(m) = stroke.strip_suffix("--") {
+ (m, "-")
+ } else {
+ stroke.rsplit_once('-').unwrap_or(("", stroke))
+ };
+ let typed = key == "space" || key.chars().count() == 1;
+ typed && mods.split('-').all(|m| m.is_empty() || m.eq_ignore_ascii_case("shift"))
+}
+
/// 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.)
@@ -337,7 +365,7 @@ fn bind_layout_shifted(cx: &mut App) {
#[cfg(test)]
mod tests {
// (not `super::*`: gpui's `test` macro would take the place of `#[test]`)
- use super::{DuplicateItems, FOLDER_CTX, NewTab, ZoomIn, short_name, without_customized};
+ use super::{DuplicateItems, FOLDER_CTX, NewTab, ZoomIn, short_name, types_text, without_customized};
use gpui_kit::KeyBinding;
#[test]
@@ -357,4 +385,15 @@ mod tests {
let custom = ["DuplicateItems".to_string(), short_name("rustyfiles::ZoomIn").to_string()].into_iter().collect();
assert_eq!(names(without_customized(defaults(), &custom)), ["NewTab"]);
}
+
+ #[test]
+ fn a_shortcut_that_types_a_character_is_known_from_one_that_does_not() {
+ // (issue 1031: F as the shortcut for a new folder)
+ for keys in ["f", "F", "shift-f", "1", "-", "shift--", "/", "space", "shift-space", "ğ"] {
+ assert!(types_text(keys), "{keys}");
+ }
+ for keys in ["ctrl-f", "alt-f", "ctrl-shift-n", "win-e", "ctrl--", "f2", "f12", "delete", "enter", "shift-f10", "ctrl-k ctrl-f", "g g", ""] {
+ assert!(!types_text(keys), "{keys}");
+ }
+ }
}
diff --git a/crates/rf-app/src/folder_view.rs b/crates/rf-app/src/folder_view.rs
index a7b12cc1..8cee6891 100644
--- a/crates/rf-app/src/folder_view.rs
+++ b/crates/rf-app/src/folder_view.rs
@@ -1036,8 +1036,9 @@ impl FolderView {
cx.observe_global::<crate::settings::Settings>(move |this, cx| {
let s = cx.global::<crate::settings::Settings>();
let sizes = (s.default_row_size, s.default_grid_size);
- if sizes != std::mem::replace(&mut default_sizes, sizes) {
- this.default_sizes_changed(sizes, cx);
+ let before = std::mem::replace(&mut default_sizes, sizes);
+ if sizes != before {
+ this.default_sizes_changed(before, sizes, cx);
}
let s = cx.global::<crate::settings::Settings>();
let (hidden, system, dot) = (s.show_hidden, s.show_system_files, !s.show_dot_files);
@@ -3146,18 +3147,37 @@ impl FolderView {
});
}
- /// Settings › the default row or icon size changed: a folder shown with the defaults (no layout of its own, the layouts not
- /// shared) takes the new size now, not only when it is opened again (issue 878: "no visible effect").
- fn default_sizes_changed(&mut self, (row, grid): (u8, u8), cx: &mut Context<Self>) {
+ /// Settings › the default row or icon size changed: a folder shown with the defaults (no layout of its own) takes the new size
+ /// now, not only when it is opened again (issue 878: "no visible effect").
+ ///
+ /// WITH THE LAYOUTS SHARED ("the same layout in all folders") there is one layout, so the default size IS its size: every tab takes
+ /// the size that changed and the shared layout keeps it. Until 1.0.10 the setting was never read in that mode (a new tab began
+ /// with the built-in size or the shared layout's): one way it can "still not work" after the two earlier fixes.
+ fn default_sizes_changed(&mut self, before: (u8, u8), now: (u8, u8), cx: &mut Context<Self>) {
+ let (row, grid) = (now.0.min(ROW_SIZES.len() as u8 - 1), now.1.min(GRID_SIZES.len() as u8 - 1));
if cx.global::<crate::settings::Settings>().sync_layout {
+ // (only the size that was changed: the other may have been chosen in the tab)
+ if before.0 != now.0 {
+ self.row_size = row;
+ }
+ if before.1 != now.1 {
+ self.grid_size = grid;
+ }
+ let key = std::path::Path::new(SHARED_LAYOUT);
+ let mut shared = crate::layouts::get(key, cx).unwrap_or_else(|| self.layout_now());
+ let sizes = shared_sizes_after((shared.row_size, shared.grid_size), before, (row, grid));
+ if sizes != (shared.row_size, shared.grid_size) {
+ (shared.row_size, shared.grid_size) = sizes;
+ crate::layouts::set(key, shared, cx);
+ }
return;
}
let Location::Path(p) = self.location().clone() else { return };
if crate::layouts::get(&p, cx).is_some() {
return;
}
- self.row_size = row.min(ROW_SIZES.len() as u8 - 1);
- self.grid_size = grid.min(GRID_SIZES.len() as u8 - 1);
+ self.row_size = row;
+ self.grid_size = grid;
}
/// Per-folder memory: the user changed the view, sort or grouping of this folder.
@@ -3221,8 +3241,15 @@ impl FolderView {
let first = !std::mem::replace(&mut self.layout_seeded, true);
if s.sync_layout {
// the tab keeps its own from folder to folder; a new tab (and every tab at a start) begins with the shared one
- if first && let Some(l) = crate::layouts::get(std::path::Path::new(SHARED_LAYOUT), cx) {
- self.apply_layout_values(l, false, cx);
+ if first {
+ match crate::layouts::get(std::path::Path::new(SHARED_LAYOUT), cx) {
+ Some(l) => self.apply_layout_values(l, false, cx),
+ // nothing shared yet: the default sizes of Settings, as without sharing (issue 878)
+ None => {
+ self.row_size = s.default_row_size.min(ROW_SIZES.len() as u8 - 1);
+ self.grid_size = s.default_grid_size.min(GRID_SIZES.len() as u8 - 1);
+ }
+ }
}
return;
}
@@ -8680,6 +8707,26 @@ fn cut_to_width(text: &str, avail: Pixels, width: &dyn Fn(&str) -> Pixels) -> St
"…".to_string()
}
+/// The shared layout's (row, grid) sizes after the default sizes of Settings went from `before` to `now`: the one that changed is
+/// taken, the other stays as the shared layout has it. Pure.
+fn shared_sizes_after(shared: (u8, u8), before: (u8, u8), now: (u8, u8)) -> (u8, u8) {
+ (if before.0 != now.0 { now.0 } else { shared.0 }, if before.1 != now.1 { now.1 } else { shared.1 })
+}
+
+#[cfg(test)]
+mod shared_size_tests {
+ use super::shared_sizes_after;
+
+ #[test]
+ fn the_default_size_that_changed_becomes_the_shared_layouts_and_the_other_stays() {
+ // (issue 878, with the same layout in all folders) the row default goes from 1 to 4: the shared grid size, chosen in a tab, stays
+ assert_eq!(shared_sizes_after((1, 3), (1, 2), (4, 2)), (4, 3));
+ assert_eq!(shared_sizes_after((2, 3), (1, 2), (1, 0)), (2, 0));
+ assert_eq!(shared_sizes_after((2, 3), (1, 2), (4, 0)), (4, 0));
+ assert_eq!(shared_sizes_after((2, 3), (1, 2), (1, 2)), (2, 3));
+ }
+}
+
/// The key in the layouts file under which "the same layout in all folders" keeps its one layout (`<` cannot start a real path).
const SHARED_LAYOUT: &str = "<all folders>";
/// The layouts' key of the one layout all searches share.
diff --git a/crates/rf-app/src/keymap_editor.rs b/crates/rf-app/src/keymap_editor.rs
index cb4e643a..287e36f8 100644
--- a/crates/rf-app/src/keymap_editor.rs
+++ b/crates/rf-app/src/keymap_editor.rs
@@ -100,7 +100,7 @@ pub fn init(cx: &mut App) {
});
// bind just the new key (re-binding the whole map would stack duplicates)
match cx.build_action(&format!("rustyfiles::{action}"), None) {
- Ok(a) => match KeyBinding::load(&keys, a, None, false, None, cx) {
+ Ok(a) => match crate::actions::user_binding(&keys, a, cx) {
Ok(b) => cx.bind_keys([b]),
Err(e) => log::warn!("keybinding {keys:?}: {e}"),
},