quadraui 0.6.1

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
//! macOS rasteriser for [`crate::ActivityBar`].
//!
//! Vertical strip of icon rows. Layout (row positions, top-vs-bottom
//! placement) is the shared [`crate::primitives::activity_bar::ActivityBar::layout`]
//! (matching `gtk::activity_bar` / `win::activity_bar`). Content painting uses the
//! shared [`crate::primitives::activity_bar::native_surface_paint::paint`] — see
//! that fn's module doc for details.
//!
//! Uses the backend's active `CTFont` for icon rendering — apps that
//! want Nerd-Font icons install a glyph-bearing font via
//! [`super::MacBackend::set_current_font`] in `setup()` — but **not**
//! at that font's own size. The icon glyph always paints at
//! [`crate::ActivityBarStyle::resolved_icon_size_px`] (defaulting to
//! [`crate::primitives::activity_bar::DEFAULT_ACTIVITY_ICON_SIZE_PX`],
//! VS-Code parity), via [`core_text::font::CTFont::clone_with_font_size`]
//! on whatever font the caller passed in — a same-family, same-fallback
//! clone at a fixed size. `size` in `clone_with_font_size` is Core Text
//! points, which on macOS are already device-independent pixels 1:1 (unlike
//! GTK's Pango / Win-GUI's DirectWrite, both of which apply the legacy 96/72
//! dpi point-to-pixel ratio — see `crate::gtk::activity_bar::activity_bar_icon_font`'s
//! doc), so `resolved_icon_size_px()` is used here without conversion.
//!
//! Returns per-row [`ActivityBarRowHit`]s so callers can route clicks
//! and query tooltips against the same frame's painted positions.
//!
//! # Coordinate space
//!
//! Spans are **bar-relative**, matching the [`crate::Backend::draw_activity_bar`]
//! contract: this function paints into `(0, 0, width, height)` and is never
//! handed the bar's origin at all, so it *cannot* fold the origin in.
//!
//! Painting into `(0, 0, width, height)` only lands at the bar's *actual*
//! screen position if the caller separately places the CGContext's origin there
//! first. `MacBackend::draw_activity_bar[_with_style]` wraps this module's
//! calls in `CGContextTranslateCTM(ctx, rect.x, rect.y)` (save/translate/restore),
//! mirroring `GtkBackend::draw_activity_bar`'s `cr.translate(rect.x, rect.y)` —
//! this module itself needed no change, since bar-relative paint is exactly
//! what a translated context wants.

use core_graphics::sys::CGContextRef;
use core_text::font::CTFont;

use crate::primitives::activity_bar::{
    native_surface_paint, ActivityBar, ActivityBarRowHit, ActivityBarStyle,
};
use crate::theme::Theme;

/// Fixed row height in points. Matches `crate::gtk::activity_bar::ACTIVITY_ROW_PX`
/// (= the vimcode native-button height baked into the GTK CSS).
pub const ACTIVITY_ROW_PX: f64 = 48.0;

/// Compute the per-row hit spans [`draw_activity_bar`] would produce for
/// `bar` in a `width` × `height` strip, without painting.
///
/// No-paint twin backing [`crate::Backend::activity_bar_layout`]. Spans
/// are **bar-relative** — `y_start` / `y_end` measured from the top edge
/// of the bar, first row at `0.0` — exactly as the trait requires.
/// Delegates to the shared [`crate::primitives::activity_bar::ActivityBar::layout`] —
/// same call `draw_activity_bar` makes internally, so the two cannot
/// drift apart, and `visible_items` order matches `gtk::activity_bar` /
/// `win::activity_bar` (bottom-pinned items first).
pub fn mac_activity_bar_layout(
    width: f64,
    height: f64,
    bar: &ActivityBar,
) -> Vec<ActivityBarRowHit> {
    bar.layout(width as f32, height as f32, ACTIVITY_ROW_PX as f32)
        .visible_items
        .into_iter()
        .map(|vi| {
            let item = match vi.side {
                crate::primitives::activity_bar::ActivitySide::Top => &bar.top_items[vi.item_idx],
                crate::primitives::activity_bar::ActivitySide::Bottom => {
                    &bar.bottom_items[vi.item_idx]
                }
            };
            ActivityBarRowHit {
                y_start: vi.bounds.y,
                y_end: vi.bounds.y + vi.bounds.height,
                id: item.id,
                tooltip: item.tooltip.clone(),
            }
        })
        .collect()
}

/// Paint `bar` into `(0, 0, width, height)` on `ctx`.
///
/// Equivalent to [`draw_activity_bar_with_style`] with
/// `ActivityBarStyle::default()`, i.e. no active-row fill — see that
/// function for the full behaviour.
///
/// `nerd_fonts_enabled` picks which half of each item's [`crate::Icon`]
/// paints — `glyph` when `true`, `fallback` when `false`.
/// Unlike GTK's hardcoded "Symbols Nerd Font", macOS paints with whatever
/// font `set_current_font` installed (see this module's header), so a
/// caller that hasn't installed a glyph-bearing font should pass `false`
/// — a wrong `false` shows a plain-but-correct glyph, a wrong `true` shows
/// tofu.
///
/// # Safety
///
/// `ctx` must be a valid `CGContextRef` borrowed for the duration of
/// the call.
#[allow(clippy::too_many_arguments)]
pub unsafe fn draw_activity_bar(
    ctx: CGContextRef,
    font: &CTFont,
    width: f64,
    height: f64,
    bar: &ActivityBar,
    theme: &Theme,
    hovered_idx: Option<usize>,
    nerd_fonts_enabled: bool,
) -> Vec<ActivityBarRowHit> {
    draw_activity_bar_with_style(
        ctx,
        font,
        width,
        height,
        bar,
        &ActivityBarStyle::default(),
        theme,
        hovered_idx,
        nerd_fonts_enabled,
    )
}

/// [`draw_activity_bar`] with an explicit [`ActivityBarStyle`] request.
/// `ActivityBarStyle::default()` reproduces [`draw_activity_bar`]
/// pixel for pixel.
///
/// # Safety
///
/// `ctx` must be a valid `CGContextRef` borrowed for the duration of
/// the call.
#[allow(clippy::too_many_arguments)]
pub unsafe fn draw_activity_bar_with_style(
    ctx: CGContextRef,
    font: &CTFont,
    width: f64,
    height: f64,
    bar: &ActivityBar,
    style: &ActivityBarStyle,
    theme: &Theme,
    hovered_idx: Option<usize>,
    nerd_fonts_enabled: bool,
) -> Vec<ActivityBarRowHit> {
    CGContextSaveGState(ctx);

    let layout = bar.layout(width as f32, height as f32, ACTIVITY_ROW_PX as f32);
    // Paint the icon glyph at a fixed, VS-Code-parity size,
    // independent of whatever size `font` itself carries — see this
    // module's doc for why `clone_with_font_size` needs no pt/px
    // conversion on macOS the way GTK/Win-GUI do.
    let icon_font = font.clone_with_font_size(style.resolved_icon_size_px() as f64);
    let mut surface = super::surface::CgSurface {
        ctx,
        font: Some(&icon_font),
    };
    let regions = native_surface_paint::paint(
        bar,
        &layout,
        style,
        &mut surface,
        theme,
        hovered_idx,
        nerd_fonts_enabled,
    );

    CGContextRestoreGState(ctx);
    regions
}

extern "C" {
    fn CGContextSaveGState(c: CGContextRef);
    fn CGContextRestoreGState(c: CGContextRef);
}

#[cfg(test)]
mod tests {
    use super::super::headless::BitmapSurface;
    use super::super::text::{make_font, system_ui_font};
    use super::super::MacBackend;
    use super::*;
    use crate::event::{Rect as QRect, Viewport};
    use crate::primitives::activity_bar::ActivityItem;
    use crate::theme::Theme;
    use crate::types::{Color, WidgetId};
    use crate::Backend;

    const W: u32 = 48;
    const H: u32 = 240; // 5 rows × 48px

    fn font() -> CTFont {
        make_font("Menlo", 14.0).expect("Menlo installed")
    }

    fn sample_bar() -> ActivityBar {
        ActivityBar {
            id: WidgetId::new("activity"),
            top_items: vec![
                ActivityItem {
                    id: WidgetId::new("activity:explorer"),
                    icon: "E".into(),
                    tooltip: "Explorer".into(),
                    is_active: true,
                    is_keyboard_selected: false,
                },
                ActivityItem {
                    id: WidgetId::new("activity:search"),
                    icon: "S".into(),
                    tooltip: "Search".into(),
                    is_active: false,
                    is_keyboard_selected: false,
                },
            ],
            bottom_items: vec![ActivityItem {
                id: WidgetId::new("activity:settings"),
                icon: "G".into(),
                tooltip: "Settings".into(),
                is_active: false,
                is_keyboard_selected: false,
            }],
            active_accent: Some(Color::rgb(80, 140, 255)),
            selection_bg: None,
            is_keyboard_focused: false,
        }
    }

    fn paint_via_backend(
        bar: &ActivityBar,
        hovered: Option<usize>,
    ) -> (BitmapSurface, Vec<ActivityBarRowHit>) {
        let surface = BitmapSurface::new(W, H);
        surface.fill(0.0, 0.0, 0.0, 0.0);

        let mut backend = MacBackend::new();
        backend.set_current_font(font());
        backend.begin_frame(Viewport::new(W as f32, H as f32, 1.0));
        let regions = std::cell::RefCell::new(Vec::new());
        backend.enter_frame_scope(surface.context_ptr(), |b| {
            let r = b.draw_activity_bar(QRect::new(0.0, 0.0, W as f32, H as f32), bar, hovered);
            *regions.borrow_mut() = r;
        });
        backend.end_frame();
        (surface, regions.into_inner())
    }

    /// Like [`paint_via_backend`] but paints into a canvas larger than the
    /// bar itself, at an arbitrary `(x, y)` origin — models the bar sitting
    /// below a title bar and/or right of other chrome.
    fn paint_via_backend_at(
        bar: &ActivityBar,
        hovered: Option<usize>,
        x: f32,
        y: f32,
        canvas_w: u32,
        canvas_h: u32,
    ) -> (BitmapSurface, Vec<ActivityBarRowHit>) {
        let surface = BitmapSurface::new(canvas_w, canvas_h);
        surface.fill(0.0, 0.0, 0.0, 0.0);

        let mut backend = MacBackend::new();
        backend.set_current_font(font());
        backend.begin_frame(Viewport::new(canvas_w as f32, canvas_h as f32, 1.0));
        let regions = std::cell::RefCell::new(Vec::new());
        backend.enter_frame_scope(surface.context_ptr(), |b| {
            let r = b.draw_activity_bar(QRect::new(x, y, W as f32, H as f32), bar, hovered);
            *regions.borrow_mut() = r;
        });
        backend.end_frame();
        (surface, regions.into_inner())
    }

    fn paint_via_backend_with_style(
        bar: &ActivityBar,
        style: &crate::ActivityBarStyle,
        hovered: Option<usize>,
    ) -> (BitmapSurface, Vec<ActivityBarRowHit>) {
        let surface = BitmapSurface::new(W, H);
        surface.fill(0.0, 0.0, 0.0, 0.0);

        let mut backend = MacBackend::new();
        backend.set_current_font(font());
        backend.begin_frame(Viewport::new(W as f32, H as f32, 1.0));
        let regions = std::cell::RefCell::new(Vec::new());
        backend.enter_frame_scope(surface.context_ptr(), |b| {
            let r = b.draw_activity_bar_with_style(
                QRect::new(0.0, 0.0, W as f32, H as f32),
                bar,
                hovered,
                style,
            );
            *regions.borrow_mut() = r;
        });
        backend.end_frame();
        (surface, regions.into_inner())
    }

    /// Bounding-box height (device px) of every non-background pixel
    /// within row `[y0, y1)` — used by icon-size tests.
    /// Excludes the bar's own 1pt right-edge separator column
    /// (`x == W - 1`), which is unconditional and would otherwise swamp
    /// a glyph-only ink measurement.
    fn ink_height_in_row(surface: &BitmapSurface, y0: u32, y1: u32) -> u32 {
        let theme = Theme::default();
        let bg = (theme.tab_bar_bg.r, theme.tab_bar_bg.g, theme.tab_bar_bg.b);
        let mut found: Option<(u32, u32)> = None;
        for y in y0..y1 {
            for x in 0..(W - 1) {
                let (r, g, b, _) = surface.pixel(x, y);
                if (r, g, b) != bg {
                    found = Some(match found {
                        Some((min_y, max_y)) => (min_y.min(y), max_y.max(y)),
                        None => (y, y),
                    });
                }
            }
        }
        found.map_or(0, |(min_y, max_y)| max_y - min_y + 1)
    }

    /// The icon glyph's ink size follows
    /// `ActivityBarStyle::icon_size_px`, not the backend's `chrome_font`
    /// size — the regression this issue reports ("icon size follows the
    /// active font"), fixed by resizing whatever font is passed in
    /// (`font.clone_with_font_size`) rather than trusting its own size.
    #[test]
    fn icon_ink_height_follows_style_not_chrome_font_size() {
        use crate::primitives::activity_bar::DEFAULT_ACTIVITY_ICON_SIZE_PX;

        // Settings (bottom-pinned, not active): no accent strip to
        // isolate the glyph ink from.
        let mut bar = sample_bar();
        bar.active_accent = None;
        let settings_y0 = H - ACTIVITY_ROW_PX as u32;
        let settings_y1 = H;

        let paint_with = |style: &crate::ActivityBarStyle, chrome_size_pt: f64| -> u32 {
            let surface = BitmapSurface::new(W, H);
            surface.fill(0.0, 0.0, 0.0, 0.0);
            let mut backend = MacBackend::new();
            backend.set_chrome_font(system_ui_font(chrome_size_pt));
            backend.begin_frame(Viewport::new(W as f32, H as f32, 1.0));
            backend.enter_frame_scope(surface.context_ptr(), |b| {
                b.draw_activity_bar_with_style(
                    QRect::new(0.0, 0.0, W as f32, H as f32),
                    &bar,
                    None,
                    style,
                );
            });
            backend.end_frame();
            ink_height_in_row(&surface, settings_y0, settings_y1)
        };

        let default_style = crate::ActivityBarStyle::default();
        let default_small_chrome = paint_with(&default_style, 6.0);
        let default_huge_chrome = paint_with(&default_style, 40.0);
        assert!(
            default_small_chrome > 0,
            "sanity: the glyph should paint some ink"
        );
        assert_eq!(
            default_small_chrome, default_huge_chrome,
            "default icon ink height must not track chrome_font's own size \
             (6pt vs 40pt chrome font should paint identically)"
        );

        let small_style = crate::ActivityBarStyle::new().with_icon_size_px(10.0);
        let small_icon = paint_with(&small_style, 11.0);
        assert!(
            small_icon < default_huge_chrome,
            "icon_size_px(10.0) ({small_icon}px ink) should paint smaller than \
             the {DEFAULT_ACTIVITY_ICON_SIZE_PX}px default ({default_huge_chrome}px ink)"
        );
    }

    #[test]
    fn background_is_tab_bar_bg() {
        let bar = sample_bar();
        let (surface, _) = paint_via_backend(&bar, None);
        let theme = Theme::default();
        // Probe deep inside the second (non-active) row, well away
        // from any glyph centre.
        let (r, g, b, _) = surface.pixel(W - 6, (ACTIVITY_ROW_PX as u32) + 4);
        assert_eq!(
            (r, g, b),
            (theme.tab_bar_bg.r, theme.tab_bar_bg.g, theme.tab_bar_bg.b),
        );
    }

    #[test]
    fn active_item_paints_accent_strip_on_left_edge() {
        // 2-px accent strip at x ∈ [0, 2). Probe column 0 inside the
        // first row (active) and assert accent colour.
        let bar = sample_bar();
        let (surface, _) = paint_via_backend(&bar, None);
        let accent = bar.active_accent.unwrap();
        // y = ACTIVITY_ROW_PX/2 sits mid-row inside the first
        // (active) item.
        let probe_y = (ACTIVITY_ROW_PX as u32) / 2;
        let (r, g, b, _) = surface.pixel(0, probe_y);
        assert_eq!((r, g, b), (accent.r, accent.g, accent.b));
        let (r2, g2, b2, _) = surface.pixel(1, probe_y);
        assert_eq!((r2, g2, b2), (accent.r, accent.g, accent.b));

        // x = 2 should already be off the accent strip — proves the
        // strip is exactly 2 points wide. Probe at row top to dodge
        // any wide-glyph render.
        let (r3, g3, b3, _) = surface.pixel(2, 2);
        let theme = Theme::default();
        assert_eq!(
            (r3, g3, b3),
            (theme.tab_bar_bg.r, theme.tab_bar_bg.g, theme.tab_bar_bg.b),
            "x=2 should be tab_bar_bg, not accent",
        );
    }

    /// `style.active_bg: Some(..)` + `active_accent: None`
    /// paints a filled active row with **zero** accent-line pixels — the
    /// legacy 2-pt left-edge strip (x ∈ [0, 2)) must show the fill colour,
    /// not any accent tint.
    #[test]
    fn active_bg_fills_row_with_zero_accent_pixels_when_accent_is_none() {
        let mut bar = sample_bar();
        bar.active_accent = None;
        let fill = Color::rgb(49, 50, 51);
        let style = crate::ActivityBarStyle::new().with_active_bg(fill);
        let (surface, _) = paint_via_backend_with_style(&bar, &style, None);

        let probe_y = (ACTIVITY_ROW_PX as u32) / 2;
        for x in [0, 1] {
            let (r, g, b, _) = surface.pixel(x, probe_y);
            assert_eq!(
                (r, g, b),
                (fill.r, fill.g, fill.b),
                "x={x} is inside the legacy accent strip; with active_accent \
                 None it must show the active_bg fill, not any accent tint"
            );
        }
        // Deep in the row, away from the glyph, should also be filled.
        let (r, g, b, _) = surface.pixel(W - 6, probe_y);
        assert_eq!((r, g, b), (fill.r, fill.g, fill.b));
    }

    #[test]
    fn row_hits_cover_painted_rows() {
        // Round-trip: returned regions must point at where the icon
        // was painted. Verify each region's y-span matches the
        // ACTIVITY_ROW_PX grid AND its id matches the item.
        //
        // `visible_items` order is now bottom-pinned-first (the
        // shared `ActivityBar::layout`'s order — matching
        // `gtk::activity_bar` / `win::activity_bar`), not this module's
        // old top-first `row_plan` order. Only the *order* of these
        // assertions changed; the y-spans themselves are unchanged.
        let bar = sample_bar();
        let (_surface, regions) = paint_via_backend(&bar, None);
        // 2 top + 1 bottom = 3 visible rows.
        assert_eq!(regions.len(), 3);

        // Bottom-pinned item comes first.
        assert_eq!(regions[0].y_start, H as f32 - ACTIVITY_ROW_PX as f32);
        assert_eq!(regions[0].y_end, H as f32);
        assert_eq!(regions[0].id, WidgetId::new("activity:settings"));

        assert_eq!(regions[1].y_start, 0.0);
        assert_eq!(regions[1].y_end, ACTIVITY_ROW_PX as f32);
        assert_eq!(regions[1].id, WidgetId::new("activity:explorer"));

        assert_eq!(regions[2].y_start, ACTIVITY_ROW_PX as f32);
        assert_eq!(regions[2].y_end, 2.0 * ACTIVITY_ROW_PX as f32);
        assert_eq!(regions[2].id, WidgetId::new("activity:search"));
    }

    #[test]
    fn right_edge_has_separator_pixel() {
        let bar = sample_bar();
        let (surface, _) = paint_via_backend(&bar, None);
        let theme = Theme::default();
        let (r, g, b, _) = surface.pixel(W - 1, H / 2);
        assert_eq!(
            (r, g, b),
            (theme.separator.r, theme.separator.g, theme.separator.b),
        );
    }

    /// Pre-migration macOS's `draw_row` closure had no
    /// `is_keyboard_selected` branch at all — arrow-key navigation was
    /// completely invisible on this backend (`ActivityItem::is_keyboard_selected`'s
    /// own doc already admitted "Both the TUI and GTK rasterizers honour
    /// this flag", i.e. not macOS). The shared
    /// `native_surface_paint::paint` now fills `bar.selection_bg` (or the
    /// `lighten(0.20)` default) for a selected row, same as GTK/Windows.
    #[test]
    fn keyboard_selected_row_paints_selection_bg() {
        let mut bar = sample_bar();
        // Search (top_items[1], not active) is the keyboard-selected row.
        bar.top_items[1].is_keyboard_selected = true;
        let (surface, _) = paint_via_backend(&bar, None);
        let theme = Theme::default();
        let expected = theme.tab_bar_bg.lighten(0.20);

        // Search paints at y ∈ [48, 96) — probe deep in the row, away
        // from the glyph and the (unrelated) accent strip.
        let probe_y = ACTIVITY_ROW_PX as u32 + ACTIVITY_ROW_PX as u32 / 2;
        let (r, g, b, _) = surface.pixel(W - 6, probe_y);
        assert_eq!(
            (r, g, b),
            (expected.r, expected.g, expected.b),
            "keyboard-selected row should paint the selection background \
             (quadraui#1081 — this used to be a no-op on macOS)",
        );
    }

    /// `cargo test -p quadraui --features macos -- --ignored --nocapture macos::activity_bar::tests::dump_smoke_ppm`
    ///
    /// Paints the sample bar (2 top items + 1 bottom-pinned) into a
    /// 48 × 240 surface and writes `/tmp/quadraui_activity_bar.ppm`.
    /// Hover row 1 so the lightened bg is visible. Open in Preview to
    /// confirm:
    /// - Top item (active) has a 2-pt accent strip on the left edge.
    /// - Middle item shows the hover-lightened bg.
    /// - Bottom item is pinned to the bottom edge.
    /// - Right-edge has a 1-pt separator column.
    #[test]
    #[ignore = "writes /tmp/quadraui_activity_bar.ppm — opt in with --ignored"]
    fn dump_smoke_ppm() {
        let bar = sample_bar();
        let (surface, _) = paint_via_backend(&bar, Some(1));
        surface.write_ppm_and_open("/tmp/quadraui_activity_bar.ppm");
    }

    #[test]
    fn hover_lightens_row_background() {
        let bar = sample_bar();
        // Hover `visible_items[1]` — the explorer row (`ActivityBar::layout`
        // orders bottom-pinned items first, so index 1 is the first top item).
        // Hover paints unconditionally regardless of `is_active`, and the
        // default style has no `active_bg`, so the probed pixel is still the
        // plain hover tint either way.
        let (surface, _) = paint_via_backend(&bar, Some(1));
        let theme = Theme::default();
        let expected = theme.tab_bar_bg.lighten(0.10);
        // Explorer paints at y ∈ [0, 48) — probe deep into that row,
        // away from the glyph centre.
        let (r, g, b, _) = surface.pixel(W - 6, 4);
        assert_eq!(
            (r, g, b),
            (expected.r, expected.g, expected.b),
            "hovered row should paint the lightened bg",
        );
    }

    /// `Backend::activity_bar_layout` must return exactly what
    /// `draw_activity_bar` painted — both walk the same
    /// `ActivityBar::layout`, and this pins that.
    #[test]
    fn layout_twin_matches_the_painted_rows() {
        let bar = sample_bar();
        let (_surface, painted) = paint_via_backend(&bar, None);

        let mut backend = MacBackend::new();
        backend.set_current_font(font());
        let computed = backend.activity_bar_layout(QRect::new(0.0, 0.0, W as f32, H as f32), &bar);

        assert_eq!(painted.len(), computed.len());
        for (p, c) in painted.iter().zip(computed.iter()) {
            assert_eq!(p.id, c.id);
            assert_eq!(p.tooltip, c.tooltip);
            assert!((p.y_start - c.y_start).abs() < 0.001);
            assert!((p.y_end - c.y_end).abs() < 0.001);
        }
    }

    /// Spans are bar-relative: every row's `y_start` is unchanged
    /// regardless of where the bar sits, and the no-paint twin agrees.
    #[test]
    fn layout_twin_spans_are_bar_relative_at_a_nonzero_origin() {
        let bar = sample_bar();
        let mut backend = MacBackend::new();
        backend.set_current_font(font());
        let at_origin = backend.activity_bar_layout(QRect::new(0.0, 0.0, W as f32, H as f32), &bar);
        let moved = backend.activity_bar_layout(QRect::new(23.0, 41.0, W as f32, H as f32), &bar);

        assert_eq!(at_origin.len(), moved.len());
        // `at_origin[0]` is the bottom-pinned `settings` item (`ActivityBar::layout`
        // orders bottom-pinned items first), so its bar-relative `y_start` is
        // `H - ACTIVITY_ROW_PX`, not `0.0`.
        assert!((at_origin[0].y_start - (H as f32 - ACTIVITY_ROW_PX as f32)).abs() < 0.001);
        for (a, m) in at_origin.iter().zip(moved.iter()) {
            assert!(
                (a.y_start - m.y_start).abs() < 0.001,
                "row {:?} moved with the bar origin: {} vs {}",
                a.id,
                a.y_start,
                m.y_start,
            );
        }
    }

    /// `MacBackend::draw_activity_bar` forwards only `rect.width` /
    /// `rect.height` into this bar-relative rasteriser, wrapping the call
    /// in `CGContextTranslateCTM` to place it at `rect`'s actual origin —
    /// painting at a non-zero origin must leave `(0, 0)` untouched and
    /// land the strip's background + right-edge separator exactly at
    /// `(rect.x, rect.y)`.
    #[test]
    fn background_paints_at_rect_origin_not_at_window_origin() {
        const ORIGIN_X: f32 = 30.0;
        const ORIGIN_Y: f32 = 28.0; // e.g. a title bar's height
        let canvas_w = ORIGIN_X as u32 + W;
        let canvas_h = ORIGIN_Y as u32 + H;

        let bar = sample_bar();
        let (surface, regions) =
            paint_via_backend_at(&bar, None, ORIGIN_X, ORIGIN_Y, canvas_w, canvas_h);
        let theme = Theme::default();

        // The window's literal top-left corner must stay exactly as the
        // surface was initialised — fully transparent — never the
        // activity bar's background fill.
        let (_, _, _, a) = surface.pixel(0, 0);
        assert_eq!(
            a, 0,
            "window origin (0,0) must stay untouched when the bar sits at ({ORIGIN_X}, {ORIGIN_Y})",
        );

        // Deep inside the (non-active) second row, offset by the bar's
        // real origin: activity-bar background.
        let probe_x = ORIGIN_X as u32 + W - 6;
        let probe_y = ORIGIN_Y as u32 + ACTIVITY_ROW_PX as u32 + 4;
        let (r, g, b, _) = surface.pixel(probe_x, probe_y);
        assert_eq!(
            (r, g, b),
            (theme.tab_bar_bg.r, theme.tab_bar_bg.g, theme.tab_bar_bg.b),
            "activity bar background should paint at rect's origin, not the window origin",
        );

        // Right-edge separator (1 pt) shifts with the origin too.
        let (sr, sg, sb, _) = surface.pixel(ORIGIN_X as u32 + W - 1, ORIGIN_Y as u32 + H / 2);
        assert_eq!(
            (sr, sg, sb),
            (theme.separator.r, theme.separator.g, theme.separator.b),
            "right-edge separator should shift with the bar's origin",
        );

        // Hit regions stay bar-relative — the CTM translate must not leak
        // into the returned spans. `regions[0]` is the bottom-pinned `settings`
        // item (`ActivityBar::layout` orders bottom-pinned items first), so its
        // bar-relative `y_start` is `H - ACTIVITY_ROW_PX`, not `0.0`.
        assert_eq!(regions[0].y_start, H as f32 - ACTIVITY_ROW_PX as f32);
    }
}