fluor 0.0.3

First-principles GUI compositor library: center-origin RU coordinates, harmonic-mean span scaling, CPU softbuffer rendering, ARM-first.
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
//! Dropdown (select) widget — a Button-family pill showing the current choice plus a chevron; clicking (or Enter/Space when focused) opens a popup list of options. Same squircle silhouette, two-tone AA edge, focus glow, and hover-tint-via-overlay conventions as [`super::Button`] so the family reads coherently.
//!
//! **Row dispatch.** Each option row owns its own dense [`HitId`] ([`DropdownRow`], a tiny [`Widget`] impl), mirroring how [`crate::host::chrome_widget::DefaultChrome`] owns four `ChromeButton`s. Per-row hover tints, click routing, and hit-testing all ride the existing id-indexed machinery for free. The app's [`crate::host::widget::Container::visit`] should hand out the dropdown itself (the pill) and then call [`Dropdown::visit_rows`] — rows only appear in the walk while the popup is open, so they never enter the tab cycle or overlay table when closed.
//!
//! **Front-to-back placement.** fluor composites under-blend (first writer wins), so the OPEN popup must be the first content painted into the frame: call [`Dropdown::render_popup_into`] at the top of the app's render, before other widgets and before the chrome flatten. Everything painted afterwards composes underneath it — no z-order machinery needed. The closed pill renders in normal widget order via [`Dropdown::render_content_into`].
//!
//! **Action model.** Poll-based like Button/Slider: row clicks and keyboard commits accumulate into a change counter; the app calls [`Dropdown::take_change`] after dispatch (and/or in `tick`) — `Some(index)` means the selection changed since last poll. `take_change` is also where row-click commits are folded into `selected` (the row widgets can't reach back into the parent during the visit walk).

use crate::canvas::PixelRect;
use crate::coord::Coord;
use crate::paint::{self, Clip, HitId};
use crate::region::Region;
use crate::text::TextRenderer;
use crate::theme;
use crate::widgets::textbox::{blit_cache_to_target, region_to_pixelrect};
use alloc::string::String;
use alloc::vec::Vec;

/// Gap between the pill's bottom edge and the popup's top edge, in stroke widths.
const POPUP_GAP_PX: Coord = 2.0;

/// One option row. A minimal [`crate::host::widget::Widget`]: carries its own hit id (stamped over the row band while the popup is open), hover state (drives the overlay tint), and a fired flag the parent folds into `selected` at [`Dropdown::take_change`] time.
pub struct DropdownRow {
    hit_id: HitId,
    /// Index into the parent's `options`. Fixed at construction.
    index: usize,
    hovered: bool,
    fired: bool,
}

impl DropdownRow {
    pub fn hit_id(&self) -> HitId {
        self.hit_id
    }
    pub fn index(&self) -> usize {
        self.index
    }
}

/// Discrete-choice selector. Closed: a pill with the selected option's label and a chevron. Open: a popup listing every option, one hoverable/clickable row each.
pub struct Dropdown {
    /// The pill's hit id (allocated first, then one per row, all from the same counter).
    hit_id: HitId,
    options: Vec<String>,
    rows: Vec<DropdownRow>,
    selected: usize,
    /// Keyboard highlight while open — mirrored into the rows' `hovered` flags so the overlay pass tints it exactly like a mouse hover.
    highlight: usize,
    open: bool,
    font: &'static str,

    /// Stroke thickness as a fraction of `font_size` — same convention as Button/Textbox.
    pub stroke_ru: f32,
    pub center_x: Coord,
    pub center_y: Coord,
    pub width: Coord,
    pub height: Coord,
    pub font_size: Coord,

    focused: bool,
    hovered: bool,
    enabled: bool,

    change_counter: u32,
    last_seen_change_counter: u32,

    // --- Pill cache (closed state): squircle bg + AA edges; label + chevron in text cache. Same layering as Button. ---
    pill_cache: Vec<u32>,
    pill_cache_w: usize,
    pill_cache_h: usize,
    pill_cache_dirty: bool,
    text_cache: Vec<u32>,
    text_cache_dirty: bool,
    inner_pill_mask: Vec<u8>,
    /// `true` when the caches were rasterized with the disabled (dimmed) palette — so enable/disable transitions re-rasterize.
    cache_disabled: bool,

    // --- Damage protocol ---
    last_painted_bbox: Option<PixelRect>,
    last_painted_focused: bool,
    /// Popup rect painted last frame (viewport pixels), if the popup was open. Unioned into `damage_rect` so both the open transition (paint new popup) and the close transition (clear stale popup pixels) get exactly one frame of damage.
    last_painted_popup: Option<PixelRect>,
    /// Whether anything about the popup (open state, highlight, selection) changed since the last paint.
    popup_dirty: bool,
}

impl Dropdown {
    /// `hit_counter` is the app's monotonic [`HitId`] allocator. The dropdown claims `1 + options.len()` ids: the pill first, then one per row in option order.
    pub fn new(
        hit_counter: &mut HitId,
        center_x: Coord,
        center_y: Coord,
        width: Coord,
        height: Coord,
        font_size: Coord,
        options: Vec<String>,
    ) -> Self {
        assert!(!options.is_empty(), "Dropdown requires at least one option");
        let hit_id = crate::host::widget::next_id(hit_counter);
        let rows = (0..options.len())
            .map(|index| DropdownRow {
                hit_id: crate::host::widget::next_id(hit_counter),
                index,
                hovered: false,
                fired: false,
            })
            .collect();
        Self {
            hit_id,
            options,
            rows,
            selected: 0,
            highlight: 0,
            open: false,
            font: "Open Sans",
            stroke_ru: 1.0 / (1 << 5) as f32,
            center_x,
            center_y,
            width,
            height,
            font_size,
            focused: false,
            hovered: false,
            enabled: true,
            change_counter: 0,
            last_seen_change_counter: 0,
            pill_cache: Vec::new(),
            pill_cache_w: 0,
            pill_cache_h: 0,
            pill_cache_dirty: true,
            text_cache: Vec::new(),
            text_cache_dirty: true,
            inner_pill_mask: Vec::new(),
            cache_disabled: false,
            last_painted_bbox: None,
            last_painted_focused: false,
            last_painted_popup: None,
            popup_dirty: false,
        }
    }

    pub fn hit_id(&self) -> HitId {
        self.hit_id
    }
    pub fn is_open(&self) -> bool {
        self.open
    }
    pub fn is_focused(&self) -> bool {
        self.focused
    }
    pub fn is_hovered(&self) -> bool {
        self.hovered
    }
    pub fn is_enabled(&self) -> bool {
        self.enabled
    }
    pub fn selected(&self) -> usize {
        self.selected
    }
    pub fn selected_label(&self) -> &str {
        &self.options[self.selected]
    }
    pub fn options(&self) -> &[String] {
        &self.options
    }

    /// True when `hit` is the pill or any row of this dropdown.
    pub fn owns_hit(&self, hit: HitId) -> bool {
        hit != crate::paint::HIT_NONE
            && (hit == self.hit_id || self.rows.iter().any(|r| r.hit_id == hit))
    }

    /// Programmatic selection (no change-counter bump — that's for user actions).
    pub fn set_selected(&mut self, index: usize) {
        let index = index.min(self.options.len() - 1);
        if index != self.selected {
            self.selected = index;
            self.text_cache_dirty = true;
            self.popup_dirty = true;
        }
    }

    pub fn set_focused(&mut self, focused: bool) {
        if focused != self.focused {
            self.focused = focused;
            // Focus loss closes without commit — covers Esc-at-app-level, click-elsewhere, tab-away.
            if !focused {
                self.close();
            }
        }
    }

    pub fn set_hovered(&mut self, hovered: bool) {
        if !self.enabled {
            return;
        }
        if hovered != self.hovered {
            self.hovered = hovered;
        }
    }

    /// Enable / disable. Disabled drops out of dispatch (capability accessors return `None`), closes the popup, and re-rasterizes with a dimmed label so the user can see it's inert (Slider-style greying — unlike Button, a disabled selector reads wrong if it stays full-brightness).
    pub fn set_enabled(&mut self, enabled: bool) {
        if enabled == self.enabled {
            return;
        }
        self.enabled = enabled;
        if !enabled {
            self.focused = false;
            self.hovered = false;
            self.close();
        }
        self.text_cache_dirty = true;
    }

    /// Open the popup with the keyboard highlight synced to the current selection.
    pub fn open(&mut self) {
        if self.open || !self.enabled {
            return;
        }
        self.open = true;
        self.highlight = self.selected;
        self.sync_row_highlight();
        self.popup_dirty = true;
    }

    /// Close the popup without committing. Idempotent.
    pub fn close(&mut self) {
        if !self.open {
            return;
        }
        self.open = false;
        for r in self.rows.iter_mut() {
            r.hovered = false;
            r.fired = false;
        }
        self.popup_dirty = true;
    }

    fn toggle_open(&mut self) {
        if self.open {
            self.close();
        } else {
            self.open();
        }
    }

    fn commit(&mut self, index: usize) {
        let index = index.min(self.options.len() - 1);
        if index != self.selected {
            self.selected = index;
            self.text_cache_dirty = true;
        }
        // Commit always counts as a change event, even re-selecting the same option — apps that re-run an action on select can distinguish via selected() if they care.
        self.change_counter = self.change_counter.wrapping_add(1);
        self.close();
    }

    /// Mirror `highlight` into the rows' hovered flags so the overlay pass tints the keyboard highlight identically to a mouse hover.
    fn sync_row_highlight(&mut self) {
        for r in self.rows.iter_mut() {
            r.hovered = r.index == self.highlight;
        }
    }

    /// Drive hover state for the pill AND the rows from the frame's hit result. Call from the app's CursorMoved path with whatever `hit_at`/hit-map lookup produced. Returns `true` if any hover state changed (request a redraw — row hover also feeds `highlight` so mouse and keyboard stay coherent).
    pub fn sync_hover(&mut self, hit: HitId) -> bool {
        if !self.enabled {
            return false;
        }
        let mut changed = false;
        let pill_hover = hit == self.hit_id;
        if pill_hover != self.hovered {
            self.hovered = pill_hover;
            changed = true;
        }
        if self.open {
            for i in 0..self.rows.len() {
                let want = self.rows[i].hit_id == hit;
                if self.rows[i].hovered != want {
                    self.rows[i].hovered = want;
                    changed = true;
                    if want {
                        self.highlight = self.rows[i].index;
                    }
                }
            }
            if changed {
                self.popup_dirty = true;
            }
        }
        changed
    }

    /// Fold pending row clicks into the selection, then report whether the selection changed since the last poll. Call after click dispatch and/or once per `tick`. Returns `Some(selected_index)` on a fresh change.
    pub fn take_change(&mut self) -> Option<usize> {
        if self.open {
            if let Some(idx) = self
                .rows
                .iter_mut()
                .find_map(|r| r.fired.then_some(r.index))
            {
                for r in self.rows.iter_mut() {
                    r.fired = false;
                }
                self.commit(idx);
            }
        }
        if self.change_counter != self.last_seen_change_counter {
            self.last_seen_change_counter = self.change_counter;
            Some(self.selected)
        } else {
            None
        }
    }

    /// Yield the option rows to a dispatch/overlay walk — only while open, so closed rows never enter hover tables or dispatch. Call right after handing the dropdown itself to the same callback (see module docs).
    pub fn visit_rows(&mut self, f: &mut dyn FnMut(&mut dyn crate::host::widget::Widget)) {
        if !self.open || !self.enabled {
            return;
        }
        for r in self.rows.iter_mut() {
            f(r);
        }
    }

    pub fn set_rect(&mut self, center_x: Coord, center_y: Coord, width: Coord, height: Coord) {
        if self.center_x != center_x
            || self.center_y != center_y
            || self.width != width
            || self.height != height
        {
            self.pill_cache_dirty = true;
            self.text_cache_dirty = true;
            self.popup_dirty = true;
        }
        self.center_x = center_x;
        self.center_y = center_y;
        self.width = width;
        self.height = height;
    }

    pub fn set_font_size(&mut self, font_size: Coord) {
        if self.font_size != font_size {
            self.pill_cache_dirty = true;
            self.text_cache_dirty = true;
            self.popup_dirty = true;
        }
        self.font_size = font_size;
    }

    pub fn bbox(&self) -> Region {
        Region::new(
            self.center_x - self.width * 0.5,
            self.center_y - self.height * 0.5,
            self.width,
            self.height,
        )
    }

    fn row_height(&self) -> Coord {
        self.font_size * 1.6
    }

    fn popup_padding(&self) -> Coord {
        self.font_size * 0.4
    }

    /// Popup rectangle in viewport pixels. Opens downward from the pill; flips upward when the viewport bottom would clip it. Deterministic from widget state + viewport height, so `damage_rect` and the painter agree without stored geometry.
    pub fn popup_region(&self, viewport_h: usize) -> Region {
        let pad = self.popup_padding();
        let h = self.rows.len() as Coord * self.row_height() + pad * 2.0;
        let w = self.width;
        let x = self.center_x - w * 0.5;
        let below_y = self.center_y + self.height * 0.5 + POPUP_GAP_PX;
        let y = if below_y + h > viewport_h as Coord {
            (self.center_y - self.height * 0.5 - POPUP_GAP_PX - h).max(0.0)
        } else {
            below_y
        };
        Region::new(x, y, w, h)
    }

    /// Damage region. `None` when nothing changed since the last paint. Unions the pill bbox (or glow envelope on focus transitions) with the popup rect on any frame where the popup is, or was, visible-and-dirty.
    pub fn damage_rect(&self, viewport_w: usize, viewport_h: usize) -> Option<PixelRect> {
        let focus_changed = self.focused != self.last_painted_focused;
        let pill_dirty = self.pill_cache_dirty || self.text_cache_dirty;
        if !pill_dirty && !focus_changed && !self.popup_dirty && self.last_painted_bbox.is_some() {
            return None;
        }
        let mut combined: Option<PixelRect> = self.last_painted_bbox;
        let current = if focus_changed {
            region_to_pixelrect(self.glow_bbox(), viewport_w, viewport_h)
        } else {
            region_to_pixelrect(self.bbox(), viewport_w, viewport_h)
        };
        combined = Some(combined.map_or(current, |c| c.union(current)));
        if self.popup_dirty {
            // Cover both where the popup was (clear on close) and where it will be (paint on open/highlight move).
            if let Some(prev) = self.last_painted_popup {
                combined = Some(combined.map_or(prev, |c| c.union(prev)));
            }
            if self.open {
                let cur = region_to_pixelrect(self.popup_region(viewport_h), viewport_w, viewport_h);
                combined = Some(combined.map_or(cur, |c| c.union(cur)));
            }
        }
        combined
    }

    /// Glow envelope — same reach math as Button so the focus glow damage matches what gets painted.
    pub fn glow_bbox(&self) -> Region {
        let horiz_factor = glow_factor_256(self.font_size, 1.5);
        let vert_factor = glow_factor_256(self.font_size, 0.75);
        let horiz_pad = paint::ray_reach_px(0x80, horiz_factor) as f32;
        let vert_pad = paint::ray_reach_px(0x40, vert_factor) as f32;
        Region::new(
            self.center_x - self.width * 0.5 - horiz_pad,
            self.center_y - self.height * 0.5 - vert_pad,
            self.width + 2.0 * horiz_pad,
            self.height + 2.0 * vert_pad,
        )
    }

    fn text_colour(&self) -> u32 {
        if self.enabled {
            theme::TEXTBOX_TEXT
        } else {
            // Greyed: keep the darkness payload, halve the α so the label visibly dims.
            (theme::TEXTBOX_TEXT & 0x00FF_FFFF) | 0x8000_0000
        }
    }

    /// Paint the OPEN popup into `canvas`, stamping each row's hit id over its band. MUST be called before any content the popup should cover (top of the app's render — see module docs). No-op when closed or disabled.
    pub fn render_popup_into(
        &mut self,
        canvas: &mut crate::canvas::Canvas,
        text: &mut TextRenderer,
        clip: Option<Clip>,
        mut hit_map: Option<&mut [HitId]>,
    ) {
        if !self.open || !self.enabled {
            // Still reconcile damage bookkeeping on the close frame.
            if self.popup_dirty && !self.open {
                self.last_painted_popup = None;
                self.popup_dirty = false;
            }
            return;
        }
        let viewport_w = canvas.width;
        let viewport_h = canvas.height;
        let region = self.popup_region(viewport_h);
        let x = region.x as isize;
        let y = region.y as isize;
        let w = region.w as isize;
        let h = region.h as isize;
        if w <= 0 || h <= 0 {
            return;
        }
        let stroke_px = (self.stroke_ru * self.font_size) as isize + 1;
        let squirdleyness = 3.0;
        let pad = self.popup_padding();
        let row_h = self.row_height();

        paint::RASTERIZE_OPS.fetch_add(1, core::sync::atomic::Ordering::Relaxed);

        // Rows first (topmost-first doctrine): marker dot + label, then hit-band stamp; the pill fill composes under them at the end.
        let text_left = region.x + pad * 2.2;
        for (i, label) in self.options.iter().enumerate() {
            let row_top = region.y + pad + i as Coord * row_h;
            let row_cy = row_top + row_h * 0.5;
            if i == self.selected {
                paint::draw_circle(
                    canvas,
                    region.x + pad * 1.2,
                    row_cy,
                    self.font_size * 0.14,
                    theme::TEXTBOX_TEXT,
                    clip,
                );
            }
            text.draw_text_left_u32(
                canvas,
                label,
                text_left,
                row_cy,
                self.font_size,
                400,
                theme::TEXTBOX_TEXT,
                self.font,
                clip,
                None,
                None,
            );
            // Stamp the row's hit band (interior of the popup only, clear of the AA edge).
            if let Some(map) = hit_map.as_deref_mut() {
                let x0 = (x + stroke_px).max(0) as usize;
                let x1 = ((x + w - stroke_px).max(0) as usize).min(viewport_w);
                let y0 = (row_top as isize).max(0) as usize;
                let y1 = ((row_top + row_h) as isize).max(0) as usize;
                let y1 = y1.min(viewport_h);
                for yy in y0..y1 {
                    let base = yy * viewport_w;
                    for xx in x0..x1 {
                        map[base + xx] = self.rows[i].hit_id;
                    }
                }
            }
        }

        // Popup pill: opaque fill + two-tone edge, drawn LAST so text/marker won the pixels they need and the fill claims the rest.
        paint::draw_squircle_pill_f(
            canvas,
            x + stroke_px,
            y + stroke_px,
            (w - 2 * stroke_px).max(0),
            (h - 2 * stroke_px).max(0),
            theme::TEXTBOX_FILL,
            squirdleyness,
        );
        paint::draw_squircle_pill_two_tone_f(
            canvas,
            x,
            y,
            w,
            h,
            theme::TEXTBOX_LIGHT_EDGE,
            theme::TEXTBOX_SHADOW_EDGE,
            squirdleyness,
            None,
            0,
        );

        self.last_painted_popup = Some(region_to_pixelrect(region, viewport_w, viewport_h));
        self.popup_dirty = false;
    }

    /// Paint the closed-state pill (selected label + chevron) in normal widget order, stamping the pill's hit id. Mirrors [`super::Button::render_content_into`].
    pub fn render_content_into(
        &mut self,
        canvas: &mut crate::canvas::Canvas,
        offset_x: Coord,
        offset_y: Coord,
        text: &mut TextRenderer,
        clip: Option<Clip>,
        hit_map: Option<&mut [HitId]>,
    ) {
        let pill_x_target = (self.center_x - self.width * 0.5 - offset_x) as isize;
        let pill_y_target = (self.center_y - self.height * 0.5 - offset_y) as isize;
        let pill_w = self.width as isize;
        let pill_h = self.height as isize;
        if pill_w <= 0 || pill_h <= 0 {
            return;
        }
        let cw = pill_w as usize;
        let ch = pill_h as usize;
        let squirdleyness = 1.75;
        let stroke_px = (self.stroke_ru * self.font_size) as isize + 1;

        if self.cache_disabled != !self.enabled {
            self.pill_cache_dirty = true;
            self.text_cache_dirty = true;
        }

        // --- Pill cache: same fill/edge family as Button. ---
        if self.pill_cache_dirty {
            paint::RASTERIZE_OPS.fetch_add(1, core::sync::atomic::Ordering::Relaxed);
            self.pill_cache.clear();
            self.pill_cache.resize(cw * ch, 0);
            self.pill_cache_w = cw;
            self.pill_cache_h = ch;
            let mut cache_damage = crate::canvas::Damage::new();
            {
                let mut cache_canvas =
                    crate::canvas::Canvas::new(&mut self.pill_cache, cw, ch, &mut cache_damage);
                let inner_w = (pill_w - 2 * stroke_px).max(0);
                let inner_h = (pill_h - 2 * stroke_px).max(0);
                if inner_w > 0 && inner_h > 0 {
                    paint::draw_squircle_pill_f(
                        &mut cache_canvas,
                        stroke_px,
                        stroke_px,
                        inner_w,
                        inner_h,
                        theme::BUTTON_FILL,
                        squirdleyness,
                    );
                }
            }
            self.inner_pill_mask.clear();
            self.inner_pill_mask.resize(cw * ch, 0);
            for i in 0..(cw * ch) {
                self.inner_pill_mask[i] = ((self.pill_cache[i] >> 24) & 0xFF) as u8;
            }
            {
                let mut cache_canvas =
                    crate::canvas::Canvas::new(&mut self.pill_cache, cw, ch, &mut cache_damage);
                paint::draw_squircle_pill_two_tone_f(
                    &mut cache_canvas,
                    0,
                    0,
                    pill_w,
                    pill_h,
                    theme::TEXTBOX_SHADOW_EDGE,
                    theme::TEXTBOX_LIGHT_EDGE,
                    squirdleyness,
                    None,
                    0,
                );
            }
            self.pill_cache_dirty = false;
        }

        // --- Text cache: selected label (left-aligned) + chevron (right side), clipped by the inner-pill mask. ---
        if self.text_cache_dirty {
            paint::RASTERIZE_OPS.fetch_add(1, core::sync::atomic::Ordering::Relaxed);
            self.text_cache.clear();
            self.text_cache.resize(cw * ch, 0);
            let colour = self.text_colour();
            let mut text_damage = crate::canvas::Damage::new();
            let mut text_canvas =
                crate::canvas::Canvas::new(&mut self.text_cache, cw, ch, &mut text_damage);
            let local_y_center = pill_h as Coord * 0.5;
            let pad = self.font_size * 0.4;
            if self.font_size > 0.0 {
                let mask_buffer = paint::AlphaMask::new(&self.inner_pill_mask, cw, ch);
                text.draw_text_left_u32(
                    &mut text_canvas,
                    &self.options[self.selected],
                    pad * 1.5,
                    local_y_center,
                    self.font_size,
                    400,
                    colour,
                    self.font,
                    None,
                    Some(&mask_buffer),
                    None,
                );
            }
            // Chevron: two 45° strokes forming a "v", right-aligned. Drawn geometrically (rotated rects) rather than as a glyph so it renders identically regardless of font coverage.
            let chev_w = self.font_size * 0.55;
            let chev_cx = pill_w as Coord - pad * 1.5 - chev_w * 0.5;
            let chev_cy = local_y_center;
            let seg_len = chev_w * 0.5 * core::f32::consts::SQRT_2;
            let seg_th = (self.stroke_ru * self.font_size + 1.5).max(1.5);
            let angle = core::f32::consts::FRAC_PI_4;
            paint::draw_rect_rotated(
                &mut text_canvas,
                chev_cx - chev_w * 0.25,
                chev_cy,
                seg_len,
                seg_th,
                angle,
                colour,
                None,
            );
            paint::draw_rect_rotated(
                &mut text_canvas,
                chev_cx + chev_w * 0.25,
                chev_cy,
                seg_len,
                seg_th,
                -angle,
                colour,
                None,
            );
            self.text_cache_dirty = false;
            self.cache_disabled = !self.enabled;
        }

        // --- Composition: text/chevron (topmost) → pill (stamps hit map). ---
        blit_cache_to_target(
            &self.text_cache,
            cw,
            ch,
            pill_x_target,
            pill_y_target,
            canvas,
            None,
            0,
            clip,
        );
        blit_cache_to_target(
            &self.pill_cache,
            self.pill_cache_w,
            self.pill_cache_h,
            pill_x_target,
            pill_y_target,
            canvas,
            hit_map,
            self.hit_id,
            clip,
        );

        // --- Focus glow, Button-identical. ---
        if self.focused {
            let horiz_factor = glow_factor_256(self.font_size, 1.5);
            let vert_factor = glow_factor_256(self.font_size, 0.75);
            paint::apply_textbox_glow_right(
                canvas, pill_x_target, pill_y_target, pill_w, pill_h,
                theme::GLOW_DARK, 0x80, horiz_factor, clip,
            );
            paint::apply_textbox_glow_left(
                canvas, pill_x_target, pill_y_target, pill_w, pill_h,
                theme::GLOW_DARK, 0x80, horiz_factor, clip,
            );
            paint::apply_textbox_glow_top(
                canvas, pill_x_target, pill_y_target, pill_w, pill_h,
                theme::GLOW_DARK, 0x40, vert_factor, clip,
            );
            paint::apply_textbox_glow_bottom(
                canvas, pill_x_target, pill_y_target, pill_w, pill_h,
                theme::GLOW_DARK, 0x40, vert_factor, clip,
            );
        }

        self.last_painted_bbox = Some(region_to_pixelrect(
            self.bbox(),
            canvas.width,
            canvas.height,
        ));
        self.last_painted_focused = self.focused;
    }
}

/// Same RU-invariant glow factor as Button's.
fn glow_factor_256(font_size: f32, radius_scale: f32) -> u32 {
    let target_radius = (font_size * radius_scale).max(8.0);
    let drop = (1240.0 / target_radius) as u32;
    (256u32.saturating_sub(drop)).clamp(96, 254)
}

mod widget_impls {
    //! Capability traits. The Dropdown itself is the pill widget (click toggles, keyboard navigates/commits); each [`DropdownRow`] is a click+hover widget that records a fired flag for the parent to fold in at [`Dropdown::take_change`].

    use super::{Dropdown, DropdownRow};
    use crate::coord::Coord;
    use crate::event::{ElementState, Key as FKey, KeyEvent, ModifiersState, NamedKey};
    use crate::host::widget::{Click, Focus, Hover, Key, PaintCtx, Widget};
    use crate::paint::HitId;
    use crate::text::TextRenderer;

    impl Widget for Dropdown {
        fn id(&self) -> HitId {
            self.hit_id()
        }
        fn paint(&mut self, _ctx: &mut PaintCtx<'_, '_>) {
            // Consumers drive rendering via render_content_into / render_popup_into — same convention as Button/Textbox.
        }
        fn click(&mut self) -> Option<&mut dyn Click> {
            self.enabled.then_some(self as &mut dyn Click)
        }
        fn key(&mut self) -> Option<&mut dyn Key> {
            self.enabled.then_some(self as &mut dyn Key)
        }
        fn focus(&mut self) -> Option<&mut dyn Focus> {
            self.enabled.then_some(self as &mut dyn Focus)
        }
        fn hover(&mut self) -> Option<&mut dyn Hover> {
            self.enabled.then_some(self as &mut dyn Hover)
        }
    }

    impl Click for Dropdown {
        fn on_click(
            &mut self,
            _x: Coord,
            _y: Coord,
            _mods: ModifiersState,
        ) -> crate::host::EventResponse {
            self.toggle_open();
            crate::host::EventResponse::Handled
        }
    }

    impl Key for Dropdown {
        fn on_key(
            &mut self,
            kev: &KeyEvent,
            _mods: ModifiersState,
            _text: &mut TextRenderer,
        ) -> crate::host::EventResponse {
            if kev.state != ElementState::Pressed {
                return crate::host::EventResponse::Pass;
            }
            let n = self.options().len();
            match &kev.logical_key {
                FKey::Named(NamedKey::Enter) | FKey::Named(NamedKey::Space) => {
                    if self.is_open() {
                        let idx = self.highlight;
                        self.commit(idx);
                    } else {
                        self.open();
                    }
                    crate::host::EventResponse::Handled
                }
                FKey::Named(NamedKey::ArrowDown) => {
                    if self.is_open() {
                        self.highlight = (self.highlight + 1) % n;
                        self.sync_row_highlight();
                        self.popup_dirty = true;
                    } else {
                        let idx = (self.selected + 1) % n;
                        self.commit(idx);
                    }
                    crate::host::EventResponse::Handled
                }
                FKey::Named(NamedKey::ArrowUp) => {
                    if self.is_open() {
                        self.highlight = (self.highlight + n - 1) % n;
                        self.sync_row_highlight();
                        self.popup_dirty = true;
                    } else {
                        let idx = (self.selected + n - 1) % n;
                        self.commit(idx);
                    }
                    crate::host::EventResponse::Handled
                }
                FKey::Named(NamedKey::Escape) if self.is_open() => {
                    self.close();
                    crate::host::EventResponse::Handled
                }
                _ => crate::host::EventResponse::Pass,
            }
        }
    }

    impl Focus for Dropdown {
        fn set_focused(&mut self, focused: bool) {
            Dropdown::set_focused(self, focused);
        }
        fn focus_bbox(&self) -> Option<crate::canvas::PixelRect> {
            let r = self.bbox();
            Some(crate::canvas::PixelRect::new(
                r.x.max(0.0) as usize,
                r.y.max(0.0) as usize,
                (r.x + r.w).max(0.0) as usize,
                (r.y + r.h).max(0.0) as usize,
            ))
        }
    }

    impl Hover for Dropdown {
        fn set_hovered(&mut self, hovered: bool) {
            Dropdown::set_hovered(self, hovered);
        }
        fn tint_delta(&self) -> u32 {
            if self.is_focused() {
                crate::paint::wrap_sub_rgb(crate::theme::BUTTON_ACTIVE, crate::theme::BUTTON_FILL)
            } else if self.is_hovered() {
                crate::paint::wrap_sub_rgb(crate::theme::BUTTON_HOVER, crate::theme::BUTTON_FILL)
            } else {
                0
            }
        }
    }

    impl Widget for DropdownRow {
        fn id(&self) -> HitId {
            self.hit_id
        }
        fn paint(&mut self, _ctx: &mut PaintCtx<'_, '_>) {
            // Rows are painted collectively by Dropdown::render_popup_into.
        }
        fn click(&mut self) -> Option<&mut dyn Click> {
            Some(self)
        }
        fn hover(&mut self) -> Option<&mut dyn Hover> {
            Some(self)
        }
    }

    impl Click for DropdownRow {
        fn on_click(
            &mut self,
            _x: Coord,
            _y: Coord,
            _mods: ModifiersState,
        ) -> crate::host::EventResponse {
            self.fired = true;
            crate::host::EventResponse::Handled
        }
    }

    impl Hover for DropdownRow {
        fn set_hovered(&mut self, hovered: bool) {
            self.hovered = hovered;
        }
        fn tint_delta(&self) -> u32 {
            if self.hovered {
                crate::paint::wrap_sub_rgb(crate::theme::TEXTBOX_HOVER, crate::theme::TEXTBOX_FILL)
            } else {
                0
            }
        }
    }
}