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
pub mod attribute;
pub mod ui_element;
mod util;

use core_foundation::array::CFArray;
use std::{
    cell::Cell,
    cmp, thread,
    time::{Duration, Instant},
};

pub use attribute::*;
pub use ui_element::*;

pub trait TreeVisitor {
    fn enter_element(&self, element: &AXUIElement) -> TreeWalkerFlow;
    fn exit_element(&self, element: &AXUIElement);
}

pub struct TreeWalker {
    attr_children: AXAttribute<CFArray<AXUIElement>>,
}

#[derive(Copy, Clone, PartialEq, Eq)]
pub enum TreeWalkerFlow {
    Continue,
    SkipSubtree,
    Exit,
}

impl TreeWalker {
    pub fn new() -> Self {
        Self {
            attr_children: AXAttribute::children(),
        }
    }

    pub fn walk(&self, root: &AXUIElement, visitor: &dyn TreeVisitor) {
        let _ = self.walk_one(root, visitor);
    }

    fn walk_one(&self, root: &AXUIElement, visitor: &dyn TreeVisitor) -> TreeWalkerFlow {
        let mut flow = visitor.enter_element(root);

        if flow == TreeWalkerFlow::Continue {
            if let Ok(children) = root.attribute(&self.attr_children) {
                for child in children.into_iter() {
                    let child_flow = self.walk_one(&*child, visitor);

                    if child_flow == TreeWalkerFlow::Exit {
                        flow = child_flow;
                        break;
                    }
                }
            }
        }

        visitor.exit_element(root);
        flow
    }
}

pub struct ElementFinder {
    implicit_wait: Option<Duration>,
    predicate: Box<dyn Fn(&AXUIElement) -> bool>,
    result: Cell<Option<AXUIElement>>,
}

impl ElementFinder {
    pub fn new<F>(predicate: F, implicit_wait: Option<Duration>) -> Self
    where
        F: 'static + Fn(&AXUIElement) -> bool,
    {
        Self {
            predicate: Box::new(predicate),
            implicit_wait,
            result: Cell::new(None),
        }
    }

    pub fn find(&self, root: &AXUIElement) -> Option<AXUIElement> {
        let mut deadline = Instant::now();
        let walker = TreeWalker::new();

        if let Some(implicit_wait) = &self.implicit_wait {
            deadline += *implicit_wait;
        }

        loop {
            walker.walk(root, self);

            if let Some(result) = self.result.take() {
                return Some(result);
            }

            if Instant::now() >= deadline {
                return None;
            }

            if let Some(implicit_wait) = &self.implicit_wait {
                thread::sleep(cmp::min(*implicit_wait, Duration::from_millis(250)));
            }
        }
    }
}

impl TreeVisitor for ElementFinder {
    fn enter_element(&self, element: &AXUIElement) -> TreeWalkerFlow {
        if (self.predicate)(element) {
            self.result.set(Some(element.clone()));
            return TreeWalkerFlow::Exit;
        }

        TreeWalkerFlow::Continue
    }

    fn exit_element(&self, _element: &AXUIElement) {}
}