hex/src/app_settings/shortcut_repair.rs
Dax 67ec50e65d
labs: double-tap on a lone modifier (#1589)
* add a labs switch for double-tap on a lone modifier

* keep double-tap on a lone modifier on macos

* say windows and linux gates are unchanged, not their saved files

* dirty a keyless double-tap only on an uncovered modifier

* rebase: give the double-tap lab its own bit and refresh the labs goldens

- the double-tap switch sits above process api transcripts instead of sharing its bit
- mac labs goldens now show transcribe files and process api transcripts as well
2026-11-09 13:00:09 -05:00

354 lines
13 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), Menu, Pause, or Scroll Lock (Labs › Special
//! keys alone, Linux), shortcuts without a key (Labs › Double-tap on a
//! lone modifier, macOS), and the persisted double-tap mode those need.
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()
}
/// Whether Labs › Double-tap on a lone modifier is on: Double-tap
/// shortcut mode accepts a dictation shortcut without a key (macOS).
pub fn double_tap_modifiers_allowed(&self) -> bool {
cfg!(target_os = "macos")
&& self.labs.double_tap_modifiers
&& crate::labs::Feature::DoubleTapModifiers.shipped()
}
/// Clears a double-tap-only choice a shortcut can't support: the lock
/// is off, or a lone modifier is not allowed by its Labs switch. With
/// the lab off, a modifier-only binding falls back exactly as a fresh
/// load does (`shortcut_mode` stays Hold, `double_tap_lock` stays on).
pub(crate) fn normalize_double_tap_settings(&mut self) {
if !self.double_tap_lock
|| (self.dictation_hotkey.key.is_none() && !self.double_tap_modifiers_allowed())
{
self.double_tap_only = false;
}
}
/// 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;
}
if self
.screenshots
.record_hotkey
.as_ref()
.is_some_and(&matches)
{
self.screenshots.record_hotkey = None;
reset = true;
}
reset
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::app_settings::HotkeyModifiers;
fn menu() -> HotkeyBinding {
HotkeyBinding {
modifiers: Default::default(),
key: Some(HotkeyKey::named("menu")),
}
}
#[test]
fn double_tap_only_on_a_lone_modifier_needs_the_lab() {
let binding = |key: Option<HotkeyKey>| HotkeyBinding {
modifiers: HotkeyModifiers::option(),
key,
};
let mut settings = AppSettings {
dictation_hotkey: binding(None),
double_tap_only: true,
..Default::default()
};
settings.normalize_double_tap_settings();
assert!(!settings.double_tap_only);
settings.labs.double_tap_modifiers = true;
settings.double_tap_only = true;
settings.normalize_double_tap_settings();
// Modifier-only dictation exists on macOS.
assert_eq!(
settings.double_tap_only,
settings.double_tap_modifiers_allowed()
);
// A keyed binding keeps the choice with the lab off.
settings.labs.double_tap_modifiers = false;
settings.dictation_hotkey = binding(Some(HotkeyKey::named("space")));
settings.double_tap_only = true;
settings.normalize_double_tap_settings();
assert!(settings.double_tap_only);
}
#[test]
fn turning_the_double_tap_modifiers_lab_off_falls_back_as_a_fresh_load() {
let mut settings = AppSettings {
dictation_hotkey: HotkeyBinding {
modifiers: HotkeyModifiers::option(),
key: None,
},
double_tap_only: true,
..Default::default()
};
settings.labs.double_tap_modifiers = true;
settings.normalize_double_tap_settings();
crate::labs::Feature::DoubleTapModifiers.set(&mut settings, false);
assert!(!settings.double_tap_only);
assert!(settings.double_tap_lock, "the lock stays on");
assert_eq!(
settings.shortcut_mode,
crate::dictation::ShortcutMode::Hold,
"the mode returns to Hold or double-tap"
);
}
#[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);
}
}
}