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 dnd;
14pub mod font;
15pub mod keyboard;
16pub mod overlay;
17pub mod platform_specific;
18pub mod program;
19pub mod system;
20pub mod task;
21pub mod user_interface;
22pub mod window;
23
24#[cfg(feature = "multi-window")]
25pub mod multi_window;
26
27// We disable debug capabilities on release builds unless the `debug` feature
28// is explicitly enabled.
29#[cfg(feature = "debug")]
30#[path = "debug/basic.rs"]
31mod debug;
32#[cfg(not(feature = "debug"))]
33#[path = "debug/null.rs"]
34mod debug;
35
36pub use iced_core as core;
37pub use iced_futures as futures;
38
39pub use debug::Debug;
40pub use program::Program;
41pub use task::Task;
42pub use user_interface::UserInterface;
43
44use crate::core::{widget, Color};
45use crate::futures::futures::channel::oneshot;
46
47use std::borrow::Cow;
48use std::fmt;
49
50/// An action that the iced runtime can perform.
51pub enum Action<T> {
52    /// Output some value.
53    Output(T),
54
55    /// Load a font from its bytes.
56    LoadFont {
57        /// The bytes of the font to load.
58        bytes: Cow<'static, [u8]>,
59        /// The channel to send back the load result.
60        channel: oneshot::Sender<Result<(), font::Error>>,
61    },
62
63    /// Run a widget operation.
64    Widget(Box<dyn widget::Operation>),
65
66    /// Run a clipboard action.
67    Clipboard(clipboard::Action),
68
69    /// Run a window action.
70    Window(window::Action),
71
72    /// Run a system action.
73    System(system::Action),
74
75    /// Exits the runtime.
76    ///
77    /// This will normally close any application windows and
78    /// terminate the runtime loop.
79    Exit,
80
81    /// Run a Dnd action.
82    Dnd(crate::dnd::DndAction),
83
84    /// Run a platform specific action
85    PlatformSpecific(crate::platform_specific::Action),
86}
87
88impl<T> Action<T> {
89    /// Creates a new [`Action::Widget`] with the given [`widget::Operation`].
90    pub fn widget(operation: impl 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 } => {
98                Err(Action::LoadFont { bytes, channel })
99            }
100            Action::Widget(operation) => Err(Action::Widget(operation)),
101            Action::Clipboard(action) => Err(Action::Clipboard(action)),
102            Action::Window(action) => Err(Action::Window(action)),
103            Action::System(action) => Err(Action::System(action)),
104            Action::Exit => Err(Action::Exit),
105            Action::Dnd(a) => Err(Action::Dnd(a)),
106            Action::PlatformSpecific(a) => Err(Action::PlatformSpecific(a)),
107        }
108    }
109}
110
111impl<T> fmt::Debug for Action<T>
112where
113    T: fmt::Debug,
114{
115    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
116        match self {
117            Action::Output(output) => write!(f, "Action::Output({output:?})"),
118            Action::LoadFont { .. } => {
119                write!(f, "Action::LoadFont")
120            }
121            Action::Widget { .. } => {
122                write!(f, "Action::Widget")
123            }
124            Action::Clipboard(action) => {
125                write!(f, "Action::Clipboard({action:?})")
126            }
127            Action::Window(_) => write!(f, "Action::Window"),
128            Action::System(action) => write!(f, "Action::System({action:?})"),
129            Action::Exit => write!(f, "Action::Exit"),
130            Action::PlatformSpecific(action) => {
131                write!(f, "Action::PlatformSpecific({:?})", action)
132            }
133            Action::Dnd(_) => write!(f, "Action::Dnd"),
134        }
135    }
136}
137
138/// Creates a [`Task`] that exits the iced runtime.
139///
140/// This will normally close any application windows and
141/// terminate the runtime loop.
142pub fn exit<T>() -> Task<T> {
143    task::effect(Action::Exit)
144}
145
146/// The appearance of a program.
147#[derive(Debug, Clone, Copy, PartialEq)]
148pub struct Appearance {
149    /// The background [`iced_core::Color`] of the application.
150    pub background_color: iced_core::Color,
151
152    /// The default text [`iced_core::Color`] of the application.
153    pub text_color: iced_core::Color,
154
155    /// The default icon [`iced_core::Color`] of the application.
156    pub icon_color: iced_core::Color,
157}
158
159/// The default style of a [`Program`].
160pub trait DefaultStyle {
161    /// Returns the default style of a [`Program`].
162    fn default_style(&self) -> Appearance;
163}
164
165impl DefaultStyle for iced_core::Theme {
166    fn default_style(&self) -> Appearance {
167        default(self)
168    }
169}
170
171/// The default [`Appearance`] of a [`Program`] with the built-in [`iced_core::Theme`].
172pub fn default(theme: &iced_core::Theme) -> Appearance {
173    let palette = theme.extended_palette();
174
175    Appearance {
176        background_color: palette.background.base.color,
177        text_color: palette.background.base.text,
178        icon_color: palette.background.base.text,
179    }
180}