mitsuami_core/services/tray.rs
1//! The app's icon in the platform's tray: macOS's menu bar extras, the
2//! StatusNotifierItem hosts on Linux (Plasma's system tray, waybar, GNOME
3//! with the AppIndicator extension), Windows' notification area.
4
5use mitsuami_reactive::{IntoValue, Value};
6
7use super::menu::{Menu, MenuEntries};
8use super::menu_data::MenuEntry;
9use crate::app_info::AppIcon;
10
11/// The app's icon in the platform's tray, with a menu: what brings back an
12/// app that runs without windows, shows it's still running and holds its
13/// Quit. One per app: [`set_tray`](super::set_tray) replaces it, and it goes
14/// when the scope that installed it ends. A click opens the menu on every
15/// platform, so give it an item that opens the window.
16///
17/// ```ignore
18/// set_tray(
19/// TrayIcon::new("Player")
20/// .icon(AppIcon::named("player-symbolic").image(AppIcon::bytes(include_bytes!("tray.png"))))
21/// .tooltip(move || format!("{} songs queued", queue.get().len()))
22/// .menu(Menu::new("").children((
23/// MenuItem::new("Open Player").on_select(move || open.set(true)),
24/// MenuSeparator,
25/// MenuItem::new("Quit").role(MenuRole::Quit).on_select(quit),
26/// ))),
27/// );
28/// ```
29///
30/// In `view!`, in a component that holds it as long as it should show,
31/// its children are its menu's:
32/// `<TrayIcon title="Player" icon=icon><MenuItem @select=quit>"Quit"</MenuItem></TrayIcon>`.
33pub struct TrayIcon {
34 pub(crate) title: Value<String>,
35 pub(crate) icon: Option<Value<AppIcon>>,
36 pub(crate) tooltip: Value<String>,
37 pub(crate) menu: Menu,
38}
39
40impl TrayIcon {
41 /// `title` is its accessible name, and its name where the platform
42 /// lists tray items (Plasma's system tray settings, Windows' taskbar
43 /// settings).
44 pub fn new(title: impl IntoValue<String>) -> TrayIcon {
45 TrayIcon {
46 title: title.into_value(),
47 icon: None,
48 tooltip: Value::Static(String::new()),
49 menu: Menu::new(String::new()),
50 }
51 }
52
53 pub fn title(mut self, title: impl IntoValue<String>) -> TrayIcon {
54 self.title = title.into_value();
55 self
56 }
57
58 /// What it shows, which the app swaps to show state. Each platform
59 /// wants its own: a monochrome template image on macOS (AppKit tints
60 /// it), an icon name from the theme on Linux ([`AppIcon::named`], a
61 /// `-symbolic` one where the theme has it), an `.ico` with the small
62 /// sizes on Windows. Without, the app's icon. An image is read when
63 /// the icon changes: to show other pixels, give other bytes or another
64 /// path.
65 pub fn icon(mut self, icon: impl IntoValue<AppIcon>) -> TrayIcon {
66 self.icon = Some(icon.into_value());
67 self
68 }
69
70 /// Text shown over it, and read by screen readers.
71 pub fn tooltip(mut self, tooltip: impl IntoValue<String>) -> TrayIcon {
72 self.tooltip = tooltip.into_value();
73 self
74 }
75
76 /// What a click opens: the same menus the menu bar takes (its title
77 /// isn't shown).
78 pub fn menu(mut self, menu: Menu) -> TrayIcon {
79 self.menu = menu;
80 self
81 }
82
83 #[doc(hidden)]
84 pub fn __tag() -> TrayIcon {
85 TrayIcon::new(String::new())
86 }
87
88 /// `<TrayIcon>…</TrayIcon>`: its children are its menu's.
89 #[doc(hidden)]
90 pub fn __children<E: MenuEntries>(mut self, children: impl FnOnce() -> E) -> TrayIcon {
91 self.menu = Menu::new(String::new()).children(children());
92 self
93 }
94}
95
96/// A tray icon as data, for [`Services::set_tray`](super::Services::set_tray).
97/// Hidden items are left out.
98#[derive(Clone, Debug, PartialEq)]
99pub struct TrayData {
100 pub title: String,
101 /// `None`: the app's icon (`AppInfo::icon`, or the platform's for it).
102 pub icon: Option<AppIcon>,
103 /// Empty for none.
104 pub tooltip: String,
105 pub menu: Vec<MenuEntry>,
106}