Skip to main content

iced_runtime/
lib.rs

1//! A renderer-agnostic native GUI runtime.
2//!
3//! ![The native path of the Iced ecosystem](https://github.com/iced-rs/iced/blob/master/docs/graphs/native.png?raw=true)
4//!
5//! `iced_runtime` takes [`iced_core`] and builds a native runtime on top of it.
6//!
7//! [`iced_core`]: https://github.com/iced-rs/iced/tree/master/core
8#![doc(
9    html_logo_url = "https://raw.githubusercontent.com/iced-rs/iced/9ab6923e943f784985e9ef9ca28b10278297225d/docs/logo.svg"
10)]
11#![cfg_attr(docsrs, feature(doc_cfg))]
12pub mod clipboard;
13pub mod font;
14pub mod image;
15pub mod keyboard;
16pub mod system;
17pub mod task;
18pub mod user_interface;
19pub mod widget;
20pub mod window;
21
22pub use iced_core as core;
23pub use iced_futures as futures;
24
25pub use task::Task;
26pub use user_interface::UserInterface;
27pub use window::Window;
28
29use crate::core::Event;
30
31use std::fmt;
32
33/// An action that the iced runtime can perform.
34pub enum Action<T> {
35    /// Output some value.
36    Output(T),
37
38    /// Run a widget operation.
39    Widget(Box<dyn core::widget::Operation>),
40
41    /// Run a clipboard action.
42    Clipboard(clipboard::Action),
43
44    /// Run a window action.
45    Window(window::Action),
46
47    /// Run a system action.
48    System(system::Action),
49
50    /// Run a font action.
51    Font(font::Action),
52
53    /// Run an image action.
54    Image(image::Action),
55
56    /// Produce an event.
57    Event {
58        /// The [`window::Id`](core::window::Id) of the event.
59        window: core::window::Id,
60        /// The [`Event`] to be produced.
61        event: Event,
62    },
63
64    /// Poll any resources that may have pending computations.
65    Tick,
66
67    /// Recreate all user interfaces and redraw all windows.
68    Reload,
69
70    /// Exits the runtime.
71    ///
72    /// This will normally close any application windows and
73    /// terminate the runtime loop.
74    Exit,
75}
76
77impl<T> Action<T> {
78    /// Creates a new [`Action::Widget`] with the given [`widget::Operation`](core::widget::Operation).
79    pub fn widget(operation: impl core::widget::Operation + 'static) -> Self {
80        Self::Widget(Box::new(operation))
81    }
82
83    fn output<O>(self) -> Result<T, Action<O>> {
84        match self {
85            Action::Output(output) => Ok(output),
86            Action::Widget(operation) => Err(Action::Widget(operation)),
87            Action::Clipboard(action) => Err(Action::Clipboard(action)),
88            Action::Window(action) => Err(Action::Window(action)),
89            Action::System(action) => Err(Action::System(action)),
90            Action::Font(action) => Err(Action::Font(action)),
91            Action::Image(action) => Err(Action::Image(action)),
92            Action::Event { window, event } => Err(Action::Event { window, event }),
93            Action::Tick => Err(Action::Tick),
94            Action::Reload => Err(Action::Reload),
95            Action::Exit => Err(Action::Exit),
96        }
97    }
98}
99
100impl<T> fmt::Debug for Action<T>
101where
102    T: fmt::Debug,
103{
104    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
105        match self {
106            Action::Output(output) => write!(f, "Action::Output({output:?})"),
107            Action::Widget { .. } => {
108                write!(f, "Action::Widget")
109            }
110            Action::Clipboard(action) => {
111                write!(f, "Action::Clipboard({action:?})")
112            }
113            Action::Window(_) => write!(f, "Action::Window"),
114            Action::System(action) => write!(f, "Action::System({action:?})"),
115            Action::Font(action) => {
116                write!(f, "Action::Font({action:?})")
117            }
118            Action::Image(_) => write!(f, "Action::Image"),
119            Action::Event { window, event } => write!(
120                f,
121                "Action::Event {{ window: {window:?}, event: {event:?} }}"
122            ),
123            Action::Tick => write!(f, "Action::Tick"),
124            Action::Reload => write!(f, "Action::Reload"),
125            Action::Exit => write!(f, "Action::Exit"),
126        }
127    }
128}
129
130/// Creates a [`Task`] that exits the iced runtime.
131///
132/// This will normally close any application windows and
133/// terminate the runtime loop.
134pub fn exit<T>() -> Task<T> {
135    task::effect(Action::Exit)
136}