use std::sync::Arc;
use crate::{create_backend, Application, Backend, Position, UIResult};
#[derive(Debug, Clone)]
pub struct UIAutomation {
backend: Arc<dyn Backend>,
}
impl UIAutomation {
pub fn new() -> Self {
Self::try_new().unwrap()
}
#[must_use]
pub fn backend_name(&self) -> &str {
self.backend.name()
}
pub fn mouse_position(&self) -> Position {
self.backend.get_mouse_position().unwrap_or_default()
}
pub fn set_cursor_position(&self, position: impl Into<Position>) {
self.backend.set_mouse_position(position.into()).unwrap();
}
pub fn applications(&self) -> Vec<Application> {
self.try_applications().unwrap()
}
pub fn try_new() -> UIResult<Self> {
Ok(Self {
backend: create_backend()?,
})
}
pub fn try_mouse_position(&self) -> UIResult<Position> {
self.backend.get_mouse_position()
}
pub fn try_set_cursor_position(&self, position: impl Into<Position>) -> UIResult<()> {
self.backend.set_mouse_position(position.into())
}
pub fn try_applications(&self) -> UIResult<Vec<Application>> {
self.backend.applications(self.backend.clone())
}
}