//! Applications a dictation mode can activate for. Each OS lists them the //! way its mode matcher identifies the focused application: macOS by the //! bundle name Finder shows, Windows and Linux by the executable name. use std::collections::HashSet; use std::path::PathBuf; use std::sync::OnceLock; #[cfg(target_os = "macos")] #[path = "application_catalog/macos.rs"] mod platform; #[cfg(target_os = "windows")] #[path = "application_catalog/windows.rs"] mod platform; #[cfg(target_os = "linux")] #[path = "application_catalog/linux.rs"] mod platform; #[derive(Clone, Debug)] pub struct InstalledApplication { pub name: String, pub bundle_id: Option, pub path: PathBuf, } static APPLICATIONS: OnceLock> = OnceLock::new(); pub fn discover() -> Vec { APPLICATIONS .get_or_init(|| { let mut applications = platform::applications(); normalize(&mut applications); applications }) .clone() } pub fn insert(applications: &mut Vec, application: InstalledApplication) { applications.push(application); normalize(applications); } /// The application a user picked in the file dialog: a macOS `.app` /// bundle, a Windows `.exe`, or a Linux `.desktop` entry or executable. pub fn metadata(path: &std::path::Path) -> color_eyre::Result { platform::metadata(path) } /// The catalog name of the application whose desktop entry is `app_id`; /// Wayland compositors report a window's app ID as its desktop file name. #[cfg(target_os = "linux")] pub fn desktop_application_name(app_id: &str) -> Option { platform::desktop_application_name(app_id) } /// The application whose bundle ID (macOS) or executable (Windows, Linux) /// is `id`, as a call's provider names it. pub fn find<'a>( applications: &'a [InstalledApplication], id: &str, ) -> Option<&'a InstalledApplication> { applications .iter() .find(|a| a.bundle_id.as_deref() == Some(id)) .or_else(|| { applications.iter().find(|a| { let key = a.bundle_id.as_deref().unwrap_or(&a.name); crate::microphone_activity::canonical_bundle_id_for(key, None) == Some(id) }) }) } /// One entry per application: macOS bundles by identifier (or path), and /// executables by name, since Windows modes match the executable name. fn normalize(applications: &mut Vec) { let mut identities = HashSet::new(); applications.retain(|application| { identities.insert(application.bundle_id.clone().unwrap_or_else(|| { if cfg!(target_os = "macos") { application.path.to_string_lossy().into_owned() } else { application.name.to_lowercase() } })) }); applications.sort_by(|left, right| { left.name .to_lowercase() .cmp(&right.name.to_lowercase()) .then_with(|| left.path.cmp(&right.path)) }); }