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::{path::{Path, PathBuf}, sync::Arc};

use crate::{Backend, Element, UIResult};

#[derive(Debug, Clone)]
pub struct Application {
    pub(crate) backend: Arc<dyn Backend>,
    pub(crate) name: String,

    #[allow(unused)]
    pub(crate) id: ApplicationId,

    pub(crate) owner: ApplicationOwner,
}

impl Application {
    #[must_use]
    pub fn owner(&self) -> &ApplicationOwner {
        &self.owner
    }

    #[must_use]
    pub fn name(&self) -> &str {
        &self.name
    }

    pub fn find<P: FnMut(&Element) -> bool>(&self, mut pred: P) -> Option<Element> {
        self.windows()
            .into_iter()
            .find_map(|element| element.find(&mut pred))
    }

    #[must_use]
    pub fn windows(&self) -> Vec<Element> {
        self.try_windows().unwrap()
    }

    pub fn try_windows(&self) -> UIResult<Vec<Element>> {
        self.backend.windows(self)
    }
}

#[derive(Debug, Clone, Copy)]
pub(crate) struct ApplicationId(pub usize);

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ApplicationOwner {
    System,
    Process {
        pid: u32,
        path: PathBuf,
    },
}

impl ApplicationOwner {
    pub fn pid(&self) -> Option<u32> {
        match self {
            Self::System => None,
            Self::Process { pid, .. } => Some(*pid),
        }
    }

    pub fn path(&self) -> Option<&Path> {
        match self {
            Self::System => None,
            Self::Process { path, .. } => Some(path),
        }
    }
}