quadraui 0.6.0

Cross-platform Rust UI toolkit: write one AppLogic and run it unmodified on TUI, GTK4 (Linux), macOS, and Windows backends
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
//! Drop-zone computation for tab drag-and-drop.
//!
//! Given a set of group rectangles (each with per-tab slot positions)
//! and a cursor position, [`compute_drop_zone`] determines where a
//! dragged tab would land: center of a group, an edge split, or a
//! reorder position within the tab bar.
//!
//! [`DropOverlay`] translates a [`DropZone`] result into visual
//! overlay geometry (highlight rect, insertion bar, ghost label
//! position) that backends render on top of the normal frame.

use crate::event::Rect;
use serde::{Deserialize, Serialize};

/// A group's bounds and per-tab slot positions, supplied by the
/// consumer's layout system. `tab_slots` contains `(start_x, end_x)`
/// pairs in the same coordinate system as `bounds`.
#[derive(Debug, Clone)]
pub struct DropGroupRect {
    pub bounds: Rect,
    pub tab_slots: Vec<(f32, f32)>,
}

/// Cardinal direction for a split drop.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum DropEdge {
    Left,
    Right,
    Top,
    Bottom,
}

/// What kind of drop zone the cursor is over.
#[derive(Debug, Clone, PartialEq, Default)]
pub enum DropZoneKind {
    /// Drop onto the center — tab joins this group.
    #[default]
    Center,
    /// Drop on an edge — split the group in this direction.
    Split(DropEdge),
    /// Drop between tabs — reorder within the tab bar.
    /// The `usize` is the insertion index (0 = before first tab,
    /// `tab_count` = after last tab).
    TabReorder(usize),
}

/// Result of [`compute_drop_zone`].
///
/// # Examples
///
/// ```
/// use quadraui::{compute_drop_zone, DropGroupRect, DropZoneKind, Rect};
///
/// let groups = vec![DropGroupRect {
///     bounds: Rect::new(0.0, 0.0, 100.0, 50.0),
///     tab_slots: vec![],
/// }];
///
/// // Cursor over the group's content area, away from any edge, lands
/// // on `Center` — the tab would join this group.
/// let zone = compute_drop_zone(50.0, 30.0, &groups, 2.0).expect("cursor is over a group");
/// assert_eq!(zone.group_idx, 0);
/// assert_eq!(zone.kind, DropZoneKind::Center);
/// ```
#[derive(Debug, Clone, PartialEq, Default)]
#[non_exhaustive]
pub struct DropZone {
    pub kind: DropZoneKind,
    /// Which group (index into the `groups` slice) this targets.
    pub group_idx: usize,
}

/// Compute which drop zone a cursor position falls into.
///
/// `tab_bar_height` is the height of the tab bar region at the top of
/// each group. Cursor positions within the tab bar region produce
/// `TabReorder`; positions in the content area below produce `Center`
/// or `Split` depending on edge proximity.
///
/// Returns `None` if the cursor is outside all groups.
pub fn compute_drop_zone(
    cursor_x: f32,
    cursor_y: f32,
    groups: &[DropGroupRect],
    tab_bar_height: f32,
) -> Option<DropZone> {
    for (gi, group) in groups.iter().enumerate() {
        let b = &group.bounds;
        if cursor_x < b.x
            || cursor_x >= b.x + b.width
            || cursor_y < b.y
            || cursor_y >= b.y + b.height
        {
            continue;
        }

        let in_tab_bar = cursor_y < b.y + tab_bar_height && tab_bar_height > 0.0;

        if in_tab_bar && !group.tab_slots.is_empty() {
            let insert_idx = tab_reorder_index(cursor_x, &group.tab_slots);
            return Some(DropZone {
                kind: DropZoneKind::TabReorder(insert_idx),
                group_idx: gi,
            });
        }

        let content_y = b.y + tab_bar_height;
        let content_h = (b.height - tab_bar_height).max(0.0);
        if content_h <= 0.0 {
            return Some(DropZone {
                kind: DropZoneKind::Center,
                group_idx: gi,
            });
        }

        let edge_w = edge_zone_size(b.width);
        let edge_h = edge_zone_size(content_h);

        let rel_x = cursor_x - b.x;
        let rel_y = cursor_y - content_y;

        if rel_x < edge_w {
            return Some(DropZone {
                kind: DropZoneKind::Split(DropEdge::Left),
                group_idx: gi,
            });
        }
        if rel_x >= b.width - edge_w {
            return Some(DropZone {
                kind: DropZoneKind::Split(DropEdge::Right),
                group_idx: gi,
            });
        }
        if rel_y < edge_h {
            return Some(DropZone {
                kind: DropZoneKind::Split(DropEdge::Top),
                group_idx: gi,
            });
        }
        if rel_y >= content_h - edge_h {
            return Some(DropZone {
                kind: DropZoneKind::Split(DropEdge::Bottom),
                group_idx: gi,
            });
        }

        return Some(DropZone {
            kind: DropZoneKind::Center,
            group_idx: gi,
        });
    }
    None
}

/// Hit-test classification for a cursor position against a set of
/// [`DropGroupRect`]s.
#[derive(Debug, Clone, PartialEq)]
pub enum DropZoneHit {
    /// Cursor is over a live drop zone.
    Zone(DropZone),
    /// Cursor is outside every group.
    Empty,
}

/// Hit-test a cursor position against `groups`, following this
/// primitive's `Empty`-variant convention on top of [`compute_drop_zone`].
/// Identical arguments and behaviour to
/// [`compute_drop_zone`] — this only wraps its `Option` result in a
/// [`DropZoneHit`] so `DropZone`, like every other primitive, has a
/// `hit_test` entry point with a named miss case instead of `None`.
pub fn drop_zone_hit_test(
    cursor_x: f32,
    cursor_y: f32,
    groups: &[DropGroupRect],
    tab_bar_height: f32,
) -> DropZoneHit {
    match compute_drop_zone(cursor_x, cursor_y, groups, tab_bar_height) {
        Some(zone) => DropZoneHit::Zone(zone),
        None => DropZoneHit::Empty,
    }
}

/// Compute the insertion index from cursor x and tab slot positions.
/// Finds the midpoint of each tab; cursor left of midpoint inserts
/// before, right inserts after.
fn tab_reorder_index(cursor_x: f32, slots: &[(f32, f32)]) -> usize {
    for (i, (start, end)) in slots.iter().enumerate() {
        let mid = (*start + *end) / 2.0;
        if cursor_x < mid {
            return i;
        }
    }
    slots.len()
}

/// Edge zone size: 20% of dimension, clamped to [3, dimension/2].
fn edge_zone_size(dimension: f32) -> f32 {
    (dimension * 0.2).clamp(3.0, dimension / 2.0)
}

// ── Drop overlay ────────────────────────────────────────────────────────────

/// Visual overlay components for rendering a drop zone indicator.
/// Backends draw these on top of the normal frame during a tab drag.
#[derive(Debug, Clone, PartialEq)]
pub struct DropOverlay {
    /// Tinted rectangle highlighting the target zone.
    pub highlight: Option<Rect>,
    /// Vertical or horizontal insertion bar for tab reorder.
    pub insertion_bar: Option<Rect>,
    /// Ghost label position: `(x, y)` near the cursor.
    pub ghost_position: Option<(f32, f32)>,
}

impl DropOverlay {
    /// Alpha applied to the highlight tint. Shared by every backend
    /// rasteriser (`gtk::drop_overlay`, `macos::drop_overlay`,
    /// `win::drop_overlay`) so they agree on a value by construction
    /// rather than by three separately-copied literals — see
    /// `PRIMITIVE_RULES.md`'s primitive-first rule.
    pub const HIGHLIGHT_ALPHA: f32 = 0.15;

    /// Minimum insertion-bar thickness (px on GTK/macOS, DIPs on
    /// Win-GUI), applied when [`DropOverlay::insertion_bar`]'s computed
    /// width/height would otherwise be thinner (e.g. a zero-width bar
    /// from [`drop_zone_overlay`]).
    pub const MIN_BAR_THICKNESS: f32 = 2.0;

    /// The rect a hit-test / accessibility consumer should treat as
    /// this overlay's bounds — [`Self::highlight`] if present,
    /// otherwise [`Self::insertion_bar`], otherwise a zero-sized
    /// [`Rect::default`] (an overlay with neither carries no visible
    /// geometry, same as `drop_zone_overlay` never constructing one).
    ///
    /// Backs [`crate::frame::Surface::DropOverlay`]: unlike every other
    /// transient-overlay `Surface` variant (`Tooltip`, `ContextMenu`,
    /// `Dialog`, `Completions`, `RichTextPopup`), this primitive has no
    /// sibling `*Layout` type carrying a single `bounds` field to
    /// borrow — this method is that single source of truth instead.
    pub fn bounds(&self) -> Rect {
        self.highlight.or(self.insertion_bar).unwrap_or_default()
    }
}

/// Compute overlay geometry for a drop zone.
///
/// `bar_height` is the thickness of the insertion bar (typically 2–3
/// pixels in GTK, 1 cell in TUI). `ghost_offset` is how far the ghost
/// label floats from the cursor (typically one line height).
pub fn drop_zone_overlay(
    zone: &DropZone,
    groups: &[DropGroupRect],
    cursor_x: f32,
    cursor_y: f32,
    tab_bar_height: f32,
    bar_thickness: f32,
    ghost_offset: f32,
) -> DropOverlay {
    let b = groups[zone.group_idx].bounds;
    let content_y = b.y + tab_bar_height;
    let content_h = (b.height - tab_bar_height).max(0.0);

    match &zone.kind {
        DropZoneKind::Center => DropOverlay {
            highlight: Some(Rect::new(b.x, content_y, b.width, content_h)),
            insertion_bar: None,
            ghost_position: Some((cursor_x + ghost_offset, cursor_y)),
        },
        DropZoneKind::Split(dir) => {
            let half_w = b.width / 2.0;
            let half_h = content_h / 2.0;
            let highlight = match dir {
                DropEdge::Left => Rect::new(b.x, content_y, half_w, content_h),
                DropEdge::Right => Rect::new(b.x + half_w, content_y, half_w, content_h),
                DropEdge::Top => Rect::new(b.x, content_y, b.width, half_h),
                DropEdge::Bottom => Rect::new(b.x, content_y + half_h, b.width, half_h),
            };
            DropOverlay {
                highlight: Some(highlight),
                insertion_bar: None,
                ghost_position: Some((cursor_x + ghost_offset, cursor_y)),
            }
        }
        DropZoneKind::TabReorder(idx) => {
            let slots = &groups[zone.group_idx].tab_slots;
            let bar_x = if *idx == 0 {
                slots.first().map_or(b.x, |(s, _)| *s)
            } else if *idx >= slots.len() {
                slots.last().map_or(b.x, |(_, e)| *e)
            } else {
                let (_, prev_end) = slots[*idx - 1];
                let (next_start, _) = slots[*idx];
                (prev_end + next_start) / 2.0
            };
            DropOverlay {
                highlight: None,
                insertion_bar: Some(Rect::new(
                    bar_x - bar_thickness / 2.0,
                    b.y,
                    bar_thickness,
                    tab_bar_height,
                )),
                ghost_position: Some((cursor_x + ghost_offset, cursor_y)),
            }
        }
    }
}

// Shared implementation of drop overlay painting, factored from
// GTK, macOS, and Windows backends to use [`crate::paint_surface::PaintSurface`].
//
// Windows's pre-migration implementation CPU-premixed the tint against
// `theme.background` before an opaque fill. This shared implementation
// uses `PaintSurface::surface_fill_rect`, which paints a real alpha blend
// on all backends, fixing Windows's drop-overlay highlight to match
// GTK/macOS's real-blend behaviour.
//
// `#[allow(dead_code)]`: only called once a real pixel backend is compiled in,
// exercised by each backend's own `Backend::draw_drop_overlay` call sites plus
// this module's own `RecordingSurface` tests.
#[cfg(any(
    feature = "gtk",
    feature = "win",
    all(feature = "macos", target_os = "macos")
))]
#[allow(dead_code)]
pub(crate) mod native_surface_paint {
    use super::DropOverlay;
    use crate::paint_surface::PaintSurface;
    use crate::theme::Theme;

    /// Paint a [`DropOverlay`] onto `surface`: a translucent highlight
    /// rect at [`DropOverlay::HIGHLIGHT_ALPHA`] and/or a solid insertion
    /// bar widened to at least [`DropOverlay::MIN_BAR_THICKNESS`], both
    /// in `theme.accent_fg`. `ghost_position` is not painted here — no
    /// backend renders a ghost label (see the per-backend module docs),
    /// so callers that want one draw it themselves on top.
    pub(crate) fn paint(overlay: &DropOverlay, surface: &mut dyn PaintSurface, theme: &Theme) {
        if let Some(h) = overlay.highlight {
            if h.width > 0.0 && h.height > 0.0 {
                surface.surface_fill_rect(
                    h,
                    theme
                        .accent_fg
                        .with_alpha(DropOverlay::HIGHLIGHT_ALPHA as f64),
                );
            }
        }

        if let Some(bar) = overlay.insertion_bar {
            if bar.height > 0.0 {
                let widened = crate::event::Rect::new(
                    bar.x,
                    bar.y,
                    bar.width.max(DropOverlay::MIN_BAR_THICKNESS),
                    bar.height,
                );
                surface.surface_fill_rect(widened, theme.accent_fg);
            }
        }
    }

    #[cfg(test)]
    mod tests {
        use super::*;
        use crate::backend::ImagePaintResult;
        use crate::event::{Rect, Viewport};
        use crate::types::Color;
        use crate::Image;

        /// Records every `surface_fill_rect` call — mirrors
        /// `primitives::scrollbar`'s `RecordingSurface` test double,
        /// scoped to just the verb this primitive uses, so this test
        /// runs on any host without Cairo/Core Graphics/Direct2D.
        #[derive(Default)]
        struct RecordingSurface {
            fills: Vec<(Rect, Color)>,
        }

        impl PaintSurface for RecordingSurface {
            fn surface_begin_frame(&mut self, _viewport: Viewport) {}
            fn surface_end_frame(&mut self) {}
            fn surface_viewport(&self) -> Viewport {
                Viewport::new(200.0, 100.0, 1.0)
            }
            fn surface_line_height(&self) -> f32 {
                16.0
            }
            fn surface_char_width(&self) -> f32 {
                8.0
            }
            fn surface_measure_text(&self, _text: &str) -> (f32, f32) {
                (0.0, 0.0)
            }
            fn surface_fill_rect(&mut self, rect: Rect, color: Color) {
                self.fills.push((rect, color));
            }
            /// Test-only recorder — `paint` never calls this verb
            /// (see this module's own doc for why no primitive here has been
            /// migrated onto it yet); this exists only so `RecordingSurface`
            /// satisfies the trait. Records into the same `fills` list as
            /// `surface_fill_rect` (radius dropped) — no test asserts on it
            /// today.
            fn surface_fill_rounded_rect(&mut self, rect: Rect, _radius: f32, color: Color) {
                self.fills.push((rect, color));
            }
            fn surface_stroke_rect(&mut self, _rect: Rect, _color: Color, _stroke_width: f32) {}
            fn surface_stroke_rounded_rect(
                &mut self,
                _rect: Rect,
                _radius: f32,
                _color: Color,
                _stroke_width: f32,
            ) {
            }
            fn surface_draw_text_run(&mut self, _rect: Rect, _text: &str, _color: Color) {}
            fn surface_draw_line(
                &mut self,
                _from: crate::Point,
                _to: crate::Point,
                _color: Color,
                _stroke_width: f32,
            ) {
            }
            fn surface_push_clip(&mut self, _rect: Rect) {}
            fn surface_pop_clip(&mut self) {}
            fn surface_draw_image(&mut self, _rect: Rect, _image: &Image) -> ImagePaintResult {
                ImagePaintResult::Unsupported
            }
        }

        /// Regression pinning this migration's found divergence: the
        /// highlight must carry real alpha (`Color::a < 255`), matching
        /// GTK/macOS's pre-migration behaviour and *correcting*
        /// Windows's pre-migration CPU-premix (see this module's doc).
        #[test]
        fn highlight_paints_with_real_alpha_not_opaque() {
            let overlay = DropOverlay {
                highlight: Some(Rect::new(10.0, 10.0, 50.0, 30.0)),
                insertion_bar: None,
                ghost_position: None,
            };
            let theme = Theme::default();
            let mut surface = RecordingSurface::default();
            paint(&overlay, &mut surface, &theme);

            assert_eq!(
                surface.fills.len(),
                1,
                "expected exactly the highlight fill"
            );
            let (rect, color) = surface.fills[0];
            assert_eq!(rect, Rect::new(10.0, 10.0, 50.0, 30.0));
            assert!(
                color.a < 255,
                "highlight fill must carry real alpha, got opaque a={}",
                color.a
            );
        }

        #[test]
        fn insertion_bar_paints_opaque() {
            let overlay = DropOverlay {
                highlight: None,
                insertion_bar: Some(Rect::new(40.0, 0.0, 2.0, 20.0)),
                ghost_position: None,
            };
            let theme = Theme::default();
            let mut surface = RecordingSurface::default();
            paint(&overlay, &mut surface, &theme);

            assert_eq!(surface.fills.len(), 1, "expected exactly the bar fill");
            let (_, color) = surface.fills[0];
            assert_eq!(
                color, theme.accent_fg,
                "insertion bar is solid theme.accent_fg"
            );
        }

        #[test]
        fn insertion_bar_widens_to_min_thickness() {
            let overlay = DropOverlay {
                highlight: None,
                insertion_bar: Some(Rect::new(40.0, 0.0, 0.0, 20.0)),
                ghost_position: None,
            };
            let mut surface = RecordingSurface::default();
            paint(&overlay, &mut surface, &Theme::default());

            let (rect, _) = surface.fills[0];
            assert_eq!(rect.width, DropOverlay::MIN_BAR_THICKNESS);
        }

        #[test]
        fn zero_size_highlight_paints_nothing() {
            let overlay = DropOverlay {
                highlight: Some(Rect::new(10.0, 10.0, 0.0, 0.0)),
                insertion_bar: None,
                ghost_position: None,
            };
            let mut surface = RecordingSurface::default();
            paint(&overlay, &mut surface, &Theme::default());
            assert!(surface.fills.is_empty());
        }

        #[test]
        fn empty_overlay_paints_nothing() {
            let overlay = DropOverlay {
                highlight: None,
                insertion_bar: None,
                ghost_position: None,
            };
            let mut surface = RecordingSurface::default();
            paint(&overlay, &mut surface, &Theme::default());
            assert!(surface.fills.is_empty());
        }

        #[test]
        fn both_highlight_and_bar_paint_two_fills() {
            let overlay = DropOverlay {
                highlight: Some(Rect::new(0.0, 0.0, 20.0, 20.0)),
                insertion_bar: Some(Rect::new(40.0, 0.0, 2.0, 20.0)),
                ghost_position: None,
            };
            let mut surface = RecordingSurface::default();
            paint(&overlay, &mut surface, &Theme::default());
            assert_eq!(surface.fills.len(), 2);
        }
    }
}

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

    fn group(x: f32, y: f32, w: f32, h: f32, slots: &[(f32, f32)]) -> DropGroupRect {
        DropGroupRect {
            bounds: Rect::new(x, y, w, h),
            tab_slots: slots.to_vec(),
        }
    }

    #[test]
    fn center_drop() {
        let groups = vec![group(0.0, 0.0, 100.0, 80.0, &[])];
        let zone = compute_drop_zone(50.0, 50.0, &groups, 20.0).unwrap();
        assert_eq!(zone.group_idx, 0);
        assert_eq!(zone.kind, DropZoneKind::Center);
    }

    #[test]
    fn left_edge_split() {
        let groups = vec![group(0.0, 0.0, 100.0, 80.0, &[])];
        let zone = compute_drop_zone(5.0, 50.0, &groups, 20.0).unwrap();
        assert_eq!(zone.kind, DropZoneKind::Split(DropEdge::Left));
    }

    #[test]
    fn right_edge_split() {
        let groups = vec![group(0.0, 0.0, 100.0, 80.0, &[])];
        let zone = compute_drop_zone(95.0, 50.0, &groups, 20.0).unwrap();
        assert_eq!(zone.kind, DropZoneKind::Split(DropEdge::Right));
    }

    #[test]
    fn top_edge_split() {
        let groups = vec![group(0.0, 0.0, 100.0, 80.0, &[])];
        // tab_bar_height=20, content starts at y=20, edge_h = 60*0.2 = 12
        let zone = compute_drop_zone(50.0, 25.0, &groups, 20.0).unwrap();
        assert_eq!(zone.kind, DropZoneKind::Split(DropEdge::Top));
    }

    #[test]
    fn bottom_edge_split() {
        let groups = vec![group(0.0, 0.0, 100.0, 80.0, &[])];
        let zone = compute_drop_zone(50.0, 75.0, &groups, 20.0).unwrap();
        assert_eq!(zone.kind, DropZoneKind::Split(DropEdge::Bottom));
    }

    #[test]
    fn tab_reorder_before_first() {
        let slots = vec![(10.0, 40.0), (40.0, 70.0)];
        let groups = vec![group(0.0, 0.0, 100.0, 80.0, &slots)];
        // Cursor at x=15, left of first tab's midpoint (25)
        let zone = compute_drop_zone(15.0, 5.0, &groups, 20.0).unwrap();
        assert_eq!(zone.kind, DropZoneKind::TabReorder(0));
    }

    #[test]
    fn tab_reorder_between_tabs() {
        let slots = vec![(10.0, 40.0), (40.0, 70.0)];
        let groups = vec![group(0.0, 0.0, 100.0, 80.0, &slots)];
        // Cursor at x=35, right of first midpoint (25), left of second (55)
        let zone = compute_drop_zone(35.0, 5.0, &groups, 20.0).unwrap();
        assert_eq!(zone.kind, DropZoneKind::TabReorder(1));
    }

    #[test]
    fn tab_reorder_after_last() {
        let slots = vec![(10.0, 40.0), (40.0, 70.0)];
        let groups = vec![group(0.0, 0.0, 100.0, 80.0, &slots)];
        // Cursor at x=60, right of second midpoint (55)
        let zone = compute_drop_zone(60.0, 5.0, &groups, 20.0).unwrap();
        assert_eq!(zone.kind, DropZoneKind::TabReorder(2));
    }

    #[test]
    fn cursor_outside_all_groups() {
        let groups = vec![group(0.0, 0.0, 100.0, 80.0, &[])];
        assert!(compute_drop_zone(200.0, 200.0, &groups, 20.0).is_none());
    }

    #[test]
    fn hit_test_returns_zone_for_center_drop() {
        let groups = vec![group(0.0, 0.0, 100.0, 80.0, &[])];
        match drop_zone_hit_test(50.0, 50.0, &groups, 20.0) {
            DropZoneHit::Zone(zone) => {
                assert_eq!(zone.kind, DropZoneKind::Center);
                assert_eq!(zone.group_idx, 0);
            }
            DropZoneHit::Empty => panic!("expected a zone hit"),
        }
    }

    #[test]
    fn hit_test_outside_all_groups_is_empty() {
        let groups = vec![group(0.0, 0.0, 100.0, 80.0, &[])];
        assert_eq!(
            drop_zone_hit_test(200.0, 200.0, &groups, 20.0),
            DropZoneHit::Empty
        );
    }

    #[test]
    fn multiple_groups_selects_correct() {
        let groups = vec![
            group(0.0, 0.0, 50.0, 80.0, &[]),
            group(50.0, 0.0, 50.0, 80.0, &[]),
        ];
        let zone = compute_drop_zone(75.0, 50.0, &groups, 20.0).unwrap();
        assert_eq!(zone.group_idx, 1);
    }

    #[test]
    fn overlay_center_covers_content_area() {
        let groups = vec![group(0.0, 0.0, 100.0, 80.0, &[])];
        let zone = DropZone {
            kind: DropZoneKind::Center,
            group_idx: 0,
        };
        let ov = drop_zone_overlay(&zone, &groups, 50.0, 50.0, 20.0, 2.0, 10.0);
        assert_eq!(ov.highlight, Some(Rect::new(0.0, 20.0, 100.0, 60.0)));
        assert!(ov.insertion_bar.is_none());
    }

    #[test]
    fn overlay_split_left_covers_left_half() {
        let groups = vec![group(0.0, 0.0, 100.0, 80.0, &[])];
        let zone = DropZone {
            kind: DropZoneKind::Split(DropEdge::Left),
            group_idx: 0,
        };
        let ov = drop_zone_overlay(&zone, &groups, 5.0, 50.0, 20.0, 2.0, 10.0);
        assert_eq!(ov.highlight, Some(Rect::new(0.0, 20.0, 50.0, 60.0)));
    }

    #[test]
    fn overlay_tab_reorder_insertion_bar() {
        let slots = vec![(10.0, 40.0), (40.0, 70.0)];
        let groups = vec![group(0.0, 0.0, 100.0, 80.0, &slots)];
        let zone = DropZone {
            kind: DropZoneKind::TabReorder(1),
            group_idx: 0,
        };
        let ov = drop_zone_overlay(&zone, &groups, 35.0, 5.0, 20.0, 2.0, 10.0);
        assert!(ov.highlight.is_none());
        let bar = ov.insertion_bar.unwrap();
        assert_eq!(bar.x, 39.0); // midpoint of prev_end(40) and next_start(40) = 40, minus half thickness
        assert_eq!(bar.width, 2.0);
        assert_eq!(bar.height, 20.0);
    }

    /// Backs [`crate::frame::Surface::DropOverlay`]: `bounds()` is
    /// this primitive's only source of a zone rect, so it needs its
    /// own direct coverage independent of the `Surface`/`FrameHitMap`
    /// round-trip test in `frame.rs`.
    #[test]
    fn bounds_prefers_highlight_then_insertion_bar_then_default() {
        let highlight = Rect::new(1.0, 2.0, 3.0, 4.0);
        let bar = Rect::new(5.0, 6.0, 7.0, 8.0);

        let both = DropOverlay {
            highlight: Some(highlight),
            insertion_bar: Some(bar),
            ghost_position: None,
        };
        assert_eq!(both.bounds(), highlight);

        let bar_only = DropOverlay {
            highlight: None,
            insertion_bar: Some(bar),
            ghost_position: None,
        };
        assert_eq!(bar_only.bounds(), bar);

        let neither = DropOverlay {
            highlight: None,
            insertion_bar: None,
            ghost_position: None,
        };
        assert_eq!(neither.bounds(), Rect::default());
    }
}