automata-windows 0.1.0

Declarative Workflow Engine for Windows UI Automation
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
// Windows implementation of UIElement.
// Top-level module (element.rs); super:: resolves to the crate root.

use super::{
    Locator, Selector, UiError, check_click_point, check_keyboard_target, force_foreground,
};
use uiautomation::UIAutomation;
use uiautomation::inputs::{Keyboard, Mouse};
use uiautomation::patterns::{
    UIInvokePattern, UISelectionItemPattern, UIValuePattern, UIWindowPattern,
};
use uiautomation::types::{Point, TreeScope, WindowVisualState};

fn map_err(e: impl std::fmt::Display) -> UiError {
    UiError::Platform(e.to_string())
}

fn uia_rect(r: &uiautomation::types::Rect) -> visioncortex::BoundingRect {
    let left = r.get_left();
    let top = r.get_top();
    visioncortex::BoundingRect {
        left,
        top,
        right: left + r.get_width(),
        bottom: top + r.get_height(),
    }
}

/// Wraps a `uiautomation::UIElement` with a convenient automation API.
#[derive(Clone)]
pub struct UIElement {
    pub(crate) inner: uiautomation::UIElement,
}

impl UIElement {
    pub(crate) fn new(inner: uiautomation::UIElement) -> Self {
        Self { inner }
    }

    // ── Properties ───────────────────────────────────────────────────────

    pub fn name(&self) -> Option<String> {
        self.inner.get_name().ok().filter(|s| !s.is_empty())
    }

    pub fn name_or_empty(&self) -> String {
        self.inner.get_name().unwrap_or_default()
    }

    /// Localized role string (e.g. "Button", "Pane", "ToolBar").
    pub fn role(&self) -> String {
        self.inner.get_localized_control_type().unwrap_or_default()
    }

    pub fn id(&self) -> Option<String> {
        self.inner
            .get_automation_id()
            .ok()
            .filter(|s| !s.is_empty())
    }

    pub fn id_or_empty(&self) -> String {
        self.inner.get_automation_id().unwrap_or_default()
    }

    pub fn process_id(&self) -> Result<u32, UiError> {
        self.inner.get_process_id().map_err(map_err)
    }

    /// Bounding box as `(x, y, width, height)`.
    pub fn bounds(&self) -> Result<(i32, i32, i32, i32), UiError> {
        self.inner
            .get_bounding_rectangle()
            .map_err(map_err)
            .map(|r| (r.get_left(), r.get_top(), r.get_width(), r.get_height()))
    }

    pub fn is_enabled(&self) -> Result<bool, UiError> {
        self.inner.is_enabled().map_err(map_err)
    }

    pub fn is_visible(&self) -> Result<bool, UiError> {
        self.inner.is_offscreen().map_err(map_err).map(|off| !off)
    }

    /// Lowercase control type string (e.g. "dialog", "property grid").
    pub fn control_type(&self) -> Result<String, UiError> {
        self.inner
            .get_localized_control_type()
            .map(|s| s.to_lowercase())
            .map_err(map_err)
    }

    /// The element's own name/label text.
    pub fn text(&self) -> Result<String, UiError> {
        text_of(&self.inner).map_err(Into::into)
    }

    /// Direct children's names joined by newlines, excluding the element's own name.
    pub fn inner_text(&self) -> Result<String, UiError> {
        let mut parts = Vec::new();
        for child in self.children()? {
            let child_name = child.inner.get_name().unwrap_or_default();
            if !child_name.is_empty() {
                parts.push(child_name);
            }
        }
        Ok(parts.join("\n"))
    }

    // ── Navigation ───────────────────────────────────────────────────────

    pub fn children(&self) -> Result<Vec<UIElement>, UiError> {
        let auto = UIAutomation::new_direct().map_err(map_err)?;
        let cond = auto.create_true_condition().map_err(map_err)?;
        self.inner
            .find_all(TreeScope::Children, &cond)
            .map_err(map_err)
            .map(|v| v.into_iter().map(UIElement::new).collect())
    }

    pub fn parent(&self) -> Result<Option<UIElement>, UiError> {
        let auto = UIAutomation::new_direct().map_err(map_err)?;
        let walker = auto.get_raw_view_walker().map_err(map_err)?;
        match walker.get_parent(&self.inner) {
            Ok(p) => Ok(Some(UIElement::new(p))),
            Err(_) => Ok(None), // at root or no parent
        }
    }

    // ── Focus ────────────────────────────────────────────────────────────

    pub fn focus(&self) -> Result<(), UiError> {
        self.inner.set_focus().map_err(map_err)
    }

    // ── Window management ────────────────────────────────────────────────

    /// Bring the window to the foreground, restoring it if minimized.
    pub fn activate_window(&self) -> Result<(), UiError> {
        let handle = self.inner.get_native_window_handle().map_err(map_err)?;
        force_foreground(handle.into()).map_err(|e| UiError::Platform(e))
    }

    pub fn minimize_window(&self) -> Result<(), UiError> {
        let wp = self
            .inner
            .get_pattern::<UIWindowPattern>()
            .map_err(|_| UiError::Internal("No WindowPattern".into()))?;
        wp.set_window_visual_state(WindowVisualState::Minimized)
            .map_err(map_err)
    }

    pub fn close(&self) -> Result<(), UiError> {
        let wp = self
            .inner
            .get_pattern::<UIWindowPattern>()
            .map_err(|_| UiError::Internal("No WindowPattern".into()))?;
        wp.close().map_err(map_err)
    }

    // ── Locator ──────────────────────────────────────────────────────────

    pub fn locator(&self, selector: Selector) -> Result<Locator, UiError> {
        Ok(Locator::new(self.clone(), selector))
    }
}

// ── ui_automata::Element impl ─────────────────────────────────────────────

impl ui_automata::Element for UIElement {
    fn name(&self) -> Option<String> {
        self.inner.get_name().ok().filter(|s| !s.is_empty())
    }

    fn role(&self) -> String {
        self.inner.get_localized_control_type().unwrap_or_default()
    }

    fn text(&self) -> Result<String, ui_automata::AutomataError> {
        text_of(&self.inner).map_err(Into::into)
    }

    fn inner_text(&self) -> Result<String, ui_automata::AutomataError> {
        let mut parts = Vec::new();
        for child in self.children()? {
            let n = child.inner.get_name().unwrap_or_default();
            if !n.is_empty() {
                parts.push(n);
            }
        }
        Ok(parts.join("\n"))
    }

    fn is_enabled(&self) -> Result<bool, ui_automata::AutomataError> {
        self.inner.is_enabled().map_err(map_err).map_err(Into::into)
    }

    fn is_visible(&self) -> Result<bool, ui_automata::AutomataError> {
        self.inner
            .is_offscreen()
            .map_err(map_err)
            .map(|off| !off)
            .map_err(Into::into)
    }

    fn process_id(&self) -> Result<u32, ui_automata::AutomataError> {
        self.inner
            .get_process_id()
            .map_err(map_err)
            .map_err(Into::into)
    }

    fn process_name(&self) -> Option<String> {
        let pid = self.inner.get_process_id().ok()?;
        crate::get_process_name(pid as i32).ok()
    }

    fn bounds(&self) -> Result<(i32, i32, i32, i32), ui_automata::AutomataError> {
        self.inner
            .get_bounding_rectangle()
            .map_err(map_err)
            .map_err(Into::into)
            .map(|r| (r.get_left(), r.get_top(), r.get_width(), r.get_height()))
    }

    fn children(&self) -> Result<Vec<Self>, ui_automata::AutomataError> {
        let auto = UIAutomation::new_direct()
            .map_err(map_err)
            .map_err(Into::<ui_automata::AutomataError>::into)?;
        let cond = auto
            .create_true_condition()
            .map_err(map_err)
            .map_err(Into::<ui_automata::AutomataError>::into)?;
        self.inner
            .find_all(TreeScope::Children, &cond)
            .map_err(map_err)
            .map_err(Into::into)
            .map(|v| v.into_iter().map(UIElement::new).collect())
    }

    fn has_parent(&self) -> bool {
        let Ok(auto) = UIAutomation::new_direct() else {
            return false;
        };
        let Ok(walker) = auto.get_raw_view_walker() else {
            return false;
        };
        walker.get_parent(&self.inner).is_ok()
    }

    fn parent(&self) -> Option<Self> {
        let auto = UIAutomation::new_direct().ok()?;
        let walker = auto.get_raw_view_walker().ok()?;
        walker.get_parent(&self.inner).ok().map(UIElement::new)
    }

    fn click(&self) -> Result<(), ui_automata::AutomataError> {
        let (x, y, w, h) = ui_automata::Element::bounds(self)?;
        let cx = x + w / 2;
        let cy = y + h / 2;
        let pid = self
            .inner
            .get_process_id()
            .map_err(map_err)
            .map_err(Into::<ui_automata::AutomataError>::into)?;
        check_click_point(cx, cy, pid).map_err(ui_automata::AutomataError::Platform)?;
        Mouse::new()
            .click(Point::new(cx, cy))
            .map_err(map_err)
            .map_err(Into::into)
    }

    fn double_click(&self) -> Result<(), ui_automata::AutomataError> {
        let (x, y, w, h) = ui_automata::Element::bounds(self)?;
        let cx = x + w / 2;
        let cy = y + h / 2;
        let pid = self
            .inner
            .get_process_id()
            .map_err(map_err)
            .map_err(Into::<ui_automata::AutomataError>::into)?;
        check_click_point(cx, cy, pid).map_err(ui_automata::AutomataError::Platform)?;
        Mouse::new()
            .double_click(Point::new(cx, cy))
            .map_err(map_err)
            .map_err(Into::into)
    }

    fn hover(&self) -> Result<(), ui_automata::AutomataError> {
        let (x, y, w, h) = ui_automata::Element::bounds(self)?;
        let point = Point::new(x + w / 2, y + h / 2);
        Mouse::new()
            .move_to(point)
            .map_err(map_err)
            .map_err(Into::into)
    }

    fn click_at(
        &self,
        x_pct: f64,
        y_pct: f64,
        kind: ui_automata::ClickType,
    ) -> Result<(), ui_automata::AutomataError> {
        let (x, y, w, h) = ui_automata::Element::bounds(self)?;
        let px = (x as f64 + w as f64 * x_pct / 100.0) as i32;
        let py = (y as f64 + h as f64 * y_pct / 100.0) as i32;
        let pid = self
            .inner
            .get_process_id()
            .map_err(map_err)
            .map_err(Into::<ui_automata::AutomataError>::into)?;
        check_click_point(px, py, pid).map_err(ui_automata::AutomataError::Platform)?;
        let mouse = Mouse::new();
        if matches!(
            kind,
            ui_automata::ClickType::Triple | ui_automata::ClickType::Middle
        ) {
            let ct = match kind {
                ui_automata::ClickType::Triple => crate::ClickType::Triple,
                ui_automata::ClickType::Middle => crate::ClickType::Middle,
                _ => unreachable!(),
            };
            return crate::mouse_click(px, py, ct).map_err(ui_automata::AutomataError::Platform);
        }
        match kind {
            ui_automata::ClickType::Left => mouse.click(Point::new(px, py)),
            ui_automata::ClickType::Double => mouse.double_click(Point::new(px, py)),
            ui_automata::ClickType::Right => mouse.right_click(Point::new(px, py)),
            ui_automata::ClickType::Triple | ui_automata::ClickType::Middle => unreachable!(),
        }
        .map_err(map_err)
        .map_err(Into::into)
    }

    fn type_text(&self, text: &str) -> Result<(), ui_automata::AutomataError> {
        let pid = self
            .inner
            .get_process_id()
            .map_err(map_err)
            .map_err(Into::<ui_automata::AutomataError>::into)?;
        check_keyboard_target(pid).map_err(ui_automata::AutomataError::Platform)?;
        Keyboard::new()
            .send_text(text)
            .map_err(map_err)
            .map_err(Into::into)
    }

    fn press_key(&self, key: &str) -> Result<(), ui_automata::AutomataError> {
        let pid = self
            .inner
            .get_process_id()
            .map_err(map_err)
            .map_err(Into::<ui_automata::AutomataError>::into)?;
        check_keyboard_target(pid).map_err(ui_automata::AutomataError::Platform)?;
        Keyboard::new()
            .send_keys(&crate::input::normalise_key(key))
            .map_err(map_err)
            .map_err(Into::into)
    }

    fn set_value(&self, value: &str) -> Result<(), ui_automata::AutomataError> {
        self.inner
            .get_pattern::<UIValuePattern>()
            .and_then(|vp| vp.set_value(value))
            .map_err(map_err)
            .map_err(Into::into)
    }

    fn focus(&self) -> Result<(), ui_automata::AutomataError> {
        self.inner.set_focus().map_err(map_err).map_err(Into::into)
    }

    fn invoke(&self) -> Result<(), ui_automata::AutomataError> {
        // Try InvokePattern first — works without bounding rect or foreground.
        if let Ok(ip) = self.inner.get_pattern::<UIInvokePattern>() {
            return ip.invoke().map_err(map_err).map_err(Into::into);
        }
        // Try SelectionItemPattern::Select() — activates XAML list nav items
        // (e.g. Windows Settings nav, WinUI ListViews) without mouse or bounds.
        if let Ok(sp) = self.inner.get_pattern::<UISelectionItemPattern>() {
            return sp.select().map_err(map_err).map_err(Into::into);
        }
        Err(ui_automata::AutomataError::Platform(format!(
            "Invoke: element '{}' supports neither InvokePattern nor SelectionItemPattern",
            self.inner.get_name().unwrap_or_default()
        )))
    }

    fn scroll_into_view(&self) -> Result<(), ui_automata::AutomataError> {
        // Fast path: try ScrollItemPattern (handles virtualised lists correctly).
        if let Ok(sip) = self
            .inner
            .get_pattern::<uiautomation::patterns::UIScrollItemPattern>()
        {
            if sip.scroll_into_view().is_ok() {
                let bounds_ok = self
                    .inner
                    .get_bounding_rectangle()
                    .map(|r| r.get_width() > 1 && r.get_height() > 1)
                    .unwrap_or(false);
                log::info!("scroll_into_view: ScrollItemPattern result — bounds_ok={bounds_ok}");
                if bounds_ok {
                    return Ok(());
                }
            }
        }

        let auto = UIAutomation::new_direct().map_err(map_err)?;
        let walker = auto.get_raw_view_walker().map_err(map_err)?;

        let tr = uia_rect(&self.inner.get_bounding_rectangle().map_err(map_err)?);
        let anchor_rect = find_clipping_ancestor(&self.inner, &tr, &walker).map_err(map_err)?;
        let anchor_rect = match anchor_rect {
            Some(r) => r,
            None => {
                log::info!(
                    "scroll_into_view: reached root without finding a clipping ancestor — element already visible"
                );
                return Ok(());
            }
        };

        scroll_until_visible(&self.inner, &tr, &anchor_rect).map_err(Into::into)
    }

    fn activate_window(&self) -> Result<(), ui_automata::AutomataError> {
        let handle = self
            .inner
            .get_native_window_handle()
            .map_err(map_err)
            .map_err(Into::<ui_automata::AutomataError>::into)?;
        force_foreground(handle.into()).map_err(|e| ui_automata::AutomataError::Platform(e))
    }

    fn minimize_window(&self) -> Result<(), ui_automata::AutomataError> {
        self.inner
            .get_pattern::<UIWindowPattern>()
            .map_err(|_| ui_automata::AutomataError::Internal("No WindowPattern".into()))?
            .set_window_visual_state(WindowVisualState::Minimized)
            .map_err(map_err)
            .map_err(Into::into)
    }

    fn close(&self) -> Result<(), ui_automata::AutomataError> {
        self.inner
            .get_pattern::<UIWindowPattern>()
            .map_err(|_| ui_automata::AutomataError::Internal("No WindowPattern".into()))?
            .close()
            .map_err(map_err)
            .map_err(Into::into)
    }

    fn hwnd(&self) -> Option<u64> {
        let handle: isize = self.inner.get_native_window_handle().ok()?.into();
        Some(handle as u64)
    }

    fn automation_id(&self) -> Option<String> {
        self.inner
            .get_automation_id()
            .ok()
            .filter(|s| !s.is_empty())
    }
}

// ── Scroll helpers ────────────────────────────────────────────────────────────

/// Walk up the UIA ancestor chain to find the first ancestor that clips the
/// target element (i.e. at least one corner of the target falls outside the
/// ancestor's bounding rect). Returns `None` if no such ancestor is found
/// (element is already fully visible).
fn find_clipping_ancestor(
    el: &uiautomation::UIElement,
    target_rect: &visioncortex::BoundingRect,
    walker: &uiautomation::core::UITreeWalker,
) -> Result<Option<visioncortex::BoundingRect>, UiError> {
    let corners = [
        target_rect.left_top(),
        target_rect.top_right(),
        target_rect.bottom_left(),
        target_rect.right_bottom(),
    ];

    log::info!(
        "scroll_into_view: target '{}' role={} rect=[{},{} {}x{}]",
        el.get_name().unwrap_or_default(),
        el.get_control_type()
            .map(|t| format!("{t:?}"))
            .unwrap_or_default(),
        target_rect.left,
        target_rect.top,
        target_rect.right - target_rect.left,
        target_rect.bottom - target_rect.top,
    );

    let mut candidate = el.clone();
    loop {
        let parent = match walker.get_parent(&candidate) {
            Ok(p) => p,
            Err(_) => return Ok(None),
        };
        let pr = uia_rect(&parent.get_bounding_rectangle().map_err(map_err)?);
        if !pr.is_empty() && corners.iter().any(|&p| !pr.have_point_inside(p)) {
            log::info!(
                "scroll_into_view: clipping ancestor '{}' role={} rect=[{},{} {}x{}]",
                parent.get_name().unwrap_or_default(),
                parent
                    .get_control_type()
                    .map(|t| format!("{t:?}"))
                    .unwrap_or_default(),
                pr.left,
                pr.top,
                pr.right - pr.left,
                pr.bottom - pr.top,
            );
            return Ok(Some(pr));
        }
        candidate = parent;
    }
}

/// Scroll the container defined by `anchor_rect` until `el` is fully visible
/// within it, or until scrolling stalls (up to 200 ticks per pass).
fn scroll_until_visible(
    el: &uiautomation::UIElement,
    target_rect: &visioncortex::BoundingRect,
    anchor_rect: &visioncortex::BoundingRect,
) -> Result<(), UiError> {
    use super::{ScrollAxis, move_cursor, scroll_wheel};
    use std::thread;
    use std::time::Duration;

    let outside_v = target_rect.top < anchor_rect.top || target_rect.bottom > anchor_rect.bottom;
    let is_degenerate_start = target_rect.left == 0
        && target_rect.top == 0
        && target_rect.right <= 1
        && target_rect.bottom <= 1;

    // Negative = scroll down/right; positive = scroll up/left.
    // When starting degenerate we don't know which way, so begin with -1.
    let initial_delta: i32 = if is_degenerate_start {
        -1
    } else if outside_v {
        if target_rect.top > anchor_rect.bottom {
            -1
        } else {
            1
        }
    } else {
        if target_rect.left > anchor_rect.right {
            1
        } else {
            -1
        }
    };

    log::info!(
        "scroll_into_view: axis={} initial_delta={} degenerate_start={is_degenerate_start}",
        if outside_v { "vertical" } else { "horizontal" },
        initial_delta,
    );

    let anchor_center = anchor_rect.center();
    move_cursor(anchor_center.x, anchor_center.y);
    thread::sleep(Duration::from_millis(30));

    let passes: &[i32] = if is_degenerate_start {
        &[initial_delta, -initial_delta]
    } else {
        &[initial_delta]
    };

    for &delta in passes {
        log::info!("scroll_into_view: pass delta={delta}");
        let mut prev_top = i32::MIN;
        let mut prev_left = i32::MIN;

        for tick in 0..200 {
            if outside_v {
                scroll_wheel(ScrollAxis::Vertical, delta);
            } else {
                scroll_wheel(ScrollAxis::Horizontal, delta);
            }
            thread::sleep(Duration::from_millis(50));

            let nr = uia_rect(&el.get_bounding_rectangle().map_err(map_err)?);
            log::trace!(
                "scroll_into_view: tick={tick} target rect=[{},{} {}x{}]",
                nr.left,
                nr.top,
                nr.right - nr.left,
                nr.bottom - nr.top,
            );
            let new_corners = [
                nr.left_top(),
                nr.top_right(),
                nr.bottom_left(),
                nr.right_bottom(),
            ];
            if new_corners
                .iter()
                .all(|&p| anchor_rect.have_point_inside(p))
            {
                log::info!("scroll_into_view: element now visible after {tick} ticks");
                return Ok(());
            }

            let is_degenerate = nr.left == 0 && nr.top == 0 && nr.right <= 1 && nr.bottom <= 1;
            if !is_degenerate && nr.top == prev_top && nr.left == prev_left {
                log::info!("scroll_into_view: stalled at tick={tick}");
                break;
            }
            prev_top = nr.top;
            prev_left = nr.left;
        }
    }

    Err(UiError::Internal(format!(
        "scroll_into_view: '{}' could not be scrolled into view",
        el.get_name().unwrap_or_default()
    )))
}

// ── Shared helpers ────────────────────────────────────────────────────────────

/// Read the text value from a UIA element: try ValuePattern first, fall back to name.
fn text_of(el: &uiautomation::UIElement) -> Result<String, UiError> {
    if let Ok(vp) = el.get_pattern::<UIValuePattern>() {
        if let Ok(val) = vp.get_value() {
            return Ok(val);
        }
    }
    Ok(el.get_name().unwrap_or_default())
}