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/// How the app runs, beyond who it is: what `App` gives its backend's
50/// `run_with`.
51#[derive(Clone, Debug, Default, PartialEq, Eq)]
52#[non_exhaustive]
53pub struct RunOptions {
54 /// One process per app id and user session: a later launch hands its
55 /// arguments to the running one ([`Ui::reopen`](crate::Ui::reopen)) and
56 /// ends, without starting the app. Needs [`AppInfo::id`].
57 pub single_instance: bool,
58}
59
60impl RunOptions {
61 pub fn new() -> RunOptions {
62 RunOptions::default()
63 }
64
65 pub fn single_instance(mut self, single: bool) -> RunOptions {
66 self.single_instance = single;
67 self
68 }
69}
70
71/// The app's icon, decoded by the platform: PNG everywhere, other formats
72/// where the platform reads them (an `.ico` file on Windows, with its
73/// sizes). Give it large (256 or 512 pixels square): platforms scale it
74/// down.
75#[derive(Clone, PartialEq)]
76pub enum AppIcon {
77 File(PathBuf),
78 /// An image file's bytes, as `include_bytes!` gives them.
79 Bytes(Arc<[u8]>),
80 /// An icon from the user's icon theme, by name, as Linux apps give
81 /// theirs (installed with the app); `image` where there's no theme
82 /// (macOS, Windows). See [`AppIcon::named`].
83 Named {
84 name: String,
85 image: Option<Arc<AppIcon>>,
86 },
87 /// An image with another for dark backgrounds, where the platform
88 /// wants one. See [`AppIcon::dark`].
89 Themed {
90 light: Arc<AppIcon>,
91 dark: Arc<AppIcon>,
92 },
93}
94
95impl AppIcon {
96 pub fn file(path: impl Into<PathBuf>) -> AppIcon {
97 AppIcon::File(path.into())
98 }
99
100 pub fn bytes(bytes: impl Into<Arc<[u8]>>) -> AppIcon {
101 AppIcon::Bytes(bytes.into())
102 }
103
104 /// An icon from the icon theme, by name: Linux shows it; macOS and
105 /// Windows show the [`image`](Self::image) given with it, or nothing.
106 pub fn named(name: impl Into<String>) -> AppIcon {
107 AppIcon::Named { name: name.into(), image: None }
108 }
109
110 /// The image a [`named`](Self::named) icon shows where there's no icon
111 /// theme. Other icons are images already, and stay as they are.
112 pub fn image(self, image: AppIcon) -> AppIcon {
113 match self {
114 AppIcon::Named { name, .. } => AppIcon::Named { name, image: Some(Arc::new(image)) },
115 // `named(…).dark(…).image(…)`: the light side's.
116 AppIcon::Themed { light, dark } => {
117 AppIcon::Themed { light: Arc::new(Arc::unwrap_or_clone(light).image(image)), dark }
118 }
119 icon => icon,
120 }
121 }
122
123 /// The image to show on a dark background, where the platform doesn't
124 /// recolor the icon for it: Windows' tray on a dark taskbar. Elsewhere
125 /// the image itself shows: macOS tints the tray's image for the menu
126 /// bar, Linux recolors a theme's symbolic icons, and nothing tells an
127 /// app what's under a Linux tray (GNOME's top bar is dark in light
128 /// mode too). A [`named`](Self::named) icon's image takes it, in either
129 /// order with [`image`](Self::image).
130 pub fn dark(self, dark: AppIcon) -> AppIcon {
131 match self {
132 AppIcon::Named { name, image: Some(image) } => {
133 AppIcon::Named { name, image: Some(Arc::new(Arc::unwrap_or_clone(image).dark(dark))) }
134 }
135 // No image yet: the pair holds the name, and `image` gives the
136 // light side.
137 AppIcon::Themed { light, .. } => AppIcon::Themed { light, dark: Arc::new(dark) },
138 light => AppIcon::Themed { light: Arc::new(light), dark: Arc::new(dark) },
139 }
140 }
141
142 /// A named icon's name in the theme.
143 pub fn name(&self) -> Option<&str> {
144 match self {
145 AppIcon::Named { name, .. } => Some(name),
146 AppIcon::Themed { light, .. } => light.name(),
147 AppIcon::File(_) | AppIcon::Bytes(_) => None,
148 }
149 }
150
151 /// The image (a file or bytes) for a light or dark background: a named
152 /// icon's image, and a [`dark`](Self::dark) one's on a dark one.
153 pub fn image_for(&self, dark: bool) -> Option<&AppIcon> {
154 match self {
155 AppIcon::File(_) | AppIcon::Bytes(_) => Some(self),
156 AppIcon::Named { image, .. } => image.as_ref()?.image_for(dark),
157 AppIcon::Themed { light, dark: on_dark } => if dark { on_dark } else { light }.image_for(dark),
158 }
159 }
160
161 /// The image's bytes: read from its file, or as given; a named icon's
162 /// image's, if it has one, and the light one of a [`dark`](Self::dark)
163 /// pair.
164 pub fn read(&self) -> Option<Arc<[u8]>> {
165 match self.image_for(false)? {
166 AppIcon::File(path) => std::fs::read(path).ok().map(Into::into),
167 AppIcon::Bytes(bytes) => Some(bytes.clone()),
168 AppIcon::Named { .. } | AppIcon::Themed { .. } => None,
169 }
170 }
171}
172
173/// Its path or length, not its bytes.
174impl fmt::Debug for AppIcon {
175 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
176 match self {
177 AppIcon::File(path) => f.debug_tuple("File").field(path).finish(),
178 AppIcon::Bytes(bytes) => write!(f, "Bytes({} bytes)", bytes.len()),
179 AppIcon::Named { name, image } => {
180 f.debug_struct("Named").field("name", name).field("image", image).finish()
181 }
182 AppIcon::Themed { light, dark } => {
183 f.debug_struct("Themed").field("light", light).field("dark", dark).finish()
184 }
185 }
186 }
187}
188
189impl mitsuami_reactive::IntoValue<AppIcon> for AppIcon {
190 fn into_value(self) -> mitsuami_reactive::Value<AppIcon> {
191 mitsuami_reactive::Value::Static(self)
192 }
193}
194
195/// A name from the icon theme: `.icon("player-symbolic")`.
196impl mitsuami_reactive::IntoValue<AppIcon> for &str {
197 fn into_value(self) -> mitsuami_reactive::Value<AppIcon> {
198 mitsuami_reactive::Value::Static(AppIcon::named(self))
199 }
200}
201
202/// What a window shows of the app, read back from the platform for tests.
203#[derive(Clone, Debug, Default, PartialEq)]
204pub struct NativeAppInfo {
205 pub id: Option<String>,
206 pub name: Option<String>,
207 pub icon: Option<NativeIcon>,
208}
209
210#[derive(Clone, Debug, PartialEq)]
211pub enum NativeIcon {
212 /// An image this large: in pixels, or in points on AppKit, which keeps
213 /// a copy at the screen's scale (a PNG without a resolution has as many
214 /// points as pixels).
215 Image { width: u32, height: u32 },
216 /// The icon theme's icon of this name.
217 Named(String),
218}