//! Shortcut keys a Labs feature allows, and the repair that puts a saved //! shortcut on one back while the feature is off: mouse buttons (Labs › //! Mouse shortcuts, macOS) and Menu, Pause, or Scroll Lock (Labs › Special //! keys alone, Linux). use super::{AppSettings, HotkeyBinding, HotkeyKey}; impl HotkeyKey { /// Menu, Pause, or Scroll Lock: keys that type nothing, which Labs › /// Special keys alone lets a Linux shortcut use (issue 767). pub fn is_special(&self) -> bool { !self.is_mouse() && Self::special_name(&self.label) } /// Whether `name` names Menu, Pause, or Scroll Lock. pub fn special_name(name: &str) -> bool { is_scroll_lock(name) || matches!(name.trim().to_ascii_lowercase().as_str(), "menu" | "pause") } /// The § key, left of 1 on ISO keyboards, which Labs › Section key /// alone lets a Linux shortcut use on its own (issue 1050). pub fn is_section(&self) -> bool { !self.is_mouse() && Self::section_name(&self.label) } /// Whether `name` names the § key: `§` or `section`. pub fn section_name(name: &str) -> bool { let name = name.trim(); name == "§" || name.eq_ignore_ascii_case("section") } /// Insert, Home, End, Page Up, or Page Down: keys that type nothing, /// which Labs › Special keys alone lets a Windows shortcut use alone /// (issue 794). #[cfg_attr(not(target_os = "windows"), allow(dead_code))] pub fn is_navigation(&self) -> bool { !self.is_mouse() && Self::navigation_name(&self.label) } /// Whether `name` names Insert, Home, End, Page Up, or Page Down. pub fn navigation_name(name: &str) -> bool { matches!( name.trim() .to_ascii_lowercase() .replace([' ', '_'], "") .as_str(), "insert" | "home" | "end" | "pageup" | "pagedown" ) } } impl HotkeyBinding { /// A navigation key alone (`HotkeyKey::is_navigation`). #[cfg_attr(not(target_os = "windows"), allow(dead_code))] pub fn is_lone_navigation_key(&self) -> bool { self.modifiers.is_empty() && self.key.as_ref().is_some_and(HotkeyKey::is_navigation) } /// The § key alone (`HotkeyKey::is_section`). pub fn is_lone_section_key(&self) -> bool { self.modifiers.is_empty() && self.key.as_ref().is_some_and(HotkeyKey::is_section) } } /// `scrolllock`, `scroll_lock`, or `Scroll Lock`. pub(super) fn is_scroll_lock(name: &str) -> bool { name.trim() .replace([' ', '_'], "") .eq_ignore_ascii_case("scrolllock") } impl AppSettings { /// Whether Labs › Special keys alone is on: on Linux a shortcut may use /// Menu, Pause, or Scroll Lock; on Windows, which takes those anyway, /// a navigation key alone. pub fn special_keys_allowed(&self) -> bool { cfg!(any(target_os = "linux", target_os = "windows")) && self.labs.standalone_special_keys && crate::labs::Feature::StandaloneSpecialKeys.shipped() } /// Whether Labs › Section key alone is on: a Linux shortcut may be the /// § key with no modifier. pub fn section_key_allowed(&self) -> bool { cfg!(target_os = "linux") && self.labs.section_key_alone && crate::labs::Feature::SectionKeyAlone.shipped() } /// With Labs › Section key alone off, a Linux shortcut on § alone goes /// back to its default (an optional one is unbound), so the listener /// never registers one. § with a modifier is unaffected. pub(crate) fn repair_section_key(&mut self) { if self.section_key_allowed() || !cfg!(target_os = "linux") { return; } if self.reset_shortcuts(HotkeyBinding::is_lone_section_key) { tracing::info!("reset § shortcuts without a modifier: Labs › Section key alone is off"); } } /// With Labs › Mouse shortcuts off, a mouse-button shortcut (issue 209) /// goes back to its default and the window notes it once. #[cfg_attr(not(target_os = "macos"), allow(dead_code))] pub(super) fn repair_mouse_shortcuts(&mut self) { if self.labs.mouse_shortcuts { return; } if self.reset_shortcuts(|binding| binding.key.as_ref().is_some_and(HotkeyKey::is_mouse)) { self.mouse_shortcut_notice = true; } if self.mouse_shortcut_notice { tracing::info!("reset mouse-button shortcuts: Labs › Mouse shortcuts is off"); } } /// With Labs › Special keys alone off, a Linux shortcut on Menu, Pause, /// or Scroll Lock goes back to its default (an optional one is unbound), /// so the listener never registers one. Other OSes keep theirs. pub(crate) fn repair_special_keys(&mut self) { if self.special_keys_allowed() { return; } if cfg!(target_os = "linux") && self .reset_shortcuts(|binding| binding.key.as_ref().is_some_and(HotkeyKey::is_special)) { tracing::info!( "reset Menu, Pause, and Scroll Lock shortcuts: Labs › Special keys alone is off" ); } // Windows: a navigation key alone needs the toggle. if cfg!(target_os = "windows") && self.reset_shortcuts(HotkeyBinding::is_lone_navigation_key) { tracing::info!( "reset navigation-key shortcuts without a modifier: Labs › Special keys alone is off" ); } } /// Puts back every shortcut `matches`: dictation, Paste last dictation, /// and Annotate to their defaults, the optional ones unbound. Returns /// whether any changed. fn reset_shortcuts(&mut self, matches: impl Fn(&HotkeyBinding) -> bool) -> bool { let mut reset = false; if matches(&self.dictation_hotkey) { self.dictation_hotkey = HotkeyBinding::default(); reset = true; } if self.paste_last_hotkey.as_ref().is_some_and(&matches) { self.paste_last_hotkey = Some(HotkeyBinding::paste_last_default()); reset = true; } if self.raw_dictation_hotkey.as_ref().is_some_and(&matches) { self.raw_dictation_hotkey = None; reset = true; } if self.typed_text_hotkey.as_ref().is_some_and(&matches) { self.typed_text_hotkey = None; reset = true; } if self.second_dictation_hotkey.as_ref().is_some_and(&matches) { self.second_dictation_hotkey = None; reset = true; } if self.terminal_paste_hotkey.as_ref().is_some_and(&matches) { self.terminal_paste_hotkey = None; reset = true; } #[cfg(hex_capture)] if matches(&self.capture.hotkey) { self.capture.hotkey = HotkeyBinding::capture_default(); reset = true; } #[cfg(hex_capture)] if self.capture.replay_hotkey.as_ref().is_some_and(&matches) { self.capture.replay_hotkey = None; reset = true; } reset } } #[cfg(test)] mod tests { use super::*; fn menu() -> HotkeyBinding { HotkeyBinding { modifiers: Default::default(), key: Some(HotkeyKey::named("menu")), } } #[test] fn menu_pause_and_scroll_lock_are_the_special_keys() { for name in ["menu", "Pause", "scrolllock", "scroll_lock", "Scroll Lock"] { assert!(HotkeyKey::special_name(name), "{name}"); assert!(HotkeyKey::named(name).is_special(), "{name}"); } assert_eq!(HotkeyKey::named("scrolllock").label, "Scroll Lock"); for name in ["f9", "space", "m", "mouse4"] { assert!(!HotkeyKey::named(name).is_special(), "{name}"); } } /// Off, a saved Menu shortcut goes back to its default on Linux (and /// stays elsewhere, where the toggle doesn't exist); on, it stays. #[test] fn special_key_shortcuts_go_back_to_their_default_while_their_labs_toggle_is_off() { let mut settings = AppSettings { dictation_hotkey: menu(), raw_dictation_hotkey: Some(menu()), ..Default::default() }; let mut on = settings.clone(); on.labs.standalone_special_keys = true; on.repair_special_keys(); assert_eq!(on.dictation_hotkey, menu()); assert_eq!( on.special_keys_allowed(), cfg!(any(target_os = "linux", target_os = "windows")) ); settings.repair_special_keys(); if cfg!(target_os = "linux") { assert_eq!(settings.dictation_hotkey, HotkeyBinding::default()); assert_eq!(settings.raw_dictation_hotkey, None); } else { assert_eq!(settings.dictation_hotkey, menu()); } } /// Issue 1050: off, a saved § shortcut without a modifier goes back to /// its default on Linux; § with a modifier, and § with the toggle on, /// stay. #[test] fn a_lone_section_key_goes_back_to_its_default_while_its_labs_toggle_is_off() { let section = HotkeyBinding { modifiers: Default::default(), key: Some(HotkeyKey::named("section")), }; assert!(section.is_lone_section_key()); assert_eq!(HotkeyKey::named("section").label, "§"); assert!(HotkeyKey::named("§").is_section()); assert!(!HotkeyKey::named("s").is_section()); let with_control = HotkeyBinding { modifiers: super::super::HotkeyModifiers::control_option(), key: Some(HotkeyKey::named("§")), }; assert!(!with_control.is_lone_section_key()); let mut settings = AppSettings { dictation_hotkey: section.clone(), raw_dictation_hotkey: Some(with_control.clone()), ..Default::default() }; let mut on = settings.clone(); on.labs.section_key_alone = true; on.repair_section_key(); assert_eq!(on.dictation_hotkey, section); settings.repair_section_key(); assert_eq!(settings.raw_dictation_hotkey, Some(with_control)); if cfg!(target_os = "linux") { assert!(on.section_key_allowed()); assert_eq!(settings.dictation_hotkey, HotkeyBinding::default()); } else { assert!(!on.section_key_allowed()); assert_eq!(settings.dictation_hotkey, section); } } }