mitsuami-core 2.5.2

Node tree, layout, styles, units, a11y model and backend contract for mitsuami
Documentation
//! Async glue: replies as futures, and the service calls on the current
//! `Ui`.

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, Reopen, Reply, SaveFile, ServiceError, TrayIcon};

struct OneShot<T> {
    value: Option<T>,
    waker: Option<Waker>,
}

/// A reply callback and the future that resolves when it is called.
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()
}

/// Puts text on the clipboard right away; await to learn whether it worked.
pub fn set_clipboard_text(text: &str) -> impl Future<Output = Result<(), ServiceError>> + use<> {
    ui().set_clipboard_text(text)
}

/// Shows an alert on the active window; resolves to the chosen button's index.
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)
}

/// Moves files and folders to the trash; see [`Services::trash`](super::Services::trash).
pub fn trash(paths: Vec<PathBuf>) -> impl Future<Output = Result<(), ServiceError>> + use<> {
    ui().trash(None, paths)
}

/// Opens a file or folder in the app the platform picks for it; see
/// [`Services::launch`](super::Services::launch).
pub fn launch(path: &Path) -> impl Future<Output = Result<(), ServiceError>> + use<> {
    ui().launch(None, Launch::Path(path.to_owned()))
}

/// Opens a URL in the app set for its scheme (the browser for `https:`).
pub fn launch_url(url: &str) -> impl Future<Output = Result<(), ServiceError>> + use<> {
    ui().launch(None, Launch::Url(url.to_owned()))
}

/// Shows a notification outside the app's windows, as the platform shows
/// its apps'; see [`Services::notify`](super::Services::notify). Resolves
/// once the platform has it.
pub fn notify(notification: Notification) -> impl Future<Output = Result<(), ServiceError>> + use<> {
    ui().notify(notification)
}

/// Ends the app once this turn is done, windows open or not; see
/// [`Ui::quit`]. An app that [runs without windows](Ui::set_runs_without_windows)
/// ends this way.
pub fn quit() {
    ui().quit();
}

/// Takes away the notification with this id, if it's still there.
pub fn withdraw_notification(id: &str) {
    ui().withdraw_notification(id);
}

/// Runs `handler` with a notification's id when the user clicks it, until
/// the current scope ends; see [`Ui::on_notification_activated`].
pub fn on_notification_activated(handler: impl Fn(&str) + 'static) {
    ui().on_notification_activated(handler);
}

/// Runs `handler` in a single-instance app each time it's launched again,
/// until the current scope ends; see [`Ui::on_reopen`].
pub fn on_reopen(handler: impl Fn(&Reopen) + 'static) {
    ui().on_reopen(handler);
}

/// Installs the app's menus; see [`Ui::set_menu`]. A window's own menus
/// are a [`MenuBar`] in its content.
pub fn set_menu(menu: MenuBar) {
    ui().set_menu(menu);
}

/// Shows the app's icon in the platform's tray until the current scope
/// ends, replacing the one shown before; see [`Ui::set_tray`].
pub fn set_tray(tray: TrayIcon) {
    ui().set_tray(tray);
}

/// Whether the tray icon is showing right now; see [`Ui::tray_shown`].
pub fn tray_shown() -> mitsuami_reactive::Signal<bool> {
    ui().tray_shown()
}