crcbl-ui 0.2.0

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
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
//! Immediate-mode UI widgets: label, button, and helpers.
//!
//! Widgets are stateless on their own — each frame you call their `render`
//! method with the current state (hovered, clicked) and they push draw
//! commands into a [`DrawList`].

use crate::draw_list::DrawList;
use crate::image::NineSliceImage;
use crate::text::{FontAtlas, GLYPH_HEIGHT, LINE_HEIGHT};
use glam::Vec2;

/// The font size at which the built-in atlas renders 1:1.
///
/// Measurement and layout both divide by this. They used to disagree — widgets
/// divided by `14.0` while [`DrawList::to_triangles`] divided by
/// [`GLYPH_HEIGHT`] — which made every bound and hit rect ~7% smaller than the
/// glyphs it was supposed to contain.
///
/// [`DrawList::to_triangles`]: crate::draw_list::DrawList::to_triangles
pub const NATURAL_FONT_SIZE: f32 = GLYPH_HEIGHT as f32;

// ---------------------------------------------------------------------------
// Style
// ---------------------------------------------------------------------------

/// Colour palette for a widget theme.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Style {
    /// Normal text colour.
    pub text: [f32; 4],
    /// Normal background colour.
    pub bg: [f32; 4],
    /// Background colour when hovered.
    pub bg_hover: [f32; 4],
    /// Background colour when pressed/active.
    pub bg_active: [f32; 4],
    /// Outline/border colour.
    pub border: [f32; 4],
}

impl Default for Style {
    fn default() -> Self {
        Self {
            text: [1.0, 1.0, 1.0, 1.0],        // white
            bg: [0.15, 0.15, 0.15, 0.9],       // dark grey
            bg_hover: [0.25, 0.25, 0.3, 0.9],  // slightly lighter
            bg_active: [0.35, 0.35, 0.4, 0.9], // lighter still
            border: [0.4, 0.4, 0.4, 1.0],      // grey
        }
    }
}

// ---------------------------------------------------------------------------
// Label
// ---------------------------------------------------------------------------

/// A static text label.
///
/// Renders a single line of text at the given position using the built-in
/// font atlas. Optionally draws a background rectangle behind the text.
#[derive(Debug, Clone)]
pub struct Label {
    /// Text content.
    pub text: String,
    /// Font size in pixels.
    pub font_size: f32,
    /// If true, draw a background rect behind the text.
    pub show_bg: bool,
}

impl Label {
    /// Create a new label.
    #[must_use]
    pub fn new(text: impl Into<String>) -> Self {
        Self {
            text: text.into(),
            font_size: 14.0,
            show_bg: false,
        }
    }

    /// Set the font size.
    #[must_use]
    pub fn with_size(mut self, size: f32) -> Self {
        self.font_size = size;
        self
    }

    /// Enable or disable the background rectangle.
    #[must_use]
    pub fn with_bg(mut self, show: bool) -> Self {
        self.show_bg = show;
        self
    }

    /// The layout scale this label's glyphs are drawn at.
    fn scale(&self) -> f32 {
        self.font_size / NATURAL_FONT_SIZE
    }

    /// Render this label into a `DrawList`.
    ///
    /// `pos` is the top-left corner of the label, and is the same anchor the
    /// background rect and [`Label::width`]/[`Label::height`] describe.
    pub fn render(&self, dl: &mut DrawList, pos: Vec2, atlas: &FontAtlas, style: &Style) {
        if self.show_bg {
            let size = self.size(atlas);
            dl.rect(pos, pos + size, style.bg);
        }

        dl.text(pos, &self.text, style.text, self.font_size);
    }

    /// Measure the rendered width.
    #[must_use]
    pub fn width(&self, atlas: &FontAtlas) -> f32 {
        atlas.text_width(&self.text, self.scale()) + 4.0
    }

    /// Measure the rendered height, which grows with the number of lines.
    #[must_use]
    pub fn height_for(&self, atlas: &FontAtlas) -> f32 {
        atlas.line_count(&self.text) as f32 * LINE_HEIGHT * self.scale()
    }

    /// Measure the rendered height of a single line.
    #[must_use]
    pub fn height(&self) -> f32 {
        LINE_HEIGHT * self.scale()
    }

    /// The label's full extent, the rect [`Label::render`] draws its background
    /// as.
    #[must_use]
    pub fn size(&self, atlas: &FontAtlas) -> Vec2 {
        Vec2::new(self.width(atlas), self.height_for(atlas))
    }
}

// ---------------------------------------------------------------------------
// Button
// ---------------------------------------------------------------------------

/// Button interaction state (computed externally from mouse input).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ButtonState {
    /// Default idle appearance.
    Idle,
    /// Mouse is hovering over the button.
    Hovered,
    /// Mouse button is pressed down on the button.
    Pressed,
}

// ---------------------------------------------------------------------------
// SkinInsets
// ---------------------------------------------------------------------------

/// The fixed corner insets of a nine-slice button skin, in **screen pixels**.
///
/// # Why a `Button` carries the insets and not the art
///
/// A skinned button needs two things: art to draw, and the sizes that art
/// refuses to stretch. Only the second is a *layout* fact, and the layout is
/// what a [`Button`] is: it measures, positions and hit-tests with no picture
/// in the room. The art is a [`ButtonSkin`] — three registered images — and
/// [`ButtonSkin::insets`] hands back exactly this type, read off the art itself,
/// so the two cannot drift.
///
/// # Screen space, so `top` is the visually-upper edge
///
/// Every rectangle in this crate is Y-**down** with the origin at the
/// framebuffer's top-left, and these insets follow it: `top` is the inset at the
/// smaller Y. That agrees with an image's own rows — its top row is its first —
/// so [`DrawList::nine_slice`] pairs `top` with the image's top band and nothing
/// flips.
#[derive(Debug, Clone, Copy, PartialEq, Default)]
pub struct SkinInsets {
    /// Fixed width of the left column, in pixels.
    pub left: f32,
    /// Fixed width of the right column, in pixels.
    pub right: f32,
    /// Fixed height of the top row, in pixels.
    pub top: f32,
    /// Fixed height of the bottom row, in pixels.
    pub bottom: f32,
}

impl SkinInsets {
    /// No fixed bands at all — a skin that stretches everywhere.
    pub const NONE: Self = Self {
        left: 0.0,
        right: 0.0,
        top: 0.0,
        bottom: 0.0,
    };

    /// Insets given per side.
    #[must_use]
    pub const fn new(left: f32, right: f32, top: f32, bottom: f32) -> Self {
        Self {
            left,
            right,
            top,
            bottom,
        }
    }

    /// The same inset on all four sides.
    #[must_use]
    pub const fn uniform(inset: f32) -> Self {
        Self::new(inset, inset, inset, inset)
    }

    /// `(left + right, top + bottom)` — the smallest box that draws all four
    /// corners at their natural size.
    ///
    /// The float twin of `crcbl_sprite::NineSlice::minimum_size`, and the floor
    /// under [`Button::minimum_size`] before the label is even considered.
    #[must_use]
    pub fn minimum_size(&self) -> Vec2 {
        Vec2::new(self.left + self.right, self.top + self.bottom)
    }

    /// The top-left corner's size, as an offset from a rect's `min`.
    #[must_use]
    pub fn min_corner(&self) -> Vec2 {
        Vec2::new(self.left, self.top)
    }

    /// The bottom-right corner's size, as an offset back from a rect's `max`.
    #[must_use]
    pub fn max_corner(&self) -> Vec2 {
        Vec2::new(self.right, self.bottom)
    }
}

// ---------------------------------------------------------------------------
// ButtonSkin
// ---------------------------------------------------------------------------

/// The art a skinned button draws: one nine-sliced image per [`ButtonState`].
///
/// ```text
///    idle              hovered           pressed
///   ┌──┬────────┬──┐  ┌──┬────────┬──┐  ┌──┬────────┬──┐
///   │  │        │  │  │  │        │  │  │  │        │  │
///   ├──┤  Play  ├──┤  ├──┤  Play  ├──┤  ├──┤  Play  ├──┤
///   │  │        │  │  │  │        │  │  │  │        │  │
///   └──┴────────┴──┘  └──┴────────┴──┘  └──┴────────┴──┘
///    three pictures — pressing swaps art, it does not tint
/// ```
///
/// Three pictures rather than one picture and three tints, because that is what
/// the feature is for. A tint can only darken or lighten uniformly; separate
/// frames let a pressed button have a sunken bevel, a different highlight, or a
/// shadow that moved — none of which is a multiply.
///
/// [`draw`](Self::draw) emits the frame for a state into a [`DrawList`] as
/// textured quads, so the frame and the label on it are drawn by one pass in
/// the order they were pushed: frame first, label over it.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct ButtonSkin {
    /// The frame drawn at rest.
    pub idle: NineSliceImage,
    /// The frame drawn under the cursor or the keyboard's selection.
    pub hovered: NineSliceImage,
    /// The frame drawn while the button holds the press.
    pub pressed: NineSliceImage,
}

impl ButtonSkin {
    /// One frame for every state — a skin that does not react.
    #[must_use]
    pub const fn uniform(frame: NineSliceImage) -> Self {
        Self {
            idle: frame,
            hovered: frame,
            pressed: frame,
        }
    }

    /// The frame `state` draws.
    #[must_use]
    pub const fn frame(&self, state: ButtonState) -> &NineSliceImage {
        match state {
            ButtonState::Idle => &self.idle,
            ButtonState::Hovered => &self.hovered,
            ButtonState::Pressed => &self.pressed,
        }
    }

    /// The insets to hand [`Button::with_skin`], in **texels**.
    ///
    /// Read off the **idle** frame, and off its clamped insets rather than its
    /// declared ones, so the layout agrees with the geometry that will actually
    /// be drawn. Idle specifically, and not "whichever state is current": a
    /// button whose minimum size changed with the cursor would resize as the
    /// mouse crossed it. [`insets_agree`](Self::insets_agree) is there for a
    /// skin whose frames might not.
    #[must_use]
    pub fn insets(&self) -> SkinInsets {
        self.idle.clamped_insets()
    }

    /// Whether all three frames have the same clamped insets.
    #[must_use]
    pub fn insets_agree(&self) -> bool {
        let idle = self.idle.clamped_insets();
        self.hovered.clamped_insets() == idle && self.pressed.clamped_insets() == idle
    }

    /// Pushes `state`'s frame stretched to `min..max`, at `scale` screen pixels
    /// per texel of its fixed bands — see [`DrawList::nine_slice`].
    pub fn draw(&self, dl: &mut DrawList, state: ButtonState, min: Vec2, max: Vec2, scale: f32) {
        dl.nine_slice(min, max, self.frame(state), scale, [1.0; 4]);
    }
}

// ---------------------------------------------------------------------------
// PointerInput / UiState
// ---------------------------------------------------------------------------

/// The pointer state for one frame.
///
/// Bundled rather than passed as three loose arguments because every clickable
/// widget needs all three, and a bare `bool, bool` pair at a call site is the
/// kind of thing that gets swapped.
#[derive(Debug, Clone, Copy, Default, PartialEq)]
pub struct PointerInput {
    /// Cursor position in screen-space pixels.
    pub pos: Vec2,
    /// Whether the primary pointer button is currently held.
    pub down: bool,
    /// Whether the primary pointer button came up this frame.
    pub released: bool,
    /// Whether the secondary pointer button went down this frame: what opens a
    /// context menu ([`Ui::context_menu`](crate::tree::Ui::context_menu)). An
    /// edge, not a level — nothing captures or drags with it — so a frame
    /// carries it once per press.
    pub secondary_pressed: bool,
}

impl PointerInput {
    /// Pointer at `pos` with no button activity.
    #[must_use]
    pub fn hovering(pos: Vec2) -> Self {
        Self {
            pos,
            down: false,
            released: false,
            secondary_pressed: false,
        }
    }
}

/// Identifies a widget across frames.
///
/// Immediate-mode widgets are rebuilt every frame, so press capture needs
/// *something* stable to latch onto. Callers pick the number; the only
/// requirement is that it names the same widget next frame and no other widget
/// this frame.
pub type WidgetId = u64;

/// The one piece of retained state an immediate-mode UI cannot do without:
/// which widget captured the current press.
///
/// Without it a click is credited to whatever the cursor happens to be over on
/// release, so pressing button A, dragging onto B and releasing fires B. With
/// it, the press latches an `active` widget and only that widget can be
/// clicked — and only if the cursor is still over it when the button comes up.
///
/// Create one, keep it alive across frames, and drive every clickable widget
/// through [`UiState::interact`].
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct UiState {
    active: Option<WidgetId>,
}

impl UiState {
    /// Create state with nothing captured.
    #[must_use]
    pub fn new() -> Self {
        Self { active: None }
    }

    /// The widget that captured the current press, if any.
    #[must_use]
    pub fn active(&self) -> Option<WidgetId> {
        self.active
    }

    /// Drop any capture — for a UI that was torn down mid-press.
    pub fn clear(&mut self) {
        self.active = None;
    }

    /// Resolve one widget's visual state and whether it was clicked.
    ///
    /// - `hovered`: the cursor is inside the widget this frame.
    /// - `pointer_down`: the pointer button is currently held.
    /// - `pointer_released`: the pointer button came up this frame.
    ///
    /// Returns `(state, clicked)`. `clicked` is true only when *this* widget
    /// captured the press **and** still has the cursor at release.
    pub fn interact(
        &mut self,
        id: WidgetId,
        hovered: bool,
        pointer_down: bool,
        pointer_released: bool,
    ) -> (ButtonState, bool) {
        // Latch the capture on the frame the press starts over this widget.
        if pointer_down && hovered && self.active.is_none() {
            self.active = Some(id);
        }
        let captured = self.active == Some(id);
        let clicked = pointer_released && captured && hovered;
        if pointer_released && captured {
            self.active = None;
        }

        let state = if captured {
            ButtonState::Pressed
        } else if hovered && self.active.is_none() {
            ButtonState::Hovered
        } else {
            // Another widget owns the press — this one stays idle even under
            // the cursor, so a drag-off does not light up its neighbour.
            ButtonState::Idle
        };
        (state, clicked)
    }
}

/// A clickable button with text.
///
/// Button is stateless — you provide the [`ButtonState`] each frame based
/// on your input system, normally via [`UiState::interact`] or the
/// [`Button::interact`] wrapper, and the button draws itself.
///
/// # Skinned and unskinned
///
/// With no [`skin`](Button::skin) the button paints itself: a filled rect and a
/// one-pixel outline in [`Style`]'s colours, then the label. That is the whole
/// widget, and [`Button::render`] emits all three commands.
///
/// With a skin, the background is a [`ButtonSkin`]'s nine-sliced frame, so
/// pressing the button swaps art rather than changing a tint, and stretching it
/// leaves the corners alone. [`Button::render`] then emits only the label — see
/// its docs for what the caller owes.
#[derive(Debug, Clone)]
pub struct Button {
    /// Button label.
    pub text: String,
    /// Font size.
    pub font_size: f32,
    /// Padding inside the button (horizontal, vertical).
    ///
    /// Measured **inside the skin's corners**, not from the button's edge: a
    /// skinned button's content box is its rect inset by the corners and then by
    /// this. With no skin the corners are zero and it is the plain inset it has
    /// always been.
    pub padding: Vec2,
    /// The skin's fixed corner sizes, or `None` for the flat painted button.
    ///
    /// Set it from [`ButtonSkin::insets`] rather than by hand, so the layout and
    /// the art it is laying out around come from one place.
    pub skin: Option<SkinInsets>,
    /// The size the caller wants the button drawn at, before
    /// [`Button::minimum_size`] is applied. `None` shrinks to fit the label.
    ///
    /// A *request*, not a guarantee — see [`Button::size`] for what happens when
    /// it is too small.
    pub fixed_size: Option<Vec2>,
}

impl Button {
    /// Create a new button with the given text.
    #[must_use]
    pub fn new(text: impl Into<String>) -> Self {
        Self {
            text: text.into(),
            font_size: 14.0,
            padding: Vec2::new(8.0, 4.0),
            skin: None,
            fixed_size: None,
        }
    }

    /// Set the font size.
    #[must_use]
    pub fn with_size(mut self, size: f32) -> Self {
        self.font_size = size;
        self
    }

    /// Lay this button out around a nine-slice skin with the given corners.
    #[must_use]
    pub fn with_skin(mut self, insets: SkinInsets) -> Self {
        self.skin = Some(insets);
        self
    }

    /// Ask for a particular size, in pixels.
    ///
    /// Clamped up to [`Button::minimum_size`] — a button is never drawn smaller
    /// than its own corners and label.
    #[must_use]
    pub fn with_fixed_size(mut self, size: Vec2) -> Self {
        self.fixed_size = Some(size);
        self
    }

    /// The layout scale this button's label is drawn at.
    fn scale(&self) -> f32 {
        self.font_size / NATURAL_FONT_SIZE
    }

    /// The skin's corners, or zeroes when there is no skin.
    fn insets(&self) -> SkinInsets {
        self.skin.unwrap_or(SkinInsets::NONE)
    }

    /// The label's own extent, with no padding and no corners around it.
    #[must_use]
    pub fn label_size(&self, atlas: &FontAtlas) -> Vec2 {
        let scale = self.scale();
        Vec2::new(
            atlas.text_width(&self.text, scale),
            atlas.line_count(&self.text) as f32 * LINE_HEIGHT * scale,
        )
    }

    /// The smallest size this button can be drawn at: **its corners plus its
    /// label**, with the padding between them.
    ///
    /// Both terms are load-bearing and neither is enough alone. The corners are
    /// the sizes the nine-slice refuses to stretch, so a box narrower than
    /// `left + right` has nowhere to put them; the label is the content, so a box
    /// that fits the corners and not the text draws the text over them.
    ///
    /// The padding is added rather than absorbed: it is the gap the caller asked
    /// for *between* the frame and the words, and a minimum that let the two
    /// touch would silently ignore it at exactly the size where it is most
    /// visible.
    ///
    /// With no skin the corners are zero and this is `label + 2 × padding` — the
    /// rect an unskinned button has always been.
    #[must_use]
    pub fn minimum_size(&self, atlas: &FontAtlas) -> Vec2 {
        self.insets().minimum_size() + self.label_size(atlas) + self.padding * 2.0
    }

    /// The size this button is actually drawn at.
    ///
    /// [`Button::fixed_size`] when it is at least [`Button::minimum_size`] on
    /// that axis, and the minimum otherwise — **each axis clamps on its own**, so
    /// a button asked for a generous width and a mean height keeps the width.
    ///
    /// # Why clamp rather than shrink
    ///
    /// [`DrawList::nine_slice`] handles a target below its minimum by squashing
    /// the corners in proportion, which is right for what it was written for: a
    /// pipe closing to nothing should close continuously rather than vanish or
    /// spill. A button is the other case. Its content is *text*, which does not
    /// shrink with the frame — a squashed button would draw a full-size label
    /// across smudged corners, which is both of the failures this slice exists to
    /// remove, at once. Growing to the minimum is visible in the layout, where a
    /// caller can see it and fix it, rather than in the pixels.
    #[must_use]
    pub fn size(&self, atlas: &FontAtlas) -> Vec2 {
        let minimum = self.minimum_size(atlas);
        self.fixed_size.map_or(minimum, |asked| asked.max(minimum))
    }

    /// The bounding rectangle for this button at the given position.
    #[must_use]
    pub fn rect(&self, pos: Vec2, atlas: &FontAtlas) -> (Vec2, Vec2) {
        (pos, pos + self.size(atlas))
    }

    /// The box the label is centred in: the button inset by its corners and then
    /// by its padding.
    ///
    /// Centring here rather than in the button's full rect is what keeps an
    /// **asymmetric** skin honest. A frame with a 4-pixel left cap and a 20-pixel
    /// right one has its content box off-centre, and a label centred in the outer
    /// rect would sit under the fat cap at the minimum size. With no skin the two
    /// boxes share a centre and this is the ordinary thing.
    #[must_use]
    pub fn content_rect(&self, pos: Vec2, atlas: &FontAtlas) -> (Vec2, Vec2) {
        let insets = self.insets();
        let (min, max) = self.rect(pos, atlas);
        (
            min + insets.min_corner() + self.padding,
            max - insets.max_corner() - self.padding,
        )
    }

    /// Where [`Button::render`] anchors the label — the top-left of its em box.
    ///
    /// Centred in [`Button::content_rect`] on both axes, so it stays centred as
    /// the button is resized.
    #[must_use]
    pub fn label_pos(&self, pos: Vec2, atlas: &FontAtlas) -> Vec2 {
        let (min, max) = self.content_rect(pos, atlas);
        min + (max - min - self.label_size(atlas)) * 0.5
    }

    /// Whether the given mouse position is inside the button area.
    #[must_use]
    pub fn hit_test(&self, pos: Vec2, mouse: Vec2, atlas: &FontAtlas) -> bool {
        let (min, max) = self.rect(pos, atlas);
        mouse.x >= min.x && mouse.x <= max.x && mouse.y >= min.y && mouse.y <= max.y
    }

    /// Hit-test this button and resolve its state through `ui`'s press capture.
    ///
    /// Returns `(state, clicked)` — feed `state` straight to
    /// [`Button::render`]. `clicked` is true only when the press *started* on
    /// this button and the cursor is still on it at release.
    pub fn interact(
        &self,
        pos: Vec2,
        atlas: &FontAtlas,
        ui: &mut UiState,
        id: WidgetId,
        pointer: PointerInput,
    ) -> (ButtonState, bool) {
        let hovered = self.hit_test(pos, pointer.pos, atlas);
        ui.interact(id, hovered, pointer.down, pointer.released)
    }

    /// Render the button into a `DrawList` at `state`'s appearance.
    ///
    /// Click detection is [`Button::interact`]'s job — a renderer that also
    /// decided whether it had been clicked could only do so from the state it
    /// was handed, which is exactly the "credited on release to whoever is
    /// under the cursor" bug.
    ///
    /// # A skinned button emits only its label
    ///
    /// The layout knows the skin's corners and nothing else, so the frame is
    /// the caller's to push: [`ButtonSkin::draw`] for the same `state` and the
    /// same rect, **before** this call — the draw list composites in the order
    /// commands went in, and a frame pushed after the label paints over it.
    pub fn render(
        &self,
        dl: &mut DrawList,
        pos: Vec2,
        atlas: &FontAtlas,
        style: &Style,
        state: ButtonState,
    ) {
        if self.skin.is_none() {
            let (min, max) = self.rect(pos, atlas);
            let bg = match state {
                ButtonState::Pressed => style.bg_active,
                ButtonState::Hovered => style.bg_hover,
                ButtonState::Idle => style.bg,
            };

            dl.rect(min, max, bg);
            dl.rect_outline(min, max, 1.0, style.border);
        }

        dl.text(
            self.label_pos(pos, atlas),
            &self.text,
            style.text,
            self.font_size,
        );
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

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

    fn atlas() -> FontAtlas {
        FontAtlas::built_in()
    }

    fn style() -> Style {
        Style::default()
    }

    // ── Label ─────────────────────────────────────────────────────────────

    #[test]
    fn label_renders_text_command() {
        let label = Label::new("Hello");
        let mut dl = DrawList::new();
        label.render(&mut dl, Vec2::ZERO, &atlas(), &style());
        assert!(!dl.is_empty());
        assert!(matches!(dl.commands()[0], DrawCommand::Text { .. }));
    }

    #[test]
    fn label_with_bg_adds_rect() {
        let label = Label::new("Hi").with_bg(true);
        let mut dl = DrawList::new();
        label.render(&mut dl, Vec2::ZERO, &atlas(), &style());
        assert_eq!(dl.len(), 2);
        assert!(matches!(dl.commands()[0], DrawCommand::Rect { .. }));
        assert!(matches!(dl.commands()[1], DrawCommand::Text { .. }));
    }

    #[test]
    fn label_width_and_height_are_positive() {
        let label = Label::new("Hello");
        assert!(label.width(&atlas()) > 0.0);
        assert!(label.height() > 0.0);
    }

    /// The bug: measurement divided by 14 while layout divided by
    /// `GLYPH_HEIGHT` (13), so a label's declared box was ~7% narrower than the
    /// glyphs drawn inside it.
    #[test]
    fn declared_bounds_contain_the_glyphs_they_describe() {
        let atlas = atlas();
        for size in [10.0, 14.0, 28.0] {
            let btn = Button::new("LongButtonLabel").with_size(size);
            let pos = Vec2::new(50.0, 50.0);
            let (min, max) = btn.rect(pos, &atlas);

            let mut dl = DrawList::new();
            btn.render(&mut dl, pos, &atlas, &style(), ButtonState::Idle);
            let (verts, _) = dl.to_triangles(Some(&atlas), None, 1.0);
            for v in &verts {
                assert!(
                    v.pos.x >= min.x - 0.001
                        && v.pos.x <= max.x + 0.001
                        && v.pos.y >= min.y - 0.001
                        && v.pos.y <= max.y + 0.001,
                    "size {size}: vertex {:?} escapes the declared rect {min:?}..{max:?}",
                    v.pos
                );
            }
        }
    }

    /// The `Text` anchor is a top-left corner, so a label's background must
    /// contain its own glyphs.
    #[test]
    fn label_background_contains_its_glyphs() {
        let atlas = atlas();
        let label = Label::new("Hi").with_bg(true);
        let pos = Vec2::new(10.0, 100.0);
        let mut dl = DrawList::new();
        label.render(&mut dl, pos, &atlas, &style());

        let DrawCommand::Rect { min, max, .. } = dl.commands()[0] else {
            panic!("expected a background Rect");
        };
        let (verts, _) = dl.to_triangles(Some(&atlas), None, 1.0);
        assert!(!verts.is_empty());
        for v in &verts {
            assert!(
                v.pos.y >= min.y - 0.001 && v.pos.y <= max.y + 0.001,
                "glyph vertex {:?} falls outside the background {min:?}..{max:?}",
                v.pos
            );
        }
    }

    // ── Button ────────────────────────────────────────────────────────────

    #[test]
    fn button_in_idle_state_renders_rect_and_text() {
        let btn = Button::new("Click");
        let mut dl = DrawList::new();
        btn.render(&mut dl, Vec2::ZERO, &atlas(), &style(), ButtonState::Idle);
        assert_eq!(dl.len(), 3);
    }

    #[test]
    fn button_hit_test_inside() {
        let btn = Button::new("Go");
        let pos = Vec2::new(10.0, 10.0);
        let (min, max) = btn.rect(pos, &atlas());
        let centre = (min + max) * 0.5;
        assert!(btn.hit_test(pos, centre, &atlas()));
    }

    #[test]
    fn button_hit_test_outside() {
        let btn = Button::new("Go");
        let pos = Vec2::new(10.0, 10.0);
        assert!(!btn.hit_test(pos, Vec2::new(0.0, 0.0), &atlas()));
    }

    /// **The sweep `docs/notes/process.md` asks for**, where the two tests
    /// above are two points.
    ///
    /// Two points cannot see a transposed axis: the centre is inside whichever
    /// way the comparison is spelled, and the origin is outside both ways. A
    /// grid that straddles all four edges can — every sample is held against the
    /// rectangle the button itself reports, so a swapped comparison, a wrong
    /// corner, or a bound that stopped being inclusive each show up as a named
    /// disagreement at a named point rather than as a count.
    #[test]
    fn button_hit_test_agrees_with_its_rect_over_a_grid() {
        /// How far outside the button the sweep reaches, so every edge is
        /// crossed rather than approached.
        const MARGIN: f32 = 6.0;
        /// Sample spacing. A negative power of two, so stepping accumulates no
        /// error and the samples are the ones this test says they are.
        const STEP: f32 = 0.5;

        let btn = Button::new("Go");
        let atlas = atlas();
        let pos = Vec2::new(10.0, 10.0);
        let (min, max) = btn.rect(pos, &atlas);
        let size = max - min;
        assert!(
            (size.x - size.y).abs() > STEP,
            "the premise: a square button hides a transposed axis, and the sweep \
             below would pass on one. This button is {size:?}"
        );

        let mut samples = 0_u32;
        let mut inside = 0_u32;
        let mut x = min.x - MARGIN;
        while x <= max.x + MARGIN {
            let mut y = min.y - MARGIN;
            while y <= max.y + MARGIN {
                let mouse = Vec2::new(x, y);
                let want = x >= min.x && x <= max.x && y >= min.y && y <= max.y;
                assert_eq!(
                    btn.hit_test(pos, mouse, &atlas),
                    want,
                    "at {mouse:?}, against a button spanning {min:?} to {max:?}"
                );
                samples += 1;
                inside += u32::from(want);
                y += STEP;
            }
            x += STEP;
        }
        assert!(
            inside > 0 && inside < samples,
            "a sweep entirely inside or entirely outside asserts nothing about an \
             edge: {inside} of {samples} samples hit"
        );

        // The boundary itself, which a grid only straddles: every corner is on
        // it, and `hit_test` documents an inclusive rectangle.
        for corner in [min, Vec2::new(max.x, min.y), Vec2::new(min.x, max.y), max] {
            assert!(
                btn.hit_test(pos, corner, &atlas),
                "the rectangle is inclusive, so its own corner {corner:?} is a hit"
            );
        }
    }

    #[test]
    fn a_press_and_release_over_one_button_is_a_click_and_frees_the_capture() {
        let btn = Button::new("Fire");
        let atlas = atlas();
        let mut ui = UiState::new();
        let centre = {
            let (min, max) = btn.rect(Vec2::ZERO, &atlas);
            (min + max) * 0.5
        };

        // Press.
        let down = PointerInput {
            pos: centre,
            down: true,
            released: false,
            secondary_pressed: false,
        };
        let up = PointerInput {
            pos: centre,
            down: false,
            released: true,
            secondary_pressed: false,
        };
        let (state, clicked) = btn.interact(Vec2::ZERO, &atlas, &mut ui, 1, down);
        assert_eq!(state, ButtonState::Pressed);
        assert!(!clicked);
        // Release over the same button.
        let (_, clicked) = btn.interact(Vec2::ZERO, &atlas, &mut ui, 1, up);
        assert!(clicked);
        assert_eq!(ui.active(), None, "the capture must be released");
    }

    #[test]
    fn button_no_click_when_not_hovered() {
        let btn = Button::new("Fire");
        let atlas = atlas();
        let mut ui = UiState::new();
        let outside = PointerInput {
            pos: Vec2::new(-100.0, -100.0),
            down: false,
            released: true,
            secondary_pressed: false,
        };
        let (state, clicked) = btn.interact(Vec2::ZERO, &atlas, &mut ui, 1, outside);
        assert_eq!(state, ButtonState::Idle);
        assert!(!clicked);
    }

    // ── press capture ─────────────────────────────────────────────────────

    /// The bug: a click was credited to whatever the cursor was over on
    /// release, so press-A / drag-to-B / release fired B.
    #[test]
    fn press_on_a_and_release_on_b_clicks_neither() {
        let atlas = atlas();
        let a = Button::new("A");
        let b = Button::new("B");
        let a_pos = Vec2::new(0.0, 0.0);
        let b_pos = Vec2::new(200.0, 0.0);
        let inside = |btn: &Button, pos: Vec2| {
            let (min, max) = btn.rect(pos, &atlas);
            (min + max) * 0.5
        };
        let a_centre = inside(&a, a_pos);
        let b_centre = inside(&b, b_pos);
        let mut ui = UiState::new();

        let held = |pos| PointerInput {
            pos,
            down: true,
            released: false,
            secondary_pressed: false,
        };
        let up = |pos| PointerInput {
            pos,
            down: false,
            released: true,
            secondary_pressed: false,
        };

        // Frame 1: press over A.
        let (a_state, a_clicked) = a.interact(a_pos, &atlas, &mut ui, 1, held(a_centre));
        let (b_state, b_clicked) = b.interact(b_pos, &atlas, &mut ui, 2, held(a_centre));
        assert_eq!(a_state, ButtonState::Pressed);
        assert_eq!(b_state, ButtonState::Idle);
        assert!(!a_clicked && !b_clicked);

        // Frame 2: still held, cursor dragged onto B. B must not light up.
        let (a_state, _) = a.interact(a_pos, &atlas, &mut ui, 1, held(b_centre));
        let (b_state, _) = b.interact(b_pos, &atlas, &mut ui, 2, held(b_centre));
        assert_eq!(a_state, ButtonState::Pressed, "A still owns the press");
        assert_eq!(b_state, ButtonState::Idle, "B must not steal the capture");

        // Frame 3: release over B. Neither fires — A lost the cursor, B never
        // captured the press.
        let (_, a_clicked) = a.interact(a_pos, &atlas, &mut ui, 1, up(b_centre));
        let (_, b_clicked) = b.interact(b_pos, &atlas, &mut ui, 2, up(b_centre));
        assert!(!a_clicked, "A lost the cursor before release");
        assert!(!b_clicked, "B never captured the press");
        assert_eq!(ui.active(), None);
    }

    #[test]
    fn hover_needs_no_capture_but_a_captured_press_suppresses_it() {
        let mut ui = UiState::new();
        // Nothing captured: hover reads as Hovered.
        assert_eq!(ui.interact(1, true, false, false).0, ButtonState::Hovered);
        // Widget 1 captures the press…
        assert_eq!(ui.interact(1, true, true, false).0, ButtonState::Pressed);
        // …so widget 2 stays idle even under the cursor.
        assert_eq!(ui.interact(2, true, true, false).0, ButtonState::Idle);
        ui.clear();
        assert_eq!(ui.active(), None);
    }

    #[test]
    fn button_rect_grows_with_text() {
        let short = Button::new("A");
        let long = Button::new("LongButton");
        let (_, short_max) = short.rect(Vec2::ZERO, &atlas());
        let (_, long_max) = long.rect(Vec2::ZERO, &atlas());
        assert!(long_max.x > short_max.x);
    }

    // ── skinned buttons ───────────────────────────────────────────────────

    /// Deliberately **asymmetric on every side**, so a transposed or mirrored
    /// pair cannot compare equal by accident: a swapped left/right shifts the
    /// content box by 16, and a swapped top/bottom by 6.
    const INSETS: SkinInsets = SkinInsets::new(4.0, 20.0, 3.0, 9.0);

    /// The minimum is the corners **and** the label, with the padding between
    /// them — neither term alone.
    #[test]
    fn a_skinned_buttons_minimum_size_is_its_corners_plus_its_label() {
        let atlas = atlas();
        let plain = Button::new("Play");
        let skinned = plain.clone().with_skin(INSETS);

        let label = plain.label_size(&atlas);
        assert!(label.x > 0.0 && label.y > 0.0);

        // Unskinned: unchanged from what a button has always measured.
        assert_eq!(
            plain.minimum_size(&atlas),
            label + plain.padding * 2.0,
            "an unskinned button's minimum must still be its label plus padding"
        );
        assert_eq!(plain.rect(Vec2::ZERO, &atlas).1, plain.minimum_size(&atlas));

        // Skinned: exactly the corners more.
        assert_eq!(
            skinned.minimum_size(&atlas) - plain.minimum_size(&atlas),
            Vec2::new(INSETS.left + INSETS.right, INSETS.top + INSETS.bottom),
            "the skin's corners must be added to the label's own minimum"
        );

        // And the corners alone are a floor even with no label at all: a
        // minimum that only measured text would let the corners overlap.
        let empty = Button::new("").with_skin(INSETS);
        let corners = INSETS.minimum_size();
        assert!(
            empty.minimum_size(&atlas).x >= corners.x && empty.minimum_size(&atlas).y >= corners.y,
            "an empty label must still leave room for the four corners"
        );
    }

    /// A caller asking for less than the minimum gets the minimum, per axis.
    #[test]
    fn a_button_asked_for_less_than_its_minimum_grows_to_it() {
        let atlas = atlas();
        let button = Button::new("Play").with_skin(INSETS);
        let minimum = button.minimum_size(&atlas);

        // Far too small on both axes.
        let squashed = button.clone().with_fixed_size(Vec2::new(1.0, 1.0));
        assert_eq!(squashed.size(&atlas), minimum);
        assert_eq!(squashed.rect(Vec2::ZERO, &atlas).1, minimum);

        // Generous on both axes: honoured exactly.
        let roomy = minimum + Vec2::new(200.0, 40.0);
        assert_eq!(button.clone().with_fixed_size(roomy).size(&atlas), roomy);

        // Each axis clamps on its own — a mean height must not cost the width.
        let mixed = button.with_fixed_size(Vec2::new(minimum.x + 200.0, 1.0));
        assert_eq!(
            mixed.size(&atlas),
            Vec2::new(minimum.x + 200.0, minimum.y),
            "the width was fine and must survive the height being clamped"
        );

        // Clamping, not squashing: the content box never inverts, so the label
        // is never drawn across the corners.
        let (content_min, content_max) = mixed.content_rect(Vec2::ZERO, &atlas);
        assert!(
            content_max.x >= content_min.x && content_max.y >= content_min.y,
            "the content box inverted: {content_min:?}..{content_max:?}"
        );
    }

    /// The label is centred in the content box, and **stays** centred as the
    /// button is resized — the property a fixed top-left anchor would fail at
    /// every width but one.
    #[test]
    fn the_label_stays_centred_when_the_button_is_resized() {
        let atlas = atlas();
        let button = Button::new("Play").with_skin(INSETS);
        let pos = Vec2::new(37.0, 11.0);
        let label = button.label_size(&atlas);

        for width in [button.minimum_size(&atlas).x, 200.0, 640.0] {
            for height in [button.minimum_size(&atlas).y, 60.0, 180.0] {
                let sized = button.clone().with_fixed_size(Vec2::new(width, height));
                let (content_min, content_max) = sized.content_rect(pos, &atlas);
                let anchor = sized.label_pos(pos, &atlas);

                // Equal slack on the left and the right, and on top and bottom.
                let before = anchor - content_min;
                let after = content_max - (anchor + label);
                assert!(
                    (before.x - after.x).abs() < 1e-3 && (before.y - after.y).abs() < 1e-3,
                    "at {width}x{height} the label is off-centre: {before:?} before, \
                     {after:?} after"
                );

                // And it is inside the content box, so it never touches a corner.
                assert!(
                    before.x >= -1e-3 && before.y >= -1e-3,
                    "at {width}x{height} the label escapes its content box"
                );
            }
        }
    }

    /// Adding a skin must not change where an *unskinned* button puts its text —
    /// centring in the content box has to reduce to the old `pos + padding`, or
    /// every existing layout shifts.
    ///
    /// The tolerance is `1e-4` **pixels** and not zero, deliberately. Centring
    /// reaches the same point by a different route — `min + (max - min - label) /
    /// 2` where the old code wrote `pos + padding` — and at a shrink-to-fit size
    /// those two associate the same sum differently, which lands up to a few ulps
    /// apart (measured: `58.000004` against `58.0`). That is four millionths of a
    /// pixel and is not a layout change; asserting bit equality here would be
    /// asserting float associativity, which is not the claim.
    #[test]
    fn an_unskinned_buttons_label_is_still_anchored_at_pos_plus_padding() {
        let atlas = atlas();
        for text in ["A", "LongButtonLabel", ""] {
            for size in [10.0, 14.0, 28.0] {
                let button = Button::new(text).with_size(size);
                let pos = Vec2::new(50.0, 50.0);
                let moved = (button.label_pos(pos, &atlas) - (pos + button.padding)).abs();
                assert!(
                    moved.x < 1e-4 && moved.y < 1e-4,
                    "{text:?} at {size}: the label moved by {moved:?} px"
                );
            }
        }
    }

    /// A skinned button emits **only** its label: the background is the
    /// [`ButtonSkin`]'s to push, and a widget that also painted a flat rect
    /// would paint it over the skin.
    #[test]
    fn a_skinned_button_draws_its_label_and_no_background() {
        let atlas = atlas();
        let button = Button::new("Play").with_skin(INSETS);
        for state in [
            ButtonState::Idle,
            ButtonState::Hovered,
            ButtonState::Pressed,
        ] {
            let mut dl = DrawList::new();
            button.render(&mut dl, Vec2::ZERO, &atlas, &style(), state);
            assert_eq!(dl.len(), 1, "{state:?} emitted more than the label");
            assert!(
                matches!(dl.commands()[0], DrawCommand::Text { .. }),
                "{state:?} emitted something that is not the label"
            );
        }

        // The unskinned button is untouched: rect, outline, label.
        let mut dl = DrawList::new();
        Button::new("Play").render(&mut dl, Vec2::ZERO, &atlas, &style(), ButtonState::Idle);
        assert_eq!(dl.len(), 3);
    }

    /// Hit testing follows the drawn rect, so a resized or clamped button is
    /// clickable exactly where it is visible — including in the corners, which
    /// are part of the button and not decoration around it.
    #[test]
    fn a_resized_skinned_button_is_clickable_across_its_whole_rect() {
        let atlas = atlas();
        let button = Button::new("Play")
            .with_skin(INSETS)
            .with_fixed_size(Vec2::new(400.0, 90.0));
        let pos = Vec2::new(10.0, 20.0);
        let (min, max) = button.rect(pos, &atlas);
        assert_eq!(max - min, Vec2::new(400.0, 90.0));

        assert!(button.hit_test(pos, (min + max) * 0.5, &atlas));
        assert!(
            button.hit_test(pos, min + Vec2::new(1.0, 1.0), &atlas),
            "the top-left corner of the skin is part of the button"
        );
        assert!(
            button.hit_test(pos, max - Vec2::new(1.0, 1.0), &atlas),
            "the bottom-right corner of the skin is part of the button"
        );
        assert!(
            !button.hit_test(pos, max + Vec2::new(2.0, 2.0), &atlas),
            "past the skin is not the button"
        );
    }

    #[test]
    fn button_different_styles_affect_bg_color() {
        let btn = Button::new("Test");
        let mut dl_hover = DrawList::new();
        btn.render(
            &mut dl_hover,
            Vec2::ZERO,
            &atlas(),
            &style(),
            ButtonState::Hovered,
        );

        let mut dl_idle = DrawList::new();
        btn.render(
            &mut dl_idle,
            Vec2::ZERO,
            &atlas(),
            &style(),
            ButtonState::Idle,
        );

        // Both should have a Rect command; colors differ.
        if let DrawCommand::Rect { color: c1, .. } = &dl_hover.commands()[0] {
            if let DrawCommand::Rect { color: c2, .. } = &dl_idle.commands()[0] {
                assert_ne!(
                    c1, c2,
                    "hover and idle should have different background colors"
                );
            } else {
                panic!("second command not Rect");
            }
        } else {
            panic!("first command not Rect");
        }
    }

    // -----------------------------------------------------------------------
    // ButtonSkin
    // -----------------------------------------------------------------------

    /// Three 16x16 frames in one atlas, with the given insets on each.
    fn skin_with(insets: [SkinInsets; 3]) -> ButtonSkin {
        let mut images = crate::image::ImageAtlas::new();
        let mut frame = |shade: u8, insets| NineSliceImage {
            image: images
                .register(16, 16, &[shade; 16 * 16 * 4])
                .expect("fits an empty page"),
            insets,
        };
        ButtonSkin {
            idle: frame(10, insets[0]),
            hovered: frame(20, insets[1]),
            pressed: frame(30, insets[2]),
        }
    }

    /// **A state draws its own frame and nothing moves**: the same nine
    /// rectangles for every state, each state sampling a different image.
    #[test]
    fn a_button_skin_swaps_the_picture_and_leaves_the_rectangles_alone() {
        let skin = skin_with([SkinInsets::new(3.0, 5.0, 2.0, 4.0); 3]);
        let quads = |state| {
            let mut dl = DrawList::new();
            skin.draw(
                &mut dl,
                state,
                Vec2::new(10.0, 10.0),
                Vec2::new(90.0, 40.0),
                2.0,
            );
            dl.commands()
                .iter()
                .map(|command| match command {
                    DrawCommand::Image {
                        min, max, uv_min, ..
                    } => (*min, *max, *uv_min),
                    other => panic!("a skin drew {other:?}"),
                })
                .collect::<Vec<_>>()
        };
        let [idle, hovered, pressed] = [
            ButtonState::Idle,
            ButtonState::Hovered,
            ButtonState::Pressed,
        ]
        .map(quads);
        assert_eq!(idle.len(), 9);
        for index in 0..9 {
            assert_eq!(
                (idle[index].0, idle[index].1),
                (pressed[index].0, pressed[index].1),
                "quad {index} moved with the state"
            );
            assert_eq!(
                (hovered[index].0, hovered[index].1),
                (idle[index].0, idle[index].1)
            );
            assert_ne!(idle[index].2, hovered[index].2, "quad {index}");
            assert_ne!(hovered[index].2, pressed[index].2, "quad {index}");
            assert_ne!(idle[index].2, pressed[index].2, "quad {index}");
        }
    }

    #[test]
    fn a_skins_insets_are_its_idle_frames_and_disagreement_is_reported() {
        let agreed = skin_with([SkinInsets::new(3.0, 5.0, 2.0, 4.0); 3]);
        assert_eq!(agreed.insets(), SkinInsets::new(3.0, 5.0, 2.0, 4.0));
        assert!(agreed.insets_agree());

        let split = skin_with([
            SkinInsets::uniform(4.0),
            SkinInsets::uniform(4.0),
            SkinInsets::uniform(6.0),
        ]);
        assert!(!split.insets_agree());
        // Clamped to the frame, as the drawing is.
        let over = skin_with([SkinInsets::new(40.0, 40.0, 0.0, 0.0); 3]);
        assert_eq!(over.insets(), SkinInsets::new(16.0, 0.0, 0.0, 0.0));
    }
}