Skip to content

Commit b9aa28d

Browse files
committed
refactor: simplify ax.rs and input.rs
- Unify get_ax_point/get_ax_size into generic get_ax_value<T> - Simplify verbose debug logging match arms in find_text dispatch
1 parent a1d3553 commit b9aa28d

2 files changed

Lines changed: 25 additions & 58 deletions

File tree

src/macos/ax.rs

Lines changed: 12 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -190,39 +190,17 @@ unsafe fn get_string_attribute(element: AXUIElementRef, attr_name: &str) -> Opti
190190
/// Get position (CGPoint) and size (CGSize) of an AX element.
191191
/// Returns None if either attribute is missing.
192192
unsafe fn get_position_and_size(element: AXUIElementRef) -> Option<(CGPoint, CGSize)> {
193-
let position = get_ax_point(element, "AXPosition")?;
194-
let size = get_ax_size(element, "AXSize")?;
193+
let position: CGPoint = get_ax_value(element, "AXPosition", K_AX_VALUE_TYPE_CGPOINT)?;
194+
let size: CGSize = get_ax_value(element, "AXSize", K_AX_VALUE_TYPE_CGSIZE)?;
195195
Some((position, size))
196196
}
197197

198-
/// Extract a CGPoint from an AXValue attribute.
199-
unsafe fn get_ax_point(element: AXUIElementRef, attr_name: &str) -> Option<CGPoint> {
200-
let attr = CFString::new(attr_name);
201-
let mut value_ref: core_foundation::base::CFTypeRef = ptr::null();
202-
let err = AXUIElementCopyAttributeValue(element, attr.as_concrete_TypeRef(), &mut value_ref);
203-
204-
if err != K_AX_ERROR_SUCCESS || value_ref.is_null() {
205-
return None;
206-
}
207-
208-
let mut point = CGPoint::new(0.0, 0.0);
209-
let ok = AXValueGetValue(
210-
value_ref as AXValueRef,
211-
K_AX_VALUE_TYPE_CGPOINT,
212-
&mut point as *mut CGPoint as *mut c_void,
213-
);
214-
215-
core_foundation::base::CFRelease(value_ref);
216-
217-
if ok {
218-
Some(point)
219-
} else {
220-
None
221-
}
222-
}
223-
224-
/// Extract a CGSize from an AXValue attribute.
225-
unsafe fn get_ax_size(element: AXUIElementRef, attr_name: &str) -> Option<CGSize> {
198+
/// Extract a typed value (CGPoint or CGSize) from an AXValue attribute.
199+
unsafe fn get_ax_value<T: Default>(
200+
element: AXUIElementRef,
201+
attr_name: &str,
202+
ax_value_type: u32,
203+
) -> Option<T> {
226204
let attr = CFString::new(attr_name);
227205
let mut value_ref: core_foundation::base::CFTypeRef = ptr::null();
228206
let err = AXUIElementCopyAttributeValue(element, attr.as_concrete_TypeRef(), &mut value_ref);
@@ -231,17 +209,17 @@ unsafe fn get_ax_size(element: AXUIElementRef, attr_name: &str) -> Option<CGSize
231209
return None;
232210
}
233211

234-
let mut size = CGSize::new(0.0, 0.0);
212+
let mut result = T::default();
235213
let ok = AXValueGetValue(
236214
value_ref as AXValueRef,
237-
K_AX_VALUE_TYPE_CGSIZE,
238-
&mut size as *mut CGSize as *mut c_void,
215+
ax_value_type,
216+
&mut result as *mut T as *mut c_void,
239217
);
240218

241219
core_foundation::base::CFRelease(value_ref);
242220

243221
if ok {
244-
Some(size)
222+
Some(result)
245223
} else {
246224
None
247225
}

src/tools/input.rs

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -413,34 +413,23 @@ pub fn find_text(params: FindTextParams) -> CallToolResult {
413413
};
414414

415415
// Primary: try accessibility tree search
416-
let ax_result = find_text_accessibility(&params.text, window_id);
417-
match &ax_result {
416+
match find_text_accessibility(&params.text, window_id) {
418417
Ok(matches) if !matches.is_empty() => {
419-
if debug {
420-
eprintln!(
421-
"[DEBUG find_text] accessibility search found {} matches for '{}'",
422-
matches.len(),
423-
params.text
424-
);
425-
}
426-
return serialize_matches(matches);
418+
return serialize_matches(&matches);
427419
}
428-
Ok(_) => {
429-
if debug {
430-
eprintln!(
431-
"[DEBUG find_text] accessibility search found no matches for '{}', falling back to OCR",
432-
params.text
433-
);
434-
}
420+
Ok(_) if debug => {
421+
eprintln!(
422+
"[DEBUG find_text] no accessibility matches for '{}', trying OCR",
423+
params.text
424+
);
435425
}
436-
Err(e) => {
437-
if debug {
438-
eprintln!(
439-
"[DEBUG find_text] accessibility search failed for '{}': {}, falling back to OCR",
440-
params.text, e
441-
);
442-
}
426+
Err(e) if debug => {
427+
eprintln!(
428+
"[DEBUG find_text] accessibility failed for '{}': {}, trying OCR",
429+
params.text, e
430+
);
443431
}
432+
_ => {}
444433
}
445434

446435
// Fallback: OCR

0 commit comments

Comments
 (0)