makeover-immediate 0.34.0

The immediate-mode renderer for makeover-layout. Immediate mode is the constraint that matters, not the library: no cascade, no retained tree, one stroke per widget. Backed by egui.
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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
//! The described things that are not fields, tables or frames.
//!
//! A meter, a token, a control, a figure. `makeover-tui` has had these since its
//! own `widget` module and this crate has not, which is the gap that showed up
//! the moment anything tried to draw a whole `quasi_router::Screen` in egui:
//! the screen walk had a renderer for the containers and nothing for four of the
//! nodes inside them, so the drawing would have landed in the consumer, one copy
//! per app. That is the divergence this suite exists to end, so it lands here.
//!
//! # What "in egui" changes, and what it does not
//!
//! The semantics are `makeover-tui`'s, deliberately: a meter is a bar and a
//! reading, a badge is round and a chip is square, a control names its key where
//! the description gave one, and a figure puts the movement on the value rather
//! than on the caption. Those are description-level readings and they do not get
//! a second opinion per host.
//!
//! What differs is forced by the target rather than chosen. A terminal spends a
//! whole cell on a character and returns a `Line` for the caller to place; egui
//! paints an arbitrary rect and answers a [`Response`], so every function here
//! draws into the `Ui` it is given and hands back what the user did to it. That
//! is also why nothing here takes a `focused` flag the way `makeover-tui`'s
//! `act` does: egui owns focus, which is the rule the crate header states.

use egui::{Align, Layout, Response, RichText, Sense, Ui, Vec2};
use makeover_layout::{Act, Figure, Meter, State, Token, Tone};

use crate::Palette;

/// The sizes a widget cannot derive from the description.
///
/// Every number a caller might reasonably want different, in one place, on the
/// footing [`FrameStyle`](crate::FrameStyle) and [`FieldStyle`](crate::FieldStyle)
/// already establish: this crate owns no sizes.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct WidgetStyle {
    /// How tall a meter's bar is drawn.
    pub meter_height: f32,
    /// How wide a meter's bar runs, or `None` to take the width on offer.
    ///
    /// `None` is the honest default in immediate mode: a bar in a side panel and
    /// a bar in a wide pane are the same description, and the available width is
    /// the only thing either of them knows.
    pub meter_width: Option<f32>,
    /// The corner radius on a meter's trough and on a token.
    pub radius: u8,
    /// Inside a token, around its label.
    pub token_padding: Vec2,
    /// Between a figure's value and its caption.
    pub figure_gap: f32,
    /// How much larger a figure's value is drawn than the body text.
    ///
    /// A multiplier rather than a size, so a figure scales with whatever text
    /// style the app has set rather than pinning a point size this crate has no
    /// business choosing.
    pub figure_scale: f32,
}

impl Default for WidgetStyle {
    /// Bars at 6pt taking the width on offer, and a figure at double text size.
    fn default() -> Self {
        Self {
            meter_height: 6.0,
            meter_width: None,
            radius: 3,
            token_padding: Vec2::new(6.0, 2.0),
            figure_gap: 2.0,
            figure_scale: 2.0,
        }
    }
}

/// A proportion as a bar and a reading.
///
/// The reading is built here from the two numbers and the noun, for the reason
/// `makeover-tui` states: [`Meter::label`] carries the noun alone, so each
/// renderer picks its own sentence order rather than the description picking one
/// for all of them.
///
/// **A bar that has run over is drawn full and reads over.** `done` may exceed
/// `total` and that is the case worth drawing, per `Meter`'s own docs: the fill
/// is clamped because a rect cannot be longer than itself, and the reading is
/// not, because "9/6" is the fact the user needs. Clamping both would hide the
/// overrun entirely, which is the bug goingson's `is_over_estimate` flag exists
/// to recover from on the other side.
///
/// A zero `total` is no set rather than a complete one, so it draws empty.
pub fn meter(ui: &mut Ui, meter: &Meter<'_>, palette: &Palette, style: &WidgetStyle) -> Response {
    let width = style
        .meter_width
        .unwrap_or_else(|| ui.available_width().max(1.0));
    ui.horizontal(|ui| {
        let (rect, response) =
            ui.allocate_exact_size(Vec2::new(width, style.meter_height), Sense::hover());
        // The trough is the sunken surface rather than a tint of the tone: a
        // bar is a thing set into the page with something in it, which is what
        // `Fill::Sunken` means, and tinting the empty half would read as a
        // second, paler proportion.
        ui.painter().rect_filled(rect, style.radius, palette.sunken);
        let share = if meter.total == 0 {
            0.0
        } else {
            (f64::from(meter.done) / f64::from(meter.total)).min(1.0)
        };
        #[expect(
            clippy::cast_possible_truncation,
            reason = "a share is 0..=1 and the product is a width in points"
        )]
        let filled = (f64::from(rect.width()) * share) as f32;
        if filled > 0.0 {
            let mut fill = rect;
            fill.set_width(filled);
            ui.painter()
                .rect_filled(fill, style.radius, palette.tone(meter.tone));
        }
        let reading = match meter.label {
            Some(label) => format!("{}/{} {label}", meter.done, meter.total),
            None => format!("{}/{}", meter.done, meter.total),
        };
        ui.label(RichText::new(reading).color(palette.content_muted));
        response
    })
    .inner
}

/// A badge or a chip.
///
/// Round for a badge, square for a chip, which is `makeover-tui`'s reading and
/// `makeover-webview`'s before it. The shape carries the difference because
/// colour is already spent on the tone.
///
/// **A chip answers a click and a badge does not**, which is
/// [`Token::interactive`] and is the whole difference between the members. The
/// `Response` comes back either way, so a caller that presses a badge is
/// pressing something this function said was not interactive; the sense is what
/// makes egui agree.
///
/// `latched` is a chip that is switched on, and it fills rather than outlines. A
/// terminal has to collide latched with focus because it has one spare axis for
/// two facts; egui does not, so it does not.
///
/// A chip's removable half is not drawn, on `makeover-tui`'s reasoning: a second
/// control inside a token is a question for whoever owns the interaction rather
/// than for a drawing.
pub fn token(
    ui: &mut Ui,
    label: &str,
    kind: Token,
    tone: Tone,
    latched: bool,
    palette: &Palette,
    style: &WidgetStyle,
) -> Response {
    let painted = palette.tone(tone);
    let radius = match kind {
        // Round enough to read as a pill whatever the height turns out to be.
        Token::Badge => u8::MAX,
        Token::Chip { .. } => style.radius,
    };
    let sense = if kind.interactive() {
        Sense::click()
    } else {
        Sense::hover()
    };

    // Laid out before the rect is allocated, because a token is exactly as wide
    // as what it says plus its padding: there is no box to fit text into here,
    // the way a table cell has one.
    let ink = if latched { palette.page } else { painted };
    let galley = ui.painter().layout_no_wrap(
        label.to_owned(),
        egui::TextStyle::Body.resolve(ui.style()),
        ink,
    );
    let size = galley.size() + style.token_padding * 2.0;
    let (rect, response) = ui.allocate_exact_size(size, sense);

    if latched {
        ui.painter().rect_filled(rect, radius, painted);
    } else {
        ui.painter().rect_stroke(
            rect,
            radius,
            egui::Stroke::new(1.0, painted),
            egui::StrokeKind::Inside,
        );
    }
    ui.painter()
        .galley(rect.center() - galley.size() / 2.0, galley, ink);

    // Say what was drawn, because painting it says nothing.
    //
    // A token allocates its rect and paints the text straight onto it, so
    // nothing reached the accessibility tree at all until 2026-08-22: an
    // interactive chip was a control a mouse could press and a screen reader
    // could not find, and a badge was text nobody could read out. The filter
    // panel's twenty-four key pills were the site -- a whole way of filtering,
    // absent.
    //
    // A chip that latches says so through `selected`, which is what a screen
    // reader announces as pressed. That is `latched`'s whole meaning: the key
    // is held down.
    let role = if kind.interactive() {
        egui::WidgetType::Button
    } else {
        egui::WidgetType::Label
    };
    response.widget_info(|| {
        let mut info = egui::WidgetInfo::labeled(role, ui.is_enabled(), label);
        if kind.interactive() {
            info.selected = Some(latched);
        }
        info
    });
    response
}

/// A control.
///
/// The key the description named is drawn beside the label where there is one,
/// which is [`Act::key`] finally being read by a second renderer: it was written
/// for a terminal, and a desktop app has keys too.
///
/// **A disabled control is drawn and does not answer**, through
/// [`State::suppresses_interaction`] rather than a second reading of what
/// disabled means, and it takes [`Palette::content_muted`] because that is the
/// intent `State::Disabled` resolves to. egui is told through `add_enabled`, so
/// its own focus walk skips it: a control that is drawn and not reachable is
/// exactly what `disabled` means on every host, and here the host already has
/// the machinery.
pub fn act(ui: &mut Ui, act: &Act<'_>, palette: &Palette, _style: &WidgetStyle) -> Response {
    let disabled = act.state.is_some_and(State::suppresses_interaction);
    let label = match act.key {
        Some(key) => format!("{}  ({key})", act.label),
        None => act.label.to_owned(),
    };
    let colour = if disabled {
        palette.content_muted
    } else {
        palette.tone(act.tone)
    };
    ui.add_enabled(
        !disabled,
        egui::Button::new(RichText::new(label).color(colour)),
    )
}

/// A figure: the value, then what it counts under it.
///
/// The tone lands on the value and its change rather than on the caption, which
/// is what [`Figure::tone`] means: the figure is an ordinary fact and it is the
/// movement that reads as good or bad. `makeover-tui` says the same thing with a
/// bold span; here it is a larger one, because egui can size text and a terminal
/// cannot.
pub fn figure(
    ui: &mut Ui,
    figure: &Figure<'_>,
    palette: &Palette,
    style: &WidgetStyle,
) -> Response {
    ui.with_layout(Layout::top_down(Align::Min), |ui| {
        let value = match figure.change {
            Some(change) => format!("{} {change}", figure.value),
            None => figure.value.to_owned(),
        };
        let size = egui::TextStyle::Body.resolve(ui.style()).size * style.figure_scale;
        let shown = ui.label(
            RichText::new(value)
                .color(palette.tone(figure.tone))
                .size(size)
                .strong(),
        );
        ui.add_space(style.figure_gap);
        ui.label(RichText::new(figure.caption).color(palette.content_muted));
        shown
    })
    .inner
}

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

    /// What the accessibility tree says a widget drew.
    fn announced(
        draw: impl FnMut(&mut Ui),
    ) -> Vec<(
        egui::accesskit::Role,
        String,
        Option<egui::accesskit::Toggled>,
    )> {
        let ctx = egui::Context::default();
        ctx.enable_accesskit();
        let mut draw = draw;
        let input = || egui::RawInput {
            screen_rect: Some(egui::Rect::from_min_size(
                egui::Pos2::ZERO,
                egui::vec2(600.0, 400.0),
            )),
            ..Default::default()
        };
        let _ = ctx.run_ui(input(), &mut draw);
        let out = ctx.run_ui(input(), &mut draw);
        out.platform_output
            .accesskit_update
            .expect("accesskit is on")
            .nodes
            .iter()
            .map(|(_, node)| {
                (
                    node.role(),
                    node.label()
                        .or_else(|| node.value())
                        .unwrap_or_default()
                        .to_owned(),
                    node.toggled(),
                )
            })
            .collect()
    }

    #[test]
    fn a_chip_is_announced_as_a_control_and_says_whether_it_is_held() {
        // A token paints its own text onto its own rect, so before 2026-08-22
        // it reached the tree as nothing: pressable by a mouse and invisible to
        // everything else.
        let p = palette();
        let style = WidgetStyle::default();
        let drawn = announced(|ui| {
            token(
                ui,
                "C#",
                Token::Chip { removable: false },
                Tone::Neutral,
                true,
                &p,
                &style,
            );
        });

        let chip = drawn
            .iter()
            .find(|(role, name, _)| *role == egui::accesskit::Role::Button && name == "C#")
            .unwrap_or_else(|| panic!("the chip is not in the tree: {drawn:?}"));
        assert_eq!(
            chip.2,
            Some(egui::accesskit::Toggled::True),
            "a latched chip is held down and says so: {drawn:?}"
        );
    }

    #[test]
    fn a_badge_is_announced_as_the_text_it_is() {
        // Not a control, and not nothing either: a badge is a word on the
        // screen and painting it is not the same as saying it.
        let p = palette();
        let style = WidgetStyle::default();
        let drawn = announced(|ui| {
            token(ui, "wav", Token::Badge, Tone::Neutral, false, &p, &style);
        });

        assert!(
            drawn
                .iter()
                .any(|(role, name, _)| *role == egui::accesskit::Role::Label && name == "wav"),
            "{drawn:?}"
        );
        assert!(
            !drawn
                .iter()
                .any(|(role, _, _)| *role == egui::accesskit::Role::Button),
            "a badge answers nothing and must not claim to: {drawn:?}"
        );
    }

    fn palette() -> Palette {
        use egui::Color32;
        Palette {
            page: Color32::from_rgb(1, 1, 1),
            raised: Color32::from_rgb(2, 2, 2),
            overlay: Color32::from_rgb(3, 3, 3),
            well: Color32::from_rgb(4, 4, 4),
            sunken: Color32::from_rgb(5, 5, 5),
            bevel_light: Color32::from_rgb(6, 6, 6),
            bevel_dark: Color32::from_rgb(7, 7, 7),
            elevation: Color32::from_black_alpha(40),
            content: Color32::from_rgb(20, 20, 20),
            content_secondary: Color32::from_rgb(120, 120, 120),
            content_muted: Color32::from_rgb(21, 21, 21),
            action: Color32::from_rgb(22, 22, 22),
            danger: Color32::from_rgb(23, 23, 23),
            success: Color32::from_rgb(24, 24, 24),
            warning: Color32::from_rgb(25, 25, 25),
            info: Color32::from_rgb(26, 26, 26),
        }
    }

    #[test]
    fn every_tone_resolves_and_no_two_share_a_colour() {
        // The reason the three status intents arrived together: a resolver
        // missing one has to invent a colour for it.
        let p = palette();
        let all = [
            p.tone(Tone::Neutral),
            p.tone(Tone::Info),
            p.tone(Tone::Success),
            p.tone(Tone::Warning),
            p.tone(Tone::Danger),
        ];
        for (i, a) in all.iter().enumerate() {
            for b in &all[i + 1..] {
                assert_ne!(a, b, "two tones resolved to one colour");
            }
        }
        assert_eq!(p.tone(Tone::Neutral), p.content, "neutral is ordinary text");
    }

    #[test]
    fn a_meter_draws_and_an_overrun_does_not_panic() {
        // `done` may exceed `total`, which is the case Meter's own docs call
        // the one worth drawing. The fill clamps; the reading does not.
        let p = palette();
        let style = WidgetStyle::default();
        egui::__run_test_ui(|ui| {
            meter(ui, &Meter::new(3, 6), &p, &style);
            meter(ui, &Meter::new(9, 6), &p, &style);
            // No set, rather than a complete one.
            meter(ui, &Meter::new(0, 0), &p, &style);
            // The overflow `makeover-layout` pins on its own side.
            meter(ui, &Meter::new(u32::MAX, u32::MAX), &p, &style);
        });
    }

    #[test]
    fn a_chip_answers_a_click_and_a_badge_does_not() {
        // `Token::interactive` is the whole difference between the members, and
        // the sense is what makes egui agree with it.
        let p = palette();
        let style = WidgetStyle::default();
        egui::__run_test_ui(|ui| {
            let badge = token(ui, "beta", Token::Badge, Tone::Info, false, &p, &style);
            assert!(!badge.sense.senses_click(), "a badge answers no click");

            let chip = token(
                ui,
                "drums",
                Token::Chip { removable: false },
                Tone::Neutral,
                false,
                &p,
                &style,
            );
            assert!(chip.sense.senses_click(), "a chip answers a click");
        });
    }

    #[test]
    fn a_disabled_control_is_drawn_and_does_not_answer() {
        // Present, visible, and not answering. Through
        // `State::suppresses_interaction` rather than a second reading here.
        let p = palette();
        let style = WidgetStyle::default();
        egui::__run_test_ui(|ui| {
            let live = act(ui, &Act::new("Save"), &p, &style);
            assert!(live.enabled());

            let gone = act(ui, &Act::new("Save").state(State::Disabled), &p, &style);
            assert!(!gone.enabled(), "a disabled control still answers");
        });
    }

    #[test]
    fn a_control_shows_the_key_the_description_named() {
        // `Act::key` was written for a terminal before there was one. A desktop
        // app has keys too, so this is its second reader.
        let p = palette();
        let style = WidgetStyle::default();
        egui::__run_test_ui(|ui| {
            act(ui, &Act::new("New").key("n"), &p, &style);
            act(ui, &Act::new("New"), &p, &style);
        });
    }

    #[test]
    fn a_figure_draws_its_movement_beside_its_value() {
        let p = palette();
        let style = WidgetStyle::default();
        egui::__run_test_ui(|ui| {
            figure(ui, &Figure::new("17", "Current streak"), &p, &style);
            figure(
                ui,
                &Figure::new("17", "Current streak")
                    .change("+3")
                    .tone(Tone::Success),
                &p,
                &style,
            );
        });
    }
}