Skip to content

A personal assistant that lives on your desktop, remembers what matters, and can use your Mac for you.

TypeScript

fix(desktop): tell the user when the panic shortcut cannot be registered

Marcelito27b98230b238
diff --git a/apps/desktop/src-tauri/src/computer/app.rs b/apps/desktop/src-tauri/src/computer/app.rs
index 8d605ed..80b8e7e 100644
--- a/apps/desktop/src-tauri/src/computer/app.rs
+++ b/apps/desktop/src-tauri/src/computer/app.rs
@@ -20,6 +20,7 @@ use super::working::Windows;
use super::working::{MainThread, Rings, Task, Working, WorkingEvent, READY_TIMEOUT, WORKING_EVENT};
use super::{Clock, PermissionKind, Permissions, SystemClock};
use crate::alert::Choice;
+use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use tauri::{AppHandle, Emitter, Manager, State};
@@ -44,6 +45,33 @@ for up to 1 hour. Stop and ⌥⇧Esc still work.";
pub const YOLO_TEXT: &str = "Turn on yolo mode? Gentle Dot will send, pay, delete, and submit without asking you first, \
for up to 1 hour. Stop and Alt+Shift+Esc still work.";
pub const YOLO_CHOICE: Choice = Choice { allow: "Turn On", refuse: "Cancel" };
+/// The yolo dialog when the panic shortcut is not registered: it promises only what works.
+pub const YOLO_TEXT_WITHOUT_PANIC: &str = "Turn on yolo mode? Gentle Dot will send, pay, delete, and submit without asking you \
+first, for up to 1 hour. Stop still works.";
+/// Told once at launch when the panic shortcut cannot be registered.
+#[cfg(not(windows))]
+pub const PANIC_UNAVAILABLE_TEXT: &str = "⌥⇧Esc cannot stop computer control: another app or macOS already uses it. \
+Use Stop in the panel or the menu bar instead.";
+#[cfg(windows)]
+pub const PANIC_UNAVAILABLE_TEXT: &str = "Alt+Shift+Esc cannot stop computer control: another app or Windows already uses \
+it. Use Stop in the panel or the tray menu instead.";
+
+/// Whether the panic shortcut was registered at launch.
+static PANIC_SHORTCUT_REGISTERED: AtomicBool = AtomicBool::new(false);
+
+/// Records whether the panic shortcut was registered, so the yolo dialog only promises it when it works.
+pub fn set_panic_shortcut_registered(registered: bool) {
+ PANIC_SHORTCUT_REGISTERED.store(registered, Ordering::SeqCst);
+}
+
+/// The yolo dialog's text, naming the panic shortcut only when it is registered.
+pub fn yolo_text(panic_registered: bool) -> &'static str {
+ if panic_registered {
+ YOLO_TEXT
+ } else {
+ YOLO_TEXT_WITHOUT_PANIC
+ }
+}
/// The tray's "Yolo mode" item, kept in step with the state.
pub struct YoloMenuItem(pub tauri::menu::CheckMenuItem<tauri::Wry>);
@@ -596,7 +624,7 @@ impl super::Dialogs for NativeDialogs {
}
fn ask_yolo(&self) -> bool {
- self.ask(YOLO_TEXT, YOLO_CHOICE)
+ self.ask(yolo_text(PANIC_SHORTCUT_REGISTERED.load(Ordering::SeqCst)), YOLO_CHOICE)
}
}
@@ -632,6 +660,36 @@ for up to 1 hour. Stop and Alt+Shift+Esc still work.");
assert_eq!(YOLO_CHOICE, Choice { allow: "Turn On", refuse: "Cancel" });
}
+ #[test]
+ fn the_yolo_dialog_promises_the_panic_shortcut_only_when_it_is_registered() {
+ assert_eq!(yolo_text(true), YOLO_TEXT);
+ assert_eq!(
+ yolo_text(false),
+ "Turn on yolo mode? Gentle Dot will send, pay, delete, and submit without asking you first, \
+for up to 1 hour. Stop still works."
+ );
+ }
+
+ #[test]
+ #[cfg(not(windows))]
+ fn a_missing_panic_shortcut_is_explained_once_on_the_mac() {
+ assert_eq!(
+ PANIC_UNAVAILABLE_TEXT,
+ "⌥⇧Esc cannot stop computer control: another app or macOS already uses it. \
+Use Stop in the panel or the menu bar instead."
+ );
+ }
+
+ #[test]
+ #[cfg(windows)]
+ fn a_missing_panic_shortcut_is_explained_once_on_windows() {
+ assert_eq!(
+ PANIC_UNAVAILABLE_TEXT,
+ "Alt+Shift+Esc cannot stop computer control: another app or Windows already uses it. \
+Use Stop in the panel or the tray menu instead."
+ );
+ }
+
#[test]
fn every_computer_dialog_defaults_to_its_refusing_button() {
for choice in [GRANT_CHOICE, CONFIRM_CHOICE, YOLO_CHOICE] {
diff --git a/apps/desktop/src-tauri/src/computer/win.rs b/apps/desktop/src-tauri/src/computer/win.rs
index 7710ce9..3cd3f08 100644
--- a/apps/desktop/src-tauri/src/computer/win.rs
+++ b/apps/desktop/src-tauri/src/computer/win.rs
@@ -491,11 +491,19 @@ fn enumerate_windows() -> Result<Vec<HWND>, String> {
// SAFETY: the callback only appends to `handles`, which outlives the call. It never stops the
// enumeration, so 0 is a failure, and a list that may be missing windows is no list.
if unsafe { EnumWindows(Some(collect), &mut handles as *mut Vec<HWND> as LPARAM) } == 0 {
- return Err(std::io::Error::last_os_error().to_string());
+ return Err(enumeration_failure(std::io::Error::last_os_error()));
}
Ok(handles)
}
+/// Why `EnumWindows` failed. It may fail without setting an error code, which would read "(os error 0)".
+fn enumeration_failure(error: std::io::Error) -> String {
+ match error.raw_os_error() {
+ Some(0) | None => "EnumWindows failed without an error code".to_string(),
+ Some(_) => error.to_string(),
+ }
+}
+
/// The visible top-level windows of this process, or why they cannot be listed.
fn own_visible_windows() -> Result<Vec<HWND>, String> {
let mut handles = enumerate_windows().map_err(|error| format!("cannot list its windows: {error}"))?;
@@ -1487,6 +1495,9 @@ pub fn focused_frame() -> Option<Frame> {
/// Prevent activation and capture of the click-through glow. A glow that cannot be kept out of
/// screenshots is not used: without it no call captures or acts (S5).
pub fn prepare_overlay(hwnd: *mut c_void) -> Result<(), String> {
+ // These extended styles are written behind tao's back, so tao's cached flags do not have them:
+ // any later tao call that sets window flags on the glow would drop WS_EX_TOOLWINDOW. Nothing
+ // does today; keep it that way, or set them again after such a call.
// SAFETY: calls on this process's window.
unsafe {
let style = GetWindowLongPtrW(hwnd, GWL_EXSTYLE);
@@ -1692,6 +1703,13 @@ mod tests {
/// T48: a bare name never resolves through PATH, so shell tools in System32 are not opened by
/// name (`cmd.exe` and `curl.exe` are in System32, which is in PATH, and neither is in App
/// Paths). No shortcut is passed, so only App Paths could answer.
+ #[test]
+ fn a_window_listing_failure_without_an_error_code_says_so() {
+ assert_eq!(enumeration_failure(std::io::Error::from_raw_os_error(0)), "EnumWindows failed without an error code");
+ let denied = std::io::Error::from_raw_os_error(5);
+ assert_eq!(enumeration_failure(denied), std::io::Error::from_raw_os_error(5).to_string());
+ }
+
#[test]
fn a_bare_name_is_not_resolved_through_path() {
for name in ["cmd", "curl", "certutil"] {
diff --git a/apps/desktop/src-tauri/src/shell.rs b/apps/desktop/src-tauri/src/shell.rs
index 46f09f2..5e8af2b 100644
--- a/apps/desktop/src-tauri/src/shell.rs
+++ b/apps/desktop/src-tauri/src/shell.rs
@@ -785,7 +785,9 @@ fn restart_daemon(app: AppHandle) {
});
}
-/// ⌥⇧Esc ends computer control from anywhere, even while another app is in front.
+/// The panic shortcut (⌥⇧Esc on macOS, Alt+Shift+Esc on Windows) ends computer control from
+/// anywhere, even while another app is in front. When another app or the system already holds it,
+/// the user is told once and the yolo dialog stops promising it; Stop keeps working.
#[cfg(any(target_os = "macos", windows))]
fn register_panic_shortcut(app: &AppHandle) {
let handler = |app: &AppHandle, _: &_, event: tauri_plugin_global_shortcut::ShortcutEvent| {
@@ -793,8 +795,11 @@ fn register_panic_shortcut(app: &AppHandle) {
computer::stop(app, Reason::Panic);
}
};
- if let Err(error) = app.global_shortcut().on_shortcut(computer::PANIC_SHORTCUT, handler) {
+ let registered = app.global_shortcut().on_shortcut(computer::PANIC_SHORTCUT, handler);
+ computer::set_panic_shortcut_registered(registered.is_ok());
+ if let Err(error) = registered {
eprintln!("gentle-dot: the computer-control panic shortcut is unavailable: {error}");
+ show_message(app, MessageDialogKind::Warning, computer::PANIC_UNAVAILABLE_TEXT);
}
}