ui_automation/
application.rs

1// Copyright (C) 2024 Tristan Gerritsen <tristan@thewoosh.org>
2// All Rights Reserved.
3
4use std::{path::{Path, PathBuf}, sync::Arc};
5
6use crate::{Backend, Element, UIResult};
7
8#[derive(Debug, Clone)]
9pub struct Application {
10    pub(crate) backend: Arc<dyn Backend>,
11    pub(crate) name: String,
12
13    #[allow(unused)]
14    pub(crate) id: ApplicationId,
15
16    pub(crate) owner: ApplicationOwner,
17}
18
19impl Application {
20    #[must_use]
21    pub fn owner(&self) -> &ApplicationOwner {
22        &self.owner
23    }
24
25    #[must_use]
26    pub fn name(&self) -> &str {
27        &self.name
28    }
29
30    pub fn find<P: FnMut(&Element) -> bool>(&self, mut pred: P) -> Option<Element> {
31        self.windows()
32            .into_iter()
33            .find_map(|element| element.find(&mut pred))
34    }
35
36    #[must_use]
37    pub fn windows(&self) -> Vec<Element> {
38        self.try_windows().unwrap()
39    }
40
41    pub fn try_windows(&self) -> UIResult<Vec<Element>> {
42        self.backend.windows(self)
43    }
44}
45
46#[derive(Debug, Clone, Copy)]
47pub(crate) struct ApplicationId(pub usize);
48
49#[derive(Debug, Clone, PartialEq, Eq)]
50pub enum ApplicationOwner {
51    System,
52    Process {
53        pid: u32,
54        path: PathBuf,
55    },
56}
57
58impl ApplicationOwner {
59    pub fn pid(&self) -> Option<u32> {
60        match self {
61            Self::System => None,
62            Self::Process { pid, .. } => Some(*pid),
63        }
64    }
65
66    pub fn path(&self) -> Option<&Path> {
67        match self {
68            Self::System => None,
69            Self::Process { path, .. } => Some(path),
70        }
71    }
72}