ui-automation 0.1.1

Control the User Interface, cross-platform.
Documentation
// Copyright (C) 2024 Tristan Gerritsen <tristan@thewoosh.org>
// All Rights Reserved.

use std::sync::Arc;

use crate::{create_backend, Application, Backend, Position, UIResult};

/// The top-level access to the UI.
///
/// ```
/// # use ui_automation::UIAutomation;
/// let automation = UIAutomation::new();
///
/// for app in automation.applications() {
///     println!("Window \"{}\"", app.name());
/// }
/// ```
#[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())
    }
}