mitsuami_core/services/mod.rs
1//! Platform services: clipboard, dialogs, the menu bar, the trash,
2//! opening files in other apps, notifications, later launches of a
3//! single-instance app and the tray icon.
4//! Widgets' context menus are made of the same menus.
5//!
6//! [`Services`] is the contract each platform implements, separately from
7//! the widget [`Backend`](crate::Backend). Tests install a scripted fake in
8//! its place, so they never open real dialogs or touch the real clipboard.
9//!
10//! App code uses the async functions here (`alert(...).await`, …) or the
11//! same methods on [`Ui`](crate::Ui).
12
13mod dialogs;
14mod futures;
15mod menu;
16mod menu_collect;
17mod menu_data;
18mod notifications;
19mod reopen;
20mod tray;
21
22pub use dialogs::{Alert, AlertStyle, FileFilter, Launch, OpenFile, SaveFile, existing_folder};
23pub(crate) use futures::reply_future;
24pub use futures::{
25 alert, clipboard_text, launch, launch_url, notify, on_notification_activated, on_reopen, open_file, quit,
26 save_file, set_clipboard_text, set_menu, set_tray, trash, tray_shown, withdraw_notification,
27};
28pub use menu::{Menu, MenuBar, MenuEntries, MenuItem, MenuSeparator, Menus};
29#[doc(hidden)]
30pub use menu_collect::{ItemMenus, install_button_menu, install_button_menu_on_open};
31pub(crate) use menu_collect::{install_context_menu, install_context_menu_on_open};
32pub use menu_data::{
33 MenuBarData, MenuCheck, MenuData, MenuEntry, MenuItemData, MenuOnOpen, MenuRole, Shortcut, find_menu_item,
34 menu_item_by_id,
35};
36pub use notifications::Notification;
37pub use reopen::Reopen;
38pub use tray::{TrayData, TrayIcon};
39
40use std::path::PathBuf;
41use std::rc::Rc;
42
43use crate::widget::NodeId;
44
45/// Delivers the user's answer. Called once, possibly long after the request.
46pub type Reply<T> = Box<dyn FnOnce(T)>;
47
48/// Why a service request failed.
49#[derive(Clone, Debug, PartialEq, Eq)]
50#[non_exhaustive]
51pub enum ServiceError {
52 /// The platform doesn't offer this service (or not right now).
53 Unavailable,
54 /// The user said no to a question the platform asked.
55 Cancelled,
56 Failed(String),
57}
58
59impl std::fmt::Display for ServiceError {
60 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
61 match self {
62 ServiceError::Unavailable => f.write_str("the service is not available"),
63 ServiceError::Cancelled => f.write_str("the user cancelled"),
64 ServiceError::Failed(why) => write!(f, "the service failed: {why}"),
65 }
66 }
67}
68
69impl std::error::Error for ServiceError {}
70
71/// What a platform provides beyond widgets.
72pub trait Services {
73 /// Reads the clipboard's text. Async because GTK and WinUI only read the
74 /// clipboard asynchronously; platforms that can may reply right away.
75 fn clipboard_text(&mut self, reply: Reply<Option<String>>);
76 /// Writes the clipboard (it can fail: e.g. another process holds it).
77 fn set_clipboard_text(&mut self, text: &str, reply: Reply<Result<(), ServiceError>>);
78
79 /// Shows an alert, attached to `parent` if given (a sheet on macOS) or
80 /// to the active window. Must not block: reply when the user answers.
81 fn alert(&mut self, parent: Option<NodeId>, alert: &Alert, reply: Reply<usize>);
82 /// Replies with the chosen paths, or `None` if cancelled.
83 fn open_file(&mut self, parent: Option<NodeId>, request: &OpenFile, reply: Reply<Option<Vec<PathBuf>>>);
84 fn save_file(&mut self, parent: Option<NodeId>, request: &SaveFile, reply: Reply<Option<PathBuf>>);
85 /// A window is about to be destroyed: close the alerts and file
86 /// dialogs open on it (asked for with it as `parent`, or shown on it as
87 /// the focused window) and reply as their cancel would, the alert's last
88 /// button or no paths. Called before the window's `Destroy` goes to the
89 /// backend, outside `apply`, so the dialogs close while their window is
90 /// still there.
91 fn window_destroyed(&mut self, window: NodeId);
92
93 /// Moves files and folders to the user's trash, as the platform's file
94 /// manager does, so they can be put back from there. Items go one after
95 /// another; the reply comes once all have gone, or with the first
96 /// failure, and those before it stay in the trash. `Unavailable`: the
97 /// item's disk has no trash. `Cancelled`: the user said no where the
98 /// platform asks first (Windows, when its settings say so).
99 fn trash(&mut self, parent: Option<NodeId>, paths: &[PathBuf], reply: Reply<Result<(), ServiceError>>);
100
101 /// Opens a file, folder or URL in the app the platform picks for it,
102 /// as its file manager would on a double-click. The platform may ask
103 /// which app when none is set; `Cancelled` if the user dismissed it,
104 /// `Unavailable` if no app opens it. Replies once the platform has
105 /// handed it over, not when the app has opened it.
106 fn launch(&mut self, parent: Option<NodeId>, target: &Launch, reply: Reply<Result<(), ServiceError>>);
107
108 /// Shows a notification outside the app's windows, as the platform
109 /// shows its apps', replacing the one with the same id
110 /// (`notification.id` is always set). Replies once the platform has it:
111 /// `Cancelled` if the user turned the app's notifications off or said
112 /// no when the platform asked, `Unavailable` if there's nothing to show
113 /// it (no notification server, a macOS app outside a bundle). When the
114 /// user clicks it, the platform brings the app forward, as it does for
115 /// its own apps, and the backend calls `activated` with its id.
116 fn notify(
117 &mut self,
118 notification: &Notification,
119 activated: Rc<dyn Fn(String)>,
120 reply: Reply<Result<(), ServiceError>>,
121 );
122 /// Takes the notification with this id away, from the screen and the
123 /// platform's list. Nothing happens if there's none.
124 fn withdraw_notification(&mut self, id: &str);
125
126 /// Installs a menu bar: the app's (`window` is `None`), or one
127 /// window's own menus, which it shows with the app's
128 /// ([`MenuBarData::merged`]). Called again whenever the bar changes;
129 /// an empty bar removes a window's menus. A window's menus may arrive
130 /// before the window is created, and after it's destroyed.
131 ///
132 /// Platforms keep their standard menus (e.g. the macOS app and Edit
133 /// menus) and call `activate` with an item's id when it is chosen.
134 fn set_menu(&mut self, window: Option<NodeId>, menu: &MenuBarData, activate: Rc<dyn Fn(u32)>);
135
136 /// Shows the app's icon in the platform's tray, or takes it away
137 /// (`None`). Called again whenever it changes. A click opens its menu
138 /// (there's no other click), and the backend calls `activate` with
139 /// the id of the item chosen. Call `shown` whenever whether it's
140 /// showing changes: on Linux the tray's host can come and go, and
141 /// plain GNOME has none. A platform without a tray keeps the default,
142 /// which shows nothing.
143 fn set_tray(&mut self, tray: Option<&TrayData>, activate: Rc<dyn Fn(u32)>, shown: Rc<dyn Fn(bool)>) {
144 let _ = (tray, activate, shown);
145 }
146}