hex/src/rune_ui/shortcut_mode.rs
Dax 6fa9f18bf4
labs: more dictation shortcuts, each with its own mode (#1603)
* more dictation shortcuts: up to four extras, each with its own mode

Labs › More dictation shortcuts (macOS) adds up to four more dictation
shortcuts beside the main one. Each saved row keeps its own binding and
activation mode, registers only while the Lab is on, and shares no key
with another active shortcut. Settings records, clears, and rebinds each
row; the listener runs every press through the same capture pipeline, so
each shortcut finishes with the mode it was pressed with.

* more dictation shortcuts: review fixes

* more dictation shortcuts: follow lone-modifier double-tap, narrow globe check

* more dictation shortcuts: doc nits

* more dictation shortcuts: rebase fixes

Moves the recorded-shortcut checks beside the rest of the shortcut text in
`control/settings/hotkeys.rs` so `control/settings.rs` stays under the
1,500-line ceiling, drops the `labs.rs` size exception now that dev moved the
Feature registry into `labs/feature.rs`, and counts Meeting notes file
alongside More dictation shortcuts in `Feature::ALL`.
2026-11-10 06:25:59 -05:00

322 lines
11 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 mode: one choice of how the dictation shortcut triggers, in place
//! of the three saved settings behind it (`shortcut_mode`, `double_tap_lock`,
//! `double_tap_only`). Of their eight combinations only four behave
//! differently: Toggle ignores both double-tap settings, and double-tap only
//! needs the lock and a shortcut with a key (or, with Labs › Double-tap on a
//! lone modifier, a shortcut without a key, one or more modifiers). General's
//! row names the choice and opens the modal in `explainer` that makes it.
use rune::El;
use super::Ui;
use super::controls::value_button;
use super::controls::{Paint, copy_row};
use super::explainer::{self, Script};
use super::icons::HAND;
use crate::app_settings::{
AppSettings, ExtraDictationMode, HotkeyBinding, HotkeyKey, HotkeyModifiers,
};
use crate::desktop::i18n::{tr, tr_fill};
use crate::dictation::ShortcutMode;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(super) enum Trigger {
HoldOrDoubleTap,
Hold,
Tap,
DoubleTap,
}
impl Trigger {
/// In the modal's order, the default first.
pub(super) const ALL: [Self; 4] = [
Self::HoldOrDoubleTap,
Self::Hold,
Self::Tap,
Self::DoubleTap,
];
pub(super) fn of(settings: &AppSettings) -> Self {
match settings.shortcut_mode {
ShortcutMode::Toggle => Self::Tap,
ShortcutMode::Hold if !settings.double_tap_lock => Self::Hold,
ShortcutMode::Hold
if settings.double_tap_only
&& (settings.dictation_hotkey.key.is_some()
|| settings.double_tap_modifiers_allowed()) =>
{
Self::DoubleTap
}
ShortcutMode::Hold => Self::HoldOrDoubleTap,
}
}
pub(super) fn apply(self, settings: &mut AppSettings) {
let (mode, lock, only) = match self {
Self::HoldOrDoubleTap => (ShortcutMode::Hold, true, false),
Self::Hold => (ShortcutMode::Hold, false, false),
Self::Tap => (ShortcutMode::Toggle, true, false),
Self::DoubleTap => (ShortcutMode::Hold, true, true),
};
settings.shortcut_mode = mode;
settings.double_tap_lock = lock;
settings.double_tap_only = only;
}
pub(super) fn key(self) -> &'static str {
match self {
Self::HoldOrDoubleTap => "trigger-hold-or-double-tap",
Self::Hold => "trigger-hold",
Self::Tap => "trigger-tap",
Self::DoubleTap => "trigger-double-tap",
}
}
pub(super) fn name(self) -> &'static str {
match self {
Self::HoldOrDoubleTap => tr("Hold or double-tap"),
Self::Hold => tr("Hold"),
Self::Tap => tr("Tap"),
Self::DoubleTap => tr("Double-tap"),
}
}
pub(super) fn caption(self) -> &'static str {
match self {
Self::HoldOrDoubleTap => tr("Hold to talk, or double-tap to talk hands-free"),
Self::Hold => tr("Hold to talk, let go to paste"),
Self::Tap => tr("Tap to talk hands-free, tap again to paste"),
Self::DoubleTap => tr("Double-tap to talk hands-free, tap again to paste"),
}
}
pub(super) fn script(self) -> &'static Script {
match self {
Self::HoldOrDoubleTap => &explainer::HOLD_OR_LOCK,
Self::Hold => &explainer::HOLD,
Self::Tap => &explainer::TAP,
Self::DoubleTap => &explainer::DOUBLE_TAP,
}
}
/// Why this shortcut can't trigger this way, if it can't: a shortcut
/// without a key can't tell a double-tap from a chord without
/// Labs › Double-tap on a lone modifier.
pub(super) fn unavailable(self, settings: &AppSettings) -> Option<String> {
self.unavailable_for(
Some(&settings.dictation_hotkey),
settings.double_tap_modifiers_allowed(),
)
}
/// [`Self::unavailable`] for a Labs › More dictation shortcuts row, whose
/// binding is its own. `modifiers_allowed` is Labs › Double-tap on a lone
/// modifier, which the row follows as the dictation shortcut does.
pub(super) fn unavailable_for(
self,
binding: Option<&HotkeyBinding>,
modifiers_allowed: bool,
) -> Option<String> {
(self == Self::DoubleTap
&& !modifiers_allowed
&& binding.is_none_or(|binding| binding.key.is_none()))
.then(|| {
let mut example = binding.cloned().unwrap_or_else(|| HotkeyBinding {
modifiers: HotkeyModifiers::option(),
key: None,
});
example.key = Some(HotkeyKey::named("space"));
tr_fill(
"Needs a shortcut with a key, like {}",
&example.keycaps().join("+"),
)
})
}
/// The choice that saves `mode` on a Labs › More dictation shortcuts row.
#[cfg_attr(not(target_os = "macos"), allow(dead_code))]
pub(super) fn from_extra(mode: ExtraDictationMode) -> Self {
match mode {
ExtraDictationMode::HoldOrDoubleTap => Self::HoldOrDoubleTap,
ExtraDictationMode::Hold => Self::Hold,
ExtraDictationMode::Tap => Self::Tap,
ExtraDictationMode::DoubleTap => Self::DoubleTap,
}
}
/// The mode a Labs › More dictation shortcuts row saves for `self`.
#[cfg_attr(not(target_os = "macos"), allow(dead_code))]
pub(super) fn to_extra(self) -> ExtraDictationMode {
match self {
Self::HoldOrDoubleTap => ExtraDictationMode::HoldOrDoubleTap,
Self::Hold => ExtraDictationMode::Hold,
Self::Tap => ExtraDictationMode::Tap,
Self::DoubleTap => ExtraDictationMode::DoubleTap,
}
}
}
/// Saves `trigger` unless it is already the choice.
pub(super) fn pick(ui: &mut Ui, trigger: Trigger) {
if Trigger::of(&ui.settings) != trigger {
ui.update(|settings| trigger.apply(settings));
}
}
/// General's row, under the shortcut: the choice, opening the modal.
pub(super) fn row(ui: &Ui, p: Paint) -> El {
copy_row(
p,
"shortcut-mode",
Some(&HAND),
tr("Shortcut mode"),
None,
value_button(
p,
"shortcut-mode-control",
tr("Shortcut mode"),
Trigger::of(&ui.settings).name(),
&rune_theme_opencode::icons::CHEVRON_RIGHT,
explainer::open,
),
false,
)
}
/// The modal's scenes.
enum Example {
/// Option alone, which can't double-tap.
Modifier,
/// Option-Space, double-tapping.
Key,
/// A three-key chord: Control-Option-Space.
Chord,
/// Loops, for `--frames` clips.
Motion,
}
pub(super) fn scene_modifier(ui: &mut Ui) {
scene(ui, Example::Modifier);
}
pub(super) fn scene_key(ui: &mut Ui) {
scene(ui, Example::Key);
}
pub(super) fn scene_chord(ui: &mut Ui) {
scene(ui, Example::Chord);
}
pub(super) fn scene_motion(ui: &mut Ui) {
scene(ui, Example::Motion);
}
fn scene(ui: &mut Ui, example: Example) {
*ui = super::mac_fixture();
let hotkey = &mut ui.settings.dictation_hotkey;
match example {
Example::Modifier | Example::Motion => hotkey.key = None,
Example::Key => {
hotkey.key = Some(HotkeyKey::named("space"));
Trigger::DoubleTap.apply(&mut ui.settings);
}
Example::Chord => {
hotkey.modifiers.control = hotkey.modifiers.option;
hotkey.key = Some(HotkeyKey::named("space"));
}
}
let held = !matches!(example, Example::Motion);
explainer::fixture(ui, held.then_some(explainer::SNAPSHOT_MS));
}
#[cfg(test)]
mod tests {
use super::*;
fn settings(key: bool) -> AppSettings {
let mut settings = AppSettings::default();
settings.dictation_hotkey.key = key.then(|| HotkeyKey::named("space"));
settings
}
#[test]
fn each_trigger_reads_back_as_itself() {
let mut settings = settings(true);
for trigger in Trigger::ALL {
trigger.apply(&mut settings);
assert_eq!(Trigger::of(&settings), trigger);
}
}
#[test]
fn the_default_is_hold_or_double_tap() {
assert_eq!(Trigger::of(&settings(false)), Trigger::HoldOrDoubleTap);
}
/// The eight saved combinations fold onto the four triggers.
#[test]
fn redundant_and_invalid_combinations_fold_away() {
for key in [false, true] {
for (lock, only) in [(false, false), (false, true), (true, false), (true, true)] {
let mut settings = settings(key);
settings.double_tap_lock = lock;
settings.double_tap_only = only;
settings.shortcut_mode = ShortcutMode::Toggle;
assert_eq!(Trigger::of(&settings), Trigger::Tap);
settings.shortcut_mode = ShortcutMode::Hold;
let expected = match (lock, only && key) {
(false, _) => Trigger::Hold,
(true, true) => Trigger::DoubleTap,
(true, false) => Trigger::HoldOrDoubleTap,
};
assert_eq!(
Trigger::of(&settings),
expected,
"lock {lock} only {only} key {key}"
);
}
}
}
#[test]
fn double_tap_needs_a_key() {
assert!(Trigger::DoubleTap.unavailable(&settings(false)).is_some());
assert!(Trigger::DoubleTap.unavailable(&settings(true)).is_none());
for trigger in [Trigger::HoldOrDoubleTap, Trigger::Hold, Trigger::Tap] {
assert!(trigger.unavailable(&settings(false)).is_none());
}
}
/// A Labs › More dictation shortcuts row follows Labs › Double-tap on a
/// lone modifier as the dictation shortcut does.
#[test]
fn a_row_needs_a_key_or_the_lone_modifier_lab() {
let lone = HotkeyBinding {
modifiers: HotkeyModifiers::option(),
key: None,
};
let keyed = HotkeyBinding {
key: Some(HotkeyKey::named("space")),
..lone.clone()
};
assert!(
Trigger::DoubleTap
.unavailable_for(Some(&lone), false)
.is_some()
);
assert!(Trigger::DoubleTap.unavailable_for(None, false).is_some());
// The lab lets a lone modifier double-tap, and an unbound row keep
// the choice until it is bound.
assert!(
Trigger::DoubleTap
.unavailable_for(Some(&lone), true)
.is_none()
);
assert!(Trigger::DoubleTap.unavailable_for(None, true).is_none());
assert!(
Trigger::DoubleTap
.unavailable_for(Some(&keyed), false)
.is_none()
);
}
}