From b2544efa27fa0ac71091b05f67a7b736446cac78 Mon Sep 17 00:00:00 2001 From: bflatastic Date: Sun, 22 Nov 2026 21:01:07 +0700 Subject: [PATCH] automation: control lookup moves to access.rs (Target::rect, ControlMap); At::resolve takes a control map instead of the access tree --- crates/automation/src/access.rs | 14 +++++++++++++- crates/automation/src/synthetic.rs | 19 +++++-------------- crates/automation/tests/it/synthetic.rs | 6 ++++-- 3 files changed, 22 insertions(+), 17 deletions(-) diff --git a/crates/automation/src/access.rs b/crates/automation/src/access.rs index 335420a1d..f37f24c86 100644 --- a/crates/automation/src/access.rs +++ b/crates/automation/src/access.rs @@ -7,7 +7,7 @@ use craft_behavior::Mixed; use craft_ui::runtime::access::Role; use craft_ui::runtime::egui::access::{AccessTree, NodeKey, name}; use craft_ui::runtime::windows::WindowHost; -use egui::ViewportId; +use egui::{Rect, ViewportId}; use serde_json::{Map, Value, json}; use crate::protocol::Params; @@ -27,6 +27,9 @@ pub const NO_PLACE: &str = "the control has no place on the screen"; /// The largest `index` a target takes. pub const MAX_INDEX: usize = 1024; +/// The place of a control a target names: its rect in screen points, or the error why it has none. +pub type ControlMap<'a> = &'a dyn Fn(&Target) -> Result; + /// A control named by `{name, role?, within?, index?}`. /// /// A name match ignores surrounding white space and one trailing `:`; `role` is an AccessKit role name, compared @@ -129,6 +132,15 @@ impl Target { (None, found) => Err(format!("{AMBIGUOUS} ({}): add `role`, `within` or `index`", found.len())), } } + + /// The rect of this target's control in the tree the host kept for `viewport`: [`NO_TREE`] before the first, + /// [`NO_PLACE`] for a control with no area. + pub fn rect(&self, host: &WindowHost, viewport: ViewportId) -> Result { + let tree = host.access_tree(viewport).ok_or_else(|| NO_TREE.to_owned())?; + tree.bounds_of(self.find(&tree)?) + .filter(|rect| rect.is_finite() && rect.width() > 0.0 && rect.height() > 0.0) + .ok_or_else(|| NO_PLACE.to_owned()) + } } /// What `ui.find` answers for the control `key`: `{name, role, rect or null, enabled, focused, value?, numeric?, diff --git a/crates/automation/src/synthetic.rs b/crates/automation/src/synthetic.rs index e39cbbf1b..a2e1c6d23 100644 --- a/crates/automation/src/synthetic.rs +++ b/crates/automation/src/synthetic.rs @@ -21,7 +21,6 @@ use craft_behavior::PointerButton; use craft_keymap::{Key, Mods}; -use craft_ui::runtime::egui::access::AccessTree; use craft_ui::runtime::windows::{Released, WindowHost}; use egui::{Pos2, Vec2, ViewportId, pos2, vec2}; use serde_json::Value; @@ -95,18 +94,10 @@ impl At { /// The screen point, through `map` for a document point; [`NO_DOCUMENT`] when the map has none, and /// [`OFF_SCREEN`] when it maps beyond [`MAX_SCREEN_POINT`]; a point that is not finite is [`NOT_FINITE`]. A control - /// point is its rect's centre plus the offset: [`access::NO_TREE`] without a tree, [`access::NO_PLACE`] for a - /// control with no area. - pub fn resolve(&self, map: DocumentMap<'_>, tree: Option<&AccessTree>) -> Result { + /// point is the centre of the rect `controls` gives it plus the offset, or the error `controls` gives. + pub fn resolve(&self, map: DocumentMap<'_>, controls: access::ControlMap<'_>) -> Result { match self { - Self::Control { target, offset } => { - let tree = tree.ok_or_else(|| access::NO_TREE.to_owned())?; - let rect = tree - .bounds_of(target.find(tree)?) - .filter(|rect| rect.is_finite() && rect.width() > 0.0 && rect.height() > 0.0) - .ok_or_else(|| access::NO_PLACE.to_owned())?; - Ok(rect.center() + *offset) - } + Self::Control { target, offset } => Ok(controls(target)?.center() + *offset), Self::Screen(at) if at.x.is_finite() && at.y.is_finite() => Ok(*at), Self::Screen(_) => Err(NOT_FINITE.to_owned()), Self::Document(at, _) if !(at.x.is_finite() && at.y.is_finite()) => Err(NOT_FINITE.to_owned()), @@ -118,9 +109,9 @@ impl At { } } - /// [`At::resolve`] against the access tree the host kept for `viewport`. + /// [`At::resolve`] against the controls of the access tree the host kept for `viewport`. fn place(&self, host: &WindowHost, viewport: ViewportId, map: DocumentMap<'_>) -> Result { - self.resolve(map, host.access_tree(viewport).as_deref()) + self.resolve(map, &|target| target.rect(host, viewport)) } } diff --git a/crates/automation/tests/it/synthetic.rs b/crates/automation/tests/it/synthetic.rs index d792b31bd..07c47287e 100644 --- a/crates/automation/tests/it/synthetic.rs +++ b/crates/automation/tests/it/synthetic.rs @@ -5,6 +5,7 @@ use std::cell::{Cell, RefCell}; use std::rc::Rc; +use craft_automation::access::NO_TREE; use craft_automation::synthetic::{At, MAX_CLICKS, MAX_SCREEN_POINT, NO_DOCUMENT, NOT_FINITE, input_method}; use craft_ui::contract::{Bind, Show as _}; use craft_ui::runtime::egui::injected::hook_without_pass; @@ -415,12 +416,13 @@ fn ui_ime_reaches_a_focused_text_field_as_a_preedit_then_a_commit_in_order() { /// A NaN or infinite point of a request is an error at the request, in either coordinate space, not a panic in the pass. #[trial] fn a_point_that_is_not_finite_is_refused() { + let no_controls = &|_: &_| Err(NO_TREE.to_owned()); for bad in [f32::NAN, f32::INFINITY, f32::NEG_INFINITY] { for at in [At::Screen(pos2(bad, 1.0)), At::Screen(pos2(1.0, bad)), At::Document(pos2(bad, 1.0), 0)] { - assert_eq!(at.resolve(&|_, p| Some(p), None), Err(NOT_FINITE.to_owned()), "{at:?}"); + assert_eq!(at.resolve(&|_, p| Some(p), no_controls), Err(NOT_FINITE.to_owned()), "{at:?}"); } } - assert_eq!(At::Screen(pos2(3.0, 4.0)).resolve(&|_, _| None, None), Ok(pos2(3.0, 4.0))); + assert_eq!(At::Screen(pos2(3.0, 4.0)).resolve(&|_, _| None, no_controls), Ok(pos2(3.0, 4.0))); } /// `ui.wheel` in lines queues whole notches in the line unit, clamped; without `unit` the turn is in points; an