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/0.13/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_auto_cfg))]
12pub mod clipboard;
13pub mod font;
14pub mod keyboard;
15pub mod system;
16pub mod task;
17pub mod user_interface;
18pub mod widget;
19pub mod window;
20
21pub use iced_core as core;
22pub use iced_debug as debug;
23pub use iced_futures as futures;
24
25pub use task::Task;
26pub use user_interface::UserInterface;
27
28use crate::futures::futures::channel::oneshot;
29
30use std::borrow::Cow;
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    /// Load a font from its bytes.
39    LoadFont {
40        /// The bytes of the font to load.
41        bytes: Cow<'static, [u8]>,
42        /// The channel to send back the load result.
43        channel: oneshot::Sender<Result<(), font::Error>>,
44    },
45
46    /// Run a widget operation.
47    Widget(Box<dyn core::widget::Operation>),
48
49    /// Run a clipboard action.
50    Clipboard(clipboard::Action),
51
52    /// Run a window action.
53    Window(window::Action),
54
55    /// Run a system action.
56    System(system::Action),
57
58    /// Recreate all user interfaces and redraw all windows.
59    Reload,
60
61    /// Exits the runtime.
62    ///
63    /// This will normally close any application windows and
64    /// terminate the runtime loop.
65    Exit,
66}
67
68impl<T> Action<T> {
69    /// Creates a new [`Action::Widget`] with the given [`widget::Operation`](core::widget::Operation).
70    pub fn widget(operation: impl core::widget::Operation + 'static) -> Self {
71        Self::Widget(Box::new(operation))
72    }
73
74    fn output<O>(self) -> Result<T, Action<O>> {
75        match self {
76            Action::Output(output) => Ok(output),
77            Action::LoadFont { bytes, channel } => {
78                Err(Action::LoadFont { bytes, channel })
79            }
80            Action::Widget(operation) => Err(Action::Widget(operation)),
81            Action::Clipboard(action) => Err(Action::Clipboard(action)),
82            Action::Window(action) => Err(Action::Window(action)),
83            Action::System(action) => Err(Action::System(action)),
84            Action::Reload => Err(Action::Reload),
85            Action::Exit => Err(Action::Exit),
86        }
87    }
88}
89
90impl<T> fmt::Debug for Action<T>
91where
92    T: fmt::Debug,
93{
94    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
95        match self {
96            Action::Output(output) => write!(f, "Action::Output({output:?})"),
97            Action::LoadFont { .. } => {
98                write!(f, "Action::LoadFont")
99            }
100            Action::Widget { .. } => {
101                write!(f, "Action::Widget")
102            }
103            Action::Clipboard(action) => {
104                write!(f, "Action::Clipboard({action:?})")
105            }
106            Action::Window(_) => write!(f, "Action::Window"),
107            Action::System(action) => write!(f, "Action::System({action:?})"),
108            Action::Reload => write!(f, "Action::Reload"),
109            Action::Exit => write!(f, "Action::Exit"),
110        }
111    }
112}
113
114/// Creates a [`Task`] that exits the iced runtime.
115///
116/// This will normally close any application windows and
117/// terminate the runtime loop.
118pub fn exit<T>() -> Task<T> {
119    task::effect(Action::Exit)
120}