Skip to main content

mitsuami_core/services/
futures.rs

1//! Async glue: replies as futures, and the service calls on the current
2//! `Ui`.
3
4use std::cell::RefCell;
5use std::future::Future;
6use std::path::{Path, PathBuf};
7use std::rc::Rc;
8use std::task::{Poll, Waker};
9
10use crate::ui::Ui;
11
12use super::{Alert, Launch, MenuBar, Notification, OpenFile, Reply, SaveFile, ServiceError};
13
14struct OneShot<T> {
15    value: Option<T>,
16    waker: Option<Waker>,
17}
18
19/// A reply callback and the future that resolves when it is called.
20pub(crate) fn reply_future<T: 'static>() -> (Reply<T>, impl Future<Output = T> + use<T>) {
21    let shared = Rc::new(RefCell::new(OneShot { value: None, waker: None }));
22    let sender = shared.clone();
23    let reply: Reply<T> = Box::new(move |value| {
24        let waker = {
25            let mut shared = sender.borrow_mut();
26            shared.value = Some(value);
27            shared.waker.take()
28        };
29        if let Some(waker) = waker {
30            waker.wake();
31        }
32    });
33    let future = std::future::poll_fn(move |cx| {
34        let mut shared = shared.borrow_mut();
35        match shared.value.take() {
36            Some(value) => Poll::Ready(value),
37            None => {
38                shared.waker = Some(cx.waker().clone());
39                Poll::Pending
40            }
41        }
42    });
43    (reply, future)
44}
45
46fn ui() -> Ui {
47    crate::task::current_ui()
48}
49
50pub fn clipboard_text() -> impl Future<Output = Option<String>> + use<> {
51    ui().clipboard_text()
52}
53
54/// Puts text on the clipboard right away; await to learn whether it worked.
55pub fn set_clipboard_text(text: &str) -> impl Future<Output = Result<(), ServiceError>> + use<> {
56    ui().set_clipboard_text(text)
57}
58
59/// Shows an alert on the active window; resolves to the chosen button's index.
60pub fn alert(alert: Alert) -> impl Future<Output = usize> + use<> {
61    ui().alert(None, alert)
62}
63
64pub fn open_file(request: OpenFile) -> impl Future<Output = Option<Vec<PathBuf>>> + use<> {
65    ui().open_file(None, request)
66}
67
68pub fn save_file(request: SaveFile) -> impl Future<Output = Option<PathBuf>> + use<> {
69    ui().save_file(None, request)
70}
71
72/// Moves files and folders to the trash; see [`Services::trash`](super::Services::trash).
73pub fn trash(paths: Vec<PathBuf>) -> impl Future<Output = Result<(), ServiceError>> + use<> {
74    ui().trash(None, paths)
75}
76
77/// Opens a file or folder in the app the platform picks for it; see
78/// [`Services::launch`](super::Services::launch).
79pub fn launch(path: &Path) -> impl Future<Output = Result<(), ServiceError>> + use<> {
80    ui().launch(None, Launch::Path(path.to_owned()))
81}
82
83/// Opens a URL in the app set for its scheme (the browser for `https:`).
84pub fn launch_url(url: &str) -> impl Future<Output = Result<(), ServiceError>> + use<> {
85    ui().launch(None, Launch::Url(url.to_owned()))
86}
87
88/// Shows a notification outside the app's windows, as the platform shows
89/// its apps'; see [`Services::notify`](super::Services::notify). Resolves
90/// once the platform has it.
91pub fn notify(notification: Notification) -> impl Future<Output = Result<(), ServiceError>> + use<> {
92    ui().notify(notification)
93}
94
95/// Takes away the notification with this id, if it's still there.
96pub fn withdraw_notification(id: &str) {
97    ui().withdraw_notification(id);
98}
99
100/// Runs `handler` with a notification's id when the user clicks it, until
101/// the current scope ends; see [`Ui::on_notification_activated`].
102pub fn on_notification_activated(handler: impl Fn(&str) + 'static) {
103    ui().on_notification_activated(handler);
104}
105
106/// Installs the app's menus; see [`Ui::set_menu`]. A window's own menus
107/// are a [`MenuBar`] in its content.
108pub fn set_menu(menu: MenuBar) {
109    ui().set_menu(menu);
110}