2021-09-01 19:21:49 +07:00
|
|
|
use crate::clipboard;
|
2023-02-04 11:12:15 +01:00
|
|
|
use crate::font;
|
2022-02-16 19:20:26 -03:00
|
|
|
use crate::system;
|
2022-07-28 02:46:51 +02:00
|
|
|
use crate::widget;
|
2021-09-01 19:21:49 +07:00
|
|
|
use crate::window;
|
|
|
|
|
|
2022-01-28 21:45:30 +07:00
|
|
|
use iced_futures::MaybeSend;
|
|
|
|
|
|
2023-02-04 11:12:15 +01:00
|
|
|
use std::borrow::Cow;
|
2021-09-13 11:20:54 +07:00
|
|
|
use std::fmt;
|
|
|
|
|
|
|
|
|
|
/// An action that a [`Command`] can perform.
|
|
|
|
|
///
|
|
|
|
|
/// [`Command`]: crate::Command
|
2021-09-01 19:21:49 +07:00
|
|
|
pub enum Action<T> {
|
2021-09-13 11:20:54 +07:00
|
|
|
/// Run a [`Future`] to completion.
|
2022-04-30 14:20:52 +02:00
|
|
|
///
|
|
|
|
|
/// [`Future`]: iced_futures::BoxFuture
|
2021-09-01 19:21:49 +07:00
|
|
|
Future(iced_futures::BoxFuture<T>),
|
2021-09-13 11:20:54 +07:00
|
|
|
|
|
|
|
|
/// Run a clipboard action.
|
2021-09-01 19:21:49 +07:00
|
|
|
Clipboard(clipboard::Action<T>),
|
2021-09-13 11:20:54 +07:00
|
|
|
|
|
|
|
|
/// Run a window action.
|
2022-08-18 14:39:15 +02:00
|
|
|
Window(window::Action<T>),
|
2022-02-16 19:20:26 -03:00
|
|
|
|
|
|
|
|
/// Run a system action.
|
2022-02-16 19:37:38 -03:00
|
|
|
System(system::Action<T>),
|
2022-07-28 02:46:51 +02:00
|
|
|
|
|
|
|
|
/// Run a widget action.
|
|
|
|
|
Widget(widget::Action<T>),
|
2023-02-04 11:12:15 +01:00
|
|
|
|
|
|
|
|
/// Load a font from its bytes.
|
|
|
|
|
LoadFont {
|
|
|
|
|
/// The bytes of the font to load.
|
|
|
|
|
bytes: Cow<'static, [u8]>,
|
|
|
|
|
|
|
|
|
|
/// The message to produce when the font has been loaded.
|
|
|
|
|
tagger: Box<dyn Fn(Result<(), font::Error>) -> T>,
|
|
|
|
|
},
|
2021-09-01 19:21:49 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<T> Action<T> {
|
|
|
|
|
/// Applies a transformation to the result of a [`Command`].
|
2022-04-30 14:20:52 +02:00
|
|
|
///
|
|
|
|
|
/// [`Command`]: crate::Command
|
2022-01-28 21:45:30 +07:00
|
|
|
pub fn map<A>(
|
|
|
|
|
self,
|
|
|
|
|
f: impl Fn(T) -> A + 'static + MaybeSend + Sync,
|
|
|
|
|
) -> Action<A>
|
2021-09-01 19:21:49 +07:00
|
|
|
where
|
2022-07-28 02:46:51 +02:00
|
|
|
A: 'static,
|
2021-09-01 19:21:49 +07:00
|
|
|
T: 'static,
|
|
|
|
|
{
|
|
|
|
|
use iced_futures::futures::FutureExt;
|
|
|
|
|
|
|
|
|
|
match self {
|
|
|
|
|
Self::Future(future) => Action::Future(Box::pin(future.map(f))),
|
|
|
|
|
Self::Clipboard(action) => Action::Clipboard(action.map(f)),
|
2022-08-18 14:39:15 +02:00
|
|
|
Self::Window(window) => Action::Window(window.map(f)),
|
2022-02-16 19:37:38 -03:00
|
|
|
Self::System(system) => Action::System(system.map(f)),
|
2022-07-28 02:46:51 +02:00
|
|
|
Self::Widget(widget) => Action::Widget(widget.map(f)),
|
2023-02-04 11:12:15 +01:00
|
|
|
Self::LoadFont { bytes, tagger } => Action::LoadFont {
|
|
|
|
|
bytes,
|
|
|
|
|
tagger: Box::new(move |result| f(tagger(result))),
|
|
|
|
|
},
|
2021-09-01 19:21:49 +07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-09-13 11:20:54 +07:00
|
|
|
|
|
|
|
|
impl<T> fmt::Debug for Action<T> {
|
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
|
match self {
|
|
|
|
|
Self::Future(_) => write!(f, "Action::Future"),
|
|
|
|
|
Self::Clipboard(action) => {
|
2023-01-27 13:25:04 -07:00
|
|
|
write!(f, "Action::Clipboard({action:?})")
|
2021-09-13 11:20:54 +07:00
|
|
|
}
|
2023-01-27 13:25:04 -07:00
|
|
|
Self::Window(action) => write!(f, "Action::Window({action:?})"),
|
|
|
|
|
Self::System(action) => write!(f, "Action::System({action:?})"),
|
2022-07-28 02:46:51 +02:00
|
|
|
Self::Widget(_action) => write!(f, "Action::Widget"),
|
2023-02-04 11:12:15 +01:00
|
|
|
Self::LoadFont { .. } => write!(f, "Action::LoadFont"),
|
2021-09-13 11:20:54 +07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|