Skip to content
Closed
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
2 changes: 1 addition & 1 deletion apps/docs/content/docs/surfaces.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Each platform supports a subset:
</thead>
<tbody>
<tr><td>macOS</td><td><code>window</code>, <code>focused</code>, <code>menu</code>, <code>menubar</code>, <code>sheet</code>, <code>popover</code>, <code>alert</code>, <code>dock</code>, <code>menu-bar-extras</code></td></tr>
<tr><td>Windows</td><td><code>window</code>, <code>focused</code>, <code>sheet</code>, <code>menu</code>, <code>start-menu</code>, <code>taskbar</code>, <code>system-tray</code>, <code>system-tray-overflow</code>, <code>action-center</code></td></tr>
<tr><td>Windows</td><td><code>window</code>, <code>focused</code>, <code>sheet</code>, <code>menu</code>, <code>popover</code>, <code>start-menu</code>, <code>taskbar</code>, <code>system-tray</code>, <code>system-tray-overflow</code>, <code>action-center</code></td></tr>
<tr><td>Linux</td><td><code>window</code>, <code>sheet</code>, <code>alert</code>, <code>menu</code>, plus GNOME Shell kinds the session can resolve</td></tr>
</tbody>
</table>
Expand Down
4 changes: 3 additions & 1 deletion crates/windows/src/system/adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ impl SystemOps for WindowsAdapter {
}

/// The surfaces this adapter can observe: a named window, the focused
/// window, a Chromium modal-as-sheet, an open application menu, and the
/// window, a Chromium modal-as-sheet, an open application menu, a popup
/// window the requested window owns (a Chromium toolbar bubble), and the
/// shell kinds the shell-surface resolver reaches on this build. Core
/// validates the requested surface against this list before the adapter
/// is ever called (the mirror of macOS `supported_surfaces`), so a kind
Expand All @@ -119,6 +120,7 @@ impl SystemOps for WindowsAdapter {
SnapshotSurface::Focused,
SnapshotSurface::Sheet,
SnapshotSurface::Menu,
SnapshotSurface::Popover,
SnapshotSurface::StartMenu,
SnapshotSurface::Taskbar,
SnapshotSurface::SystemTray,
Expand Down
1 change: 1 addition & 0 deletions crates/windows/src/system/adapter_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ fn supported_surfaces_advertises_window_focused_sheet_menu_and_shell_kinds() {
SnapshotSurface::Focused,
SnapshotSurface::Sheet,
SnapshotSurface::Menu,
SnapshotSurface::Popover,
SnapshotSurface::StartMenu,
SnapshotSurface::Taskbar,
SnapshotSurface::SystemTray,
Expand Down
1 change: 1 addition & 0 deletions crates/windows/src/system/window/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ pub(crate) mod window_op_readback;
pub(crate) mod window_op_verb;
pub(crate) mod window_op_wait;
pub(crate) mod window_ops;
pub(crate) mod window_popover;
pub(crate) mod window_resolve;
113 changes: 113 additions & 0 deletions crates/windows/src/system/window/window_popover.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
//! The popover surface: a popup window an application owns beside the window
//! it was opened from.
//!
//! Chromium draws its toolbar bubbles (the Edge and Chrome extensions menu, a
//! site-permission prompt, a profile menu) as a separate `WS_POPUP` window
//! owned by the browser frame and styled as a tool window. The window census
//! drops tool windows and the frame's own UIA tree does not contain the
//! bubble, so neither `list-windows` nor `snapshot --surface window` can reach
//! it. A popover is found instead by ownership: a visible, uncloaked popup of
//! the same process whose owner chain leads to the requested window. Chromium
//! also keeps tooltip popups under the same owner; they are click-through
//! (`WS_EX_TRANSPARENT`) and take no input, so they are never candidates. Of
//! what remains, the largest window is the one rooted.

use agent_desktop_core::{AdapterError, ProcessId, Rect};

use super::window_enum::{EnumeratedWindow, WindowHandle, enumerate_top_level};
use super::window_identity::live_window_owner;

const OWNER_CHAIN_LIMIT: usize = 8;

pub(crate) fn locate_popover(owner: WindowHandle) -> Result<Option<WindowHandle>, AdapterError> {
let Some(pid) = live_window_owner(owner) else {
return Ok(None);
};
let mut candidates = Vec::new();
enumerate_top_level(|window| {
if window.handle != owner && is_popover_of(&window, owner, pid) {
candidates.push((window.handle, window.rect));
}
true
})?;
Ok(largest_window(candidates))
}

pub(crate) fn largest_window(
candidates: impl IntoIterator<Item = (WindowHandle, Rect)>,
) -> Option<WindowHandle> {
let mut best: Option<(WindowHandle, f64)> = None;
for (handle, rect) in candidates {
let area = rect.width * rect.height;
if best.is_none_or(|(_, largest)| area > largest) {
best = Some((handle, area));
}
}
best.map(|(handle, _)| handle)
}

fn is_popover_of(window: &EnumeratedWindow, owner: WindowHandle, pid: ProcessId) -> bool {
window.visible
&& !window.cloaked
&& !window.iconic
&& !window.is_zero_sized()
&& live_window_owner(window.handle) == Some(pid)
&& is_popup(window.handle)
&& !is_click_through(window.handle)
&& owner_chain_reaches(window.handle, owner)
}

#[cfg(target_os = "windows")]
fn is_popup(handle: WindowHandle) -> bool {
use windows_sys::Win32::UI::WindowsAndMessaging::{GWL_STYLE, GetWindowLongW, WS_POPUP};

let style = unsafe { GetWindowLongW(handle, GWL_STYLE) } as u32;
style & WS_POPUP != 0
}

#[cfg(not(target_os = "windows"))]
fn is_popup(_handle: WindowHandle) -> bool {
false
}

#[cfg(target_os = "windows")]
fn is_click_through(handle: WindowHandle) -> bool {
use windows_sys::Win32::UI::WindowsAndMessaging::{
GWL_EXSTYLE, GetWindowLongW, WS_EX_TRANSPARENT,
};

let ex_style = unsafe { GetWindowLongW(handle, GWL_EXSTYLE) } as u32;
ex_style & WS_EX_TRANSPARENT != 0
}

#[cfg(not(target_os = "windows"))]
fn is_click_through(_handle: WindowHandle) -> bool {
false
}

#[cfg(target_os = "windows")]
fn owner_chain_reaches(handle: WindowHandle, owner: WindowHandle) -> bool {
use windows_sys::Win32::UI::WindowsAndMessaging::{GW_OWNER, GetWindow};

let mut current = handle;
for _ in 0..OWNER_CHAIN_LIMIT {
current = unsafe { GetWindow(current, GW_OWNER) };
if current.is_null() {
return false;
}
if current == owner {
return true;
}
}
false
}

#[cfg(not(target_os = "windows"))]
fn owner_chain_reaches(_handle: WindowHandle, _owner: WindowHandle) -> bool {
let _ = OWNER_CHAIN_LIMIT;
false
}

#[cfg(test)]
#[path = "window_popover_tests.rs"]
mod tests;
70 changes: 70 additions & 0 deletions crates/windows/src/system/window/window_popover_tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
use super::*;

fn handle(value: usize) -> WindowHandle {
value as WindowHandle
}

fn rect(width: f64, height: f64) -> Rect {
Rect {
x: 0.0,
y: 0.0,
width,
height,
}
}

#[test]
fn no_candidate_roots_nothing() {
assert!(largest_window(Vec::new()).is_none());
}

#[test]
fn the_bubble_wins_over_the_tooltip_it_owns() {
let tooltip = (handle(1), rect(136.0, 25.0));
let bubble = (handle(2), rect(384.0, 678.0));
assert_eq!(largest_window(vec![tooltip, bubble]), Some(handle(2)));
assert_eq!(largest_window(vec![bubble, tooltip]), Some(handle(2)));
}

#[test]
fn an_equal_area_keeps_the_first_window_enumerated() {
let first = (handle(1), rect(100.0, 100.0));
let second = (handle(2), rect(100.0, 100.0));
assert_eq!(largest_window(vec![first, second]), Some(handle(1)));
}

#[cfg(target_os = "windows")]
#[test]
fn a_window_that_does_not_exist_owns_no_popover() {
assert!(matches!(locate_popover(std::ptr::null_mut()), Ok(None)));
}

#[cfg(target_os = "windows")]
#[test]
fn an_owned_tool_popup_roots_and_a_hidden_or_click_through_popup_does_not() {
use crate::tree::fixture::{bootstrap, fixture_popover::PopoverFixture};
use windows_sys::Win32::UI::WindowsAndMessaging::{
GWL_EXSTYLE, SW_HIDE, SW_SHOWNOACTIVATE, SetWindowLongW, ShowWindow, WS_EX_TOOLWINDOW,
WS_EX_TRANSPARENT,
};

bootstrap();
let fixture = PopoverFixture::create().expect("the popover fixture opens");
let owner = fixture.owner_handle() as WindowHandle;
let popup = fixture.popup_handle() as WindowHandle;
assert_eq!(locate_popover(owner).unwrap(), Some(popup));
assert_eq!(locate_popover(popup).unwrap(), None);

unsafe { ShowWindow(popup, SW_HIDE) };
assert_eq!(locate_popover(owner).unwrap(), None);
unsafe { ShowWindow(popup, SW_SHOWNOACTIVATE) };
assert_eq!(locate_popover(owner).unwrap(), Some(popup));
unsafe {
SetWindowLongW(
popup,
GWL_EXSTYLE,
(WS_EX_TOOLWINDOW | WS_EX_TRANSPARENT) as i32,
);
}
assert_eq!(locate_popover(owner).unwrap(), None);
}
39 changes: 39 additions & 0 deletions crates/windows/src/tree/fixture/fixture_popover.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use super::LocalFixture;
use windows_sys::Win32::UI::WindowsAndMessaging::{
GWL_EXSTYLE, GWL_STYLE, GWLP_HWNDPARENT, GetWindowLongPtrW, GetWindowLongW, SetWindowLongPtrW,
SetWindowLongW, WS_EX_TOOLWINDOW, WS_POPUP, WS_VISIBLE,
};

pub(crate) struct PopoverFixture {
popup: LocalFixture,
owner: LocalFixture,
}

impl PopoverFixture {
pub(crate) fn create() -> Result<Self, String> {
let owner = LocalFixture::create()?;
let (left, top) = owner.origin();
let popup = LocalFixture::create_at(left + 40, top + 40)?;
let hwnd = popup.handle() as _;
unsafe {
SetWindowLongW(hwnd, GWL_STYLE, (WS_POPUP | WS_VISIBLE) as i32);
SetWindowLongW(hwnd, GWL_EXSTYLE, WS_EX_TOOLWINDOW as i32);
SetWindowLongPtrW(hwnd, GWLP_HWNDPARENT, owner.handle());
if GetWindowLongPtrW(hwnd, GWLP_HWNDPARENT) != owner.handle()
|| GetWindowLongW(hwnd, GWL_STYLE) as u32 & WS_POPUP == 0
|| GetWindowLongW(hwnd, GWL_EXSTYLE) as u32 & WS_EX_TOOLWINDOW == 0
{
return Err("the fixture did not become an owned tool popup".into());
}
}
Ok(Self { popup, owner })
}

pub(crate) fn owner_handle(&self) -> isize {
self.owner.handle()
}

pub(crate) fn popup_handle(&self) -> isize {
self.popup.handle()
}
}
2 changes: 2 additions & 0 deletions crates/windows/src/tree/fixture/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,8 @@ pub(crate) mod fixture_overlay;
#[cfg(all(test, target_os = "windows"))]
pub(crate) mod fixture_pattern;
#[cfg(all(test, target_os = "windows"))]
pub(crate) mod fixture_popover;
#[cfg(all(test, target_os = "windows"))]
pub(crate) mod fixture_spawn;
#[cfg(all(test, target_os = "windows"))]
pub(crate) mod fixture_window;
Expand Down
4 changes: 2 additions & 2 deletions crates/windows/src/tree/observe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use serde_json::json;
use super::chromium;
use super::surfaces::surface_root;
use super::walk::walker::{DEFAULT_MAX_RAW_DEPTH, WalkBudget, WalkOutcome};
use super::walk::walker_source::walk_uia_subtree;
use super::walk::walker_source::walk_uia_subtree_on_surface;

/// Runs one observation: resolve the root, walk it, detect a Chromium shell,
/// and report completeness honestly.
Expand Down Expand Up @@ -55,7 +55,7 @@ pub(crate) fn observe_tree(
let budget = WalkBudget::new(request.max_logical_depth, request.deadline)
.with_max_raw_depth(request.max_raw_depth.min(DEFAULT_MAX_RAW_DEPTH));
let walk_started = Instant::now();
let outcome = walk_uia_subtree(&element, &root, budget)?;
let outcome = walk_uia_subtree_on_surface(&element, &root, request.surface, budget)?;
finish_observation(
root,
request,
Expand Down
4 changes: 4 additions & 0 deletions crates/windows/src/tree/resolve/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,3 +165,7 @@ pub(crate) mod resolve_search;
#[cfg(all(test, target_os = "windows"))]
#[path = "resolve_tests.rs"]
mod windows_only;

#[cfg(all(test, target_os = "windows"))]
#[path = "resolve_popover_tests.rs"]
mod popover_tests;
79 changes: 79 additions & 0 deletions crates/windows/src/tree/resolve/resolve_popover_tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
use super::resolve_anchor::resolve_locator_anchor;
use crate::system::window::window_identity::live_window_owner;
use crate::tree::fixture::{ensure_test_apartment, fixture_popover::PopoverFixture};
use crate::tree::walk::walker::WalkBudget;
use crate::tree::walk::walker_source::UiaTreeSource;
use agent_desktop_core::{Deadline, SnapshotSurface};
use std::ops::ControlFlow;

#[test]
fn a_ref_from_a_live_popover_resolves_back_into_that_popover() {
ensure_test_apartment();
let fixture = PopoverFixture::create().expect("the owned popup fixture starts");
let owner_pid = live_window_owner(fixture.owner_handle() as _)
.expect("the owner window belongs to a live process");
let token = crate::system::process_identity::token_for_pid(owner_pid)
.expect("the owner process token is readable")
.expect("the owner process is live");
let owner = agent_desktop_core::WindowInfo {
id: format!("w-{}", fixture.owner_handle()),
title: String::new(),
app: String::new(),
pid: owner_pid,
process_instance: Some(token.clone()),
bounds: None,
state: Default::default(),
};
let deadline = Deadline::after(10_000).expect("the resolution deadline is valid");
let popup = crate::tree::automation::root_from_hwnd(fixture.popup_handle(), deadline)
.expect("the popup HWND roots a UI Automation element");
let source = UiaTreeSource::for_root(&popup).expect("the popup provider resolves");
let prepared = source
.prepare_root(&popup)
.expect("the popup root is prepared");
let target = crate::tree::walk::walker_fake::scan_subtree(
&source,
&prepared,
&WalkBudget::new(10, deadline),
10,
&mut |path, _, evidence, _| {
if evidence
.name
.known()
.is_some_and(|name| name == "fixture-button")
{
ControlFlow::Break((path.to_vec(), evidence))
} else {
ControlFlow::Continue(())
}
},
)
.expect("the popup tree can be scanned")
.expect("the popup exposes its button");
let (path, evidence) = target;
let mut entry = crate::tree::walk::walker_fake::ref_entry(
evidence.role.known().expect("the button role is known"),
);
entry.identity.name = Some("fixture-button".into());
entry.identity.native_id = evidence.identifiers.preferred_identifier().cloned();
entry.scope.path = path.into();
entry.process = agent_desktop_core::RefProcess {
pid: owner_pid,
process_instance: Some(token),
};
entry.identity.unique_at_capture = true;
entry.source.source_app = Some(String::new());
entry.source.source_window_id = Some(owner.id.clone());
entry.source.source_window_title = Some(String::new());
entry.source.source_surface = SnapshotSurface::Popover;
entry.scope.path_is_absolute = true;

let resolved = resolve_locator_anchor(&entry, deadline)
.expect("a ref captured on a popover must resolve against the same popover");

assert!(
resolved
.downcast_ref::<crate::tree::element::UIAElement>()
.is_some()
);
}
Loading