use std::cell::RefCell;
use std::future::Future;
use std::path::{Path, PathBuf};
use std::rc::Rc;
use std::task::{Poll, Waker};
use crate::ui::Ui;
use super::{Alert, Launch, MenuBar, Notification, OpenFile, Reply, SaveFile, ServiceError};
struct OneShot<T> {
value: Option<T>,
waker: Option<Waker>,
}
pub(crate) fn reply_future<T: 'static>() -> (Reply<T>, impl Future<Output = T> + use<T>) {
let shared = Rc::new(RefCell::new(OneShot { value: None, waker: None }));
let sender = shared.clone();
let reply: Reply<T> = Box::new(move |value| {
let waker = {
let mut shared = sender.borrow_mut();
shared.value = Some(value);
shared.waker.take()
};
if let Some(waker) = waker {
waker.wake();
}
});
let future = std::future::poll_fn(move |cx| {
let mut shared = shared.borrow_mut();
match shared.value.take() {
Some(value) => Poll::Ready(value),
None => {
shared.waker = Some(cx.waker().clone());
Poll::Pending
}
}
});
(reply, future)
}
fn ui() -> Ui {
crate::task::current_ui()
}
pub fn clipboard_text() -> impl Future<Output = Option<String>> + use<> {
ui().clipboard_text()
}
pub fn set_clipboard_text(text: &str) -> impl Future<Output = Result<(), ServiceError>> + use<> {
ui().set_clipboard_text(text)
}
pub fn alert(alert: Alert) -> impl Future<Output = usize> + use<> {
ui().alert(None, alert)
}
pub fn open_file(request: OpenFile) -> impl Future<Output = Option<Vec<PathBuf>>> + use<> {
ui().open_file(None, request)
}
pub fn save_file(request: SaveFile) -> impl Future<Output = Option<PathBuf>> + use<> {
ui().save_file(None, request)
}
pub fn trash(paths: Vec<PathBuf>) -> impl Future<Output = Result<(), ServiceError>> + use<> {
ui().trash(None, paths)
}
pub fn launch(path: &Path) -> impl Future<Output = Result<(), ServiceError>> + use<> {
ui().launch(None, Launch::Path(path.to_owned()))
}
pub fn launch_url(url: &str) -> impl Future<Output = Result<(), ServiceError>> + use<> {
ui().launch(None, Launch::Url(url.to_owned()))
}
pub fn notify(notification: Notification) -> impl Future<Output = Result<(), ServiceError>> + use<> {
ui().notify(notification)
}
pub fn quit() {
ui().quit();
}
pub fn withdraw_notification(id: &str) {
ui().withdraw_notification(id);
}
pub fn on_notification_activated(handler: impl Fn(&str) + 'static) {
ui().on_notification_activated(handler);
}
pub fn set_menu(menu: MenuBar) {
ui().set_menu(menu);
}