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