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;
30use crate::futures::futures::channel::oneshot;
31
32use std::borrow::Cow;
33use std::fmt;
34
35/// An action that the iced runtime can perform.
36pub enum Action<T> {
37    /// Output some value.
38    Output(T),
39
40    /// Load a font from its bytes.
41    LoadFont {
42        /// The bytes of the font to load.
43        bytes: Cow<'static, [u8]>,
44        /// The channel to send back the load result.
45        channel: oneshot::Sender<Result<(), font::Error>>,
46    },
47
48    /// Run a widget operation.
49    Widget(Box<dyn core::widget::Operation>),
50
51    /// Run a clipboard action.
52    Clipboard(clipboard::Action),
53
54    /// Run a window action.
55    Window(window::Action),
56
57    /// Run a system action.
58    System(system::Action),
59
60    /// Run an image action.
61    Image(image::Action),
62
63    /// Produce an event.
64    Event {
65        /// The [`window::Id`](core::window::Id) of the event.
66        window: core::window::Id,
67        /// The [`Event`] to be produced.
68        event: Event,
69    },
70
71    /// Poll any resources that may have pending computations.
72    Tick,
73
74    /// Recreate all user interfaces and redraw all windows.
75    Reload,
76
77    /// Exits the runtime.
78    ///
79    /// This will normally close any application windows and
80    /// terminate the runtime loop.
81    Exit,
82}
83
84impl<T> Action<T> {
85    /// Creates a new [`Action::Widget`] with the given [`widget::Operation`](core::widget::Operation).
86    pub fn widget(operation: impl core::widget::Operation + 'static) -> Self {
87        Self::Widget(Box::new(operation))
88    }
89
90    fn output<O>(self) -> Result<T, Action<O>> {
91        match self {
92            Action::Output(output) => Ok(output),
93            Action::LoadFont { bytes, channel } => Err(Action::LoadFont { bytes, channel }),
94            Action::Widget(operation) => Err(Action::Widget(operation)),
95            Action::Clipboard(action) => Err(Action::Clipboard(action)),
96            Action::Window(action) => Err(Action::Window(action)),
97            Action::System(action) => Err(Action::System(action)),
98            Action::Image(action) => Err(Action::Image(action)),
99            Action::Event { window, event } => Err(Action::Event { window, event }),
100            Action::Tick => Err(Action::Tick),
101            Action::Reload => Err(Action::Reload),
102            Action::Exit => Err(Action::Exit),
103        }
104    }
105}
106
107impl<T> fmt::Debug for Action<T>
108where
109    T: fmt::Debug,
110{
111    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
112        match self {
113            Action::Output(output) => write!(f, "Action::Output({output:?})"),
114            Action::LoadFont { .. } => {
115                write!(f, "Action::LoadFont")
116            }
117            Action::Widget { .. } => {
118                write!(f, "Action::Widget")
119            }
120            Action::Clipboard(action) => {
121                write!(f, "Action::Clipboard({action:?})")
122            }
123            Action::Window(_) => write!(f, "Action::Window"),
124            Action::System(action) => write!(f, "Action::System({action:?})"),
125            Action::Image(_) => write!(f, "Action::Image"),
126            Action::Event { window, event } => write!(
127                f,
128                "Action::Event {{ window: {window:?}, event: {event:?} }}"
129            ),
130            Action::Tick => write!(f, "Action::Tick"),
131            Action::Reload => write!(f, "Action::Reload"),
132            Action::Exit => write!(f, "Action::Exit"),
133        }
134    }
135}
136
137/// Creates a [`Task`] that exits the iced runtime.
138///
139/// This will normally close any application windows and
140/// terminate the runtime loop.
141pub fn exit<T>() -> Task<T> {
142    task::effect(Action::Exit)
143}