uzor 1.3.0

Core UI engine — geometry, interaction, input state
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
//! Dropdown render entry point and per-kind layout pipelines.
//!
//! # API
//!
//! - `register_input_coordinator_dropdown` — registers the composite + child
//!   hit-rects with an `InputCoordinator`.  No drawing.  Use when explicit
//!   z-order control is needed.
//! - `register_context_manager_dropdown` — convenience wrapper: registers and
//!   draws in one call using a `ContextManager`.
//!
//! # Draw order (non-Custom kinds)
//!
//! 1. Shadow rect
//! 2. Opaque frame background + border
//! 3. Per-kind content
//! 4. Submenu sibling panel (if `view.kind` carries submenu data and state has one open)

use crate::app_context::ContextManager;
use crate::input::core::coordinator::LayerId;
use crate::input::{InputCoordinator, Sense, WidgetKind};
use crate::render::{RenderContext, TextAlign, TextBaseline};
use crate::types::{Rect, WidgetId, WidgetState, CompositeId};

use super::settings::DropdownSettings;
use super::state::DropdownState;
use super::types::{
    DropdownItem, DropdownItemRight, DropdownRenderKind, DropdownView, DropdownViewKind,
};

// ---------------------------------------------------------------------------
// Public API — measurement
// ---------------------------------------------------------------------------

/// Measure the natural size of a Flat-kind dropdown panel for the given items.
///
/// Width — widest item from the set, computed as
/// `item_padding_x + (icon? + gap) + label + middle_gap + right? + item_padding_x`,
/// clamped to at least `style.min_width()`. Right content (Shortcut / Subtitle /
/// Toggle / Checkmark / Chevron / Submenu marker) is included so dropdowns
/// auto-grow to fit secondary text.
///
/// Height — sum of per-row heights (Item / Submenu / Header / Separator) plus
/// 2× padding. If `style.max_visible_items() > 0`, the visible item count is
/// clamped to it.
pub fn measure_flat(
    items:    &[DropdownItem<'_>],
    settings: &DropdownSettings,
) -> (f64, f64) {
    let style    = settings.style.as_ref();
    let pad      = style.padding();
    let item_pad = style.item_padding_x();
    let item_h   = style.item_height();
    let header_h = style.header_height();
    let sep_h    = style.separator_height();
    let icon_sz  = style.icon_size();
    let icon_gap = style.icon_text_gap();
    let toggle_w = style.toggle_track_w();
    let max_vis  = style.max_visible_items();

    // Conservative monospace-ish text-width estimate (mlc convention).
    // Matches the same fallback used in chrome::tab_width / measure_button etc.
    const CHAR_PX: f64 = 7.0;
    // Minimum visual gap between left content (label) and right content
    // (shortcut / subtitle / toggle / chevron) so they don't touch.
    const MID_GAP: f64 = 24.0;

    let mut max_content_w = 0.0_f64;
    let mut item_count    = 0usize;
    let mut content_h     = 0.0_f64;

    for it in items {
        match it {
            DropdownItem::Item { label, icon, right, .. } => {
                if max_vis > 0 && item_count >= max_vis {
                    continue;
                }
                item_count += 1;
                content_h += item_h;

                let icon_part = if icon.is_some() { icon_sz + icon_gap } else { 0.0 };
                let label_w   = label.len() as f64 * CHAR_PX;
                let right_w   = right_content_width(right, toggle_w);
                let row_w = item_pad
                    + icon_part
                    + label_w
                    + if right_w > 0.0 { MID_GAP + right_w } else { 0.0 }
                    + item_pad;
                max_content_w = max_content_w.max(row_w);
            }
            DropdownItem::Submenu { label, .. } => {
                if max_vis > 0 && item_count >= max_vis {
                    continue;
                }
                item_count += 1;
                content_h += item_h;
                let label_w = label.len() as f64 * CHAR_PX;
                // Submenu row reserves space for the right-arrow chevron.
                let chevron_w = icon_sz; // arrow drawn at icon-size scale
                let row_w = item_pad + label_w + MID_GAP + chevron_w + item_pad;
                max_content_w = max_content_w.max(row_w);
            }
            DropdownItem::Header { label } => {
                content_h += header_h;
                let label_w = label.len() as f64 * CHAR_PX;
                max_content_w = max_content_w.max(item_pad + label_w + item_pad);
            }
            DropdownItem::Separator => content_h += sep_h,
        }
    }

    let w = max_content_w.max(style.min_width());
    let h = content_h + pad * 2.0;
    (w, h)
}

/// Width of the trailing right-side widget for an item row.
fn right_content_width(right: &DropdownItemRight<'_>, toggle_w: f64) -> f64 {
    const CHAR_PX: f64 = 7.0;
    match right {
        DropdownItemRight::None         => 0.0,
        DropdownItemRight::Shortcut(s)  => s.len() as f64 * CHAR_PX,
        DropdownItemRight::Subtitle(s)  => s.len() as f64 * CHAR_PX,
        DropdownItemRight::Toggle(_)    => toggle_w,
    }
}

// ---------------------------------------------------------------------------
// Public API — registration only
// ---------------------------------------------------------------------------

/// Register the dropdown composite and all child hit-rects with the coordinator.
///
/// No drawing occurs.  Use when explicit z-order control is needed.
///
/// Returns the [`CompositeId`] assigned to the dropdown composite.
pub fn register_input_coordinator_dropdown(
    coord:    &mut InputCoordinator,
    id:       impl Into<WidgetId>,
    rect:     Rect,
    state:    &DropdownState,
    view:     &DropdownView<'_>,
    settings: &DropdownSettings,
    kind:     DropdownRenderKind,
    layer:    &LayerId,
) -> CompositeId {
    let dd_id = coord.register_composite(id, WidgetKind::Dropdown, rect, Sense::CLICK, layer);

    if !view.open {
        return dd_id;
    }

    let origin = view.position_override.unwrap_or(state.effective_origin());
    let frame  = compute_frame(origin, rect, view, settings);
    let layout = compute_layout(frame, settings);

    match kind {
        DropdownRenderKind::Flat => {
            if let DropdownViewKind::Flat { items, submenu_items, .. } = &view.kind {
                register_flat_hits(coord, &dd_id, layout.content, items, settings, "item");
                if let Some((trigger_id, sub_items)) = submenu_items {
                    if state.submenu_open.as_deref() == Some(trigger_id) {
                        let sub_frame = compute_submenu_frame(frame, items, sub_items, trigger_id, settings, view.submenu_width);
                        let sub_layout = compute_layout(sub_frame, settings);
                        register_flat_hits(coord, &dd_id, sub_layout.content, sub_items, settings, "sub-item");
                    }
                }

            }
        }
        DropdownRenderKind::Custom => {}
    }

    dd_id
}

// ---------------------------------------------------------------------------
// Public API — convenience wrapper (ContextManager)
// ---------------------------------------------------------------------------

/// Register + draw a dropdown in one call using a `ContextManager`.
///
/// Returns the [`CompositeId`] assigned to the dropdown composite.
pub fn register_context_manager_dropdown(
    ctx_mgr:  &mut ContextManager,
    render:   &mut dyn RenderContext,
    id:       impl Into<WidgetId>,
    rect:     Rect,
    state:    &mut DropdownState,
    view:     &mut DropdownView<'_>,
    settings: &DropdownSettings,
    kind:     DropdownRenderKind,
    layer:    &LayerId,
) -> CompositeId {
    let coord = &mut ctx_mgr.input;
    let dd_id =
        register_input_coordinator_dropdown(coord, id, rect, state, view, settings, kind, layer);

    if view.open {
        draw_dropdown(render, rect, state, view, settings, kind);
    }

    dd_id
}

// ---------------------------------------------------------------------------
// Internal draw pipeline
// ---------------------------------------------------------------------------

/// Pure paint — `uzor::l0::dropdown::draw_dropdown`.  No L1 / L2 / L3 dep.
pub fn draw_dropdown(
    ctx:      &mut dyn RenderContext,
    rect:     Rect,
    state:    &DropdownState,
    view:     &DropdownView<'_>,
    settings: &DropdownSettings,
    kind:     DropdownRenderKind,
) {
    if let DropdownViewKind::Custom(ref draw) = view.kind {
        let origin = view.position_override.unwrap_or(state.effective_origin());
        let frame  = compute_frame(origin, rect, view, settings);
        draw(ctx, frame, state, settings);
        return;
    }

    let origin = view.position_override.unwrap_or(state.effective_origin());
    let frame  = compute_frame(origin, rect, view, settings);
    let layout = compute_layout(frame, settings);

    draw_frame(ctx, frame, settings);

    match kind {
        DropdownRenderKind::Flat => {
            if let DropdownViewKind::Flat {
                items,
                hovered_id,
                submenu_items,
                submenu_hovered_id,
            } = &view.kind
            {
                draw_flat_list(ctx, layout.content, items, *hovered_id, state, settings);

                // Submenu sibling panel
                if let Some((trigger_id, sub_items)) = submenu_items {
                    if state.submenu_open.as_deref() == Some(trigger_id) {
                        let sub_frame = compute_submenu_frame(frame, items, sub_items, trigger_id, settings, view.submenu_width);
                        draw_frame(ctx, sub_frame, settings);
                        let sub_layout = compute_layout(sub_frame, settings);
                        // Prefer caller-supplied hover id if present, otherwise
                        // fall back to the auto-synced value on state.
                        let sub_hov = submenu_hovered_id
                            .or(state.submenu_hovered_id.as_deref());
                        draw_flat_list(ctx, sub_layout.content, sub_items, sub_hov, state, settings);
                    }
                }
            }
        }
        DropdownRenderKind::Custom => {}
    }
}

// ---------------------------------------------------------------------------
// Frame draw helper
// ---------------------------------------------------------------------------

fn draw_frame(ctx: &mut dyn RenderContext, frame: Rect, settings: &DropdownSettings) {
    let theme = settings.theme.as_ref();
    let style = settings.style.as_ref();

    // Shadow rect
    let (sx, sy) = style.shadow_offset();
    ctx.set_fill_color(theme.shadow());
    ctx.fill_rounded_rect(
        frame.x + sx,
        frame.y + sy,
        frame.width,
        frame.height,
        style.radius(),
    );

    // Opaque background (dropdowns are always solid)
    ctx.set_fill_color(theme.bg());
    ctx.fill_rounded_rect(frame.x, frame.y, frame.width, frame.height, style.radius());

    // Border
    ctx.set_stroke_color(theme.border());
    ctx.set_stroke_width(style.border_width());
    ctx.set_line_dash(&[]);
    ctx.stroke_rounded_rect(frame.x, frame.y, frame.width, frame.height, style.radius());
}

// ---------------------------------------------------------------------------
// Layout helpers
// ---------------------------------------------------------------------------

/// Compute the panel frame rect from the anchor / origin.
fn compute_frame(
    origin:   (f64, f64),
    rect:     Rect,
    _view:    &DropdownView<'_>,
    _settings: &DropdownSettings,
) -> Rect {
    // Use the caller-provided rect dimensions; override origin from state.
    Rect::new(origin.0, origin.1, rect.width, rect.height)
}

struct DropdownLayout {
    content: Rect,
}

fn compute_layout(frame: Rect, settings: &DropdownSettings) -> DropdownLayout {
    let pad = settings.style.padding();
    DropdownLayout {
        content: Rect::new(
            frame.x + pad,
            frame.y + pad,
            (frame.width  - pad * 2.0).max(0.0),
            (frame.height - pad * 2.0).max(0.0),
        ),
    }
}

/// Compute the submenu panel frame (sibling to the right of parent),
/// row-to-head: the submenu's top edge aligns with the trigger row's top.
/// Width / height come from `measure_flat(sub_items)`.
fn compute_submenu_frame(
    parent:        Rect,
    main_items:    &[DropdownItem<'_>],
    sub_items:     &[DropdownItem<'_>],
    trigger_id:    &str,
    settings:      &DropdownSettings,
    submenu_width: super::types::SubmenuWidth,
) -> Rect {
    let style    = settings.style.as_ref();
    let pad      = style.padding();
    let gap      = style.submenu_gap();

    // Walk main_items computing each row's Y until we hit the trigger.
    let mut cursor_y = parent.y + pad;
    for item in main_items {
        let h = match item {
            DropdownItem::Header { .. }     => style.header_height(),
            DropdownItem::Separator         => style.separator_height(),
            DropdownItem::Item { .. }       => style.item_height(),
            DropdownItem::Submenu { id, .. } => {
                if *id == trigger_id { break; }
                style.item_height()
            }
        };
        cursor_y += h;
    }

    let (sw, sh) = measure_flat(sub_items, settings);
    let width = match submenu_width {
        super::types::SubmenuWidth::Auto          => sw,
        super::types::SubmenuWidth::InheritParent => parent.width,
    };
    // Natural sizing — frame keeps its own padding so its first row
    // sits inside, just like the parent panel's first row sits below
    // the parent's top padding.
    Rect::new(
        parent.x + parent.width + gap,
        cursor_y - pad,
        width,
        sh,
    )
}

// ---------------------------------------------------------------------------
// Template: Flat list
// ---------------------------------------------------------------------------

fn draw_flat_list(
    ctx:        &mut dyn RenderContext,
    content:    Rect,
    items:      &[DropdownItem<'_>],
    hovered_id: Option<&str>,
    state:      &DropdownState,
    settings:   &DropdownSettings,
) {
    let theme = settings.theme.as_ref();
    let style = settings.style.as_ref();

    let mut cursor_y = content.y;

    for item in items {
        match item {
            DropdownItem::Header { label } => {
                let h = style.header_height();
                // Header text
                ctx.set_fill_color(theme.header_text());
                ctx.set_font(&format!("bold {}px sans-serif", style.font_size()));
                ctx.set_text_align(TextAlign::Left);
                ctx.set_text_baseline(TextBaseline::Middle);
                ctx.fill_text(label, content.x + style.item_padding_x(), cursor_y + h / 2.0);
                // Bottom border
                ctx.set_fill_color(theme.header_border());
                ctx.fill_rect(content.x, cursor_y + h - 1.0, content.width, 1.0);
                cursor_y += h;
            }

            DropdownItem::Item { id, label, icon, right, disabled, danger, accent_color } => {
                let h       = style.item_height();
                let hovered = hovered_id == Some(id);
                let selected = state.selected_id.as_deref() == Some(id);

                // Background
                let bg = if hovered {
                    if *danger { theme.item_bg_danger_hover() } else { theme.item_bg_hover() }
                } else if selected {
                    theme.item_bg_selected()
                } else {
                    theme.item_bg_normal()
                };
                ctx.set_fill_color(bg);
                ctx.fill_rounded_rect(
                    content.x,
                    cursor_y,
                    content.width,
                    h,
                    style.item_hover_radius(),
                );

                // Left accent bar
                if let Some(color) = accent_color {
                    let inset = style.accent_bar_inset_y();
                    ctx.set_fill_color(color);
                    ctx.fill_rect(content.x, cursor_y + inset, style.accent_bar_w(), h - inset * 2.0);
                }

                // Icon (if present)
                let text_x = if icon.is_some() {
                    let icon_x = content.x + style.item_padding_x();
                    let icon_y = cursor_y + (h - style.icon_size()) / 2.0;
                    let icon_color = if *disabled {
                        theme.item_text_disabled()
                    } else {
                        theme.item_text()
                    };
                    ctx.set_fill_color(icon_color);
                    ctx.set_font(&format!("{}px sans-serif", style.icon_size()));
                    ctx.set_text_align(TextAlign::Left);
                    ctx.set_text_baseline(TextBaseline::Top);
                    ctx.fill_text(icon.unwrap_or(""), icon_x, icon_y);
                    icon_x + style.icon_size() + style.icon_text_gap()
                } else {
                    content.x + style.item_padding_x()
                };

                // Label
                let text_color = if *disabled {
                    theme.item_text_disabled()
                } else if *danger {
                    theme.item_text_danger()
                } else if hovered {
                    theme.item_text_hover()
                } else {
                    theme.item_text()
                };
                ctx.set_fill_color(text_color);
                ctx.set_font(&format!("{}px sans-serif", style.font_size()));
                ctx.set_text_align(TextAlign::Left);
                ctx.set_text_baseline(TextBaseline::Middle);
                ctx.fill_text(label, text_x, cursor_y + h / 2.0);

                // Right content
                match right {
                    DropdownItemRight::Shortcut(s) => {
                        ctx.set_fill_color(theme.shortcut_text());
                        ctx.set_font(&format!("{}px sans-serif", style.font_size_subtitle()));
                        ctx.set_text_align(TextAlign::Right);
                        ctx.fill_text(
                            s,
                            content.x + content.width - style.item_padding_x(),
                            cursor_y + h / 2.0,
                        );
                    }
                    DropdownItemRight::Subtitle(s) => {
                        ctx.set_fill_color(theme.item_text_disabled());
                        ctx.set_font(&format!("{}px sans-serif", style.font_size_subtitle()));
                        ctx.set_text_align(TextAlign::Right);
                        ctx.fill_text(
                            s,
                            content.x + content.width - style.item_padding_x(),
                            cursor_y + h / 2.0,
                        );
                    }
                    DropdownItemRight::Toggle(on) => {
                        draw_toggle_pill(ctx, content, cursor_y, h, *on, settings);
                    }
                    DropdownItemRight::None => {}
                }

                cursor_y += h;
            }

            DropdownItem::Separator => {
                let h = style.separator_height();
                ctx.set_fill_color(theme.separator());
                ctx.fill_rect(content.x, cursor_y + h / 2.0 - 0.5, content.width, 1.0);
                cursor_y += h;
            }

            DropdownItem::Submenu { id, label, icon, trigger, chevron_hover } => {
                let h       = style.item_height();
                let hovered = hovered_id == Some(id);
                // Chevron of a ChevronClick row may be hovered while the
                // row body is not — but only when the row opted in via
                // `chevron_hover: true`. Otherwise the chevron is purely
                // decorative and follows the row's own hover state.
                let chev_hovered = *chevron_hover
                    && state.submenu_chevron_hovered_id.as_deref() == Some(id);

                let bg = if hovered { theme.item_bg_hover() } else { theme.item_bg_normal() };
                ctx.set_fill_color(bg);
                ctx.fill_rounded_rect(
                    content.x,
                    cursor_y,
                    content.width,
                    h,
                    style.item_hover_radius(),
                );

                // Icon
                let text_x = if icon.is_some() {
                    let ix = content.x + style.item_padding_x();
                    let iy = cursor_y + (h - style.icon_size()) / 2.0;
                    ctx.set_fill_color(theme.item_text());
                    ctx.set_font(&format!("{}px sans-serif", style.icon_size()));
                    ctx.set_text_align(TextAlign::Left);
                    ctx.set_text_baseline(TextBaseline::Top);
                    ctx.fill_text(icon.unwrap_or(""), ix, iy);
                    ix + style.icon_size() + style.icon_text_gap()
                } else {
                    content.x + style.item_padding_x()
                };

                // Label
                let text_color = if hovered { theme.item_text_hover() } else { theme.item_text() };
                ctx.set_fill_color(text_color);
                ctx.set_font(&format!("{}px sans-serif", style.font_size()));
                ctx.set_text_align(TextAlign::Left);
                ctx.set_text_baseline(TextBaseline::Middle);
                ctx.fill_text(label, text_x, cursor_y + h / 2.0);

                match trigger {
                    super::types::SubmenuTrigger::Hover => {
                        // Hover-trigger row — chevron is a paint-only call,
                        // not a widget. No coord registration. Uses the same
                        // atomic chevron paint primitive as the sticky chevron
                        // in ChevronClick rows so the two row kinds line up.
                        use crate::ui::widgets::atomic::chevron::{
                            render::draw_chevron, settings::ChevronSettings,
                            types::{ChevronDirection, ChevronUseCase, ChevronView,
                                    ChevronVisualKind, HitAreaPolicy, PlacementPolicy,
                                    VisibilityPolicy},
                        };
                        let chev_size = (h * 0.6).clamp(10.0, 18.0);
                        let chev_x = content.x + content.width - style.item_padding_x() - chev_size;
                        let chev_y = cursor_y + (h - chev_size) / 2.0;
                        let cv = ChevronView {
                            direction:   ChevronDirection::Right,
                            use_case:    ChevronUseCase::SubmenuTrigger,
                            visibility:  VisibilityPolicy::Always,
                            placement:   PlacementPolicy::Standalone,
                            hit_area:    HitAreaPolicy::None,
                            visual_kind: ChevronVisualKind::Stroked,
                            hovered: false, pressed: false,
                            ..Default::default()
                        };
                        draw_chevron(ctx,
                            Rect::new(chev_x, chev_y, chev_size, chev_size),
                            &cv,
                            &ChevronSettings::default());
                    }
                    super::types::SubmenuTrigger::ChevronClick => {
                        // ChevronClick — real sticky chevron widget,
                        // registered separately by `register_flat_hits`.
                        use crate::ui::widgets::atomic::sticky_chevron::{
                            draw_sticky_chevron, StickyAnchor, StickyChevronSpec, StickyVisibility,
                        };
                        use crate::ui::widgets::atomic::chevron::types::{
                            ChevronDirection, ChevronVisualKind,
                        };
                        let chev_size_sticky = (h * 0.6).clamp(10.0, 18.0);
                        let row_rect = Rect::new(content.x, cursor_y, content.width, h);
                        let spec = StickyChevronSpec {
                            direction: ChevronDirection::Right,
                            size: chev_size_sticky,
                            inset: style.item_padding_x(),
                            anchor: StickyAnchor::E,
                            visibility: StickyVisibility::Always,
                            visual: ChevronVisualKind::Stroked,
                            hover_visual: *chevron_hover,
                            interactive: true,
                        };
                        let chev_state = if chev_hovered {
                            WidgetState::Hovered
                        } else {
                            WidgetState::Normal
                        };
                        draw_sticky_chevron(ctx, row_rect, &spec, chev_state, WidgetState::Normal);
                    }
                }

                cursor_y += h;
            }
        }
    }
}

// ---------------------------------------------------------------------------
// Template: Inline (split button)
// ---------------------------------------------------------------------------

// ---------------------------------------------------------------------------

fn register_flat_hits(
    coord:       &mut InputCoordinator,
    parent:      &CompositeId,
    content:     Rect,
    items:       &[DropdownItem<'_>],
    settings:    &DropdownSettings,
    item_prefix: &str,
) {
    let style     = settings.style.as_ref();
    let mut cursor_y = content.y;

    for item in items {
        match item {
            DropdownItem::Header { .. } => {
                cursor_y += style.header_height();
            }
            DropdownItem::Item { id, disabled, .. } => {
                let h = style.item_height();
                if !disabled {
                    coord.register_child(
                        parent,
                        format!("{}:{}:{}", parent.0.0, item_prefix, id),
                        WidgetKind::Button,
                        Rect::new(content.x, cursor_y, content.width, h),
                        Sense::CLICK | Sense::HOVER,
                    );
                }
                cursor_y += h;
            }
            DropdownItem::Separator => {
                cursor_y += style.separator_height();
            }
            DropdownItem::Submenu { id, trigger, chevron_hover, .. } => {
                let h = style.item_height();
                let row = Rect::new(content.x, cursor_y, content.width, h);
                let chev_size = (h * 0.6).clamp(10.0, 18.0);
                let pad_x = style.item_padding_x();
                use crate::ui::widgets::atomic::sticky_chevron::{
                    register_sticky_chevron, StickyAnchor, StickyChevronSpec, StickyVisibility,
                };
                use crate::ui::widgets::atomic::chevron::types::{
                    ChevronDirection, ChevronVisualKind,
                };
                match trigger {
                    super::types::SubmenuTrigger::Hover => {
                        // Row body is the hit zone — opens submenu on row hover.
                        // Chevron is rendered as plain text in draw_flat_list,
                        // no separate widget registration.
                        coord.register_child(
                            parent,
                            format!("{}:submenu:{}", parent.0.0, id),
                            WidgetKind::Button,
                            row,
                            Sense::CLICK | Sense::HOVER,
                        );
                    }
                    super::types::SubmenuTrigger::ChevronClick => {
                        // Row body is inert — only the chevron opens the
                        // submenu. The chevron is a real interactive sticky.
                        let spec = StickyChevronSpec {
                            direction: ChevronDirection::Right,
                            size: chev_size,
                            inset: pad_x,
                            anchor: StickyAnchor::E,
                            visibility: StickyVisibility::Always,
                            visual: ChevronVisualKind::Stroked,
                            hover_visual: *chevron_hover,
                            interactive: true,
                        };
                        let _ = register_sticky_chevron(
                            coord, parent, row, &spec, WidgetState::Normal,
                            &format!("submenu:{id}"),
                        );
                    }
                }
                cursor_y += h;
            }
        }
    }
}


// ---------------------------------------------------------------------------
// Toggle pill helper (used by Flat rows with DropdownItemRight::Toggle)
// ---------------------------------------------------------------------------

fn draw_toggle_pill(
    ctx:      &mut dyn RenderContext,
    content:  Rect,
    cursor_y: f64,
    row_h:    f64,
    on:       bool,
    settings: &DropdownSettings,
) {
    let theme     = settings.theme.as_ref();
    let style     = settings.style.as_ref();
    let track_w   = style.toggle_track_w();
    let track_h   = style.toggle_track_h();
    let thumb_d   = style.toggle_thumb_d();
    let pad       = style.item_padding_x();

    let track_x = content.x + content.width - pad - track_w;
    let track_y = cursor_y + (row_h - track_h) / 2.0;

    let track_color = if on { theme.toggle_on() } else { theme.toggle_off() };
    ctx.set_fill_color(track_color);
    ctx.fill_rounded_rect(track_x, track_y, track_w, track_h, track_h / 2.0);

    let thumb_margin = (track_h - thumb_d) / 2.0;
    let thumb_x = if on {
        track_x + track_w - thumb_d - thumb_margin
    } else {
        track_x + thumb_margin
    };
    let thumb_y = track_y + thumb_margin;

    ctx.set_fill_color(theme.toggle_thumb());
    ctx.fill_rounded_rect(thumb_x, thumb_y, thumb_d, thumb_d, thumb_d / 2.0);
}