hex/src/app_settings/shortcut_repair.rs
Dax b1a37aaa78
Labs: terminal paste shortcut on Windows and Linux (#1534)
* Add Labs terminal paste shortcut for Windows and Linux

* fix windows build: drop the unused key_down from input/modifiers

* allow the terminal-paste shortcut code to go unused on macos

* mask the second shortcut's menu modifier on auto-repeats

the mask was only set on the first press, so process_button cleared it on each
repeat key-down and a held capture over the repeat delay leaked a bare alt/win
release. set it on every trigger key-down like the dictation key and paste
last do.

* keep side_matches private

* keep the shortcut and dictation helpers under the source cap

windows/dictation.rs, rune_ui/general.rs, and linux/dictation.rs crossed 1,500
non-test lines after the rebase. move the registered-shortcut tables and the
General pane's shortcut rows into submodules, and refresh the Linux Labs
golden for Transform typed text and Terminal paste shortcut.

* call the global setting Terminal paste in typed text's refusal

the lab now owns the name Terminal paste shortcut, so the typed-text errors
and log line must name the global paste_with_shift setting instead.

* drop the terminal paste lab's unused encoded bit

no accessor reads it and no other lab has a bit without a reader; the feature
reads its saved switch directly through Feature::on.

* raw dictation has a hex command; the terminal paste shortcut does not
2026-11-08 07:30:29 -05:00

272 lines
10 KiB
Rust
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//! 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);
}
}
}