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    /// Announce a message to assistive technology via a live region.
71    ///
72    /// The text will be spoken by screen readers using an assertive
73    /// live-region announcement on the next accessibility tree update.
74    Announce(String),
75
76    /// Exits the runtime.
77    ///
78    /// This will normally close any application windows and
79    /// terminate the runtime loop.
80    Exit,
81}
82
83impl<T> Action<T> {
84    /// Creates a new [`Action::Widget`] with the given [`widget::Operation`](core::widget::Operation).
85    pub fn widget(operation: impl core::widget::Operation + 'static) -> Self {
86        Self::Widget(Box::new(operation))
87    }
88
89    fn output<O>(self) -> Result<T, Action<O>> {
90        match self {
91            Action::Output(output) => Ok(output),
92            Action::Widget(operation) => Err(Action::Widget(operation)),
93            Action::Clipboard(action) => Err(Action::Clipboard(action)),
94            Action::Window(action) => Err(Action::Window(action)),
95            Action::System(action) => Err(Action::System(action)),
96            Action::Font(action) => Err(Action::Font(action)),
97            Action::Image(action) => Err(Action::Image(action)),
98            Action::Event { window, event } => Err(Action::Event { window, event }),
99            Action::Tick => Err(Action::Tick),
100            Action::Reload => Err(Action::Reload),
101            Action::Exit => Err(Action::Exit),
102            Action::Announce(text) => Err(Action::Announce(text)),
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::Widget { .. } => {
115                write!(f, "Action::Widget")
116            }
117            Action::Clipboard(action) => {
118                write!(f, "Action::Clipboard({action:?})")
119            }
120            Action::Window(_) => write!(f, "Action::Window"),
121            Action::System(action) => write!(f, "Action::System({action:?})"),
122            Action::Font(action) => {
123                write!(f, "Action::Font({action:?})")
124            }
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            Action::Announce(text) => {
134                write!(f, "Action::Announce({text:?})")
135            }
136        }
137    }
138}
139
140/// Creates a [`Task`] that announces the given text to assistive
141/// technology via a live region.
142///
143/// Screen readers will speak the text using an assertive announcement.
144#[cfg(feature = "a11y")]
145#[cfg_attr(docsrs, doc(cfg(feature = "a11y")))]
146pub fn announce<T>(text: impl Into<String>) -> Task<T> {
147    task::effect(Action::Announce(text.into()))
148}
149
150/// Creates a [`Task`] that exits the iced runtime.
151///
152/// This will normally close any application windows and
153/// terminate the runtime loop.
154pub fn exit<T>() -> Task<T> {
155    task::effect(Action::Exit)
156}