mitsuami_core/services/
futures.rs1use 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
19pub(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
54pub fn set_clipboard_text(text: &str) -> impl Future<Output = Result<(), ServiceError>> + use<> {
56 ui().set_clipboard_text(text)
57}
58
59pub 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
72pub fn trash(paths: Vec<PathBuf>) -> impl Future<Output = Result<(), ServiceError>> + use<> {
74 ui().trash(None, paths)
75}
76
77pub fn launch(path: &Path) -> impl Future<Output = Result<(), ServiceError>> + use<> {
80 ui().launch(None, Launch::Path(path.to_owned()))
81}
82
83pub fn launch_url(url: &str) -> impl Future<Output = Result<(), ServiceError>> + use<> {
85 ui().launch(None, Launch::Url(url.to_owned()))
86}
87
88pub fn notify(notification: Notification) -> impl Future<Output = Result<(), ServiceError>> + use<> {
92 ui().notify(notification)
93}
94
95pub fn withdraw_notification(id: &str) {
97 ui().withdraw_notification(id);
98}
99
100pub fn on_notification_activated(handler: impl Fn(&str) + 'static) {
103 ui().on_notification_activated(handler);
104}
105
106pub fn set_menu(menu: MenuBar) {
109 ui().set_menu(menu);
110}