quadraui 0.1.3

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
//! Direct2D / DirectWrite rasteriser for [`crate::StatusBar`] (issue #25).
//!
//! Painting moved to the shared
//! [`crate::primitives::status_bar::native_surface_paint::paint`] (#860,
//! `PaintSurface` Phase 2d slice 3/9) — see that fn's doc for the named
//! divergences (bold-aware measurement: GTK/Win measured a segment's own
//! `bold` weight, macOS ignored it; GTK's missing zero-size guard, now
//! applying the already-fixed quadraui#791 shape uniformly) found while
//! unifying `gtk::draw_status_bar`, `macos::status_bar::draw_status_bar`
//! and `win::status_bar::draw_status_bar` into one implementation. This
//! module now only carries [`win_status_bar_layout`] (pure layout, still
//! needed by `WinBackend::status_bar_layout` for no-paint hit-test
//! queries); the deprecated `draw_status_bar` compatibility shim over
//! the shared [`super::surface::D2dSurface`] adapter was removed in
//! issue #1109 (zero uses in coord-tui's `main` and vimcode's
//! `develop`). `MIN_GAP_DIP` stays put — it's still
//! [`win_status_bar_layout`]'s own measurer constant, untouched by this
//! migration.
//!
//! [`win_status_bar_layout`] is pure geometry generic over
//! [`StatusMeasure`] — no Direct2D/DirectWrite type in its signature —
//! so it compiles and runs everywhere, including a plain `cargo test
//! --features win` on Linux. `super::mod`'s `mod status_bar;` is no
//! longer whole-module gated; see `backend.rs`'s module docs.
//!
//! # Theme
//!
//! Takes the live theme as a `&Theme` parameter (quadraui#789) — the
//! caller ([`crate::win::WinBackend::draw_status_bar`]) passes
//! `&self.current_theme`, the same field `Backend::set_theme` writes.
//! Callers that don't have segment-level colours to fall back on (the
//! bar's own background, when it has no segments) get that live theme's
//! background, not [`Theme::default`].

#[cfg(target_os = "windows")]
use super::text::DWrite;
use crate::event::Rect;
use crate::primitives::status_bar::{
    StatusSegmentMeasure, PIXEL_EDGE_INSET, PIXEL_SEGMENT_PADDING,
};
#[cfg(all(test, target_os = "windows"))]
use crate::theme::Theme;
use crate::{StatusBar, StatusBarLayout};

/// Minimum gap (DIPs) reserved between the left and right segment
/// groups — the DirectWrite twin of [`crate::gtk::MIN_GAP_PX`]. Still
/// used by [`win_status_bar_layout`] — the shared
/// [`crate::primitives::status_bar::native_surface_paint::paint`] carries
/// its own independent copy of the same value (see that module's doc).
pub const MIN_GAP_DIP: f32 = 16.0;

/// Bold-aware text measurement for [`win_status_bar_layout`]. A status
/// segment's `bold` flag changes its measured width, so this can't reuse
/// [`crate::primitives::layout_metrics::TextMeasure`] (no `bold`
/// parameter) the way `win::tab_bar`/`win::data_table` do — a real
/// difference this rasteriser has always had (`DWrite::measure_text_styled`
/// vs. every other module's plain `measure_text`), not new duplication.
pub trait StatusMeasure {
    fn width_of(&self, text: &str, bold: bool) -> f32;
}

#[cfg(target_os = "windows")]
impl StatusMeasure for DWrite {
    fn width_of(&self, text: &str, bold: bool) -> f32 {
        self.measure_text_styled(text, bold)
            .map(|(w, _)| w)
            .unwrap_or(0.0)
    }
}

/// Compute a [`StatusBar`]'s layout without painting — the measurer twin
/// of the shared `paint`, and what
/// [`crate::win::WinBackend::status_bar_layout`] calls directly. Both this
/// function and `paint` measure a segment's width the same bold-aware
/// way, so a no-paint hit-test call always agrees with what the last
/// paint drew.
pub fn win_status_bar_layout(
    measure: &dyn StatusMeasure,
    rect: Rect,
    bar: &StatusBar,
) -> StatusBarLayout {
    // #1155: `layout_padded` (not plain `layout`) so this no-paint twin
    // agrees with the shared `native_surface_paint::paint`'s outer edge
    // inset + per-segment padding — see that fn's doc.
    bar.layout_padded(
        rect.width,
        rect.height,
        MIN_GAP_DIP,
        PIXEL_EDGE_INSET,
        PIXEL_SEGMENT_PADDING,
        |seg| StatusSegmentMeasure::new(measure.width_of(&seg.text, seg.bold)),
    )
}

// #1155: pins `win_status_bar_layout`'s outer-edge-inset + per-segment
// padding numerically, the same way `gtk::testing`'s
// `find_locates_status_bar_segment_by_text` and
// `primitives::status_bar`'s own unit tests do — see this issue's
// non-blocking review note that Windows had no test asserting the actual
// inset numbers, relying entirely on the shared `layout_padded` unit
// tests. Unlike the `#[cfg(target_os = "windows")]`-gated `mod tests`
// below (which needs a real `DWrite`), this measures through a trivial
// fake `StatusMeasure` — `win_status_bar_layout` is pure geometry (see
// this module's doc, "Issue #1078") — so it runs on every host, including
// this Linux sandbox's `cargo test --features win`.
#[cfg(test)]
mod layout_padding_tests {
    use super::{win_status_bar_layout, StatusMeasure, MIN_GAP_DIP};
    use crate::event::Rect;
    use crate::primitives::status_bar::{
        StatusBarSegment, PIXEL_EDGE_INSET, PIXEL_SEGMENT_PADDING,
    };
    use crate::types::{Color, WidgetId};
    use crate::StatusBar;

    /// Fixed-width-per-character fake — no real font metrics needed, just
    /// something deterministic to measure `layout_padded`'s inset/padding
    /// arithmetic against.
    struct FixedWidthMeasure {
        px_per_char: f32,
    }

    impl StatusMeasure for FixedWidthMeasure {
        fn width_of(&self, text: &str, _bold: bool) -> f32 {
            text.chars().count() as f32 * self.px_per_char
        }
    }

    fn segment(text: &str) -> StatusBarSegment {
        StatusBarSegment {
            text: text.to_string(),
            fg: Color::rgb(0, 0, 0),
            bg: Color::rgb(10, 20, 30),
            bold: false,
            action_id: None,
        }
    }

    /// A lone left segment must start `PIXEL_EDGE_INSET` in from the
    /// bar's own left edge (issue #1155), not flush at x=0.
    #[test]
    fn lone_left_segment_starts_after_edge_inset() {
        let measure = FixedWidthMeasure { px_per_char: 8.0 };
        let bar = StatusBar {
            id: WidgetId::new("status"),
            left_segments: vec![segment("hi")],
            right_segments: vec![],
        };
        let layout = win_status_bar_layout(&measure, Rect::new(0.0, 0.0, 400.0, 20.0), &bar);

        let left = layout
            .visible_segments
            .first()
            .expect("left segment visible");
        assert_eq!(
            left.bounds.x, PIXEL_EDGE_INSET,
            "left-most segment should start `PIXEL_EDGE_INSET` in from x=0"
        );
    }

    /// A lone right segment must end `PIXEL_EDGE_INSET` short of the
    /// bar's own right edge (issue #1155), not flush against it.
    #[test]
    fn lone_right_segment_ends_before_edge_inset() {
        let measure = FixedWidthMeasure { px_per_char: 8.0 };
        let bar_width = 400.0;
        let bar = StatusBar {
            id: WidgetId::new("status"),
            left_segments: vec![],
            right_segments: vec![segment("bye")],
        };
        let layout = win_status_bar_layout(&measure, Rect::new(0.0, 0.0, bar_width, 20.0), &bar);

        let right = layout
            .visible_segments
            .first()
            .expect("right segment visible");
        assert_eq!(
            right.bounds.x + right.bounds.width,
            bar_width - PIXEL_EDGE_INSET,
            "right-most segment should end `PIXEL_EDGE_INSET` short of the bar's right edge"
        );
    }

    /// Each segment's measured text reserves `PIXEL_SEGMENT_PADDING` on
    /// both sides — its bounds are wider than the raw measured text
    /// width by `2 * PIXEL_SEGMENT_PADDING`.
    #[test]
    fn segment_bounds_add_padding_on_both_sides_of_measured_text() {
        let px_per_char = 8.0;
        let measure = FixedWidthMeasure { px_per_char };
        let text = "hi";
        let bar = StatusBar {
            id: WidgetId::new("status"),
            left_segments: vec![segment(text)],
            right_segments: vec![],
        };
        let layout = win_status_bar_layout(&measure, Rect::new(0.0, 0.0, 400.0, 20.0), &bar);

        let left = layout
            .visible_segments
            .first()
            .expect("left segment visible");
        let measured_width = text.chars().count() as f32 * px_per_char;
        assert_eq!(
            left.bounds.width,
            measured_width + 2.0 * PIXEL_SEGMENT_PADDING,
            "segment bounds should be the measured text width plus \
             `PIXEL_SEGMENT_PADDING` on both sides"
        );
    }

    /// Sanity: `MIN_GAP_DIP` is still reserved between left and right
    /// groups on top of the new edge inset/padding, i.e. this change
    /// composes with the pre-existing gap rather than replacing it.
    #[test]
    fn min_gap_still_reserved_between_groups() {
        let measure = FixedWidthMeasure { px_per_char: 8.0 };
        let bar = StatusBar {
            id: WidgetId::new("status"),
            left_segments: vec![segment("L")],
            right_segments: vec![segment("R")],
        };
        // Bar just wide enough for both groups plus insets/padding, with
        // exactly `MIN_GAP_DIP` of slack between them.
        let left_w = 1.0 * 8.0 + 2.0 * PIXEL_SEGMENT_PADDING;
        let right_w = 1.0 * 8.0 + 2.0 * PIXEL_SEGMENT_PADDING;
        let width = 2.0 * PIXEL_EDGE_INSET + left_w + right_w + MIN_GAP_DIP;
        let layout = win_status_bar_layout(&measure, Rect::new(0.0, 0.0, width, 20.0), &bar);

        assert_eq!(
            layout.visible_segments.len(),
            2,
            "both segments should still fit with exactly MIN_GAP_DIP of slack"
        );
        let left = &layout.visible_segments[0];
        let right = &layout.visible_segments[1];
        assert!(
            (right.bounds.x - (left.bounds.x + left.bounds.width) - MIN_GAP_DIP).abs() < 0.01,
            "gap between groups should be exactly MIN_GAP_DIP"
        );
    }
}

// #1078: every test below paints through a real `DWrite`/`HeadlessSurface`
// — gated the same way the whole module used to be, rather than
// pretending they run on Linux.
#[cfg(all(test, target_os = "windows"))]
mod tests {
    use super::*;
    use crate::primitives::status_bar::{StatusBarHit, StatusBarSegment, StatusSegmentSide};
    use crate::types::{Color, WidgetId};
    use crate::win::testing::HeadlessSurface;

    const W: f32 = 200.0;
    const H: f32 = 20.0;

    fn bar() -> StatusBar {
        StatusBar {
            id: WidgetId::new("status"),
            left_segments: vec![StatusBarSegment {
                text: "NORMAL".into(),
                fg: Color::rgb(0, 0, 0),
                bg: Color::rgb(10, 20, 30),
                bold: false,
                action_id: Some(WidgetId::new("status:mode")),
            }],
            right_segments: vec![StatusBarSegment {
                text: "Ln 3, Col 8".into(),
                fg: Color::rgb(0, 0, 0),
                bg: Color::rgb(40, 50, 60),
                bold: false,
                action_id: Some(WidgetId::new("status:cursor")),
            }],
        }
    }

    /// Paint `bar` via the shared
    /// [`crate::primitives::status_bar::native_surface_paint::paint`]
    /// through a [`super::super::surface::D2dSurface`] over `surface`'s headless
    /// target — the same adapter the now-removed `draw_status_bar` shim
    /// used (issue #1109), exercised here directly.
    fn paint(
        surface: &HeadlessSurface,
        dwrite: &DWrite,
        rect: Rect,
        bar: &StatusBar,
        hovered_id: Option<&WidgetId>,
        pressed_id: Option<&WidgetId>,
    ) -> StatusBarLayout {
        surface
            .paint(|target| {
                let mut raw = super::super::surface::D2dSurface {
                    target,
                    dwrite: Some(dwrite),
                };
                crate::primitives::status_bar::native_surface_paint::paint(
                    bar,
                    &mut raw,
                    &Theme::default(),
                    rect.x,
                    rect.y,
                    rect.width,
                    rect.height,
                    hovered_id,
                    pressed_id,
                );
            })
            // `HeadlessSurface::paint`'s closure returns `()` (it just
            // drives Direct2D's `BeginDraw`/`EndDraw` bracket) — the
            // layout `paint` computed during painting is discarded, so
            // recompute it via the same measurer
            // (`win_status_bar_layout`), matching this file's pre-#860
            // test shape. `hovered_id`/`pressed_id` never affect layout
            // (only which segment gets tinted), so this is exact.
            .map(|_| win_status_bar_layout(dwrite, rect, bar))
            .expect("paint status bar")
    }

    /// Does `color` appear anywhere on row `y` between `x0` and `x1`
    /// (bar-local DIPs, half-open)?
    ///
    /// A *scan* rather than a single `pixel_at` probe: `draw_status_bar`
    /// paints each segment's label with `DrawText` into the segment's own
    /// rect, left- and top-aligned with no padding (the primitive's layout
    /// adds none), so the exact pixel at a segment's left edge or centre
    /// may well land on a glyph stem. Which pixels the glyphs cover is a
    /// DirectWrite font-rasterisation detail (hinting, ClearType fringes,
    /// whichever `Segoe UI` version the host ships) and is *not* what these
    /// assertions are about — the claim under test is "this segment's `bg`
    /// was filled across this segment's own bounds", and inter-glyph gaps
    /// make that observable no matter where the ink lands. See
    /// `tab_bar`'s sibling test, which dodges the same hazard by sampling
    /// below the glyph band.
    fn row_contains(surface: &HeadlessSurface, x0: f32, x1: f32, y: u32, color: Color) -> bool {
        (x0.max(0.0) as u32..x1.max(0.0) as u32).any(|x| {
            let px = surface.pixel_at(x, y);
            (px.r, px.g, px.b) == (color.r, color.g, color.b)
        })
    }

    /// Paint↔click round trip: the segment's painted background and the
    /// layout's own `hit_test` over those same bounds must agree on which
    /// (if any) `WidgetId` was clicked.
    #[test]
    fn paint_and_hit_test_round_trip() {
        let surface = HeadlessSurface::new(W as u32, H as u32).expect("create surface");
        let (dwrite, _, _) = DWrite::new("Segoe UI", 10.0, None).expect("create DWrite");
        let bar = bar();
        let rect = Rect::new(0.0, 0.0, W, H);

        let layout = paint(&surface, &dwrite, rect, &bar, None, None);

        // Since issue #1155 the left segment starts at bar-local
        // x=`PIXEL_EDGE_INSET`, not at x=0 — its fill colour must be
        // visible somewhere across its own bounds.
        let mid_y = (H / 2.0) as u32;
        let left_vs = layout
            .visible_segments
            .iter()
            .find(|vs| vs.side == StatusSegmentSide::Left)
            .expect("left segment is visible");
        assert!(
            row_contains(
                &surface,
                left_vs.bounds.x,
                left_vs.bounds.x + left_vs.bounds.width,
                mid_y,
                Color::rgb(10, 20, 30),
            ),
            "left segment's bg should be painted at its own bounds"
        );

        // Hit-test the left segment at its own painted centre rather than
        // a hardcoded `x = 1.0`: issue #1155 reserves `PIXEL_EDGE_INSET`
        // between the bar's left edge and its left-most segment, so x=1.0
        // now lands in that inset (correctly `Empty`), which made this a
        // stale assertion rather than a real regression.
        let left_cx = left_vs.bounds.x + left_vs.bounds.width / 2.0;
        let left_hit = layout.hit_test(left_cx, H / 2.0);
        assert_eq!(
            left_hit,
            StatusBarHit::Segment(WidgetId::new("status:mode"))
        );

        // …and the reserved outer inset itself is *not* clickable — the
        // positive half of the same #1155 claim, pinned here on the paint
        // path (the pure-layout twin is `layout_padding_tests` above).
        assert_eq!(
            layout.hit_test(PIXEL_EDGE_INSET / 2.0, H / 2.0),
            StatusBarHit::Empty,
            "the `PIXEL_EDGE_INSET` strip left of the first segment should \
             not hit any segment (issue #1155)"
        );

        // Right segment is right-aligned; its hit-test centre must resolve
        // to the cursor segment, and that x must fall inside painted
        // (non-default-background) pixels.
        let right_vs = layout
            .visible_segments
            .iter()
            .find(|vs| vs.side == StatusSegmentSide::Right)
            .expect("right segment is visible");
        let cx = right_vs.bounds.x + right_vs.bounds.width / 2.0;
        let right_hit = layout.hit_test(cx, H / 2.0);
        assert_eq!(
            right_hit,
            StatusBarHit::Segment(WidgetId::new("status:cursor"))
        );
        assert!(
            row_contains(
                &surface,
                right_vs.bounds.x,
                right_vs.bounds.x + right_vs.bounds.width,
                mid_y,
                Color::rgb(40, 50, 60),
            ),
            "right segment's bg should be painted at its own hit-tested bounds"
        );
    }

    /// A click outside every segment's bounds (the gap) resolves to
    /// `Empty`, and `win_status_bar_layout` (no-paint) must produce byte-
    /// identical `hit_regions` to what `paint` used to paint — same
    /// measurer, same bar, same rect.
    #[test]
    fn no_paint_layout_matches_paint_layout() {
        let (dwrite, _, _) = DWrite::new("Segoe UI", 10.0, None).expect("create DWrite");
        let bar = bar();
        let rect = Rect::new(0.0, 0.0, W, H);

        let surface = HeadlessSurface::new(W as u32, H as u32).expect("create surface");
        let painted = paint(&surface, &dwrite, rect, &bar, None, None);
        let no_paint = win_status_bar_layout(&dwrite, rect, &bar);

        assert_eq!(painted, no_paint);
    }

    /// Regression for quadraui#791, ported to the shared paint path: a bar
    /// narrower than its segments' measured text used to let that text
    /// overflow the bar's own rect (segments never priority-drop on the
    /// left, and the right side always keeps at least its highest-
    /// priority segment "even if it alone overflows" — see
    /// `StatusBar::layout`'s doc). Paint a status bar inset in a larger
    /// canvas, deliberately narrower than its segment text, and assert
    /// every pixel outside the bar's own rect stays untouched.
    #[test]
    fn paint_does_not_escape_rect_bounds() {
        let canvas_w = 240u32;
        let canvas_h = 40u32;
        let sentinel = Color::rgb(1, 2, 3);
        // Narrow enough that "NORMAL" alone overflows it.
        let bar_rect = Rect::new(20.0, 10.0, 40.0, H);
        let bar = bar();
        let (dwrite, _, _) = DWrite::new("Segoe UI", 10.0, None).expect("create DWrite");

        let surface = HeadlessSurface::new(canvas_w, canvas_h).expect("create surface");
        surface
            .fill_rect(
                Rect::new(0.0, 0.0, canvas_w as f32, canvas_h as f32),
                sentinel,
            )
            .expect("fill sentinel");
        let _ = paint(&surface, &dwrite, bar_rect, &bar, None, None);

        for y in 0..canvas_h {
            for x in 0..canvas_w {
                let inside = (x as f32) >= bar_rect.x
                    && (x as f32) < bar_rect.x + bar_rect.width
                    && (y as f32) >= bar_rect.y
                    && (y as f32) < bar_rect.y + bar_rect.height;
                if inside {
                    continue;
                }
                let px = surface.pixel_at(x, y);
                assert_eq!(
                    (px.r, px.g, px.b),
                    (sentinel.r, sentinel.g, sentinel.b),
                    "pixel ({x}, {y}) outside the bar's own rect should stay untouched",
                );
            }
        }
    }

    /// A zero-size rect must not panic (no degenerate clip pushed) and
    /// paint must agree with the no-paint layout — mirrors
    /// `macos::status_bar`'s zero-size guard.
    #[test]
    fn zero_size_rect_is_a_no_op() {
        let (dwrite, _, _) = DWrite::new("Segoe UI", 10.0, None).expect("create DWrite");
        let bar = bar();
        let rect = Rect::new(0.0, 0.0, 0.0, 0.0);

        let surface = HeadlessSurface::new(10, 10).expect("create surface");
        let painted = paint(&surface, &dwrite, rect, &bar, None, None);

        assert_eq!(painted, win_status_bar_layout(&dwrite, rect, &bar));
    }
}