crisol-ui-kit 1.9.0

Crisol UI kit: themes, style tokens, icon caches, fonts, localization, base components, and widgets.
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
//! Transient notifications stacked in a window corner, like `sonner` in React.

use egui::{Area, Color32, Context, Id, Order, Pos2, pos2};

use crate::Icons;
use crate::theme;

mod view;

/// The meaning of a toast, which picks its icon and accent color.
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "snake_case"))]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub enum ToastKind {
    /// Neutral information (accent color, `info` icon).
    #[default]
    Info,
    /// A finished action (success color, check icon).
    Success,
    /// Something needs attention (warning color, triangle icon).
    Warning,
    /// A failure (danger color, cross icon).
    Error,
    /// Work in progress; a spinner and no timeout until it is updated.
    Loading,
}

impl ToastKind {
    pub(super) fn icon(self) -> &'static str {
        match self {
            Self::Info => "lucide-info",
            Self::Success => "lucide-circle-check",
            Self::Warning => "lucide-triangle-alert",
            Self::Error => "lucide-circle-x",
            Self::Loading => "lucide-loader-circle",
        }
    }

    pub(super) fn color(self, theme: &theme::Theme) -> Color32 {
        match self {
            Self::Info => theme.link,
            Self::Success => theme.success,
            Self::Warning => theme.warning,
            Self::Error => theme.danger,
            Self::Loading => theme.text_muted,
        }
    }
}

#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "snake_case"))]
/// The corner (or edge center) the stack grows from.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub enum ToastPosition {
    /// Top left corner.
    TopLeft,
    /// Top edge, centered.
    TopCenter,
    /// Top right corner.
    TopRight,
    /// Bottom left corner.
    BottomLeft,
    /// Bottom edge, centered.
    BottomCenter,
    /// Bottom right corner, above the status bar.
    #[default]
    BottomRight,
}

impl ToastPosition {
    fn is_top(self) -> bool {
        matches!(self, Self::TopLeft | Self::TopCenter | Self::TopRight)
    }
}

/// The identity of a toast, for [`Toasts::update`] and [`Toasts::dismiss`].
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(transparent))]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct ToastId(pub(super) u64);

/// What happened to a toast this frame.
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "snake_case"))]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum ToastEvent {
    /// The action button was clicked; the toast closes.
    Action(ToastId),
    /// The close button was clicked.
    Closed(ToastId),
    /// The timer ran out.
    Expired(ToastId),
}

/// One notification. Build it, then pass it to [`Toasts::add`].
#[derive(Clone, Debug)]
pub struct Toast {
    pub(super) kind: ToastKind,
    pub(super) title: String,
    pub(super) description: Option<String>,
    pub(super) action: Option<String>,
    pub(super) duration: Option<f32>,
    pub(super) closable: bool,
}

impl Toast {
    /// A toast with a title and the default 4 s timeout (none for `Loading`).
    pub fn new(kind: ToastKind, title: impl Into<String>) -> Self {
        Self {
            kind,
            title: title.into(),
            description: None,
            action: None,
            duration: (kind != ToastKind::Loading).then_some(DEFAULT_DURATION),
            closable: true,
        }
    }

    /// A second, muted line under the title. It wraps.
    pub fn description(mut self, description: impl Into<String>) -> Self {
        self.description = Some(description.into());
        self
    }

    /// A small button on the right that reports [`ToastEvent::Action`].
    pub fn action(mut self, label: impl Into<String>) -> Self {
        self.action = Some(label.into());
        self
    }

    /// Seconds before the toast closes on its own. Hovering any toast pauses the timer.
    pub fn duration(mut self, seconds: f32) -> Self {
        self.duration = Some(seconds.max(0.5));
        self
    }

    /// Keep the toast until it is closed or updated.
    pub fn persistent(mut self) -> Self {
        self.duration = None;
        self
    }

    /// Show the × button (on by default).
    pub fn closable(mut self, closable: bool) -> Self {
        self.closable = closable;
        self
    }
}

const DEFAULT_DURATION: f32 = 4.0;
const ENTER: f32 = 0.18;
const EXIT: f32 = 0.14;
const GAP: f32 = 8.0;
const MARGIN: f32 = 16.0;

struct Entry {
    id: ToastId,
    toast: Toast,
    remaining: Option<f32>,
    shown_at: Option<f64>,
    closing_at: Option<f64>,
    dismissed: bool,
    height: f32,
}

/// The toast queue and its renderer.
///
/// Keep one `Toasts` in the application state, push toasts from anywhere, and call
/// [`Toasts::show`] once per frame after the panels. Nothing repaints while the stack is
/// empty; while toasts are visible, the frame only wakes up for the next timeout or
/// animation step.
///
/// ```
/// # use crisol_ui_kit::components::{Toast, ToastKind, Toasts};
/// # let ctx = egui::Context::default();
/// # let mut icons = crisol_ui_kit::Icons::new();
/// let mut toasts = Toasts::new();
/// toasts.success("Saved");
/// let id = toasts.loading("Exporting…");
/// toasts.update(id, Toast::new(ToastKind::Success, "Exported").description("report.pdf"));
/// # let mut output = ctx.run_ui(egui::RawInput::default(), |ui| {
/// for event in toasts.show(ui.ctx(), &mut icons) {
///     let _ = event;
/// }
/// # });
/// # output.textures_delta.clear();
/// ```
pub struct Toasts {
    entries: Vec<Entry>,
    next: u64,
    position: ToastPosition,
    width: f32,
    max_visible: usize,
    offset: Pos2,
    last_tick: Option<f64>,
}

impl Default for Toasts {
    fn default() -> Self {
        Self::new()
    }
}

impl Toasts {
    /// An empty stack in the bottom-right corner, 356 pt wide, at most 4 visible.
    pub fn new() -> Self {
        Self {
            entries: Vec::new(),
            next: 0,
            position: ToastPosition::default(),
            width: 356.0,
            max_visible: 4,
            offset: Pos2::ZERO,
            last_tick: None,
        }
    }

    /// Where the stack grows from.
    pub fn position(mut self, position: ToastPosition) -> Self {
        self.position = position;
        self
    }

    /// Change the corner at run time.
    pub fn set_position(&mut self, position: ToastPosition) {
        self.position = position;
    }

    /// Toast width in points; it shrinks on narrow windows.
    pub fn width(mut self, width: f32) -> Self {
        self.width = width.max(200.0);
        self
    }

    /// How many toasts are visible at once; older ones wait their turn.
    pub fn max_visible(mut self, count: usize) -> Self {
        self.max_visible = count.max(1);
        self
    }

    /// Extra inset from the anchor edges, such as the title or status bar height.
    pub fn offset(mut self, x: f32, y: f32) -> Self {
        self.offset = pos2(x, y);
        self
    }

    /// Queue a toast and return its id.
    pub fn add(&mut self, toast: Toast) -> ToastId {
        self.next += 1;
        let id = ToastId(self.next);
        self.entries.push(Entry {
            id,
            remaining: toast.duration,
            toast,
            shown_at: None,
            closing_at: None,
            dismissed: false,
            height: 64.0,
        });
        id
    }

    /// Queue an informational toast.
    pub fn info(&mut self, title: impl Into<String>) -> ToastId {
        self.add(Toast::new(ToastKind::Info, title))
    }

    /// Queue a success toast.
    pub fn success(&mut self, title: impl Into<String>) -> ToastId {
        self.add(Toast::new(ToastKind::Success, title))
    }

    /// Queue a warning toast.
    pub fn warning(&mut self, title: impl Into<String>) -> ToastId {
        self.add(Toast::new(ToastKind::Warning, title))
    }

    /// Queue an error toast.
    pub fn error(&mut self, title: impl Into<String>) -> ToastId {
        self.add(Toast::new(ToastKind::Error, title))
    }

    /// Queue a persistent loading toast; replace it later with [`Self::update`].
    pub fn loading(&mut self, title: impl Into<String>) -> ToastId {
        self.add(Toast::new(ToastKind::Loading, title))
    }

    /// Replace a toast in place (for example, loading → success) and restart its timer.
    /// Returns `false` when the toast is already gone.
    pub fn update(&mut self, id: ToastId, toast: Toast) -> bool {
        let Some(entry) = self.entries.iter_mut().find(|entry| entry.id == id) else {
            return false;
        };
        entry.remaining = toast.duration;
        entry.toast = toast;
        entry.closing_at = None;
        entry.dismissed = false;
        true
    }

    /// Close a toast with its exit animation.
    pub fn dismiss(&mut self, id: ToastId) {
        if let Some(entry) = self.entries.iter_mut().find(|entry| entry.id == id) {
            entry.dismissed = true;
        }
    }

    /// Close every toast.
    pub fn dismiss_all(&mut self) {
        for entry in &mut self.entries {
            entry.dismissed = true;
        }
    }

    /// Whether no toast is queued or visible.
    pub fn is_empty(&self) -> bool {
        self.entries.is_empty()
    }

    /// Queued and visible toasts.
    pub fn len(&self) -> usize {
        self.entries.len()
    }

    /// Paint the stack above every panel and return what happened this frame.
    pub fn show(&mut self, ctx: &Context, icons: &mut Icons) -> Vec<ToastEvent> {
        let mut events = Vec::new();
        if self.entries.is_empty() {
            self.last_tick = None;
            return events;
        }
        let tokens = theme::current(ctx);
        let motion = theme::motion_enabled(ctx);
        let now = ctx.input(|input| input.time);
        let dt = self.last_tick.map_or(0.0, |last| (now - last).max(0.0) as f32);
        self.last_tick = Some(now);
        let screen = ctx.content_rect();
        let width = self.width.min(screen.width() - 2.0 * MARGIN).max(160.0);
        let paused = self.hovered(ctx);

        let visible = self.entries.len().min(self.max_visible);
        let mut cursor = 0.0;
        let mut wake: Option<f32> = None;
        // Newest closest to the anchor edge.
        for slot in 0..visible {
            let index = self.entries.len() - visible + (visible - 1 - slot);
            let entry = &mut self.entries[index];
            let shown_at = *entry.shown_at.get_or_insert(now);

            if entry.closing_at.is_none() {
                if entry.dismissed {
                    entry.closing_at = Some(now);
                } else if let Some(remaining) = entry.remaining.as_mut() {
                    // A toast that just appeared starts its timer this frame.
                    if !paused && shown_at < now {
                        *remaining -= dt;
                    }
                    if *remaining <= 0.0 {
                        entry.closing_at = Some(now);
                        events.push(ToastEvent::Expired(entry.id));
                    } else if !paused {
                        wake = Some(wake.map_or(*remaining, |wake| wake.min(*remaining)));
                    }
                }
            }

            let enter = if motion {
                ((now - shown_at) as f32 / ENTER).clamp(0.0, 1.0)
            } else {
                1.0
            };
            let exit = match entry.closing_at {
                Some(at) if motion => 1.0 - ((now - at) as f32 / EXIT).clamp(0.0, 1.0),
                Some(_) => 0.0,
                None => 1.0,
            };
            if enter < 1.0 || entry.closing_at.is_some() {
                ctx.request_repaint();
            }
            let visibility = view::ease_out(enter) * exit;
            let slide = (1.0 - view::ease_out(enter)) * 16.0;

            let x = match self.position {
                ToastPosition::TopLeft | ToastPosition::BottomLeft => screen.left() + MARGIN + self.offset.x,
                ToastPosition::TopCenter | ToastPosition::BottomCenter => screen.center().x - width * 0.5,
                ToastPosition::TopRight | ToastPosition::BottomRight => screen.right() - MARGIN - width - self.offset.x,
            };
            let target_y = if self.position.is_top() {
                screen.top() + MARGIN + self.offset.y + cursor - slide
            } else {
                screen.bottom() - MARGIN - self.offset.y - cursor - entry.height + slide
            };
            let area_id = Id::unique(("workbench_toast", entry.id.0));
            let y = if motion {
                ctx.animate_value_with_time(area_id.with("y"), target_y, 0.2)
            } else {
                target_y
            };
            cursor += (entry.height + GAP) * exit;

            let id = entry.id;
            let toast = &entry.toast;
            let area = Area::new(area_id)
                .order(Order::Foreground)
                .fixed_pos(pos2(x, y))
                .interactable(true)
                .show(ctx, |ui| {
                    ui.multiply_opacity(visibility);
                    view::paint(ui, icons, toast, &tokens, width)
                });
            entry.height = area.response.rect.height();
            match area.inner {
                Some(ToastEvent::Action(_)) => {
                    events.push(ToastEvent::Action(id));
                    entry.closing_at.get_or_insert(now);
                }
                Some(ToastEvent::Closed(_)) => {
                    events.push(ToastEvent::Closed(id));
                    entry.closing_at.get_or_insert(now);
                }
                _ => {}
            }
        }

        let before = self.entries.len();
        self.entries.retain(|entry| match entry.closing_at {
            Some(at) => motion && ((now - at) as f32) < EXIT,
            None => true,
        });
        let removed = before > self.entries.len();
        if let Some(wake) = wake {
            ctx.request_repaint_after(std::time::Duration::from_secs_f32(wake.max(0.016)));
        }
        if removed && !self.entries.is_empty() {
            // A queued toast takes the freed slot, and the stack closes the gap.
            ctx.request_repaint();
        }
        if self.entries.iter().any(|entry| entry.toast.kind == ToastKind::Loading) {
            ctx.request_repaint_after(std::time::Duration::from_millis(33));
        }
        events
    }

    fn hovered(&self, ctx: &Context) -> bool {
        let Some(pointer) = ctx.pointer_hover_pos() else {
            return false;
        };
        self.entries.iter().any(|entry| {
            ctx.memory(|memory| {
                memory
                    .area_rect(Id::unique(("workbench_toast", entry.id.0)))
                    .is_some_and(|rect| rect.contains(pointer))
            })
        })
    }
}

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

    #[test]
    fn loading_toasts_do_not_expire_and_update_in_place() {
        let mut toasts = Toasts::new();
        let id = toasts.loading("Working");
        assert_eq!(toasts.entries[0].remaining, None);
        assert!(toasts.update(id, Toast::new(ToastKind::Success, "Done")));
        assert_eq!(toasts.entries[0].remaining, Some(DEFAULT_DURATION));
        assert_eq!(toasts.len(), 1);
    }

    #[test]
    fn dismiss_starts_the_exit() {
        let mut toasts = Toasts::new();
        let id = toasts.info("Hello");
        toasts.dismiss(id);
        assert!(toasts.entries[0].dismissed);
        assert!(!toasts.update(ToastId(99), Toast::new(ToastKind::Info, "x")));
    }
}