Skip to main content

mitsuami_core/
app_info.rs

1//! Who the app is to the platform: its id, name and icon.
2
3use std::fmt;
4use std::path::PathBuf;
5use std::sync::Arc;
6
7/// The app's id, name and icon, which the platform shows in its task
8/// switcher, dock or taskbar, and uses to match the app's windows to it.
9/// Each platform takes what it has a place for, and a packaged app's own
10/// (a macOS bundle's, an MSIX package's) wins over what's set here.
11#[derive(Clone, Debug, Default, PartialEq)]
12pub struct AppInfo {
13    /// Reverse DNS, as `org.example.Player`: the Wayland app id and X11
14    /// class on Linux, which match the app's `.desktop` file and its icon
15    /// in the theme; the AppUserModelID of an unpackaged Windows app. A
16    /// macOS app's id is its bundle's.
17    pub id: Option<String>,
18    /// The name people know it by: in the app menu's About, Hide and Quit
19    /// on macOS, after each window's title on KDE, as GLib's application
20    /// name on GTK. Windows takes it from the executable or its shortcut.
21    pub name: Option<String>,
22    /// Shown for the app on macOS (unless its bundle has an icon), on each
23    /// window on Windows, and on KDE when the icon theme has none named
24    /// after the id. GTK shows only the theme's icon named after the id.
25    pub icon: Option<AppIcon>,
26}
27
28impl AppInfo {
29    pub fn new() -> AppInfo {
30        AppInfo::default()
31    }
32
33    pub fn id(mut self, id: impl Into<String>) -> AppInfo {
34        self.id = Some(id.into());
35        self
36    }
37
38    pub fn name(mut self, name: impl Into<String>) -> AppInfo {
39        self.name = Some(name.into());
40        self
41    }
42
43    pub fn icon(mut self, icon: AppIcon) -> AppInfo {
44        self.icon = Some(icon);
45        self
46    }
47}
48
49/// The app's icon, decoded by the platform: PNG everywhere, other formats
50/// where the platform reads them (an `.ico` file on Windows, with its
51/// sizes). Give it large (256 or 512 pixels square): platforms scale it
52/// down.
53#[derive(Clone, PartialEq)]
54pub enum AppIcon {
55    File(PathBuf),
56    /// An image file's bytes, as `include_bytes!` gives them.
57    Bytes(Arc<[u8]>),
58}
59
60impl AppIcon {
61    pub fn file(path: impl Into<PathBuf>) -> AppIcon {
62        AppIcon::File(path.into())
63    }
64
65    pub fn bytes(bytes: impl Into<Arc<[u8]>>) -> AppIcon {
66        AppIcon::Bytes(bytes.into())
67    }
68
69    /// The image's bytes: read from its file, or as given.
70    pub fn read(&self) -> Option<Arc<[u8]>> {
71        match self {
72            AppIcon::File(path) => std::fs::read(path).ok().map(Into::into),
73            AppIcon::Bytes(bytes) => Some(bytes.clone()),
74        }
75    }
76}
77
78/// Its path or length, not its bytes.
79impl fmt::Debug for AppIcon {
80    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
81        match self {
82            AppIcon::File(path) => f.debug_tuple("File").field(path).finish(),
83            AppIcon::Bytes(bytes) => write!(f, "Bytes({} bytes)", bytes.len()),
84        }
85    }
86}
87
88/// What a window shows of the app, read back from the platform for tests.
89#[derive(Clone, Debug, Default, PartialEq)]
90pub struct NativeAppInfo {
91    pub id: Option<String>,
92    pub name: Option<String>,
93    pub icon: Option<NativeIcon>,
94}
95
96#[derive(Clone, Debug, PartialEq)]
97pub enum NativeIcon {
98    /// An image this large: in pixels, or in points on AppKit, which keeps
99    /// a copy at the screen's scale (a PNG without a resolution has as many
100    /// points as pixels).
101    Image { width: u32, height: u32 },
102    /// The icon theme's icon of this name.
103    Named(String),
104}