Skip to content

YC (S26) | Open Computer History | Continuously record your company computer work, map your workflows, help you find work worth automating, and power your agents' context

feat: windows ui automation (#1694)

tribhuwanba5f0b390f66
diff --git a/Cargo.lock b/Cargo.lock
index e66a43809..1c2f1471a 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -5965,6 +5965,7 @@ dependencies = [
"tokio",
"tracing",
"tracing-subscriber",
+ "uiautomation",
"url",
"which 6.0.3",
"zip 0.6.6",
diff --git a/screenpipe-core/Cargo.toml b/screenpipe-core/Cargo.toml
index 713989436..4ebfff798 100644
--- a/screenpipe-core/Cargo.toml
+++ b/screenpipe-core/Cargo.toml
@@ -70,3 +70,7 @@ objc-foundation = "0.1.1"
core-foundation = "=0.10.0"
core-graphics = { version = "0.24.0", features = ["highsierra"] }
+
+[target.'cfg(target_os = "windows")'.dependencies]
+uiautomation = { version = "0.16.1" }
+
diff --git a/screenpipe-core/examples/win_automation.rs b/screenpipe-core/examples/win_automation.rs
new file mode 100644
index 000000000..180c57851
--- /dev/null
+++ b/screenpipe-core/examples/win_automation.rs
@@ -0,0 +1,107 @@
+use screenpipe_core::operator::platforms;
+use screenpipe_core::operator::Selector;
+use screenpipe_core::operator::AutomationError;
+
+// like a playground, just uncomment
+fn main() -> Result<(), AutomationError> {
+ let engine = platforms::create_engine(true, true)?;
+
+ // get the root element
+ // let root_element = engine.get_root_element();
+ // println!("root element: {:?}", root_element);
+
+ // Get an element by process id
+ // let element_by_id = engine.get_element_by_id(12304)?;
+ // println!("element by pid: {:?}", element_by_id);
+
+ // get the focused element
+ // let focused_element = engine.get_focused_element()?;
+ // println!("Focused element: {:?}", focused_element);
+
+ // get all applications
+ // let applications = engine.get_applications()?;
+ // println!("running applications element: {:?}", applications);
+
+ // get an application by name
+ // let app_by_name = engine.get_application_by_name("terminal")?;
+ // println!("Application by name: {:?}", app_by_name);
+
+ // find elements
+ // let selector = Selector::Role { role: "button".to_string(), name: None };
+ // let elements = engine.find_elements(&selector, None)?;
+ // println!("Found elements: {:?}", elements);
+
+ // find a single element
+ // let sel = Selector::Role { role: "text".to_string(), name: None };
+ // let element = engine.find_element(&sel, None)?;
+ // println!("found single element: {:?}", element);
+
+ // open an application
+ // let opened_app = engine.open_application("msedge")?;
+ // println!("opened application: {:?}", opened_app);
+
+ // open a URL in a browser
+ // let ele = engine.open_url("https://github.com", Some("msedge"))?;
+ // println!("ele: {:?}", ele);
+
+ // perform actions on an element
+ // get root of an application
+ let element = engine.get_application_by_name("firefox")?;
+ println!("app element: {:?}", element);
+ // find a button
+ // let sel = Selector::Role { role: "button".to_string(), name: None };
+ // let but = engine.find_element(&sel, Some(&element))?;
+ // println!("button ele: {:?}", but);
+ // // actions on that button
+ // but.focus()?;
+ // but.click()?;
+ // but.double_click()?;
+ // but.right_click()?;
+
+ std::thread::sleep(std::time::Duration::from_millis(200));
+
+ // find a input
+ // let input = Selector::Role { role: "edit".to_string(), name: None };
+ // let inp = engine.find_element(&input, Some(&element))?;
+ // println!("input element: {:?}", inp);
+ // // some input
+ // inp.focus()?;
+ // std::thread::sleep(std::time::Duration::from_millis(200));
+ // inp.type_text("check")?;
+ // inp.press_key("{enter}")?;
+ // // inp.press_key("check{enter}")?; // this is also valid
+
+ // inp.set_value("")?;
+
+ // get some texts from application root
+ // (heavy computational task keep depth low)
+ let text = element.text(10)?;
+ println!("element text: {:?}", text);
+
+
+ // check if enabled
+ // let is_enabled = element.is_enabled()?;
+ // println!("Is enabled: {:?}", is_enabled);
+
+ // check if visible
+ // let is_visible = inp.is_visible()?;
+ // println!("Is visible: {:?}", is_visible);
+
+ // check if focused
+ // let is_focused = inp.is_focused()?;
+ // println!("Is focused: {:?}", is_focused);
+
+ // an action
+ // element.perform_action("focus")?;
+
+ // // find a scrollbar in application ele
+ // let sclb = Selector::Role { role: "scrollbar".to_string(), name: None };
+ // let sclb_ele = engine.find_element(&sclb, Some(&element))?;
+ // println!("scroll bar ele: {:?}", sclb_ele);
+ // // locate
+ // let _ = element.locator(sclb)?;
+ // // scroll the scrollbar
+ // sclb_ele.scroll("down", 10.0)?;
+
+ Ok(())
+}
diff --git a/screenpipe-core/src/operator/platforms/mod.rs b/screenpipe-core/src/operator/platforms/mod.rs
index f0fd23322..48aa4ded5 100644
--- a/screenpipe-core/src/operator/platforms/mod.rs
+++ b/screenpipe-core/src/operator/platforms/mod.rs
@@ -6,7 +6,8 @@ pub trait AccessibilityEngine: Send + Sync {
fn get_root_element(&self) -> UIElement;
#[cfg(target_os = "windows")]
- fn get_element_by_id(&self, _id: &str) -> Result<UIElement, AutomationError>;
+ fn get_element_by_id(&self, id: i32) -> Result<UIElement, AutomationError>;
+
/// Get the currently focused element
fn get_focused_element(&self) -> Result<UIElement, AutomationError>;
diff --git a/screenpipe-core/src/operator/platforms/windows.rs b/screenpipe-core/src/operator/platforms/windows.rs
index f9898adc8..7879d5f6b 100644
--- a/screenpipe-core/src/operator/platforms/windows.rs
+++ b/screenpipe-core/src/operator/platforms/windows.rs
@@ -2,82 +2,344 @@ use crate::operator::element::UIElementImpl;
use crate::operator::platforms::AccessibilityEngine;
use crate::operator::ClickResult;
use crate::operator::{AutomationError, Locator, Selector, UIElement, UIElementAttributes};
+use std::sync::Arc;
use std::fmt::Debug;
-
-pub struct WindowsEngine;
+use std::hash::{Hash, Hasher};
+use uiautomation::UIAutomation;
+use uiautomation::controls::ControlType;
+use uiautomation::inputs::Mouse;
+use uiautomation::inputs::Keyboard;
+use uiautomation::variants::Variant;
+use uiautomation::patterns;
+use tracing::debug;
+use std::collections::{
+ HashMap,
+ hash_map::DefaultHasher
+};
+use uiautomation::types::{
+ TreeScope,
+ UIProperty,
+ ScrollAmount
+};
+
+// thread-safety
+#[derive(Clone)]
+pub struct ThreadSafeWinUIAutomation(Arc<UIAutomation>);
+
+// send and sync for wrapper
+unsafe impl Send for ThreadSafeWinUIAutomation {}
+unsafe impl Sync for ThreadSafeWinUIAutomation {}
+
+#[allow(unused)]
+// there is no need of `use_background_apps` or `activate_app`
+// windows IUIAutomation will get current running app &
+// background running app spontaneously, keeping it anyway!!
+pub struct WindowsEngine {
+ automation: ThreadSafeWinUIAutomation,
+ use_background_apps: bool,
+ activate_app: bool,
+}
impl WindowsEngine {
pub fn new(use_background_apps: bool, activate_app: bool) -> Result<Self, AutomationError> {
- Err(AutomationError::UnsupportedPlatform(
- "Windows implementation is not yet available".to_string(),
- ))
+ let automation = UIAutomation::new().map_err(|e| AutomationError::PlatformError(e.to_string()))?;
+ let arc_automation = ThreadSafeWinUIAutomation(Arc::new(automation));
+ Ok(Self {
+ automation: arc_automation,
+ use_background_apps,
+ activate_app,
+ })
}
}
impl AccessibilityEngine for WindowsEngine {
fn get_root_element(&self) -> UIElement {
- panic!("Windows implementation is not yet available")
+ let root = self.automation.0.get_root_element().unwrap();
+ let arc_root = ThreadSafeWinUIElement(Arc::new(root));
+ UIElement::new(Box::new(WindowsUIElement { element: arc_root }))
}
- fn get_element_by_id(&self, _id: &str) -> Result<UIElement, AutomationError> {
- Err(AutomationError::UnsupportedPlatform(
- "Windows implementation is not yet available".to_string(),
- ))
+ fn get_element_by_id(&self, id: i32) -> Result<UIElement, AutomationError> {
+ let root_element = self.automation.0.get_root_element().unwrap();
+ let condition = self.automation.0.create_property_condition(
+ UIProperty::ProcessId,
+ Variant::from(id),
+ None
+ ).unwrap();
+ let ele = root_element.find_first(TreeScope::Subtree, &condition)
+ .map_err(|e| AutomationError::ElementNotFound(e.to_string()))?;
+ let arc_ele = ThreadSafeWinUIElement(Arc::new(ele));
+
+ Ok(UIElement::new(Box::new(WindowsUIElement { element: arc_ele })))
}
fn get_focused_element(&self) -> Result<UIElement, AutomationError> {
- Err(AutomationError::UnsupportedPlatform(
- "Windows implementation is not yet available".to_string(),
- ))
+ let element = self.automation.0.get_focused_element()
+ .map_err(|e| AutomationError::ElementNotFound(e.to_string()))?;
+ let arc_element = ThreadSafeWinUIElement(Arc::new(element));
+
+ Ok(UIElement::new(Box::new(WindowsUIElement { element: arc_element })))
}
fn get_applications(&self) -> Result<Vec<UIElement>, AutomationError> {
- Err(AutomationError::UnsupportedPlatform(
- "Windows implementation is not yet available".to_string(),
- ))
- }
-
- fn get_application_by_name(&self, _name: &str) -> Result<UIElement, AutomationError> {
- Err(AutomationError::UnsupportedPlatform(
- "Windows implementation is not yet available".to_string(),
- ))
+ let root = self.automation.0.get_root_element().unwrap();
+ let condition = self.automation.0.create_property_condition(
+ UIProperty::ControlType,
+ Variant::from(ControlType::Window as i32),
+ None
+ ).unwrap();
+ let elements = root.find_all(TreeScope::Subtree, &condition)
+ .map_err(|e| AutomationError::ElementNotFound(e.to_string()))?;
+ let arc_elements: Vec<UIElement> = elements.into_iter()
+ .map(|ele| {
+ let arc_ele = ThreadSafeWinUIElement(Arc::new(ele));
+ UIElement::new(Box::new(WindowsUIElement { element: arc_ele }))
+ }).collect();
+
+ Ok(arc_elements)
+ }
+
+ fn get_application_by_name(&self, name: &str) -> Result<UIElement, AutomationError> {
+ debug!("searching application from name: {}", name);
+
+ // first find element by matcher
+ let root_ele = self.automation.0.get_root_element().unwrap();
+ let automation = WindowsEngine::new(false, false)
+ .map_err(|e| AutomationError::PlatformError(e.to_string()))?;
+ let matcher = automation.automation.0.create_matcher()
+ .control_type(ControlType::Window)
+ .contains_name(name)
+ .from_ref(&root_ele)
+ .depth(7)
+ .timeout(5000);
+ let ele_res = matcher.find_first()
+ .map_err(|e| AutomationError::ElementNotFound(e.to_string()));
+
+ // fallback to find by pid
+ let ele = match ele_res {
+ Ok(ele) => ele,
+ Err(_) => {
+ let pid = match get_pid_by_name(name) {
+ Some(pid) => pid,
+ None => {
+ return Err(AutomationError::PlatformError(
+ format!("no running application found from name: {:?}", name)
+ ));
+ }
+ };
+ let condition = automation.automation.0.create_property_condition(
+ UIProperty::ProcessId,
+ Variant::from(pid as i32),
+ None
+ ).unwrap();
+ root_ele.find_first(TreeScope::Subtree, &condition)
+ .map_err(|e| AutomationError::ElementNotFound(e.to_string()))?
+ }
+ };
+ let arc_ele = ThreadSafeWinUIElement(Arc::new(ele));
+ return Ok(UIElement::new(Box::new(WindowsUIElement { element: arc_ele })));
}
fn find_elements(
&self,
- _selector: &Selector,
- _root: Option<&UIElement>,
+ selector: &Selector,
+ root: Option<&UIElement>
) -> Result<Vec<UIElement>, AutomationError> {
- Err(AutomationError::UnsupportedPlatform(
- "Windows implementation is not yet available".to_string(),
- ))
- }
- fn find_element(
- &self,
- selector: &Selector,
- root: Option<&UIElement>,
- ) -> Result<UIElement, AutomationError> {
- Err(AutomationError::UnsupportedPlatform(
- "Windows implementation is not yet available".to_string(),
- ))
+ let root_ele = if let Some(el) = root {
+ if let Some(ele) = el.as_any().downcast_ref::<WindowsUIElement>() {
+ &ele.element.0
+ } else {
+ &Arc::new(self.automation.0.get_root_element().unwrap())
+ }
+ } else {
+ &Arc::new(self.automation.0.get_root_element().unwrap())
+ };
+
+ // make condition according to selector
+ let condition = match selector {
+ Selector::Role { role, name: _ } => {
+ let roles = map_generic_role_to_win_roles(role);
+ let role_condition = self.automation.0.create_property_condition(
+ UIProperty::ControlType,
+ Variant::from(roles as i32),
+ None
+ ).unwrap();
+ debug!("role conditions: {:#?} for finding element: {:#?}",
+ role_condition, root_ele);
+ role_condition
+ },
+ Selector::Id(id) => self.automation.0.create_property_condition(
+ UIProperty::AutomationId,
+ Variant::from(id.as_str()),
+ None
+ ).unwrap(),
+ Selector::Name(_name) => self.automation.0.create_property_condition(
+ UIProperty::ControlType,
+ Variant::from(ControlType::Window as i32),
+ None
+ ).unwrap(),
+ Selector::Text(_text) => self.automation.0.create_property_condition(
+ UIProperty::ControlType,
+ Variant::from(ControlType::Text as i32),
+ None
+ ).unwrap(),
+ Selector::Path(_) => {
+ return Err(AutomationError::UnsupportedOperation("`Path` selector not supported".to_string()));
+ },
+ Selector::Attributes(_attributes) => {
+ return Err(AutomationError::UnsupportedOperation("`Attributes` selector not supported".to_string()));
+ },
+ Selector::Filter(_filter) => {
+ return Err(AutomationError::UnsupportedOperation("`Filter` selector not supported".to_string()));
+ },
+ Selector::Chain(_selectors) => {
+ return Err(AutomationError::UnsupportedOperation("`selectors` selector not supported".to_string()));
+ },
+ };
+
+ let elements = root_ele.find_all(TreeScope::Subtree, &condition)
+ .map_err(|e| AutomationError::ElementNotFound(e.to_string()))?;
+ let arc_elements: Vec<UIElement> = elements.into_iter()
+ .map(|ele| {
+ let arc_ele = ThreadSafeWinUIElement(Arc::new(ele));
+ UIElement::new(Box::new(WindowsUIElement { element: arc_ele }))
+ }).collect();
+
+ Ok(arc_elements)
+ }
+
+ fn find_element(&self, selector: &Selector, root: Option<&UIElement>) -> Result<UIElement, AutomationError> {
+ let root_ele = if let Some(el) = root {
+ if let Some(ele) = el.as_any().downcast_ref::<WindowsUIElement>() {
+ &ele.element.0
+ } else {
+ &Arc::new(self.automation.0.get_root_element().unwrap())
+ }
+ } else {
+ &Arc::new(self.automation.0.get_root_element().unwrap())
+ };
+
+ match selector {
+ Selector::Role { role, name: _ } => {
+ let roles = map_generic_role_to_win_roles(role);
+ let role_condition = self.automation.0.create_property_condition(
+ UIProperty::ControlType,
+ Variant::from(roles as i32),
+ None
+ ).unwrap();
+ debug!("role conditions: {:#?} for finding element: {:#?}",
+ role_condition, root_ele);
+ // find a element that satisfies the roles
+ let element = root_ele.find_first(TreeScope::Subtree, &role_condition)
+ .map_err(|e| AutomationError::ElementNotFound(
+ format!("element not found, Err: {}", e.to_string()
+ )))?;
+ debug!("found element: {:#?}", element);
+
+ let arc_ele = ThreadSafeWinUIElement(Arc::new(element));
+
+ Ok(UIElement::new(Box::new(WindowsUIElement { element: arc_ele })))
+ },
+ Selector::Id(id) => {
+ let condition = self.automation.0.create_property_condition(
+ UIProperty::AutomationId,
+ Variant::from(id.as_str()),
+ None
+ ).unwrap();
+
+ let ele = root_ele.find_first(TreeScope::Subtree, &condition)
+ .map_err(|e| AutomationError::ElementNotFound(
+ format!("ID: '{}', Err: {}", id, e.to_string())))?;
+ let arc_ele = ThreadSafeWinUIElement(Arc::new(ele));
+
+ Ok(UIElement::new(Box::new(WindowsUIElement { element: arc_ele })))
+ },
+ Selector::Name(name) => {
+ // `name` and `window` can be same
+ let condition = self.automation.0.create_property_condition(
+ UIProperty::ControlType,
+ Variant::from(ControlType::Window as i32),
+ None
+ ).unwrap();
+
+ let ele = root_ele.find_first(TreeScope::Subtree, &condition)
+ .map_err(|e| AutomationError::ElementNotFound(
+ format!("Name: '{}', Err: {}", name, e.to_string())))?;
+ let arc_ele = ThreadSafeWinUIElement(Arc::new(ele));
+
+ Ok(UIElement::new(Box::new(WindowsUIElement { element: arc_ele })))
+ },
+ Selector::Text(text) => {
+ let condition = self.automation.0.create_property_condition(
+ UIProperty::ControlType,
+ Variant::from(ControlType::Text as i32),
+ None
+ ).unwrap();
+
+ let ele = root_ele.find_first(TreeScope::Subtree, &condition)
+ .map_err(|e| AutomationError::ElementNotFound(
+ format!("Text: '{}', Err: {}", text, e.to_string())))?;
+ let arc_ele = ThreadSafeWinUIElement(Arc::new(ele));
+
+ Ok(UIElement::new(Box::new(WindowsUIElement { element: arc_ele })))
+ }
+ Selector::Path(_) => {
+ return Err(AutomationError::UnsupportedOperation("`Path` selector not supported".to_string()));
+ },
+ Selector::Attributes(_attributes) => {
+ return Err(AutomationError::UnsupportedOperation("`Attributes` selector not supported".to_string()));
+ },
+ Selector::Filter(_filter) => {
+ return Err(AutomationError::UnsupportedOperation("`Filter` selector not supported".to_string()));
+ },
+ Selector::Chain(_selectors) => {
+ return Err(AutomationError::UnsupportedOperation("`selectors` selector not supported".to_string()));
+ },
+ }
}
- fn open_application(&self, _app_name: &str) -> Result<UIElement, AutomationError> {
- Err(AutomationError::UnsupportedPlatform(
- "Windows implementation is not yet available".to_string(),
- ))
+ fn open_application(&self, app_name: &str) -> Result<UIElement, AutomationError> {
+ let status = std::process::Command::new("powershell")
+ .args(["-NoProfile", "-WindowStyle", "hidden", "-Command","start", app_name])
+ .status()
+ .map_err(|e| AutomationError::PlatformError(e.to_string()))?;
+ if !status.success() {
+ return Err(AutomationError::PlatformError("Failed to open application".to_string()));
+ }
+
+ std::thread::sleep(std::time::Duration::from_millis(200));
+
+ self.get_application_by_name(app_name)
}
- fn open_url(&self, _url: &str, _browser: Option<&str>) -> Result<UIElement, AutomationError> {
- Err(AutomationError::UnsupportedPlatform(
- "Windows implementation is not yet available".to_string(),
- ))
+ fn open_url(&self, url: &str, browser: Option<&str>) -> Result<UIElement, AutomationError> {
+ let browser = browser.unwrap_or(""); // when empty it'll open url in system's default browser
+ let status = std::process::Command::new("powershell")
+ .args(["-NoProfile", "-WindowStyle", "hidden", "-Command", "start", browser, url])
+ .status()
+ .map_err(|e| AutomationError::PlatformError(e.to_string()))?;
+ if !status.success() {
+ return Err(AutomationError::PlatformError("Failed to open URL".to_string()));
+ }
+
+ std::thread::sleep(std::time::Duration::from_millis(200));
+
+ self.get_application_by_name(browser)
}
}
-// Placeholder WindowsUIElement that implements UIElementImpl
-pub struct WindowsUIElement;
+// thread-safety
+#[derive(Clone)]
+pub struct ThreadSafeWinUIElement(Arc<uiautomation::UIElement>);
+
+// send and sync for wrapper
+unsafe impl Send for ThreadSafeWinUIElement {}
+unsafe impl Sync for ThreadSafeWinUIElement {}
+
+pub struct WindowsUIElement {
+ element: ThreadSafeWinUIElement,
+}
impl Debug for WindowsUIElement {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
@@ -87,140 +349,380 @@ impl Debug for WindowsUIElement {
impl UIElementImpl for WindowsUIElement {
fn object_id(&self) -> usize {
- 0
+ // use hashed `AutomationId` as object_id
+ let stable_id = format!("{:?}",
+ self.element.0.get_automation_id().unwrap_or_default());
+ let mut hasher = DefaultHasher::new();
+ stable_id.hash(&mut hasher);
+ let id = hasher.finish() as usize;
+ debug!("Stable ID: {:?}", stable_id);
+ debug!("Hash: {:?}", id);
+ id
}
fn id(&self) -> Option<String> {
- None
+ self.element.0.get_automation_id().ok()
}
fn role(&self) -> String {
- "".to_string()
+ self.element.0.get_control_type().unwrap().to_string()
}
fn attributes(&self) -> UIElementAttributes {
+ let mut properties = HashMap::new();
+ // there are alot of properties, including neccessary ones
+ // ref: https://docs.rs/uiautomation/0.16.1/uiautomation/types/enum.UIProperty.html
+ let property_list = vec![
+ UIProperty::Name,
+ UIProperty::HelpText,
+ UIProperty::LabeledBy,
+ UIProperty::ValueValue,
+ UIProperty::ControlType,
+ UIProperty::AutomationId,
+ UIProperty::FullDescription,
+ ];
+ for property in property_list {
+ if let Ok(value) = self.element.0.get_property_value(property) {
+ properties.insert(
+ format!("{:?}", property),
+ Some(serde_json::to_value(value.to_string()).unwrap_or_default()),
+ );
+ } else {
+ properties.insert(format!("{:?}", property), None);
+ }
+ }
UIElementAttributes {
- role: "".to_string(),
- label: None,
- value: None,
- description: None,
- properties: std::collections::HashMap::new(),
+ role: self.role(),
+ label: self.element.0.get_labeled_by().ok()
+ .map(|e| e.get_name().unwrap_or_default()),
+ value: self.element.0.get_property_value(UIProperty::ValueValue)
+ .ok().and_then(|v| v.get_string().ok()),
+ description: self.element.0.get_help_text().ok(),
+ properties,
}
}
fn children(&self) -> Result<Vec<UIElement>, AutomationError> {
- Err(AutomationError::UnsupportedPlatform(
- "Windows implementation is not yet available".to_string(),
- ))
+ let children = self.element.0.get_cached_children()
+ .map_err(|e| AutomationError::ElementNotFound(e.to_string()))?;
+ Ok(children.into_iter()
+ .map(|ele| UIElement::new(
+ Box::new(WindowsUIElement {
+ element: ThreadSafeWinUIElement(Arc::new(ele))
+ }))).collect()
+ )
}
fn parent(&self) -> Result<Option<UIElement>, AutomationError> {
- Err(AutomationError::UnsupportedPlatform(
- "Windows implementation is not yet available".to_string(),
- ))
+ let parent = self.element.0.get_cached_parent();
+ match parent {
+ Ok(par) => {
+ let par_ele = UIElement::new(Box::new(WindowsUIElement {
+ element: ThreadSafeWinUIElement(Arc::new(par)),
+ }));
+ Ok(Some(par_ele))
+ }
+ Err(e) => Err(AutomationError::ElementNotFound(e.to_string())),
+ }
}
fn bounds(&self) -> Result<(f64, f64, f64, f64), AutomationError> {
- Err(AutomationError::UnsupportedPlatform(
- "Windows implementation is not yet available".to_string(),
+ let rect = self.element.0.get_bounding_rectangle().map_err(|e| AutomationError::ElementNotFound(e.to_string()))?;
+ Ok((
+ rect.get_left() as f64,
+ rect.get_top() as f64,
+ rect.get_width() as f64,
+ rect.get_height() as f64,
))
}
fn click(&self) -> Result<ClickResult, AutomationError> {
- Err(AutomationError::UnsupportedPlatform(
- "Windows implementation is not yet available".to_string(),
- ))
+ self.element.0.try_focus();
+ let point = self.element.0.get_clickable_point()
+ .map_err(|e| AutomationError::PlatformError(e.to_string()))?
+ .ok_or_else(|| AutomationError::PlatformError("No clickable point found".to_string()))?;
+ let mouse = Mouse::default();
+ mouse.click(point).map_err(|e| AutomationError::PlatformError(e.to_string()))?;
+
+ Ok(ClickResult {
+ method: "Single Click".to_string(),
+ coordinates: Some((point.get_x() as f64, point.get_y() as f64)),
+ details: "Clicked by Mouse".to_string(),
+ })
}
fn double_click(&self) -> Result<ClickResult, AutomationError> {
- Err(AutomationError::UnsupportedPlatform(
- "Windows implementation is not yet available".to_string(),
- ))
+ self.element.0.try_focus();
+ let point = self.element.0.get_clickable_point()
+ .map_err(|e| AutomationError::PlatformError(e.to_string()))?
+ .ok_or_else(|| AutomationError::PlatformError("No clickable point found".to_string()))?;
+ let mouse = Mouse::default();
+ mouse.double_click(point).map_err(|e| AutomationError::PlatformError(e.to_string()))?;
+ Ok(ClickResult {
+ method: "Double Click".to_string(),
+ coordinates: Some((point.get_x() as f64, point.get_y() as f64)),
+ details: "Clicked by Mouse".to_string(),
+ })
}
fn right_click(&self) -> Result<(), AutomationError> {
- Err(AutomationError::UnsupportedPlatform(
- "Windows implementation is not yet available".to_string(),
- ))
+ self.element.0.try_focus();
+ let point = self.element.0.get_clickable_point()
+ .map_err(|e| AutomationError::PlatformError(e.to_string()))?
+ .ok_or_else(|| AutomationError::PlatformError("No clickable point found".to_string()))?;
+ let mouse = Mouse::default();
+ mouse.right_click(point).map_err(|e| AutomationError::PlatformError(e.to_string()))?;
+ Ok(())
}
fn hover(&self) -> Result<(), AutomationError> {
- Err(AutomationError::UnsupportedPlatform(
- "Windows implementation is not yet available".to_string(),
- ))
+ return Err(AutomationError::UnsupportedOperation("`hover` doesn't not support".to_string()));
}
fn focus(&self) -> Result<(), AutomationError> {
- Err(AutomationError::UnsupportedPlatform(
- "Windows implementation is not yet available".to_string(),
- ))
- }
-
- fn type_text(&self, _text: &str) -> Result<(), AutomationError> {
- Err(AutomationError::UnsupportedPlatform(
- "Windows implementation is not yet available".to_string(),
- ))
+ self.element.0.set_focus()
+ .map_err(|e| AutomationError::PlatformError(e.to_string()))
+ }
+
+ fn type_text(&self, text: &str) -> Result<(), AutomationError> {
+ let control_type = self.element.0.get_control_type()
+ .map_err(|e| AutomationError::PlatformError(e.to_string()))?;
+ // check if element accepts input
+ if control_type == ControlType::Edit {
+ let keyboard = Keyboard::default();
+ keyboard.send_text(text)
+ .map_err(|e| AutomationError::PlatformError(e.to_string()))?;
+ Ok(())
+ } else {
+ Err(AutomationError::PlatformError("Element is not editable".to_string()))
+ }
}
- fn press_key(&self, _key: &str) -> Result<(), AutomationError> {
- Err(AutomationError::UnsupportedPlatform(
- "Windows implementation is not yet available".to_string(),
- ))
+ fn press_key(&self, key: &str) -> Result<(), AutomationError> {
+ let control_type = self.element.0.get_control_type()
+ .map_err(|e| AutomationError::PlatformError(e.to_string()))?;
+ // check if element accepts input, similar :D
+ if control_type == ControlType::Edit {
+ let keyboard = Keyboard::default();
+ keyboard.send_keys(key)
+ .map_err(|e| AutomationError::PlatformError(e.to_string()))?;
+ Ok(())
+ } else {
+ Err(AutomationError::PlatformError("Element is not editable".to_string()))
+ }
}
fn get_text(&self, max_depth: usize) -> Result<String, AutomationError> {
- Err(AutomationError::UnsupportedPlatform(
- "Windows implementation is not yet available".to_string(),
- ))
+ let automation = WindowsEngine::new(false, false)
+ .map_err(|e| AutomationError::PlatformError(e.to_string()))?;
+ let mut all_texts = String::new();
+ // run the text matcher
+ let txt_matcher = automation.automation.0.create_matcher()
+ .control_type(ControlType::Text)
+ .from_ref(&self.element.0)
+ .depth(max_depth as u32)
+ .timeout(1000);
+
+ if let Ok(text_eles) = txt_matcher.find_all() {
+ for text_ele in text_eles {
+ // get name
+ if let Ok(name) = text_ele.get_property_value(UIProperty::Name) {
+ if let Ok(name_text) = name.get_string() {
+ all_texts.push_str(&name_text);
+ }
+ }
+ // get value
+ if let Ok(value) = text_ele.get_property_value(UIProperty::ValueValue) {
+ if let Ok(val_text) = value.get_string(){
+ all_texts.push_str(&val_text);
+ }
+ }
+ }
+ }
+ Ok(all_texts)
}
- fn set_value(&self, _value: &str) -> Result<(), AutomationError> {
- Err(AutomationError::UnsupportedPlatform(
- "Windows implementation is not yet available".to_string(),
- ))
+ fn set_value(&self, value: &str) -> Result<(), AutomationError> {
+ let value_par = self.element.0.get_pattern::<patterns::UIValuePattern>()
+ .map_err(|e| AutomationError::PlatformError(e.to_string()));
+ debug!("setting value: {:#?} to ui element {:#?}", &value, &self.element.0);
+
+ if let Ok(v) = value_par {
+ v.set_value(value).map_err(|e| AutomationError::PlatformError(e.to_string()))
+ } else {
+ Err(AutomationError::PlatformError("`UIValuePattern` is not found".to_string()))
+ }
}
fn is_enabled(&self) -> Result<bool, AutomationError> {
- Err(AutomationError::UnsupportedPlatform(
- "Windows implementation is not yet available".to_string(),
- ))
+ self.element.0.is_enabled().map_err(|e| AutomationError::ElementNotFound(e.to_string()))
}
fn is_visible(&self) -> Result<bool, AutomationError> {
- Err(AutomationError::UnsupportedPlatform(
- "Windows implementation is not yet available".to_string(),
- ))
+ // offscreen means invisible, right?
+ self.element.0.is_offscreen().map_err(|e| AutomationError::ElementNotFound(e.to_string()))
}
fn is_focused(&self) -> Result<bool, AutomationError> {
- Err(AutomationError::UnsupportedPlatform(
- "Windows implementation is not yet available".to_string(),
- ))
+ // start a instance of `uiautomation` just to check the
+ // current focused element is same as focused element or not
+ let automation = WindowsEngine::new(false, false)
+ .map_err(|e| AutomationError::PlatformError(e.to_string()))?;
+ let focused_element = automation.automation.0.get_focused_element()
+ .map_err(|e| AutomationError::PlatformError(e.to_string()))?;
+ if Arc::ptr_eq(&self.element.0, &Arc::new(focused_element)) {
+ Ok(true)
+ } else {
+ Ok(false)
+ }
}
- fn perform_action(&self, _action: &str) -> Result<(), AutomationError> {
- Err(AutomationError::UnsupportedPlatform(
- "Windows implementation is not yet available".to_string(),
- ))
+ fn perform_action(&self, action: &str) -> Result<(), AutomationError> {
+ // actions those don't take args
+ match action {
+ "focus" => self.focus(),
+ "invoke" => {
+ let invoke_pat = self.element.0.get_pattern::<patterns::UIInvokePattern>()
+ .map_err(|e| AutomationError::PlatformError(e.to_string()))?;
+ invoke_pat.invoke().map_err(|e| AutomationError::PlatformError(e.to_string()))
+ },
+ "click" => self.click().map(|_| ()),
+ "double_click" => self.double_click().map(|_| ()),
+ "right_click" => self.right_click().map(|_| ()),
+ "toggle" => {
+ let toggle_pattern = self.element.0.get_pattern::<patterns::UITogglePattern>()
+ .map_err(|e| AutomationError::PlatformError(e.to_string()))?;
+ toggle_pattern.toggle().map_err(|e| AutomationError::PlatformError(e.to_string()))
+ },
+ "expand_collapse" => {
+ let expand_collapse_pattern = self.element.0.get_pattern::<patterns::UIExpandCollapsePattern>()
+ .map_err(|e| AutomationError::PlatformError(e.to_string()))?;
+ expand_collapse_pattern.expand().map_err(|e| AutomationError::PlatformError(e.to_string()))
+ },
+ _ => Err(AutomationError::UnsupportedOperation(format!("action '{}' not supported", action))),
+ }
}
fn as_any(&self) -> &dyn std::any::Any {
self
}
- fn create_locator(&self, _selector: Selector) -> Result<Locator, AutomationError> {
- Err(AutomationError::UnsupportedPlatform(
- "Windows implementation is not yet available".to_string(),
- ))
+ fn create_locator(&self, selector: Selector) -> Result<Locator, AutomationError> {
+ let automation = WindowsEngine::new(false, false)
+ .map_err(|e| AutomationError::PlatformError(e.to_string()))?;
+
+ let attrs = self.attributes();
+ debug!("creating locator for element: control_type={:#?}, label={:#?}",
+ attrs.role, attrs.label);
+
+ let self_element = UIElement::new(Box::new(WindowsUIElement {
+ element: self.element.clone(),
+ }));
+
+ Ok(Locator::new(std::sync::Arc::new(automation), selector).within(self_element))
}
fn clone_box(&self) -> Box<dyn UIElementImpl> {
- Box::new(WindowsUIElement)
+ Box::new(WindowsUIElement { element: self.element.clone() })
+ }
+
+ fn scroll(&self, direction: &str, amount: f64) -> Result<(), AutomationError> {
+ let scroll_pattern = self.element.0.get_pattern::<patterns::UIScrollPattern>()
+ .map_err(|e| AutomationError::PlatformError(e.to_string()))?;
+
+ let scroll_amount = if amount > 0.0 {
+ ScrollAmount::SmallIncrement
+ } else if amount < 0.0 {
+ ScrollAmount::SmallDecrement
+ } else {
+ ScrollAmount::NoAmount
+ };
+
+ let times = amount.abs() as usize;
+ for _ in 0..times {
+ match direction {
+ "up" => scroll_pattern.scroll(ScrollAmount::NoAmount, scroll_amount)
+ .map_err(|e| AutomationError::PlatformError(e.to_string())),
+ "down" => scroll_pattern.scroll(ScrollAmount::NoAmount, scroll_amount)
+ .map_err(|e| AutomationError::PlatformError(e.to_string())),
+ "left" => scroll_pattern.scroll(scroll_amount, ScrollAmount::NoAmount)
+ .map_err(|e| AutomationError::PlatformError(e.to_string())),
+ "right" => scroll_pattern.scroll(scroll_amount, ScrollAmount::NoAmount)
+ .map_err(|e| AutomationError::PlatformError(e.to_string())),
+ _ => Err(AutomationError::UnsupportedOperation("Invalid scroll direction".to_string())),
+ }?
+ }
+ Ok(())
}
+}
- fn scroll(&self, _direction: &str, _amount: f64) -> Result<(), AutomationError> {
- Err(AutomationError::UnsupportedPlatform(
- "Windows implementation is not yet available".to_string(),
- ))
+// make easier to pass roles
+fn map_generic_role_to_win_roles(role: &str) -> ControlType {
+ match role.to_lowercase().as_str() {
+ "window" => ControlType::Window,
+ "button" => ControlType::Button,
+ "checkbox" => ControlType::CheckBox,
+ "menu" => ControlType::Menu,
+ "menuitem" => ControlType::MenuItem,
+ "dialog" => ControlType::Window,
+ "text" => ControlType::Text,
+ "tree" => ControlType::Tree,
+ "treeitem" => ControlType::TreeItem,
+ "data" | "dataitem" => ControlType::DataGrid,
+ "datagrid" => ControlType::DataGrid,
+ "url" | "urlfield" => ControlType::Edit,
+ "list" => ControlType::List,
+ "image" => ControlType::Image,
+ "title" => ControlType::TitleBar,
+ "listitem" => ControlType::ListItem,
+ "combobox" => ControlType::ComboBox,
+ "tab" => ControlType::Tab,
+ "tabitem" => ControlType::TabItem,
+ "toolbar" => ControlType::ToolBar,
+ "appbar" => ControlType::AppBar,
+ "calendar" => ControlType::Calendar,
+ "edit" => ControlType::Edit,
+ "hyperlink" => ControlType::Hyperlink,
+ "progressbar" => ControlType::ProgressBar,
+ "radiobutton" => ControlType::RadioButton,
+ "scrollbar" => ControlType::ScrollBar,
+ "slider" => ControlType::Slider,
+ "spinner" => ControlType::Spinner,
+ "statusbar" => ControlType::StatusBar,
+ "tooltip" => ControlType::ToolTip,
+ "custom" => ControlType::Custom,
+ "group" => ControlType::Group,
+ "thumb" => ControlType::Thumb,
+ "document" => ControlType::Document,
+ "splitbutton" => ControlType::SplitButton,
+ "pane" => ControlType::Pane,
+ "header" => ControlType::Header,
+ "headeritem" => ControlType::HeaderItem,
+ "table" => ControlType::Table,
+ "titlebar" => ControlType::TitleBar,
+ "separator" => ControlType::Separator,
+ "semanticzoom" => ControlType::SemanticZoom,
+ _ => ControlType::Custom, // keep as it is for unknown roles
+ }
+}
+
+fn get_pid_by_name(name: &str) -> Option<i32> {
+ // window title shouldn't be empty
+ let command = format!(
+ "Get-Process | Where-Object {{ $_.MainWindowTitle -ne '' -and $_.Name -like '*{}*' }} | ForEach-Object {{ $_.Id }}",
+ name
+ );
+
+ let output = std::process::Command::new("powershell")
+ .args(["-NoProfile", "-WindowStyle", "hidden", "-Command", &command])
+ .output()
+ .expect("Failed to execute PowerShell script");
+
+ if output.status.success() {
+ // return only parent pid
+ let pid_str = String::from_utf8_lossy(&output.stdout);
+ pid_str.lines().next()?.trim().parse().ok()
+ } else {
+ None
}
}