iced_native/command/
action.rs

1use crate::clipboard;
2use crate::system;
3use crate::widget;
4use crate::window;
5
6use iced_futures::MaybeSend;
7
8use std::fmt;
9
10/// An action that a [`Command`] can perform.
11///
12/// [`Command`]: crate::Command
13pub enum Action<T> {
14    /// Run a [`Future`] to completion.
15    ///
16    /// [`Future`]: iced_futures::BoxFuture
17    Future(iced_futures::BoxFuture<T>),
18
19    /// Run a clipboard action.
20    Clipboard(clipboard::Action<T>),
21
22    /// Run a window action.
23    Window(window::Action<T>),
24
25    /// Run a system action.
26    System(system::Action<T>),
27
28    /// Run a widget action.
29    Widget(widget::Action<T>),
30}
31
32impl<T> Action<T> {
33    /// Applies a transformation to the result of a [`Command`].
34    ///
35    /// [`Command`]: crate::Command
36    pub fn map<A>(
37        self,
38        f: impl Fn(T) -> A + 'static + MaybeSend + Sync,
39    ) -> Action<A>
40    where
41        A: 'static,
42        T: 'static,
43    {
44        use iced_futures::futures::FutureExt;
45
46        match self {
47            Self::Future(future) => Action::Future(Box::pin(future.map(f))),
48            Self::Clipboard(action) => Action::Clipboard(action.map(f)),
49            Self::Window(window) => Action::Window(window.map(f)),
50            Self::System(system) => Action::System(system.map(f)),
51            Self::Widget(widget) => Action::Widget(widget.map(f)),
52        }
53    }
54}
55
56impl<T> fmt::Debug for Action<T> {
57    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
58        match self {
59            Self::Future(_) => write!(f, "Action::Future"),
60            Self::Clipboard(action) => {
61                write!(f, "Action::Clipboard({action:?})")
62            }
63            Self::Window(action) => write!(f, "Action::Window({action:?})"),
64            Self::System(action) => write!(f, "Action::System({action:?})"),
65            Self::Widget(_action) => write!(f, "Action::Widget"),
66        }
67    }
68}