Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 18 additions & 7 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -822,7 +822,9 @@ jobs:

# The insert check's UI Automation reader on this desktop (docs/dictation.md §28): a paste
# into Notepad is written, the test's own read-only EDIT stays not written, the focus moving
# to its other EDIT is unconfirmed. Only the small voltip-focus crate is built.
# to its other EDIT is unconfirmed. And the copy that reads the selection (§19, §35):
# Notepad's selection comes back while Ctrl+Alt of the shortcut is held, the clipboard put
# back. Only the small voltip-focus crate is built.
- name: Build the insert check's reader test
run: cargo test -p voltip-focus --test focus_windows --no-run

Expand Down Expand Up @@ -974,12 +976,13 @@ jobs:
libwebkit2gtk-4.1-dev libgtk-3-dev libsoup-3.0-dev libdbus-1-dev \
libayatana-appindicator3-dev librsvg2-dev libasound2-dev libssl-dev \
libx11-dev libxtst-dev libxcb1-dev patchelf pkg-config cmake \
xvfb xdotool scrot x11-utils python3-pil \
xvfb xdotool scrot x11-utils python3-pil python3-tk \
xclip pulseaudio pulseaudio-utils libasound2-plugins \
weston sway wtype wl-clipboard grim dbus \
at-spi2-core libglib2.0-bin python3-gi gir1.2-gtk-3.0
command -v Xvfb xdotool scrot xclip pulseaudio pactl paplay weston sway wtype wl-copy grim dbus-daemon gdbus
/usr/bin/python3 -c 'import PIL; print("PIL", PIL.__version__)'
/usr/bin/python3 -c 'import tkinter; print("Tk", tkinter.TkVersion)'

- name: Rust toolchain (rust-toolchain.toml)
run: |
Expand Down Expand Up @@ -1033,9 +1036,15 @@ jobs:
run: xvfb-run -a -s "-screen 0 1280x1024x24 -noreset" cargo test -p voltip-hooks --lib -- --ignored --test-threads=1 --nocapture

# The whole clipboard across a paste on a real X server (docs/dictation.md §14.3): an image
# and HTML xclip copied come back, and the dictation is marked for clipboard managers.
- name: Real clipboard under Xvfb (cargo test -p voltip-inject --test real_display -- --ignored)
run: xvfb-run -a -s "-screen 0 1280x1024x24 -noreset" cargo test -p voltip-inject --test real_display real_an_image -- --ignored --test-threads=1 --nocapture
# and HTML xclip copied come back, and the dictation is marked for clipboard managers. The
# copy that reads the selection (§19, 从选中文字学习 §35): a Tk entry's selection comes back
# with Ctrl+Insert, a modifier still held from the shortcut let go first, the clipboard put
# back; while a grabbed hotkey key is down the copy never reaches the entry.
- name: Real clipboard and selection copy under Xvfb (cargo test -p voltip-inject --test real_display -- --ignored)
run: >-
xvfb-run -a -s "-screen 0 1280x1024x24 -noreset" cargo test -p voltip-inject --test real_display --
--ignored --test-threads=1 --nocapture
real_an_image real_copy_chord_reads_the_selection_of_an_x11_text_widget regression_x11_hotkey_grab_swallows_the_copy_until_the_key_is_up

# The insert check's AT-SPI reader against a GTK 3 window on a real X server, in a private
# D-Bus session with the accessibility bus switched on (docs/dictation.md §28): a typed text
Expand Down Expand Up @@ -1412,8 +1421,10 @@ jobs:

# The insert check's Accessibility reader (docs/dictation.md §28): a paste into the test's own
# text view, which can be selected but not edited, stays not written; a paste into a TextEdit
# document is written. The test has its own main (AppKit runs on the main thread); its
# binary is granted Accessibility and event posting as the event tap's is.
# document is written, and the copy that reads the selection (§19, §35) brings TextEdit's
# selection back with Cmd+C and puts the pasteboard back. The test has its own main (AppKit
# runs on the main thread); its binary is granted Accessibility and event posting as the
# event tap's is.
- name: Real insert check (Accessibility granted to the test binary)
if: matrix.arch == 'aarch64'
timeout-minutes: 15
Expand Down
17 changes: 16 additions & 1 deletion apps/desktop/src-tauri/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ pub struct Cli {
/// happens when none runs.
#[arg(long, conflicts_with_all = ["toggle", "edit_toggle", "cancel", "quit", "start_hidden", "list_devices", "list_compute", "list_models", "download_model", "transcribe_file"])]
pub copy_last: bool,
/// Have the running instance learn from the selected text (从选中文字学习, docs/dictation.md
/// §35): the corrected text selected in another application; bind it to a compositor shortcut
/// where no global hotkey exists (Wayland). Nothing happens when none runs.
#[arg(long, conflicts_with_all = ["toggle", "edit_toggle", "cancel", "quit", "copy_last", "start_hidden", "list_devices", "list_compute", "list_models", "download_model", "transcribe_file"])]
pub learn_selection: bool,
/// Print the input devices (`id<TAB>name`, default first) and exit.
#[arg(long, conflicts_with_all = ["list_models", "transcribe_file"])]
pub list_devices: bool,
Expand Down Expand Up @@ -145,6 +150,8 @@ pub enum Action {
Quit,
/// `--copy-last`: the same way, for 复制上一条结果 (a new instance has no result yet).
CopyLast,
/// `--learn-selection`: the same way, for 从选中文字学习 (a new instance has no history open).
LearnSelection,
/// `--list-devices`.
ListDevices,
/// `--list-compute`.
Expand Down Expand Up @@ -230,6 +237,9 @@ impl Cli {
if self.copy_last {
return Action::CopyLast;
}
if self.learn_selection {
return Action::LearnSelection;
}
Action::Gui { start_hidden: self.start_hidden, remote: self.remote() }
}
}
Expand All @@ -250,6 +260,11 @@ pub fn copy_last_from_args(args: &[String]) -> bool {
Cli::parse_args(args).is_ok_and(|cli| cli.copy_last)
}

/// Whether a second instance's arguments ask this one for 从选中文字学习 (`--learn-selection`).
pub fn learn_selection_from_args(args: &[String]) -> bool {
Cli::parse_args(args).is_ok_and(|cli| cli.learn_selection)
}

/// What `--transcribe-file --json` prints.
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct TranscribeOutput {
Expand Down Expand Up @@ -525,6 +540,6 @@ pub fn run_headless(action: &Action, data_dir: &Path, out: &mut dyn Write, err:
};
transcribe_file(&transcriber, &args, out, err)
}
Action::Gui { .. } | Action::Quit | Action::CopyLast => ExitCode::Ok,
Action::Gui { .. } | Action::Quit | Action::CopyLast | Action::LearnSelection => ExitCode::Ok,
}
}
6 changes: 6 additions & 0 deletions apps/desktop/src-tauri/src/dictation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,12 @@ impl Injector for NativeInjector {
voltip_platform::foreground::is_terminal(voltip_platform::HostOs::current(), app_id)
}

/// docs/dictation.md §35: every terminal of this host, macOS's included
/// (`voltip_platform::insert_check::holds_program_output`).
fn holds_program_output(&self, app_id: &str) -> bool {
voltip_platform::insert_check::holds_program_output(voltip_platform::HostOs::current(), app_id)
}

/// docs/dictation.md §28: the platform's reader of the focused control.
fn focus_reader(&self) -> Option<Arc<dyn FocusReader>> {
self.focus.clone()
Expand Down
94 changes: 85 additions & 9 deletions apps/desktop/src-tauri/src/hotkey.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@
//!
//! 复制上一条结果 (`Settings.copy_last_hotkey`, docs/dictation.md §28) is a third, optional chord
//! that answers its press only: it puts the newest result on the clipboard ([`crate::copy_last`]);
//! on pure Wayland the compositor runs `--copy-last` instead.
//! on pure Wayland the compositor runs `--copy-last` instead. 从选中文字学习
//! (`Settings.learn_hotkey`, §35) is a fourth: one transition each, the press where the copy chord
//! can reach the application then, the release under the X11 key grab ([`selection_timing_for`]);
//! the compositor runs `--learn-selection` on pure Wayland.
//!
//! The lone-key trigger (`Settings.solo_key`, docs/dictation.md §13.1) is watched by
//! [`crate::solo_key`] next to the chords, with the same lifecycle: installed by [`apply`],
Expand Down Expand Up @@ -55,7 +58,8 @@ pub fn edge_command(pressed: bool, source: EdgeSource, purpose: TakeKind) -> UiC
}

/// What the shell registers: the dictation hotkey, the voice-edit hotkey (`None` = off), the
/// lone-key trigger (`None` = off) and the 复制上一条结果 chord (`None` = none).
/// lone-key trigger (`None` = off), the 复制上一条结果 chord and the 从选中文字学习 chord (`None` =
/// none).
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Chords {
/// `Settings.hotkey`.
Expand All @@ -66,6 +70,8 @@ pub struct Chords {
pub solo_key: Option<SoloKey>,
/// `Settings.copy_last_hotkey`.
pub copy_last_hotkey: Option<String>,
/// `Settings.learn_hotkey`.
pub learn_hotkey: Option<String>,
}

impl From<&Settings> for Chords {
Expand All @@ -75,6 +81,7 @@ impl From<&Settings> for Chords {
edit_hotkey: settings.edit_hotkey.clone(),
solo_key: settings.solo_key,
copy_last_hotkey: settings.copy_last_hotkey.clone(),
learn_hotkey: settings.learn_hotkey.clone(),
}
}
}
Expand Down Expand Up @@ -162,6 +169,11 @@ pub fn copy_last_command() -> String {
remote_command("--copy-last")
}

/// The same for 从选中文字学习 (docs/dictation.md §35): `voltip-desktop --learn-selection`.
pub fn learn_command() -> String {
remote_command("--learn-selection")
}

fn remote_command(flag: &str) -> String {
#[cfg(unix)]
{
Expand Down Expand Up @@ -475,6 +487,18 @@ pub fn apply<R: Runtime>(app: &AppHandle<R>, bridge: &Bridge, registry: &Arc<Hot
}
}
}
if let Some(learn) = &chords.learn_hotkey {
// The core refuses to save one of the other three chords; a hand-edited file may hold it.
let taken = std::iter::once(&chords.hotkey).chain(chords.edit_hotkey.as_ref()).chain(chords.copy_last_hotkey.as_ref());
if taken.into_iter().any(|other| Hotkey::same_chord(learn, other)) {
status.learn_error = Some(format!("{learn} 与另一个快捷键相同,未生效"));
} else {
match register_learn(app, learn, session) {
Ok(label) => status.learn_registered = Some(label),
Err(e) => status.learn_error = Some(e),
}
}
}
let kept = sync_solo(bridge, registry, chords.solo_key, session, &mut status);
registry.store_applied(&|event| bridge.publish(event), status, kept);
// A take running across a settings change keeps its cancel key.
Expand Down Expand Up @@ -746,6 +770,46 @@ fn register_copy_last<R: Runtime>(app: &AppHandle<R>, text: &str, session: Optio
}
}

/// The transition of the 从选中文字学习 chord that starts it (docs/dictation.md §35): the press, or
/// the release where the X11 key grab holds the keyboard until the key is up.
pub fn learn_fires_on(timing: SelectionTiming) -> ShortcutState {
match timing {
SelectionTiming::AtPress => ShortcutState::Pressed,
SelectionTiming::AfterKeyUp => ShortcutState::Released,
}
}

/// Register the 从选中文字学习 chord (docs/dictation.md §35); the display label, or why not.
fn register_learn<R: Runtime>(app: &AppHandle<R>, text: &str, session: Option<SessionKind>) -> Result<String, String> {
let hotkey = Hotkey::parse(text).map_err(|e| format!("{text}: {e}"))?;
let label = hotkey.display();
let remote = learn_command();
if let Some(blocked) = session_block(session, &label, &remote) {
tracing::warn!(hotkey = %label, backend = %backend_name(), error = %blocked, "no global hotkey on this session; the compositor must run {remote}");
return Err(blocked);
}
let shortcut = hotkey.to_tauri_shortcut();
let fires = learn_fires_on(selection_timing_for(session));
let result = app.global_shortcut().on_shortcut(shortcut.as_str(), move |app, _shortcut, event| {
if event.state() == fires {
crate::learn_selection(app, voltip_core::EdgeSource::Hotkey);
}
});
match result {
Ok(()) => {
tracing::info!(hotkey = %label, shortcut = %shortcut, fires = ?fires, "learn hotkey registered");
if let Some(caveat) = session_caveat(session, &remote) {
tracing::warn!(hotkey = %label, "{caveat}");
}
Ok(label)
}
Err(e) => {
tracing::warn!(hotkey = %label, error = %e, "learn hotkey registration failed");
Err(format!("{label} 未能生效:{e}"))
}
}
}

/// Watch the bridge for settings changes and keep the OS registration in sync.
pub fn follow_settings<R: Runtime>(app: AppHandle<R>, bridge: Bridge, registry: Arc<HotkeyRegistry>) {
if let Some(session) = linux_session() {
Expand Down Expand Up @@ -976,19 +1040,23 @@ mod tests {
assert!(!cancel_key_wanted(&DictationPhase::Cancelled { injected_chars: 0 }));

// Bare, and under each chord's modifiers: a `hold` take is cancelled with the chord down.
let chords = Chords { hotkey: "Ctrl+Alt+Space".into(), edit_hotkey: Some("Ctrl+Alt+E".into()), solo_key: None, copy_last_hotkey: None };
let chords =
Chords { hotkey: "Ctrl+Alt+Space".into(), edit_hotkey: Some("Ctrl+Alt+E".into()), solo_key: None, copy_last_hotkey: None, learn_hotkey: None };
assert_eq!(cancel_shortcuts(&chords), ["Escape", "Control+Alt+Escape"]);
let chords = Chords { hotkey: "Alt+Shift+Z".into(), edit_hotkey: Some("Meta+E".into()), solo_key: None, copy_last_hotkey: None };
let chords = Chords { hotkey: "Alt+Shift+Z".into(), edit_hotkey: Some("Meta+E".into()), solo_key: None, copy_last_hotkey: None, learn_hotkey: None };
assert_eq!(cancel_shortcuts(&chords), ["Escape", "Alt+Shift+Escape", "Super+Escape"]);
let chords = Chords { hotkey: "not a chord".into(), edit_hotkey: None, solo_key: None, copy_last_hotkey: None };
let chords = Chords { hotkey: "not a chord".into(), edit_hotkey: None, solo_key: None, copy_last_hotkey: None, learn_hotkey: None };
assert_eq!(cancel_shortcuts(&chords), ["Escape"]);
// Task Manager and Force Quit stay the system's.
let chords = Chords { hotkey: "Shift+Ctrl+D".into(), edit_hotkey: Some("Alt+Meta+E".into()), solo_key: None, copy_last_hotkey: None };
let chords =
Chords { hotkey: "Shift+Ctrl+D".into(), edit_hotkey: Some("Alt+Meta+E".into()), solo_key: None, copy_last_hotkey: None, learn_hotkey: None };
assert_eq!(cancel_shortcuts(&chords), ["Escape"]);
// docs/dictation.md §13.1: a lone modifier held in `hold` adds its own; a mouse button none.
let chords = Chords { hotkey: "Ctrl+Alt+Space".into(), edit_hotkey: None, solo_key: Some(SoloKey::RightShift), copy_last_hotkey: None };
let chords =
Chords { hotkey: "Ctrl+Alt+Space".into(), edit_hotkey: None, solo_key: Some(SoloKey::RightShift), copy_last_hotkey: None, learn_hotkey: None };
assert_eq!(cancel_shortcuts(&chords), ["Escape", "Control+Alt+Escape", "Shift+Escape"]);
let chords = Chords { hotkey: "Ctrl+Alt+Space".into(), edit_hotkey: None, solo_key: Some(SoloKey::MouseBack), copy_last_hotkey: None };
let chords =
Chords { hotkey: "Ctrl+Alt+Space".into(), edit_hotkey: None, solo_key: Some(SoloKey::MouseBack), copy_last_hotkey: None, learn_hotkey: None };
assert_eq!(cancel_shortcuts(&chords), ["Escape", "Control+Alt+Escape"]);
assert_eq!(SoloKey::ALL.into_iter().filter_map(solo_modifier).count(), 4, "the four right-hand modifiers");

Expand Down Expand Up @@ -1084,6 +1152,14 @@ mod tests {

/// docs/dictation.md §19: X11 grabs the keyboard while the hotkey is down — the copy waits for
/// the key-up there (XWayland too); elsewhere it goes out at the press.
#[test]
fn the_learn_chord_fires_when_the_copy_can_reach_the_application() {
assert_eq!(learn_fires_on(SelectionTiming::AtPress), ShortcutState::Pressed);
assert_eq!(learn_fires_on(SelectionTiming::AfterKeyUp), ShortcutState::Released, "the X11 grab ends at the key-up");
assert_eq!(learn_fires_on(selection_timing_for(Some(SessionKind::X11))), ShortcutState::Released);
assert_eq!(learn_fires_on(selection_timing_for(None)), ShortcutState::Pressed);
}

#[test]
fn selection_timing_follows_the_x11_grab() {
assert_eq!(selection_timing_for(Some(SessionKind::X11)), SelectionTiming::AfterKeyUp);
Expand All @@ -1093,7 +1169,7 @@ mod tests {
let settings = Settings { hotkey: "Ctrl+Alt+Space".into(), edit_hotkey: None, solo_key: Some(SoloKey::RightCtrl), ..Settings::default() };
assert_eq!(
Chords::from(&settings),
Chords { hotkey: "Ctrl+Alt+Space".into(), edit_hotkey: None, solo_key: Some(SoloKey::RightCtrl), copy_last_hotkey: None }
Chords { hotkey: "Ctrl+Alt+Space".into(), edit_hotkey: None, solo_key: Some(SoloKey::RightCtrl), copy_last_hotkey: None, learn_hotkey: None }
);
assert!(matches!(
chorded_command(),
Expand Down
Loading
Loading