icedtea 0.5.0

Native desktop widgets and chrome for iced
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
//! Native desktop widgets and chrome for [iced](https://iced.rs/).
//!
//! icedtea starts a themed window, paints controls from semantic tokens,
//! and routes one [`action::Action`] table through menus, toolbars,
//! shortcuts, and the command palette. Constructors return iced
//! [`Element`]s and emit your messages.
//!
//! # First compose
//!
//! One `Action` feeds the toolbar. The same message is Save:
//!
//! ```
//! use icedtea::action::{Action, ActionTable};
//! use icedtea::i18n::Direction;
//! use icedtea::pattern;
//! use icedtea::shortcut::Shortcut;
//! use icedtea::theme;
//! let tok = theme::named("dark").tokens;
//! let save = ();
//! let mut table = ActionTable::new();
//! table.insert(
//!     Action::new("file.save", "Save", save)
//!         .with_shortcut(Shortcut::parse("ctrl+s").unwrap()),
//! );
//! let chrome: icedtea::Element<'_, ()> =
//!     pattern::toolbar(table.iter(), tok, Direction::Ltr);
//! let _ = chrome;
//! assert_eq!(table.invoke("file.save"), Some(save));
//! ```
//!
//! # Boot
//!
//! [`run!`] opens that window. [`daemon!`] is the same `Prepared`
//! settings on `iced::daemon`: no window until [`Prepared::open`],
//! and the process stays up when the last window closes.
//! The same program is
//! [`examples/hello.rs`](https://github.com/indynull/icedtea/blob/master/examples/hello.rs).
//!
//! ```ignore
//! icedtea::run!(
//!     icedtea::Boot::new("Notes", "dev.example.hello"),
//!     Hello::new,
//!     Hello::update,
//!     Hello::view,
//!     Hello::theme,
//!     Hello::subscription
//! )
//! ```
//!
//! # Keys
//!
//! Subscribe with [`key::listen`]. [`key::handle`] matches the table
//! after an open modal and after focused text:
//!
//! ```
//! use icedtea::action::{Action, ActionTable};
//! use icedtea::key::{handle, KeyContext};
//! use icedtea::shortcut::Shortcut;
//! let save = ();
//! let mut table = ActionTable::new();
//! table.insert(
//!     Action::new("file.save", "Save", save)
//!         .with_shortcut(Shortcut::parse("ctrl+s").unwrap()),
//! );
//! let ev = iced::keyboard::Event::KeyPressed {
//!     key: iced::keyboard::Key::Character("s".into()),
//!     modified_key: iced::keyboard::Key::Character("s".into()),
//!     physical_key: iced::keyboard::key::Physical::Unidentified(
//!         iced::keyboard::key::NativeCode::Unidentified,
//!     ),
//!     location: iced::keyboard::Location::Standard,
//!     modifiers: icedtea::shortcut::primary(),
//!     text: None,
//!     repeat: false,
//! };
//! assert_eq!(handle(KeyContext::default(), &table, &ev), Some(save));
//! ```
//!
//! # Tokens (Material Design 3)
//!
//! [`theme::named`] picks a colorway seeded from M3 schemes
//! ([`m3::scheme_light`] / [`m3::scheme_dark`]). Control paint paths
//! read roles through [`Tokens::scheme`](theme::Tokens::scheme). Short
//! fields (`primary`, `text`, …) are aliases of those roles.
//! [`theme::mix`] builds washes:
//!
//! ```
//! use icedtea::theme;
//! let tok = theme::named("dark").tokens;
//! let s = tok.scheme();
//! assert_eq!(theme::selection_fill(tok), s.secondary_container);
//! assert_eq!(s.on_surface, tok.text);
//! ```
//!
//! # A widget
//!
//! The hello editor is [`widget::textarea`]. The application owns the
//! buffer:
//!
//! ```
//! use icedtea::a11y::{A11y, Role};
//! use icedtea::theme;
//! use icedtea::widget;
//! #[derive(Clone)]
//! enum Message {
//!     Edit(icedtea::iced::widget::text_editor::Action),
//! }
//! let tok = theme::named("dark").tokens;
//! let content = icedtea::iced::widget::text_editor::Content::new();
//! let editor: icedtea::Element<'_, Message> = widget::textarea(
//!     &content,
//!     Message::Edit,
//!     tok,
//!     icedtea::layout::FILL,
//!     A11y::new("notes", Role::TextBox),
//! );
//! let _ = editor;
//! ```
//!
//! # A pattern
//!
//! [`pattern::toolbar`] paints the same `file.save` row:
//!
//! ```
//! use icedtea::action::{Action, ActionTable};
//! use icedtea::i18n::Direction;
//! use icedtea::pattern;
//! use icedtea::theme;
//! let tok = theme::named("dark").tokens;
//! let save = ();
//! let mut table = ActionTable::new();
//! table.insert(Action::new("file.save", "Save", save));
//! let bar: icedtea::Element<'_, ()> =
//!     pattern::toolbar(table.iter(), tok, Direction::Ltr);
//! let _ = bar;
//! ```
//!
//! # Scope
//!
//! icedtea is chrome, actions, layout, and theme for iced desktop
//! applications. Constructors return [`Element`]s and emit the
//! application's messages.
//!
//! ## Non-goals
//!
//! - A new renderer or a fork of iced. icedtea tracks iced releases.
//! - A stylesheet or markup language. Authors write Rust.
//! - Mobile, web, or embedded targets.
//! - A visual form designer.
//! - An in-process web view, print pipeline, or multimedia stack.
//! - Multiple-document-interface window mosaics.
//! - Binding the look to one desktop shell. Themes may follow system
//!   light/dark; chrome stays icedtea’s.
//! - Domain widgets for a specific product (session timelines, containers,
//!   editors' language services). Applications own those.
//! - Document undo/redo. Applications own history.
//! - Sample documents and bitmaps as library API.
//! - A second collection widget for variable-height cards. Extend list.
//! - Library-owned parse caches or live-update daemons.
//! - System-wide hotkeys, host focus steal, or baking another toolkit’s
//!   theme files.
//!
//! # Modules
//!
//! | Start here | Role |
//! | --- | --- |
//! | [`app`] / [`run!`] | Boot the window |
//! | [`action`] / [`key`] / [`shortcut`] | Commands and chords |
//! | [`theme`] / [`variant`] / [`typo`] | Color, variant, type |
//! | [`widget`] / [`collection`] | Controls, lists, tables |
//! | [`select`] | Select-and-copy contract for content text |
//! | [`layout`] / [`pattern`] / [`window`] | Recipes and chrome |
//!
//! The [guide](https://indynull.github.io/icedtea/) walks composition
//! and lists every public constructor.
//! [crates.io](https://crates.io/crates/icedtea) ·
//! [source](https://github.com/indynull/icedtea).

#![cfg_attr(docsrs, feature(doc_cfg))]

pub mod a11y;
pub mod action;
pub mod app;
pub mod catalog;
pub mod chrome;
pub mod clipboard;
pub mod collection;
pub mod density;
pub mod dialog;
pub mod dnd;
pub mod field;
pub mod focus;
pub mod fuzzy;
pub mod host;
mod host_canvas;
mod host_chrome;
mod host_font;
pub mod i18n;
pub mod icon;
pub mod key;
pub mod layout;
pub mod m3;
mod menubar;
pub mod nav;
pub mod palette;
pub mod pattern;
pub mod persist;
mod scroll;
pub mod select;
pub mod shortcut;
pub mod style;
pub mod theme;
pub mod toast;
pub mod typo;
pub mod variant;
pub mod widget;
pub mod window;
pub mod workspace;

pub use app::{bootstrap, Boot, Prepared};
pub use host::{copy_text, native_dialog, paste_text};
pub use iced::{self, Element, Task};

/// Boot theme and window settings, then start iced's application builder.
///
/// ```ignore
/// icedtea::run!(
///     icedtea::Boot::new("demo", "dev.example.demo"),
///     Demo::new,
///     Demo::update,
///     Demo::view,
///     Demo::theme,
/// );
/// ```
#[macro_export]
macro_rules! run {
    ($boot:expr, $new:expr, $update:expr, $view:expr, $theme:expr) => {
        $crate::run!($boot, $new, $update, $view, $theme, |_| {
            $crate::iced::Subscription::none()
        })
    };
    ($boot:expr, $new:expr, $update:expr, $view:expr, $theme:expr, $sub:expr) => {{
        let __prep = $crate::bootstrap(&$boot);
        let __title = __prep.title.clone();
        let __direction = __prep.direction();
        debug_assert!(
            __direction == $crate::i18n::Direction::Ltr
                || __direction == $crate::i18n::Direction::Rtl
        );
        $crate::typo::install_platform_faces();
        $crate::iced::application($new, $update, $view)
            .title($crate::app::WindowTitle(__title))
            .theme($theme)
            .subscription($sub)
            .settings(__prep.iced_settings)
            .window(__prep.window)
            .default_font($crate::typo::UI)
            .run()
    }};
}

/// Boot theme and iced settings, then start `iced::daemon`.
///
/// No window until the application returns [`Prepared::open`].
/// Closing every window leaves the process up; quit with
/// [`iced::exit`]. `view` takes `window::Id`. Theme is the same
/// `fn(&State) -> Theme` as [`run!`].
///
/// ```ignore
/// icedtea::daemon!(
///     icedtea::Boot::new("HUD", "dev.example.hud").overlay().size(780.0, 560.0),
///     Hud::new,
///     Hud::update,
///     Hud::view,
///     Hud::theme,
/// );
/// ```
#[macro_export]
macro_rules! daemon {
    ($boot:expr, $new:expr, $update:expr, $view:expr, $theme:expr) => {
        $crate::daemon!($boot, $new, $update, $view, $theme, |_| {
            $crate::iced::Subscription::none()
        })
    };
    ($boot:expr, $new:expr, $update:expr, $view:expr, $theme:expr, $sub:expr) => {{
        let __prep = $crate::bootstrap(&$boot);
        let __title = __prep.title.clone();
        let __direction = __prep.direction();
        debug_assert!(
            __direction == $crate::i18n::Direction::Ltr
                || __direction == $crate::i18n::Direction::Rtl
        );
        $crate::typo::install_platform_faces();
        $crate::iced::daemon($new, $update, $view)
            .title(move |_, _| __title.clone())
            .theme(move |state, _| $theme(state))
            .subscription($sub)
            .settings(__prep.iced_settings)
            .default_font($crate::typo::UI)
            .run()
    }};
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn dark_selection_is_scheme_secondary_container() {
        let t = theme::named("dark").tokens;
        assert_eq!(theme::selection_fill(t), t.selection);
        assert_eq!(t.selection, crate::m3::scheme_dark().secondary_container);
    }

    #[test]
    fn docs_describe_icedtea() {
        let readme = include_str!("../README.md");
        let root = include_str!("lib.rs");
        let tour = root.split("#![cfg_attr").next().unwrap_or(root);
        let arch = include_str!("../book/src/architecture.md");
        let first = include_str!("../book/src/first-window.md");
        let hello = include_str!("../examples/hello.rs");
        let tour_msg = "crate-root rustdoc is a tour, not the GitHub README";
        if tour.contains("include_str!(\"../README.md\")") {
            panic!("{tour_msg}");
        }
        assert!(tour.contains("Action"));
        assert!(tour.contains("toolbar"));
        assert!(tour.contains("Boot"));
        assert!(tour.contains("Tokens"));
        assert!(tour.contains("pattern"));
        assert!(tour.contains("file.save"));
        assert!(readme.contains("icedtea::run!"));
        assert!(readme.contains("example hello"));
        assert!(readme.contains("examples/hello.rs"));
        assert!(!readme.contains("struct Hello"));
        assert!(!readme.contains("count.inc"));
        assert!(hello.contains("file.save"));
        assert!(hello.contains("pattern::toolbar"));
        assert!(hello.contains("Action::new"));
        assert!(!hello.contains("count.inc"));
        assert!(!hello.contains("n: i32"));
        assert!(first.contains("examples/hello.rs"));
        let install = include_str!("../book/src/install.md");
        for src in [readme, install] {
            let at = src
                .find("icedtea =")
                .expect("install story names the crate");
            let ver_msg = "first icedtea line is the crates.io version";
            if !src[at..].starts_with("icedtea = \"0.5\"") {
                panic!("{ver_msg}");
            }
        }
        assert!(arch.contains("Action"));
        assert!(arch.contains("Tokens"));
        assert!(arch.contains("Boot"));
        assert!(arch.contains("pattern"));
        assert!(!arch.contains("Rust is all you need"));
        assert!(!arch.contains("book.iced.rs/philosophy"));
        assert!(!readme.contains("The Elm Architecture"));
        for (name, src) in [
            ("widget", include_str!("widget.rs")),
            ("pattern", include_str!("pattern.rs")),
            ("a11y", include_str!("a11y.rs")),
            ("action", include_str!("action.rs")),
            ("key", include_str!("key.rs")),
            ("layout", include_str!("layout/mod.rs")),
        ] {
            let head = src.split("pub ").next().unwrap_or(src);
            let a11y = head.contains("A11y");
            let action = head.contains("Action");
            let sash = head.contains("listen_sash");
            let handle = head.contains("handle");
            let recipe_msg = format!("{name} module docs must name the intended recipe");
            if !(a11y || action || sash || handle) {
                panic!("{recipe_msg}");
            }
        }
    }

    #[test]
    fn run_macro_starts_iced_application_builder() {
        let src = include_str!("lib.rs");
        assert!(src.contains("$crate::iced::application($new, $update, $view)"));
        assert!(src.contains("$crate::iced::daemon($new, $update, $view)"));
        let daemon_src = src.split("macro_rules! daemon").nth(1).unwrap();
        let daemon_msg = "daemon leaves window open to Prepared::open";
        if daemon_src.contains(".window(") {
            panic!("{daemon_msg}");
        }
        let prep = bootstrap(&Boot::new("tea", "dev.icedtea.tea"));
        assert!(!prep.title.is_empty());
        assert!(prep.iced_settings.fonts.is_empty());
        let overlay = bootstrap(&Boot::new("hud", "dev.hud").overlay().size(780.0, 560.0));
        assert!(!overlay.window.decorations);
        assert!(!overlay.window.exit_on_close_request);
        let (a, _) = overlay.open();
        let (b, _) = overlay.open();
        assert_ne!(a, b);
        let (desk, _) = overlay.open_desktop();
        assert_ne!(desk, a);
    }
}