crcbl-ui 0.2.1

Crucible immediate-mode GUI
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
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
//! Tooltips: when one shows and hides, where it is drawn, and that the pointer
//! passes through it — each held to the rule in `tooltip.rs` it exists for.

use std::time::Duration;

use glam::Vec2;

use super::*;
use crate::draw_list::{DrawCommand, DrawList};
use crate::style::{Declaration, Sides};

/// The viewport every page is laid out in.
const VIEW: Vec2 = Vec2::new(400.0, 200.0);
/// The tooltip's background, set by the page's own sheet over
/// `default.css`'s, to find it in a draw list by.
const TIP: [f32; 4] = [1.0, 0.0, 0.0, 1.0];
/// The background of the block under the anchor, which the tooltip covers.
const UNDER: [f32; 4] = [0.0, 0.0, 1.0, 1.0];
/// The background of the pop-up the anchor opens, which the tooltip covers.
const MENU: [f32; 4] = [0.0, 1.0, 0.0, 1.0];
/// What the anchor's tooltip says.
const HINT: &str = "Does the thing (F5)";
/// A frame much shorter than the delay.
const FRAME: Duration = Duration::from_millis(16);

const fn px(value: f32) -> LengthAuto {
    LengthAuto::Px(value)
}

/// What a frame is driven by beside the pointer and navigation.
#[derive(Clone, Copy, Default)]
struct Options {
    /// How long the frame was.
    dt: Duration,
    /// Leave the anchor, and its tooltip call, unbuilt.
    gone: bool,
    /// Open a pop-up hanging from the anchor.
    menu: bool,
    /// The wheel's movement, handed over before the build.
    wheel: Vec2,
}

/// What a page built.
struct Page {
    /// A button at the top-left, whose tooltip is [`HINT`].
    anchor: Option<Response>,
    /// A button filling the width under the anchor, with no tooltip.
    under: Response,
    /// A button at the bottom-right corner, with a tooltip too wide to fit
    /// beside it.
    corner: Response,
    /// The anchor's tooltip, if it was built.
    tip: Option<Response>,
    /// The corner's tooltip, if it was built.
    corner_tip: Option<Response>,
}

/// One frame: a `VIEW`-sized column holding the anchor, the block under it,
/// and the corner button, laid out.
fn page(ui: &mut Ui, pointer: PointerInput, nav: NavInput, options: Options) -> Page {
    ui.begin_frame_with(pointer, nav);
    ui.set_text_input(TextInput {
        dt: options.dt,
        ..TextInput::default()
    });
    if options.wheel != Vec2::ZERO {
        ui.scroll_wheel(options.wheel);
    }
    let mut anchor = None;
    let mut tip = None;
    let mut under = None;
    let mut corner = None;
    let mut corner_tip = None;
    let page = [
        Declaration::Width(px(VIEW.x)),
        Declaration::Height(px(VIEW.y)),
        Declaration::FlexDirection(FlexDirection::Column),
    ];
    ui.block("#page", &page, |ui| {
        let size = [
            Declaration::Width(px(60.0)),
            Declaration::Height(px(20.0)),
            Declaration::FlexShrink(0.0),
        ];
        if !options.gone {
            let built = ui.block_with("#anchor", &size, Behavior::BUTTON, |_| {});
            tip = ui.tooltip(&built, HINT);
            if options.menu {
                ui.open_popup(built.key);
            }
            let menu = [
                Declaration::Width(px(120.0)),
                Declaration::Height(px(40.0)),
                Declaration::Background(MENU),
            ];
            ui.popup(built.key, "#menu", &menu, |_| {});
            anchor = Some(built);
        }
        let below = [
            Declaration::Width(px(VIEW.x)),
            Declaration::Height(px(40.0)),
            Declaration::FlexShrink(0.0),
            Declaration::Background(UNDER),
        ];
        under = Some(ui.block_with("#under", &below, Behavior::BUTTON, |_| {}));
        let low = [
            Declaration::Position(Position::Absolute),
            Declaration::Inset(Sides::Left, px(VIEW.x - 60.0)),
            Declaration::Inset(Sides::Top, px(VIEW.y - 20.0)),
            Declaration::Width(px(60.0)),
            Declaration::Height(px(20.0)),
        ];
        let built = ui.block_with("#corner", &low, Behavior::BUTTON, |_| {});
        corner_tip = ui.tooltip(&built, "A tooltip wider than the button");
        corner = Some(built);
    });
    ui.layout(
        Vec2::ZERO,
        AvailableSpace::definite(VIEW),
        &FontAtlas::built_in(),
    );
    Page {
        anchor,
        under: under.expect("the page builds the block under the anchor"),
        corner: corner.expect("the page builds the corner button"),
        tip,
        corner_tip,
    }
}

/// A tree whose sheet colours the tooltip [`TIP`], laid out once with the
/// pointer nowhere so that the next frame has rectangles to hover.
fn laid_out() -> (Ui, Page) {
    let mut ui = Ui::new();
    ui.add_stylesheet("tip.css", "tooltip { background: #ff0000 }");
    let page = page(&mut ui, idle(), NavInput::default(), Options::default());
    (ui, page)
}

fn idle() -> PointerInput {
    PointerInput::hovering(Vec2::splat(-1.0))
}

fn centre(ui: &Ui, key: NodeKey) -> Vec2 {
    let (min, max) = ui.rect(key).expect("laid out");
    (min + max) * 0.5
}

/// A frame `dt` long with the pointer resting at `at`.
fn rest(ui: &mut Ui, at: Vec2, dt: Duration) -> Page {
    let options = Options {
        dt,
        ..Options::default()
    };
    page(ui, PointerInput::hovering(at), NavInput::default(), options)
}

/// Rests the pointer at `at` until the delay has passed: the first frame
/// starts the count and the second ends it. Asserts that the one tooltip
/// shown then is the widget's under `at`.
fn rest_until_shown(ui: &mut Ui, at: Vec2) -> Page {
    rest(ui, at, FRAME);
    let page = rest(ui, at, TOOLTIP_DELAY);
    let over = |key| {
        ui.rect(key)
            .is_some_and(|(min, max)| at.cmpge(min).all() && at.cmplt(max).all())
    };
    let over_anchor = page.anchor.is_some_and(|anchor| over(anchor.key));
    assert_eq!(
        (page.tip.is_some(), page.corner_tip.is_some()),
        (over_anchor, over(page.corner.key)),
        "the tooltip shown after the delay is not the hovered widget's"
    );
    page
}

/// The index of the first filled rect of `color` in `list`.
fn rect_of(list: &DrawList, color: [f32; 4]) -> usize {
    list.commands()
        .iter()
        .position(|command| matches!(command, DrawCommand::Rect { color: at, .. } if *at == color))
        .unwrap_or_else(|| panic!("no rect of {color:?} was drawn"))
}

/// **A tooltip shows once the pointer has rested for the delay, and not a
/// moment before**: the count starts at the first frame that found the anchor
/// hovered, and a millisecond short of the delay builds nothing.
#[test]
fn a_tooltip_shows_after_the_hover_delay_and_not_before() {
    let (mut ui, built) = laid_out();
    let at = centre(&ui, built.anchor.expect("built").key);
    assert!(
        rest(&mut ui, at, TOOLTIP_DELAY).tip.is_none(),
        "shown on the first frame"
    );
    let short = TOOLTIP_DELAY - Duration::from_millis(1);
    assert!(
        rest(&mut ui, at, short).tip.is_none(),
        "shown before the delay"
    );
    let shown = rest(&mut ui, at, Duration::from_millis(1));
    let tip = shown.tip.expect("shown at the delay");
    let span = ui.child_keys(tip.key)[0];
    assert_eq!(ui.text(span), Some(HINT));
    assert_eq!(tip.key, Ui::tooltip_key(shown.anchor.expect("built").key));
}

/// **[`Ui::set_tooltip_delay`] replaces the delay.**
#[test]
fn the_delay_can_be_replaced() {
    let (mut ui, built) = laid_out();
    let at = centre(&ui, built.anchor.expect("built").key);
    ui.set_tooltip_delay(Duration::from_millis(40));
    rest(&mut ui, at, FRAME);
    assert!(
        rest(&mut ui, at, FRAME).tip.is_none(),
        "shown before the new delay"
    );
    assert!(
        rest(&mut ui, at, FRAME * 2).tip.is_some(),
        "not shown at the new delay"
    );
}

/// **A tooltip hangs below its anchor and is drawn over everything**: over
/// the block built after the anchor, and over a pop-up open from it.
#[test]
fn a_tooltip_hangs_below_its_anchor_drawn_over_the_tree_and_every_popup() {
    let (mut ui, built) = laid_out();
    let anchor = built.anchor.expect("built").key;
    let at = centre(&ui, anchor);
    let menu = Options {
        menu: true,
        ..Options::default()
    };
    page(
        &mut ui,
        PointerInput::hovering(at),
        NavInput::default(),
        menu,
    );
    let hover = |dt| Options { dt, ..menu };
    page(
        &mut ui,
        PointerInput::hovering(at),
        NavInput::default(),
        hover(FRAME),
    );
    let shown = page(
        &mut ui,
        PointerInput::hovering(at),
        NavInput::default(),
        hover(TOOLTIP_DELAY),
    );
    let tip = shown.tip.expect("shown after the delay");
    let (anchor_min, anchor_max) = ui.rect(anchor).expect("laid out");
    let (tip_min, tip_max) = ui.rect(tip.key).expect("laid out");
    assert_eq!(tip_min, Vec2::new(anchor_min.x, anchor_max.y));
    assert!(tip_max.x < VIEW.x, "the tooltip is not as wide as its text");

    let mut list = DrawList::new();
    ui.emit(&mut list);
    let tip = rect_of(&list, TIP);
    assert!(
        tip > rect_of(&list, UNDER),
        "drawn under the block built after it"
    );
    assert!(tip > rect_of(&list, MENU), "drawn under the open pop-up");
}

/// **A tooltip that would run past the bottom of the viewport flips above its
/// anchor**, and is shifted left to stay inside it.
#[test]
fn a_tooltip_at_the_viewports_corner_flips_above_and_shifts_inside() {
    let (mut ui, built) = laid_out();
    let corner = built.corner.key;
    let at = centre(&ui, corner);
    let shown = rest_until_shown(&mut ui, at);
    let tip = shown.corner_tip.expect("shown after the delay");
    let (corner_min, _) = ui.rect(corner).expect("laid out");
    let (tip_min, tip_max) = ui.rect(tip.key).expect("laid out");
    assert_eq!(tip_max.y, corner_min.y, "not flipped above the anchor");
    assert_eq!(tip_max.x, VIEW.x, "not shifted inside the viewport");
    assert!(
        tip_min.x < corner_min.x,
        "the fixture's tooltip is no wider than its anchor"
    );
}

/// **The pointer passes through a tooltip**: a click on the block under it
/// reaches the block, and the press hides the tooltip.
#[test]
fn a_click_on_what_lies_under_a_tooltip_reaches_it() {
    let (mut ui, built) = laid_out();
    let at = centre(&ui, built.anchor.expect("built").key);
    let shown = rest_until_shown(&mut ui, at);
    let (min, max) = ui.rect(shown.tip.expect("shown").key).expect("laid out");
    let (under_min, under_max) = ui.rect(shown.under.key).expect("laid out");
    let at = Vec2::new(min.x + 2.0, (min.y + max.y) * 0.5);
    assert!(
        at.cmpge(under_min).all() && at.cmplt(under_max).all() && at.cmplt(max).all(),
        "the fixture's tooltip does not cover the block under it"
    );
    let press = PointerInput {
        pos: at,
        down: true,
        released: false,
        secondary_pressed: false,
    };
    let pressed = page(&mut ui, press, NavInput::default(), Options::default());
    assert!(
        pressed.under.pressed,
        "the press did not reach the block under"
    );
    let release = PointerInput {
        pos: at,
        down: false,
        released: true,
        secondary_pressed: false,
    };
    let released = page(&mut ui, release, NavInput::default(), Options::default());
    assert!(
        released.under.clicked,
        "the click did not reach the block under"
    );
}

/// **A press hides the tooltip until the pointer leaves its anchor**: resting
/// on after the click does not bring it back; leaving and resting again does.
#[test]
fn a_press_hides_the_tooltip_until_the_pointer_leaves_and_comes_back() {
    let (mut ui, built) = laid_out();
    let at = centre(&ui, built.anchor.expect("built").key);
    rest_until_shown(&mut ui, at);
    let press = PointerInput {
        pos: at,
        down: true,
        released: false,
        secondary_pressed: false,
    };
    let pressed = page(&mut ui, press, NavInput::default(), Options::default());
    assert!(pressed.tip.is_none(), "the press left the tooltip up");
    let release = PointerInput {
        pos: at,
        down: false,
        released: true,
        secondary_pressed: false,
    };
    page(&mut ui, release, NavInput::default(), Options::default());
    assert!(
        rest(&mut ui, at, TOOLTIP_DELAY * 2).tip.is_none(),
        "back after the click"
    );
    rest(&mut ui, Vec2::splat(-1.0), FRAME);
    rest_until_shown(&mut ui, at);
}

/// **A secondary press hides the tooltip too, and so does `ui_menu`**: each
/// opens a context menu the tooltip would sit over.
#[test]
fn a_secondary_press_or_ui_menu_hides_the_tooltip() {
    let (mut ui, built) = laid_out();
    let at = centre(&ui, built.anchor.expect("built").key);
    rest_until_shown(&mut ui, at);
    let secondary = PointerInput {
        secondary_pressed: true,
        ..PointerInput::hovering(at)
    };
    let pressed = page(&mut ui, secondary, NavInput::default(), Options::default());
    assert!(pressed.tip.is_none(), "the secondary press left it up");

    let (mut ui, built) = laid_out();
    let anchor = built.anchor.expect("built").key;
    page(&mut ui, idle(), NavInput::NEXT, Options::default());
    assert_eq!(ui.focused(), Some(anchor));
    let held = Options {
        dt: TOOLTIP_DELAY,
        ..Options::default()
    };
    page(&mut ui, idle(), NavInput::NAVIGATION, held);
    let shown = page(&mut ui, idle(), NavInput::NAVIGATION, held);
    assert!(
        shown.tip.is_some(),
        "the focused anchor's tooltip never showed"
    );
    let menu = page(&mut ui, idle(), NavInput::MENU, Options::default());
    assert!(menu.tip.is_none(), "ui_menu left it up");
}

/// **The pointer leaving the anchor hides its tooltip.**
#[test]
fn leaving_the_anchor_hides_the_tooltip() {
    let (mut ui, built) = laid_out();
    let at = centre(&ui, built.anchor.expect("built").key);
    rest_until_shown(&mut ui, at);
    let away = centre(&ui, built.under.key);
    assert!(
        rest(&mut ui, away, FRAME).tip.is_none(),
        "still up once the pointer left"
    );
}

/// **A wheel hides the tooltip**, though nothing under the pointer scrolls.
#[test]
fn a_wheel_hides_the_tooltip() {
    let (mut ui, built) = laid_out();
    let at = centre(&ui, built.anchor.expect("built").key);
    rest_until_shown(&mut ui, at);
    let wheel = Options {
        dt: FRAME,
        wheel: Vec2::new(0.0, 10.0),
        ..Options::default()
    };
    let wheeled = page(
        &mut ui,
        PointerInput::hovering(at),
        NavInput::default(),
        wheel,
    );
    assert!(wheeled.tip.is_none(), "the wheel left the tooltip up");
}

/// **A tooltip goes with its widget**, and a widget built again starts the
/// delay again.
#[test]
fn a_tooltip_goes_with_its_widget_and_starts_over_when_it_returns() {
    let (mut ui, built) = laid_out();
    let anchor = built.anchor.expect("built").key;
    let at = centre(&ui, anchor);
    rest_until_shown(&mut ui, at);
    let gone = Options {
        dt: FRAME,
        gone: true,
        ..Options::default()
    };
    let without = page(
        &mut ui,
        PointerInput::hovering(at),
        NavInput::default(),
        gone,
    );
    assert!(without.tip.is_none());
    assert_eq!(
        ui.rect(Ui::tooltip_key(anchor)),
        None,
        "the tooltip outlived its widget"
    );
    rest(&mut ui, at, FRAME);
    assert!(
        rest(&mut ui, at, FRAME).tip.is_none(),
        "the delay did not start over"
    );
}

/// **Focus by navigation shows the focused widget's tooltip after the same
/// delay, and moving focus off it hides it.**
#[test]
fn navigation_focus_shows_the_tooltip_after_the_delay_and_blur_hides_it() {
    let (mut ui, _) = laid_out();
    let nav = |dt| Options {
        dt,
        ..Options::default()
    };
    let landed = page(&mut ui, idle(), NavInput::NEXT, nav(FRAME));
    assert!(
        landed.anchor.expect("built").focused,
        "focus did not land on the anchor"
    );
    assert!(landed.tip.is_none(), "shown on the frame focus landed");
    let short = TOOLTIP_DELAY - FRAME;
    assert!(
        page(&mut ui, idle(), NavInput::NAVIGATION, nav(short))
            .tip
            .is_none()
    );
    let shown = page(&mut ui, idle(), NavInput::NAVIGATION, nav(FRAME));
    assert!(
        shown.tip.is_some(),
        "not shown once focus had been held for the delay"
    );
    let moved = page(&mut ui, idle(), NavInput::NEXT, nav(FRAME));
    assert!(moved.under.focused, "focus did not move off the anchor");
    assert!(moved.tip.is_none(), "still up once focus moved off");
}

/// **The next widget's tooltip follows sooner**: moving straight from a shown
/// tooltip to another widget waits the reshow delay, not the whole one; after
/// a rest away from every widget longer than the delay, the whole delay
/// again.
#[test]
fn the_next_tooltip_waits_only_the_reshow_delay() {
    let (mut ui, built) = laid_out();
    let anchor = centre(&ui, built.anchor.expect("built").key);
    let corner = centre(&ui, built.corner.key);
    rest_until_shown(&mut ui, anchor);
    let reshow = TOOLTIP_DELAY / TOOLTIP_RESHOW_DIVISOR;
    assert!(
        rest(&mut ui, corner, FRAME).corner_tip.is_none(),
        "shown at once"
    );
    let short = reshow - Duration::from_millis(1);
    assert!(
        rest(&mut ui, corner, short).corner_tip.is_none(),
        "shown before the reshow delay"
    );
    assert!(
        rest(&mut ui, corner, Duration::from_millis(1))
            .corner_tip
            .is_some(),
        "not shown at the reshow delay"
    );

    rest(&mut ui, Vec2::splat(-1.0), TOOLTIP_DELAY + FRAME);
    rest(&mut ui, anchor, FRAME);
    assert!(
        rest(&mut ui, anchor, reshow).tip.is_none(),
        "the reshow delay outlived a rest away"
    );
    assert!(
        rest(&mut ui, anchor, TOOLTIP_DELAY - reshow).tip.is_some(),
        "not shown at the whole delay"
    );
}

/// **A shown tooltip goes after the auto-pop delay and stays gone** while
/// the pointer rests; leaving and coming back shows it again.
#[test]
fn a_tooltip_goes_after_the_auto_pop_delay() {
    let (mut ui, built) = laid_out();
    let anchor = centre(&ui, built.anchor.expect("built").key);
    rest_until_shown(&mut ui, anchor);
    let autopop = TOOLTIP_DELAY * TOOLTIP_AUTOPOP_FACTOR;
    assert!(
        rest(&mut ui, anchor, autopop - Duration::from_millis(1))
            .tip
            .is_some(),
        "gone before the auto-pop delay"
    );
    assert!(
        rest(&mut ui, anchor, Duration::from_millis(1))
            .tip
            .is_none(),
        "still up at the auto-pop delay"
    );
    assert!(
        rest(&mut ui, anchor, FRAME).tip.is_none(),
        "back while the pointer rests"
    );
    rest(&mut ui, Vec2::splat(-1.0), FRAME);
    assert!(
        rest_until_shown(&mut ui, anchor).tip.is_some(),
        "not shown again on coming back"
    );
}