Offline, privacy-first grammar checker. Fast, open-source, Rust-powered
feat(desktop): expand role selection (#3546)
Elijah Potterd4a8994d4159
diff --git a/harper-desktop/src-tauri/Cargo.toml b/harper-desktop/src-tauri/Cargo.tomlindex dd7b6223e..c92b5ba6d 100644--- a/harper-desktop/src-tauri/Cargo.toml+++ b/harper-desktop/src-tauri/Cargo.toml@@ -43,6 +43,7 @@ objc2-app-kit = { version = "0.3.2", default-features = false, features = ["std","libc","NSRunningApplication",+ "NSWorkspace",] }objc2-foundation = { version = "0.3.2", default-features = false, features = ["std",diff --git a/harper-desktop/src-tauri/src/mac_broker.rs b/harper-desktop/src-tauri/src/mac_broker.rsindex 4263eec65..e9ed5a2cb 100644--- a/harper-desktop/src-tauri/src/mac_broker.rs+++ b/harper-desktop/src-tauri/src/mac_broker.rs@@ -3,11 +3,12 @@ use accessibility::ui_element::AXUIElement;use accessibility::{Error, TreeVisitor, TreeWalker, TreeWalkerFlow};use accessibility_sys::{AXIsProcessTrusted, AXIsProcessTrustedWithOptions, AXUIElementCopyAttributeValue,- AXUIElementCopyParameterizedAttributeValue, AXUIElementGetPid, AXValueCreate, AXValueGetType,- AXValueGetValue, AXValueRef, error_string, kAXBoundsForRangeParameterizedAttribute,- kAXErrorIllegalArgument, kAXErrorNoValue, kAXErrorParameterizedAttributeUnsupported,- kAXErrorSuccess, kAXFocusedApplicationAttribute, kAXTrustedCheckOptionPrompt,- kAXValueTypeCFRange, kAXValueTypeCGRect, pid_t,+ AXUIElementCopyParameterizedAttributeValue, AXUIElementGetPid, AXUIElementSetAttributeValue,+ AXValueCreate, AXValueGetType, AXValueGetValue, AXValueRef, error_string,+ kAXBoundsForRangeParameterizedAttribute, kAXErrorAttributeUnsupported, kAXErrorIllegalArgument,+ kAXErrorNoValue, kAXErrorParameterizedAttributeUnsupported, kAXErrorSuccess,+ kAXFocusedApplicationAttribute, kAXTrustedCheckOptionPrompt, kAXValueTypeCFRange,+ kAXValueTypeCGRect, pid_t,};use core::{ffi::c_void, mem::MaybeUninit};use core_foundation::array::CFArray;@@ -23,10 +24,10 @@ use core_graphics::window::{kCGWindowListExcludeDesktopElements, kCGWindowListOptionOnScreenOnly, kCGWindowOwnerPID,};use harper_core::linting::{Lint, Suggestion};-use objc2_app_kit::NSRunningApplication;+use objc2_app_kit::{NSRunningApplication, NSWorkspace};use objc2_foundation::NSRect;use std::{- cell::RefCell,+ cell::{Cell, RefCell},collections::BTreeMap,error::Error as StdError,path::Path,@@ -41,6 +42,11 @@ use crate::os_broker::{AccessibilityPermissionStatus, OsBroker};use crate::rect::{ActionableLint, Rect};const WINDOW_MOVEMENT_SETTLE_DURATION: Duration = Duration::from_millis(150);+const ACCESSIBILITY_ACTIVATION_RETRY_INTERVAL: Duration = Duration::from_secs(10);+const ACCESSIBILITY_ACTIVATION_VERIFICATION_RETRY_INTERVAL: Duration = Duration::from_millis(250);+const ACCESSIBILITY_ACTIVATION_SLOW_VERIFICATION_RETRY_INTERVAL: Duration = Duration::from_secs(1);+const ACCESSIBILITY_ACTIVATION_FAST_VERIFICATION_ATTEMPTS: u8 = 20;+const CHROMIUM_ACCESSIBILITY_SETTLE_DURATION: Duration = Duration::from_secs(3);const WINDOW_FRAME_TOLERANCE: f64 = 0.5;type WindowInfo = CFDictionary<CFString, CFType>;@@ -53,6 +59,7 @@ pub struct MacBroker {last_focused: Option<pid_t>,integrations: Rc<RefCell<Vec<Integration>>>,window_movement: Option<WindowMovementState>,+ accessibility_activation: Option<AccessibilityActivationState>,}#[derive(Debug, Clone)]@@ -62,12 +69,42 @@ struct WindowMovementState {last_changed_at: Instant,}+/// Tracks accessibility activation for the focused app and any value to restore.+#[derive(Debug, Clone)]+struct AccessibilityActivationState {+ pid: pid_t,+ bundle_id: String,+ status: AccessibilityActivationStatus,+ last_attempted_at: Instant,+ enhanced_user_interface_restore_value: Option<bool>,+}++/// State of the focused app's accessibility activation attempt.+#[derive(Debug, Clone, Copy)]+enum AccessibilityActivationStatus {+ Pending {+ ready_at: Instant,+ verification_attempts: u8,+ },+ Ready,+ RetryLater,+}++/// Result of checking whether an activated app exposes usable text geometry.+#[derive(Debug, Clone, Copy, PartialEq, Eq)]+enum AccessibilityActivationVerification {+ FoundTextRangeBounds,+ FoundSupportedTextElement,+ NoSupportedTextElement,+}+impl MacBroker {pub fn new(integrations: Rc<RefCell<Vec<Integration>>>) -> Self {Self {last_focused: None,integrations,window_movement: None,+ accessibility_activation: None,}}@@ -108,6 +145,157 @@ impl MacBroker {now.duration_since(state.last_changed_at) < WINDOW_MOVEMENT_SETTLE_DURATION}++ /// Clears activation state and restores any saved AX attribute value.+ fn reset_accessibility_activation(&mut self) {+ if let Some(state) = self.accessibility_activation.take() {+ release_accessibility_activation(&state);+ }+ }++ /// Activates the focused app and waits until text range bounds are usable.+ fn ensure_accessibility_activation(+ &mut self,+ pid: pid_t,+ bundle_id: &str,+ app: &AXUIElement,+ ) -> bool {+ let needs_new_activation = match &self.accessibility_activation {+ Some(state) => state.pid != pid || state.bundle_id != bundle_id,+ None => true,+ };++ if needs_new_activation {+ self.reset_accessibility_activation();+ return self.request_enhanced_user_interface(pid, bundle_id, app);+ }++ let Some(status) = self+ .accessibility_activation+ .as_ref()+ .map(|state| state.status)+ else {+ return self.request_enhanced_user_interface(pid, bundle_id, app);+ };++ let now = Instant::now();+ match status {+ AccessibilityActivationStatus::Ready => true,+ AccessibilityActivationStatus::Pending {+ ready_at,+ verification_attempts,+ } => {+ if now < ready_at {+ return false;+ }++ let verification = verify_accessibility_activation(app);++ if verification == AccessibilityActivationVerification::FoundTextRangeBounds {+ eprintln!(+ "Accessibility activation verified for {bundle_id} pid {pid}: {verification:?}"+ );+ if let Some(state) = &mut self.accessibility_activation {+ state.status = AccessibilityActivationStatus::Ready;+ }+ return true;+ }++ let next_verification_attempts = verification_attempts.saturating_add(1);+ let retry_interval = accessibility_activation_verification_retry_interval(+ next_verification_attempts,+ );++ eprintln!(+ "Accessibility activation for {bundle_id} pid {pid} is not ready for text metrics yet: {verification:?}; retrying verification in {} ms",+ retry_interval.as_millis()+ );+ if let Some(state) = &mut self.accessibility_activation {+ state.status = AccessibilityActivationStatus::Pending {+ ready_at: instant_after(now, retry_interval),+ verification_attempts: next_verification_attempts,+ };+ }++ false+ }+ AccessibilityActivationStatus::RetryLater => {+ let Some(last_attempted_at) = self+ .accessibility_activation+ .as_ref()+ .map(|state| state.last_attempted_at)+ else {+ return self.request_enhanced_user_interface(pid, bundle_id, app);+ };++ if now.duration_since(last_attempted_at) < ACCESSIBILITY_ACTIVATION_RETRY_INTERVAL {+ return false;+ }++ self.reset_accessibility_activation();+ self.request_enhanced_user_interface(pid, bundle_id, app)+ }+ }+ }++ /// Requests `AXEnhancedUserInterface`, preserving the previous value if readable.+ fn request_enhanced_user_interface(+ &mut self,+ pid: pid_t,+ bundle_id: &str,+ app: &AXUIElement,+ ) -> bool {+ let now = Instant::now();+ let settle_duration = CHROMIUM_ACCESSIBILITY_SETTLE_DURATION;++ match set_enhanced_user_interface_preserving_previous(app, true) {+ Ok(enhanced_user_interface_restore_value) => {+ eprintln!(+ "Requested AXEnhancedUserInterface for {bundle_id} pid {pid}; waiting for {} debounce",+ "Chromium"+ );+ self.accessibility_activation = Some(AccessibilityActivationState {+ pid,+ bundle_id: bundle_id.to_string(),+ status: AccessibilityActivationStatus::Pending {+ ready_at: instant_after(now, settle_duration),+ verification_attempts: 0,+ },+ last_attempted_at: now,+ enhanced_user_interface_restore_value,+ });+ false+ }+ Err(error) if is_unsupported_accessibility_activation_error(error) => {+ eprintln!(+ "AXEnhancedUserInterface unsupported for {bundle_id} pid {pid}: {}",+ error_string(error)+ );+ self.accessibility_activation = Some(AccessibilityActivationState {+ pid,+ bundle_id: bundle_id.to_string(),+ status: AccessibilityActivationStatus::RetryLater,+ last_attempted_at: now,+ enhanced_user_interface_restore_value: None,+ });+ false+ }+ Err(error) => {+ eprintln!(+ "Unable to request AXEnhancedUserInterface for {bundle_id} pid {pid}: {}",+ error_string(error)+ );+ self.accessibility_activation = Some(AccessibilityActivationState {+ pid,+ bundle_id: bundle_id.to_string(),+ status: AccessibilityActivationStatus::RetryLater,+ last_attempted_at: now,+ enhanced_user_interface_restore_value: None,+ });+ false+ }+ }+ }}impl Default for MacBroker {@@ -116,6 +304,12 @@ impl Default for MacBroker {}}+impl Drop for MacBroker {+ fn drop(&mut self) {+ self.reset_accessibility_activation();+ }+}+impl OsBroker for MacBroker {fn get_boxes(&mut self,@@ -125,17 +319,35 @@ impl OsBroker for MacBroker {Ok(Some(pid)) => pid,Ok(None) => {self.window_movement = None;+ self.reset_accessibility_activation();return Vec::new();}Err(err) => {self.window_movement = None;+ self.reset_accessibility_activation();eprintln!("Unable to identify focused window: {err}");return Vec::new();}};- if !is_pid_approved(pid, &self.integrations.borrow()) {+ let bundle_identifier = match bundle_identifier_for_pid(pid) {+ Ok(Some(bundle_identifier)) => bundle_identifier,+ Ok(None) => {+ self.window_movement = None;+ self.reset_accessibility_activation();+ return Vec::new();+ }+ Err(error) => {+ self.window_movement = None;+ self.reset_accessibility_activation();+ eprintln!("Unable to identify focused app bundle: {error}");+ return Vec::new();+ }+ };++ if !Config::is_integration_enabled_in(&self.integrations.borrow(), &bundle_identifier) {self.window_movement = None;+ self.reset_accessibility_activation();return Vec::new();}@@ -144,6 +356,9 @@ impl OsBroker for MacBroker {}let el = AXUIElement::application(pid);+ if !self.ensure_accessibility_activation(pid, &bundle_identifier, &el) {+ return Vec::new();+ }let walker = TreeWalker::new();let collector = RectCollector::new(lint_text);@@ -241,29 +456,36 @@ fn display_name_from_app_path(path: &str) -> Option<String> {fn focused_window_pid() -> Result<pid_t, Box<dyn StdError>> {let system = AXUIElement::system_wide();- let app = ax_element_attribute(&system, kAXFocusedApplicationAttribute)?;+ let app = match ax_element_attribute(&system, kAXFocusedApplicationAttribute) {+ Ok(app) => app,+ Err(error) => {+ if let Some(pid) = frontmost_application_pid() {+ return Ok(pid);+ }++ return Err(error);+ }+ };let mut pid: pid_t = 0;let err = unsafe { AXUIElementGetPid(app.as_concrete_TypeRef(), &mut pid) };if err != kAXErrorSuccess {+ if let Some(pid) = frontmost_application_pid() {+ return Ok(pid);+ }+return Err(format!("AXUIElementGetPid failed: {}", error_string(err)).into());}Ok(pid)}-fn is_pid_approved(pid: pid_t, integrations: &[Integration]) -> bool {- let bundle_identifier = match bundle_identifier_for_pid(pid) {- Ok(Some(bundle_identifier)) => bundle_identifier,- Ok(None) => return false,- Err(error) => {- eprintln!("Unable to identify focused app bundle: {error}");- return false;- }- };-- Config::is_integration_enabled_in(integrations, &bundle_identifier)+/// Fallback PID lookup when system-wide focused-application AX lookup fails.+fn frontmost_application_pid() -> Option<pid_t> {+ NSWorkspace::sharedWorkspace()+ .frontmostApplication()+ .map(|app| app.processIdentifier())}fn bundle_identifier_for_pid(pid: pid_t) -> Result<Option<String>, Box<dyn StdError>> {@@ -277,6 +499,126 @@ fn bundle_identifier_for_pid(pid: pid_t) -> Result<Option<String>, Box<dyn StdErOk(Some(bundle_identifier.to_string()))}+/// Backs off activation verification after repeated misses.+fn accessibility_activation_verification_retry_interval(verification_attempts: u8) -> Duration {+ if verification_attempts <= ACCESSIBILITY_ACTIVATION_FAST_VERIFICATION_ATTEMPTS {+ ACCESSIBILITY_ACTIVATION_VERIFICATION_RETRY_INTERVAL+ } else {+ ACCESSIBILITY_ACTIVATION_SLOW_VERIFICATION_RETRY_INTERVAL+ }+}++/// Adds a duration to an instant without panicking on overflow.+fn instant_after(now: Instant, duration: Duration) -> Instant {+ now.checked_add(duration).unwrap_or(now)+}++/// Restores `AXEnhancedUserInterface` when a previous value was captured.+fn release_accessibility_activation(state: &AccessibilityActivationState) {+ if state.enhanced_user_interface_restore_value.is_none() {+ return;+ }++ let app = AXUIElement::application(state.pid);++ restore_boolean_attribute(+ &app,+ "AXEnhancedUserInterface",+ state.enhanced_user_interface_restore_value,+ );+}++/// Returns whether an AX error means the target does not support this attribute.+fn is_unsupported_accessibility_activation_error(error: i32) -> bool {+ error == kAXErrorAttributeUnsupported || error == kAXErrorNoValue+}++/// Sets `AXEnhancedUserInterface` while returning its old value when readable.+fn set_enhanced_user_interface_preserving_previous(+ app: &AXUIElement,+ enabled: bool,+) -> Result<Option<bool>, i32> {+ set_boolean_attribute_preserving_previous(app, "AXEnhancedUserInterface", enabled)+}++/// Restores a boolean AX attribute if a previous value was captured.+fn restore_boolean_attribute(element: &AXUIElement, name: &str, restore_value: Option<bool>) {+ let Some(restore_value) = restore_value else {+ return;+ };++ if let Err(error) = set_boolean_attribute(element, name, restore_value) {+ eprintln!(+ "Unable to restore {name} to {restore_value}: {}",+ error_string(error)+ );+ }+}++/// Sets a boolean AX attribute and returns the previous value when readable.+fn set_boolean_attribute_preserving_previous(+ element: &AXUIElement,+ name: &str,+ value: bool,+) -> Result<Option<bool>, i32> {+ let previous = boolean_attribute_value(element, name).ok();+ set_boolean_attribute(element, name, value)?;++ Ok(previous)+}++/// Reads a boolean AX attribute from an accessibility element.+fn boolean_attribute_value(element: &AXUIElement, name: &str) -> Result<bool, String> {+ let attribute = CFString::new(name);+ let mut value = ptr::null();+ let error = unsafe {+ AXUIElementCopyAttributeValue(+ element.as_concrete_TypeRef(),+ attribute.as_concrete_TypeRef(),+ &mut value,+ )+ };++ if error != kAXErrorSuccess {+ return Err(format!("error:{}", error_string(error)));+ }++ if value.is_null() {+ return Err("null".to_string());+ }++ let value = unsafe { CFType::wrap_under_create_rule(value) };+ if !value.instance_of::<CFBoolean>() {+ return Err("non_boolean".to_string());+ }++ let value = unsafe { CFBoolean::wrap_under_get_rule(value.as_CFTypeRef() as _) };+ Ok(bool::from(value))+}++/// Writes a boolean AX attribute and returns the raw AX error on failure.+fn set_boolean_attribute(element: &AXUIElement, name: &str, value: bool) -> Result<(), i32> {+ let attribute = CFString::new(name);+ let value = if value {+ CFBoolean::true_value()+ } else {+ CFBoolean::false_value()+ };+ let error = unsafe {+ AXUIElementSetAttributeValue(+ element.as_concrete_TypeRef(),+ attribute.as_concrete_TypeRef(),+ value.as_CFTypeRef(),+ )+ };++ if error == kAXErrorSuccess {+ Ok(())+ } else {+ Err(error)+ }+}+fn window_frame_changed(previous: Rect, current: Rect) -> bool {!nearly_equal(previous.x, current.x)|| !nearly_equal(previous.y, current.y)@@ -405,6 +747,183 @@ fn ax_element_attribute(Ok(unsafe { AXUIElement::wrap_under_create_rule(value as _) })}+/// Outcome of asking macOS for the bounds of a text range.+#[derive(Debug, Clone, Copy)]+enum TextRangeBoundsProbe {+ Success(Rect),+ Unavailable,+ InvalidRangeValue,+ AxError(i32),+}++impl TextRangeBoundsProbe {+ /// Returns true only when the probe produced non-zero text geometry.+ fn has_usable_text_metrics(self) -> bool {+ matches!(self, Self::Success(_))+ }+}++/// Converts a Core Foundation value to a Rust string when it is a `CFString`.+fn cf_type_to_string(value: &CFType) -> Option<String> {+ value+ .instance_of::<CFString>()+ .then(|| unsafe { CFString::wrap_under_get_rule(value.as_CFTypeRef() as _) }.to_string())+}++/// Probes `AXBoundsForRange` for a specific text range on an element.+fn probe_element_rect_for_text_range(+ element: &AXUIElement,+ start_index: isize,+ length: isize,+) -> TextRangeBoundsProbe {+ let range = CFRange {+ location: start_index,+ length,+ };++ let range_value_ref = unsafe {+ AXValueCreate(+ kAXValueTypeCFRange,+ &range as *const CFRange as *const c_void,+ )+ };++ if range_value_ref.is_null() {+ return TextRangeBoundsProbe::InvalidRangeValue;+ }++ let range_value = unsafe { CFType::wrap_under_create_rule(range_value_ref as _) };+ let attr = CFString::new(kAXBoundsForRangeParameterizedAttribute);+ let mut value = ptr::null();++ let error = unsafe {+ AXUIElementCopyParameterizedAttributeValue(+ element.as_concrete_TypeRef(),+ attr.as_concrete_TypeRef(),+ range_value.as_CFTypeRef(),+ &mut value,+ )+ };++ if error == kAXErrorNoValue || error == kAXErrorParameterizedAttributeUnsupported {+ return TextRangeBoundsProbe::Unavailable;+ }++ if error != kAXErrorSuccess {+ return TextRangeBoundsProbe::AxError(error);+ }++ if value.is_null() {+ return TextRangeBoundsProbe::Unavailable;+ }++ let value = unsafe { CFType::wrap_under_create_rule(value) };+ let ax_value = value.as_CFTypeRef() as AXValueRef;+ let value_type = unsafe { AXValueGetType(ax_value) };++ if value_type != kAXValueTypeCGRect {+ return TextRangeBoundsProbe::Unavailable;+ }++ let mut rect = MaybeUninit::<NSRect>::uninit();++ let ok = unsafe {+ AXValueGetValue(+ ax_value,+ kAXValueTypeCGRect,+ rect.as_mut_ptr() as *mut c_void,+ )+ };++ if !ok {+ return TextRangeBoundsProbe::Unavailable;+ }++ let rect = unsafe { rect.assume_init() };++ let rect = Rect {+ x: rect.origin.x,+ y: rect.origin.y,+ width: rect.size.width,+ height: rect.size.height,+ };++ if rect_has_usable_text_metrics(rect) {+ TextRangeBoundsProbe::Success(rect)+ } else {+ TextRangeBoundsProbe::Unavailable+ }+}++/// Returns whether a text rectangle has enough geometry to render a highlight.+fn rect_has_usable_text_metrics(rect: Rect) -> bool {+ rect.width > 0.0 && rect.height > 0.0+}++/// Walks an app's accessibility tree to determine whether activation is ready.+fn verify_accessibility_activation(app: &AXUIElement) -> AccessibilityActivationVerification {+ let walker = TreeWalker::new();+ let probe = AccessibilityActivationProbe::new();++ walker.walk(app, &probe);++ probe.result()+}++/// Tree visitor used to verify that the activated app exposes usable text bounds.+struct AccessibilityActivationProbe {+ found_supported_text_element: Cell<bool>,+ found_text_range_bounds: Cell<bool>,+}++impl AccessibilityActivationProbe {+ /// Creates an empty activation probe for one accessibility-tree walk.+ fn new() -> Self {+ Self {+ found_supported_text_element: Cell::new(false),+ found_text_range_bounds: Cell::new(false),+ }+ }++ /// Summarizes what the probe found during the tree walk.+ fn result(&self) -> AccessibilityActivationVerification {+ if self.found_text_range_bounds.get() {+ AccessibilityActivationVerification::FoundTextRangeBounds+ } else if self.found_supported_text_element.get() {+ AccessibilityActivationVerification::FoundSupportedTextElement+ } else {+ AccessibilityActivationVerification::NoSupportedTextElement+ }+ }+}++impl TreeVisitor for AccessibilityActivationProbe {+ /// Checks each AX element for supported text and usable range bounds.+ fn enter_element(&self, element: &AXUIElement) -> TreeWalkerFlow {+ if let Ok(value) = element.value()+ && is_supported_text_element(element)+ {+ self.found_supported_text_element.set(true);++ let string = cf_type_to_string(&value).unwrap_or_default();+ let bounds = if string.is_empty() {+ TextRangeBoundsProbe::Unavailable+ } else {+ probe_element_rect_for_text_range(element, 0, 1)+ };++ if bounds.has_usable_text_metrics() {+ self.found_text_range_bounds.set(true);+ return TreeWalkerFlow::Exit;+ }+ }++ TreeWalkerFlow::Continue+ }++ fn exit_element(&self, _element: &AXUIElement) {}+}+struct RectCollector<'a> {rects: RefCell<Vec<ActionableLint>>,lint_text: RefCell<&'a mut dyn FnMut(&str) -> BTreeMap<String, Vec<Lint>>>,@@ -413,7 +932,7 @@ struct RectCollector<'a> {impl TreeVisitor for RectCollector<'_> {fn enter_element(&self, element: &AXUIElement) -> TreeWalkerFlow {if let Ok(value) = element.value()- && is_textarea(element)+ && is_supported_text_element(element){let string =unsafe { CFString::wrap_under_get_rule(value.as_CFTypeRef() as _).to_string() };@@ -486,89 +1005,97 @@ fn apply_suggestion_to_element(}}-fn is_textarea(el: &AXUIElement) -> bool {- if let Ok(role) = el.role()- && role == "AXTextArea"- {- return true;+/// Returns whether an accessibility element has a text role Harper can lint.+fn is_supported_text_element(el: &AXUIElement) -> bool {+ if let Ok(role) = el.role() {+ return is_supported_text_role(&role.to_string());}false}-fn element_rect_for_text_range(- element: &AXUIElement,- start_index: isize,- length: isize,-) -> Result<Option<Rect>, Error> {- let range = CFRange {- location: start_index,- length,- };+/// Returns whether an AX role represents editable text supported by the highlighter.+fn is_supported_text_role(role: &str) -> bool {+ matches!(role, "AXTextArea" | "AXTextField")+}- let range_value_ref = unsafe {- AXValueCreate(- kAXValueTypeCFRange,- &range as *const CFRange as *const c_void,- )- };+#[cfg(test)]+mod tests {+ use super::*;- if range_value_ref.is_null() {- return Err(Error::Ax(kAXErrorIllegalArgument));+ #[test]+ fn supported_text_roles_include_text_area_and_text_field() {+ assert!(is_supported_text_role("AXTextArea"));+ assert!(is_supported_text_role("AXTextField"));}- let range_value = unsafe { CFType::wrap_under_create_rule(range_value_ref as _) };- let attr = CFString::new(kAXBoundsForRangeParameterizedAttribute);- let mut value = ptr::null();-- let error = unsafe {- AXUIElementCopyParameterizedAttributeValue(- element.as_concrete_TypeRef(),- attr.as_concrete_TypeRef(),- range_value.as_CFTypeRef(),- &mut value,- )- };-- if error == kAXErrorSuccess {- // Continue.- } else if error == kAXErrorNoValue || error == kAXErrorParameterizedAttributeUnsupported {- return Ok(None);- } else {- return Err(Error::Ax(error));+ #[test]+ fn supported_text_roles_reject_unrelated_roles() {+ assert!(!is_supported_text_role("AXButton"));+ assert!(!is_supported_text_role("AXStaticText"));+ assert!(!is_supported_text_role(""));}- if value.is_null() {- return Ok(None);+ #[test]+ fn chromium_activation_uses_chromium_settle_duration() {+ assert_eq!(+ CHROMIUM_ACCESSIBILITY_SETTLE_DURATION,+ Duration::from_secs(3)+ );}- let value = unsafe { CFType::wrap_under_create_rule(value) };- let ax_value = value.as_CFTypeRef() as AXValueRef;-- if unsafe { AXValueGetType(ax_value) } != kAXValueTypeCGRect {- return Ok(None);+ #[test]+ fn verification_retry_slows_after_fast_attempts() {+ assert_eq!(+ accessibility_activation_verification_retry_interval(+ ACCESSIBILITY_ACTIVATION_FAST_VERIFICATION_ATTEMPTS+ ),+ ACCESSIBILITY_ACTIVATION_VERIFICATION_RETRY_INTERVAL+ );+ assert_eq!(+ accessibility_activation_verification_retry_interval(+ ACCESSIBILITY_ACTIVATION_FAST_VERIFICATION_ATTEMPTS + 1+ ),+ ACCESSIBILITY_ACTIVATION_SLOW_VERIFICATION_RETRY_INTERVAL+ );}- let mut rect = MaybeUninit::<NSRect>::uninit();-- let ok = unsafe {- AXValueGetValue(- ax_value,- kAXValueTypeCGRect,- rect.as_mut_ptr() as *mut c_void,- )- };-- if !ok {- return Ok(None);+ #[test]+ fn text_range_bounds_probe_requires_non_zero_geometry() {+ let usable = TextRangeBoundsProbe::Success(Rect {+ x: 10.0,+ y: 20.0,+ width: 1.0,+ height: 12.0,+ });+ let unavailable = TextRangeBoundsProbe::Unavailable;++ assert!(usable.has_usable_text_metrics());+ assert!(!unavailable.has_usable_text_metrics());+ assert!(rect_has_usable_text_metrics(Rect {+ x: 0.0,+ y: 0.0,+ width: 1.0,+ height: 1.0,+ }));+ assert!(!rect_has_usable_text_metrics(Rect {+ x: 0.0,+ y: 0.0,+ width: 0.0,+ height: 1.0,+ }));}+}- let rect = unsafe { rect.assume_init() };-- Ok(Some(Rect {- x: rect.origin.x,- y: rect.origin.y,- width: rect.size.width,- height: rect.size.height,- }))+fn element_rect_for_text_range(+ element: &AXUIElement,+ start_index: isize,+ length: isize,+) -> Result<Option<Rect>, Error> {+ match probe_element_rect_for_text_range(element, start_index, length) {+ TextRangeBoundsProbe::Success(rect) => Ok(Some(rect)),+ TextRangeBoundsProbe::InvalidRangeValue => Err(Error::Ax(kAXErrorIllegalArgument)),+ TextRangeBoundsProbe::AxError(error) => Err(Error::Ax(error)),+ TextRangeBoundsProbe::Unavailable => Ok(None),+ }}