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