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 backend;
13pub mod clipboard;
14pub mod font;
15pub mod image;
16pub mod keyboard;
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;
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    /// Run a backend action.
57    Backend(backend::Action),
58
59    /// Produce an event.
60    Event {
61        /// The [`window::Id`](core::window::Id) of the event.
62        window: core::window::Id,
63        /// The [`Event`] to be produced.
64        event: Event,
65    },
66
67    /// Poll any resources that may have pending computations.
68    Tick,
69
70    /// Recreate all user interfaces and redraw all windows.
71    Reload,
72
73    /// Exits the runtime.
74    ///
75    /// This will normally close any application windows and
76    /// terminate the runtime loop.
77    Exit,
78}
79
80impl<T> Action<T> {
81    /// Creates a new [`Action::Widget`] with the given [`widget::Operation`](core::widget::Operation).
82    pub fn widget(operation: impl core::widget::Operation + 'static) -> Self {
83        Self::Widget(Box::new(operation))
84    }
85
86    fn output<O>(self) -> Result<T, Action<O>> {
87        match self {
88            Action::Output(output) => Ok(output),
89            Action::Widget(operation) => Err(Action::Widget(operation)),
90            Action::Clipboard(action) => Err(Action::Clipboard(action)),
91            Action::Window(action) => Err(Action::Window(action)),
92            Action::System(action) => Err(Action::System(action)),
93            Action::Font(action) => Err(Action::Font(action)),
94            Action::Image(action) => Err(Action::Image(action)),
95            Action::Backend(action) => Err(Action::Backend(action)),
96            Action::Event { window, event } => Err(Action::Event { window, event }),
97            Action::Tick => Err(Action::Tick),
98            Action::Reload => Err(Action::Reload),
99            Action::Exit => Err(Action::Exit),
100        }
101    }
102}
103
104impl<T> fmt::Debug for Action<T>
105where
106    T: fmt::Debug,
107{
108    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
109        match self {
110            Action::Output(output) => write!(f, "Action::Output({output:?})"),
111            Action::Widget { .. } => {
112                write!(f, "Action::Widget")
113            }
114            Action::Clipboard(action) => {
115                write!(f, "Action::Clipboard({action:?})")
116            }
117            Action::Window(_) => write!(f, "Action::Window"),
118            Action::System(action) => write!(f, "Action::System({action:?})"),
119            Action::Font(action) => {
120                write!(f, "Action::Font({action:?})")
121            }
122            Action::Image(action) => write!(f, "Action::Image({action:?})"),
123            Action::Backend(action) => write!(f, "Action::Backend({action:?})"),
124            Action::Event { window, event } => write!(
125                f,
126                "Action::Event {{ window: {window:?}, event: {event:?} }}"
127            ),
128            Action::Tick => write!(f, "Action::Tick"),
129            Action::Reload => write!(f, "Action::Reload"),
130            Action::Exit => write!(f, "Action::Exit"),
131        }
132    }
133}
134
135/// Creates a [`Task`] that exits the iced runtime.
136///
137/// This will normally close any application windows and
138/// terminate the runtime loop.
139pub fn exit<T>() -> Task<T> {
140    task::effect(Action::Exit)
141}