mirror of
https://github.com/anomalyco/hex.git
synced 2026-11-28 13:02:21 +01:00
279 lines
9.3 KiB
Rust
279 lines
9.3 KiB
Rust
//! 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, HotkeyBinding, HotkeyKey, HotkeyModifiers};
|
||
use crate::desktop::i18n::{tr, tr_fill};
|
||
use crate::dictation::ShortcutMode;
|
||
|
||
/// The four ways a shortcut triggers, shared with Labs › More dictation
|
||
/// shortcuts' rows, which save it as is.
|
||
pub(super) use crate::app_settings::ExtraDictationMode as Trigger;
|
||
|
||
impl Trigger {
|
||
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 => Self::DoubleTap.for_binding(
|
||
Some(&settings.dictation_hotkey),
|
||
settings.double_tap_modifiers_allowed(),
|
||
),
|
||
ShortcutMode::Hold => Self::HoldOrDoubleTap,
|
||
}
|
||
}
|
||
|
||
pub(super) fn apply(self, settings: &mut AppSettings) {
|
||
let (toggle, lock, only) = self.switches();
|
||
settings.shortcut_mode = if toggle {
|
||
ShortcutMode::Toggle
|
||
} else {
|
||
ShortcutMode::Hold
|
||
};
|
||
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.for_binding(binding, modifiers_allowed) != self).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("+"),
|
||
)
|
||
})
|
||
}
|
||
}
|
||
|
||
/// 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()
|
||
);
|
||
}
|
||
}
|