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
48 changes: 36 additions & 12 deletions src/app_executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::{
};
use accessibility::{AXUIElement, AXUIElementActions, AXUIElementAttributes};
use accessibility_sys::{
kAXErrorAttributeUnsupported, kAXErrorCannotComplete, kAXFocusedAttribute,
kAXErrorAttributeUnsupported, kAXErrorCannotComplete, kAXFocusedAttribute, kAXPopoverRole,
};
use core_foundation::{base::TCFType, boolean::CFBoolean, number::CFNumber, string::CFString};
use log::Level;
Expand Down Expand Up @@ -455,11 +455,29 @@ impl AppExecutor {
}

fn select_app_window(&mut self, vis_level: VisibilityCheckingLevel) -> Option<Frame> {
let screen_frame = Frame::from_origion(self.screen_size);

// NOTE: prioritize system alarms
if let Ok(app) = AXUIElement::application_with_bundle("com.apple.coreservices.uiagent")
&& let Ok(window) = app.focused_window()
{
let frame = window.get_frame(screen_frame);
self.last_window_frame = frame;
self.is_electron = false;

self.selected = Some(ElementOfInterest::new(
Some(window),
None,
RoleOfInterest::Generic,
frame,
));

return Some(frame);
}

let (pid, is_electron) = get_focused_pid()?;
self.is_electron = is_electron;

let focused_app = AXUIElement::application(pid);
let screen_frame = Frame::from_origion(self.screen_size);

// HACK: need this to bootstrap UI tree generation for some electron apps,
// e.g. Discord
Expand All @@ -473,11 +491,20 @@ impl AppExecutor {
let (focused_window, window_frame) = if vis_level == VisibilityCheckingLevel::Loosest {
(focused_app, screen_frame)
} else {
let window = focused_app.focused_window().unwrap_or(focused_app);
let frame = window
.get_frame()
.and_then(|f| f.intersect(&screen_frame))
.unwrap_or(screen_frame);
let mut window = focused_app.focused_window();
// NOTE: prioritize popover windows, e.g. Apple Music search
if let Ok(windows) = focused_app.windows()
&& windows.len() > 1
{
for win in windows.iter() {
if win.role().is_ok_and(|r| r == kAXPopoverRole) {
window = Ok(win.clone());
break;
}
}
}
let window = window.unwrap_or(focused_app);
let frame = window.get_frame(screen_frame);
(window, frame)
};
self.last_window_frame = window_frame;
Expand Down Expand Up @@ -1071,10 +1098,7 @@ impl AppExecutor {
.and_then(|ele| ele.parent().ok())
{
let screen_frame = Frame::from_origion(self.screen_size);
let frame = parent_element
.get_frame()
.and_then(|f| f.intersect(&screen_frame))
.unwrap_or(screen_frame);
let frame = parent_element.get_frame(screen_frame);
self.selected = Some(ElementOfInterest {
element: Some(parent_element),
context: None,
Expand Down
13 changes: 8 additions & 5 deletions src/ax_element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ pub trait GetAttribute {
fn get_attribute(&self, attribute_name: &str) -> Option<CFType>;
fn get_attribute_string(&self, attribute_name: &str) -> Option<String>;
fn get_string_value_or_description(&self) -> Option<String>;
fn get_frame(&self) -> Option<Frame>;
fn get_frame(&self, default: Frame) -> Frame;
fn get_dom_classes(&self) -> Option<Vec<String>>;
fn inspect(&self) -> String;
fn is_clickable(&self) -> bool;
Expand Down Expand Up @@ -368,7 +368,7 @@ impl GetAttribute for AXUIElement {
.map(|cf| cf.to_string())
}

fn get_frame(&self) -> Option<Frame> {
fn get_frame(&self, default_frame: Frame) -> Frame {
let cf_array_in = CFArray::from_CFTypes(&[
CFString::new(kAXPositionAttribute),
CFString::new(kAXSizeAttribute),
Expand All @@ -385,7 +385,7 @@ impl GetAttribute for AXUIElement {
)
};

if err != kAXErrorSuccess || values_ref.is_null() {
let frame = if err != kAXErrorSuccess || values_ref.is_null() {
None
} else {
let values_array: CFArray<CFType> =
Expand All @@ -401,10 +401,13 @@ impl GetAttribute for AXUIElement {
.and_then(|size_ptr| cftype_to_rust_type::<CGSize>(*size_ptr, kAXValueTypeCGSize));

match (pos, size) {
(Some(p), Some(s)) => Some(Frame::new(p.x, p.y, p.x + s.width, p.y + s.height)),
(Some(p), Some(s)) => {
Frame::new(p.x, p.y, p.x + s.width, p.y + s.height).intersect(&default_frame)
}
_ => None,
}
}
};
frame.unwrap_or(default_frame)
}

fn inspect(&self) -> String {
Expand Down
Loading