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