Skip to content

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

TypeScript

fix(desktop): ask for a confirmation whenever UI Automation leaves part unread

Marcelito7d1fd2e43d8e
diff --git a/apps/desktop/src-tauri/src/computer/win.rs b/apps/desktop/src-tauri/src/computer/win.rs
index e79e970..334424d 100644
--- a/apps/desktop/src-tauri/src/computer/win.rs
+++ b/apps/desktop/src-tauri/src/computer/win.rs
@@ -25,7 +25,7 @@ use super::coords::{fit_long_edge, Point, ScreenshotGeometry, MAX_LONG_EDGE};
use super::encode::{bgra_to_jpeg, JPEG_QUALITY};
use super::blocklist::{blocked_name, blocked_reason, AppIdentity, UNIDENTIFIED};
use super::risk::{AxElement, Focus};
-use super::winaccess::{element, is_default, unreadable, UIA_BUTTON, UIA_WINDOW};
+use super::winaccess::{element, is_default, known_chain, unreadable, UIA_BUTTON, UIA_WINDOW};
use super::winapps::{
answers_to, app_pids, executable_target, fronts_same_app, hosted_page, request, shortcut_named, strip_ci, Request,
TopWindow,
@@ -1077,8 +1077,9 @@ pub fn keyboard_refusal(app: &AppIdentity) -> Option<String> {
const AX_ANCESTORS: usize = 4;
/// How far up the focused element its window is looked for.
const WINDOW_SEARCH: usize = 32;
-/// At most this many of a window's buttons are checked for the default one. The query itself is
-/// one cross-process call, bounded by the UI Automation timeout.
+/// At most this many of a window's buttons are checked for the default one; a window with more
+/// has an unknown default, so Return asks. The query itself is one cross-process call, bounded by
+/// the UI Automation timeout.
const DEFAULT_BUTTON_SCAN: i32 = 1024;
/// UI Automation gives up on an unresponsive app after this long.
const UIA_TIMEOUT_MS: u32 = 500;
@@ -1230,8 +1231,9 @@ pub fn element_at(at: Point) -> Vec<AxElement> {
// there: what is under the glow cannot be read, so it asks for a confirmation.
let own = std::process::id() as i32;
// SAFETY: a call on a live COM object.
- // A process Windows does not report counts as Gentle Dot's: the element is not read either.
- let hit_own = unsafe { current.CurrentProcessId() }.map_or(true, |pid| pid == own);
+ // A process Windows does not report (or reports as no process) counts as Gentle Dot's: the
+ // element is not read either.
+ let hit_own = unsafe { current.CurrentProcessId() }.map_or(true, |pid| pid <= 0 || pid == own);
// Only Gentle Dot's own window there (whose actions are refused anyway) is described.
if hit_own && !window_owner_at(at).is_some_and(|owner| owner.pid == own) {
return vec![unreadable()];
@@ -1248,7 +1250,7 @@ pub fn element_at(at: Point) -> Vec<AxElement> {
chain.push(describe(&parent));
current = parent;
}
- chain
+ known_chain(chain)
}
/// The window holding `of`: its nearest Window ancestor, as macOS reads `AXWindow`. None when
@@ -1283,9 +1285,9 @@ fn default_button(
let cache = automation.client.CreateCacheRequest()?;
cache.AddProperty(uia::UIA_LegacyIAccessibleStatePropertyId)?;
let found = window.FindAllBuildCache(uia::TreeScope_Descendants, &buttons, &cache)?;
- let count = found.Length()?.min(DEFAULT_BUTTON_SCAN);
+ let total = found.Length()?;
// Every button is read: one that cannot be could be the default, so the scan fails.
- for i in 0..count {
+ for i in 0..total.min(DEFAULT_BUTTON_SCAN) {
let button = found.GetElement(i)?;
let state = button.GetCachedPropertyValue(uia::UIA_LegacyIAccessibleStatePropertyId)?;
let state = i32::try_from(&state)?;
@@ -1293,6 +1295,10 @@ fn default_button(
return Ok(Some(button));
}
}
+ // Buttons left unread could hold the default: the answer is not known, so Return asks.
+ if total > DEFAULT_BUTTON_SCAN {
+ return Err(windows::Win32::Foundation::E_FAIL.into());
+ }
Ok(None)
}
}
@@ -1483,7 +1489,7 @@ mod tests {
let elsewhere = KeyboardRecipient { front_pid: i32::MAX, focused_pid: None };
let result = post_typed(&InputEvent::Text("x".into()), &elsewhere);
assert!(
- result == Err(TypedError::RecipientChanged) || result == Err(TypedError::FrontUnknown),
+ matches!(result, Err(TypedError::RecipientChanged | TypedError::FrontUnknown | TypedError::NoFocus)),
"{result:?}"
);
}
diff --git a/apps/desktop/src-tauri/src/computer/winaccess.rs b/apps/desktop/src-tauri/src/computer/winaccess.rs
index 2730ca3..e046a42 100644
--- a/apps/desktop/src-tauri/src/computer/winaccess.rs
+++ b/apps/desktop/src-tauri/src/computer/winaccess.rs
@@ -71,6 +71,19 @@ pub fn unreadable() -> AxElement {
}
}
+/// The chain the risk check gets for the element under a point and its ancestors: when any of
+/// them could not be read, the whole chain is the unreadable element. The risk check reads only
+/// the first pressable element and what is inside it, so an unreadable ancestor next to a
+/// harmless button would otherwise be let through (CC7 review).
+pub fn known_chain(chain: Vec<AxElement>) -> Vec<AxElement> {
+ let unknown = unreadable();
+ if chain.contains(&unknown) {
+ vec![unknown]
+ } else {
+ chain
+ }
+}
+
#[cfg(test)]
mod tests {
use super::*;
@@ -140,6 +153,23 @@ mod tests {
assert!(assess(Act::Drag(&[named(UIA_TEXT, "x")], &[unreadable()]), None).is_some());
}
+ #[test]
+ fn a_chain_with_any_unreadable_element_needs_a_confirmation() {
+ let open = named(UIA_BUTTON, "Open");
+ for chain in [
+ vec![open.clone(), unreadable()],
+ vec![named(UIA_TEXT, "Open"), unreadable(), named(UIA_WINDOW, "Files")],
+ vec![unreadable(), open.clone()],
+ ] {
+ let known = known_chain(chain.clone());
+ assert_eq!(known, vec![unreadable()], "{chain:?}");
+ assert!(assess(Act::Click(&known), None).is_some(), "{chain:?}");
+ }
+ // A chain read in full is kept as it is.
+ let read = vec![named(UIA_TEXT, "Open"), open, named(UIA_WINDOW, "Files")];
+ assert_eq!(known_chain(read.clone()), read);
+ }
+
#[test]
fn return_in_a_windows_chat_box_or_on_a_default_button_needs_a_confirmation() {
let chat = Focus { element: named(UIA_EDIT, "Message #general"), ..Focus::default() };