diff options
| author | duckyriley <[email protected]> | 2026-11-26 10:30:50 -0400 |
|---|---|---|
| committer | duckyriley <[email protected]> | 2026-11-26 10:30:50 -0400 |
| commit | 7a7e3da4103f8dcf3c69f0cc6b9d0cb666e89ce9 (patch) | |
| tree | 2a2bb60c973b861b2bd9a525f513630f784d8b43 /src/config.rs | |
| parent | 9ef7ba021629c9fa3e111798c61b35a02dfaae20 (diff) | |
| download | ducky-7a7e3da4103f8dcf3c69f0cc6b9d0cb666e89ce9.tar.gz ducky-7a7e3da4103f8dcf3c69f0cc6b9d0cb666e89ce9.zip | |
Use Command for macOS shortcuts
Diffstat (limited to 'src/config.rs')
| -rw-r--r-- | src/config.rs | 104 |
1 files changed, 74 insertions, 30 deletions
diff --git a/src/config.rs b/src/config.rs index 3532f80..6181d77 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,4 +1,5 @@ use anyhow::{Context, Result, bail}; +use gpui::Keystroke; use mlua::{Lua, Table}; use std::{collections::HashMap, path::PathBuf}; @@ -43,32 +44,63 @@ pub fn color(s: &str) -> Result<u32> { } Ok(u32::from_str_radix(s, 16)?) } -impl Config { - pub fn action_for(&self, key: &gpui::Keystroke) -> Option<String> { - self.shortcuts.iter().find_map(|(binding, action)| { - let parsed = gpui::Keystroke::parse(binding).ok()?; - (parsed.modifiers == key.modifiers && parsed.key.eq_ignore_ascii_case(&key.key)) - .then(|| action.clone()) +pub const MACOS: bool = cfg!(target_os = "macos"); +pub const SHORTCUT_MODIFIER: &str = if MACOS { "Cmd" } else { "Ctrl" }; +pub const ALT_MODIFIER: &str = if MACOS { "Option" } else { "Alt" }; + +fn parse_binding(binding: &str) -> Result<Keystroke, gpui::InvalidKeystrokeError> { + let mut key = Keystroke::parse(binding)?; + // macOS reserves Command+Tab for switching applications. + if MACOS && key.modifiers.control && !key.modifiers.platform && key.key != "tab" { + key.modifiers.control = false; + key.modifiers.platform = true; + } + Ok(key) +} + +fn binding_value<'a>(bindings: &'a HashMap<String, String>, key: &Keystroke) -> Option<&'a str> { + let stroke = key.unparse(); + bindings + .iter() + .filter(|(binding, _)| { + parse_binding(binding).is_ok_and(|parsed| { + parsed.modifiers == key.modifiers && parsed.key.eq_ignore_ascii_case(&key.key) + }) }) + .min_by_key(|(binding, _)| (binding.as_str() != stroke, binding.as_str())) + .map(|(_, value)| value.as_str()) +} + +impl Config { + pub fn action_for(&self, key: &Keystroke) -> Option<&str> { + binding_value(&self.shortcuts, key) + } + + pub fn bytes_for(&self, key: &Keystroke) -> Option<&str> { + binding_value(&self.bindings, key) } + pub fn shortcut(&self, action: &str) -> String { - let mut keys = self - .shortcuts + self.shortcuts .iter() - .filter(|(_, a)| a.as_str() == action) - .map(|(k, _)| k.clone()) - .collect::<Vec<_>>(); - keys.sort(); - keys.first() - .map(|k| { - k.split('-') + .filter(|(_, value)| value.as_str() == action) + .map(|(binding, _)| binding) + .min() + .and_then(|binding| parse_binding(binding).ok()) + .map(|key| { + let stroke = key.unparse(); + let modifiers = stroke.strip_suffix(&key.key).unwrap_or_default(); + modifiers + .split_terminator('-') .map(|part| match part { "ctrl" => "Ctrl".into(), - "alt" => "Alt".into(), + "cmd" => "Cmd".into(), + "alt" => ALT_MODIFIER.into(), "shift" => "Shift".into(), s => s.to_uppercase(), }) - .collect::<Vec<String>>() + .chain(std::iter::once(key.key.to_uppercase())) + .collect::<Vec<_>>() .join("+") }) .unwrap_or_else(|| "unbound".into()) @@ -209,8 +241,7 @@ impl Config { ); } for (key, action) in &c.shortcuts { - gpui::Keystroke::parse(key) - .map_err(|e| anyhow::anyhow!("invalid shortcut {key}: {e}"))?; + parse_binding(key).map_err(|e| anyhow::anyhow!("invalid shortcut {key}: {e}"))?; let valid = matches!( action.as_str(), "new_tab" @@ -254,16 +285,31 @@ mod tests { #[test] fn lua_shortcuts_separate_copy_interrupt_and_tab_selection() { let c = Config::parse(DEFAULT, std::path::Path::new("/tmp")).unwrap(); + let primary = if MACOS { "cmd" } else { "ctrl" }; for (key, action) in [ - ("ctrl-c", "copy"), - ("ctrl-alt-c", "interrupt"), - ("alt-4", "tab:4"), - ("ctrl-r", "reload"), + (format!("{primary}-c"), "copy"), + (format!("{primary}-alt-c"), "interrupt"), + (format!("{primary}-r"), "reload"), + ("alt-4".into(), "tab:4"), + ("ctrl-tab".into(), "next_tab"), + ("ctrl-shift-tab".into(), "previous_tab"), ] { - assert_eq!( - c.action_for(&gpui::Keystroke::parse(key).unwrap()) - .as_deref(), - Some(action) + assert_eq!(c.action_for(&Keystroke::parse(&key).unwrap()), Some(action)); + } + assert_eq!(c.shortcut("copy"), format!("{SHORTCUT_MODIFIER}+C")); + assert_eq!(c.shortcut("font_smaller"), format!("{SHORTCUT_MODIFIER}+-")); + assert_eq!(c.shortcut("next_tab"), "Ctrl+TAB"); + assert_eq!(c.shortcut("tab:4"), format!("{ALT_MODIFIER}+4")); + assert_eq!( + c.bytes_for(&Keystroke::parse(&format!("{primary}-g")).unwrap()), + Some("git status\r") + ); + if MACOS { + assert!(c.action_for(&Keystroke::parse("ctrl-c").unwrap()).is_none()); + assert!(c.bytes_for(&Keystroke::parse("ctrl-g").unwrap()).is_none()); + assert!( + c.action_for(&Keystroke::parse("cmd-tab").unwrap()) + .is_none() ); } let custom = Config::parse( @@ -272,9 +318,7 @@ mod tests { ) .unwrap(); assert_eq!( - custom - .action_for(&gpui::Keystroke::parse("alt-x").unwrap()) - .as_deref(), + custom.action_for(&gpui::Keystroke::parse("alt-x").unwrap()), Some("copy") ); assert!( |