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
//! `CommandLine` primitive: a single-line input/output surface for editor
//! command prompts (`:`, `/`, `?`) and transient messages.
//!
//! Display-only �� the engine handles keystroke input and updates
//! `text` / `cursor_offset` each frame. Both TUI and GTK rasterisers
//! draw text with an optional insert cursor; alignment can be flipped
//! for right-aligned count displays.
//!
//! [`CommandLineLayout`] closes the character-offset hit-test
//! gap that made the command line mouse-selectable on TUI (via a
//! terminal-only inverted-cell read-back trick) and structurally unable to
//! be on GTK. `CommandLine::layout` resolves click/selection geometry the
//! same way every other primitive does — see [`CommandLineLayout::hit_test`]
//! and [`CommandLineLayout::selection_bounds`].

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

/// Declarative description of a command line surface.
///
/// # Examples
///
/// ```
/// use quadraui::{CommandLine, CommandLineMeasure, Rect, WidgetId};
///
/// let cmd = CommandLine {
///     id: WidgetId::new("cmdline:editor"),
///     text: ":wq".to_string(),
///     cursor_offset: Some(3),
///     right_align: false,
/// };
///
/// let rect = Rect::new(0.0, 23.0, 80.0, 1.0);
/// let layout = cmd.layout(rect, CommandLineMeasure::new(1.0));
///
/// assert_eq!(layout.hit_test(0.0), 0);
/// assert_eq!(layout.hit_test(100.0), cmd.text.len());
/// ```
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct CommandLine {
    pub id: WidgetId,
    /// Full display text (includes prompt character if any, e.g. `:wq`).
    pub text: String,
    /// Byte offset within `text` at which to draw the insert cursor.
    /// `None` suppresses the cursor (message-display mode).
    #[serde(default)]
    pub cursor_offset: Option<usize>,
    /// When `true`, right-align the text (used for count/match displays).
    #[serde(default)]
    pub right_align: bool,
}

/// Backend-supplied character metrics for [`CommandLine::layout`]. TUI
/// passes `1.0` (one cell per character); GTK/macOS pass the monospace
/// advance width of the active font, same convention as
/// [`crate::primitives::text_input::TextInputMeasure`].
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct CommandLineMeasure {
    pub char_width: f32,
}

impl CommandLineMeasure {
    pub fn new(char_width: f32) -> Self {
        Self { char_width }
    }

    /// Build from the backend's own [`crate::backend::Metrics`]
    /// (`backend.measure()`) instead of reading `backend.char_width()`
    /// by hand.
    pub fn from_metrics(m: &crate::backend::Metrics) -> Self {
        Self::new(m.char_width)
    }
}

/// Fully-resolved layout for a `CommandLine`.
///
/// Provides character-offset hit testing and selection bounds for all backends.
/// [`Self::hit_test`] maps a click x-coordinate to a **byte offset** into
/// [`CommandLine::text`] (matching [`CommandLine::cursor_offset`]'s contract),
/// and [`Self::selection_bounds`] turns a `(start, end)` byte-offset pair into a
/// paintable rect so a host can render a drag-selection highlight.
#[derive(Debug, Clone, PartialEq, Default)]
pub struct CommandLineLayout {
    /// Bounds this layout was computed for (matches the `rect` argument).
    pub bounds: Rect,
    /// x at which the first character is painted — `bounds.x` when
    /// left-aligned, shifted right when `right_align` pushes (short) text
    /// toward the far edge.
    pub text_origin_x: f32,
    /// Column width in surface-native units (TUI: 1.0 cells; GTK/macOS:
    /// the backend's monospace char width).
    pub char_width: f32,
    /// Byte offset of each character column in `text`, plus a trailing
    /// entry for `text.len()` — i.e. `len() == text.chars().count() + 1`.
    /// Lets `hit_test` / `selection_bounds` map columns to byte offsets
    /// (and back) without re-walking `text` on every call or storing a
    /// second copy of it.
    char_byte_offsets: Vec<usize>,
}

impl CommandLineLayout {
    /// Map an absolute x-coordinate to a byte offset into the source `text`,
    /// clamped to `[0, text.len()]`. Points left of the first character return `0`;
    /// points at or right of the last column return `text.len()`.
    ///
    /// Coordinate frame: **ABSOLUTE** — `x` is compared directly against
    /// `text_origin_x`, which already carries `bounds.x` and any right-align shift.
    pub fn hit_test(&self, x: f32) -> usize {
        let last = self.char_byte_offsets.last().copied().unwrap_or(0);
        if self.char_width <= 0.0 || self.char_byte_offsets.len() <= 1 {
            return last;
        }
        if x <= self.text_origin_x {
            return self.char_byte_offsets[0];
        }
        let col = ((x - self.text_origin_x) / self.char_width).floor() as usize;
        let max_col = self.char_byte_offsets.len() - 1;
        self.char_byte_offsets[col.min(max_col)]
    }

    /// Rect covering the single character column at `byte_offset` — its
    /// left edge to the next character's left edge. A `byte_offset` that
    /// doesn't land exactly on a known column (e.g. stale, or mid-char) is
    /// snapped to the column it falls within.
    pub fn char_bounds(&self, byte_offset: usize) -> Rect {
        let col = self.column_for_byte_offset(byte_offset);
        Rect::new(
            self.text_origin_x + col as f32 * self.char_width,
            self.bounds.y,
            self.char_width,
            self.bounds.height,
        )
    }

    /// Rect spanning the selection `[start, end)` (byte offsets, either order).
    /// Returns `None` for an empty/zero-width selection.
    pub fn selection_bounds(&self, sel: (usize, usize)) -> Option<Rect> {
        let (lo, hi) = if sel.0 <= sel.1 { sel } else { (sel.1, sel.0) };
        if lo == hi {
            return None;
        }
        let lo_col = self.column_for_byte_offset(lo);
        let hi_col = self.column_for_byte_offset(hi);
        if hi_col <= lo_col {
            return None;
        }
        Some(Rect::new(
            self.text_origin_x + lo_col as f32 * self.char_width,
            self.bounds.y,
            (hi_col - lo_col) as f32 * self.char_width,
            self.bounds.height,
        ))
    }

    /// Column index whose character starts at or covers `byte_offset`.
    fn column_for_byte_offset(&self, byte_offset: usize) -> usize {
        match self.char_byte_offsets.binary_search(&byte_offset) {
            Ok(i) => i,
            Err(0) => 0,
            Err(i) => i - 1,
        }
    }
}

impl CommandLine {
    /// Compute the click/selection geometry for painting or hit-testing
    /// this command line in `rect`, using `measure`'s column width.
    pub fn layout(&self, rect: Rect, measure: CommandLineMeasure) -> CommandLineLayout {
        let char_width = measure.char_width.max(0.0);
        let mut char_byte_offsets: Vec<usize> = self.text.char_indices().map(|(b, _)| b).collect();
        char_byte_offsets.push(self.text.len());
        let n_chars = char_byte_offsets.len().saturating_sub(1);

        let text_origin_x = if self.right_align && char_width > 0.0 {
            let text_w = n_chars as f32 * char_width;
            (rect.x + rect.width - text_w).max(rect.x)
        } else {
            rect.x
        };

        CommandLineLayout {
            bounds: rect,
            text_origin_x,
            char_width,
            char_byte_offsets,
        }
    }
}

/// Shared [`PaintSurface`](crate::paint_surface::PaintSurface)-backed paint
/// for [`CommandLine`]. Unifies selection highlight rendering across backends:
/// all backends now paint [`CommandLineLayout::selection_bounds`]'s rect,
/// clipped to the command bar bounds, with insert-cursor positioning via
/// [`CommandLineLayout::char_bounds`].
#[cfg(any(
    feature = "gtk",
    feature = "win",
    all(feature = "macos", target_os = "macos")
))]
#[allow(dead_code)]
pub(crate) mod native_surface_paint {
    use super::{CommandLine, CommandLineLayout, CommandLineMeasure};
    use crate::event::Rect;
    use crate::paint_surface::PaintSurface;
    use crate::theme::Theme;

    /// Insert-cursor width in surface-native units (px/DIP/pt): 2.0.
    const CURSOR_WIDTH: f32 = 2.0;

    /// Paint a [`CommandLine`] into `rect` on `surface`, highlighting
    /// `selection` (a `(start, end)` byte-offset pair, either order) if
    /// given, and return the resolved [`CommandLineLayout`] — same
    /// contract as [`crate::Backend::draw_command_line_selection`]. A
    /// `None` (or empty/zero-width) `selection` paints identically to
    /// [`crate::Backend::draw_command_line`].
    ///
    /// A non-positive `rect.width`/`rect.height` short-circuits to the
    /// no-paint layout without touching `surface` at all — see this
    /// module's doc, "Zero-size guard".
    pub(crate) fn paint(
        cmd: &CommandLine,
        surface: &mut dyn PaintSurface,
        theme: &Theme,
        rect: Rect,
        char_width: f32,
        selection: Option<(usize, usize)>,
    ) -> CommandLineLayout {
        let layout = cmd.layout(rect, CommandLineMeasure::new(char_width));

        if rect.width <= 0.0 || rect.height <= 0.0 {
            return layout;
        }

        surface.surface_push_clip(rect);
        surface.surface_fill_rect(rect, theme.command_line_bg);

        if let Some(sel) = selection {
            if let Some(r) = layout.selection_bounds(sel) {
                surface.surface_fill_rect_alpha(r, theme.selection, theme.selection_alpha);
            }
        }

        if !cmd.text.is_empty() {
            // `layout.text_origin_x` is an absolute coordinate carrying `rect.x` and any
            // right-align shift; paint starting there, bounded to the remainder of the bar.
            let text_rect = Rect::new(
                layout.text_origin_x,
                rect.y,
                (rect.x + rect.width - layout.text_origin_x).max(0.0),
                rect.height,
            );
            surface.surface_draw_text_run(text_rect, &cmd.text, theme.command_line_fg);

            if let Some(offset) = cmd.cursor_offset {
                let col_rect = layout.char_bounds(offset);
                let cursor_rect = Rect::new(col_rect.x, col_rect.y, CURSOR_WIDTH, col_rect.height);
                surface.surface_fill_rect(cursor_rect, theme.cursor);
            }
        }

        surface.surface_pop_clip();
        layout
    }

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

        /// Records every surface verb this primitive's paint uses —
        /// mirrors `primitives::status_bar`'s identical test double, so
        /// this test runs on any host without Cairo/Core Graphics/
        /// Direct2D.
        #[derive(Default)]
        struct RecordingSurface {
            fills: Vec<(Rect, Color)>,
            text_runs: Vec<(Rect, String, Color)>,
            clip_pushes: Vec<Rect>,
            clip_pops: usize,
        }

        impl PaintSurface for RecordingSurface {
            fn surface_begin_frame(&mut self, _viewport: crate::Viewport) {}
            fn surface_end_frame(&mut self) {}
            fn surface_viewport(&self) -> crate::Viewport {
                crate::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) {
                (text.chars().count() as f32 * 8.0, 16.0)
            }
            fn surface_fill_rect(&mut self, rect: Rect, color: Color) {
                self.fills.push((rect, color));
            }
            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) {
                self.text_runs.push((rect, text.to_string(), 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) {
                self.clip_pushes.push(rect);
            }
            fn surface_pop_clip(&mut self) {
                self.clip_pops += 1;
            }
            fn surface_draw_image(&mut self, _rect: Rect, _image: &Image) -> ImagePaintResult {
                ImagePaintResult::Unsupported
            }
        }

        fn sample(text: &str, cursor: Option<usize>, right_align: bool) -> CommandLine {
            CommandLine {
                id: WidgetId::new("cmdline"),
                text: text.into(),
                cursor_offset: cursor,
                right_align,
            }
        }

        #[test]
        fn fills_background_then_text_then_cursor() {
            let cmd = sample(":wq", Some(1), false);
            let theme = Theme::default();
            let mut surface = RecordingSurface::default();
            let rect = Rect::new(0.0, 0.0, 100.0, 20.0);
            let layout = paint(&cmd, &mut surface, &theme, rect, 8.0, None);

            assert_eq!(surface.fills[0], (rect, theme.command_line_bg));
            assert_eq!(surface.text_runs.len(), 1);
            assert_eq!(surface.text_runs[0].1, ":wq");
            assert_eq!(surface.text_runs[0].2, theme.command_line_fg);

            // Cursor rect is the second fill, at char_bounds(1).
            let cursor_rect = layout.char_bounds(1);
            assert_eq!(surface.fills[1].0.x, cursor_rect.x);
            assert_eq!(surface.fills[1].1, theme.cursor);
        }

        #[test]
        fn no_selection_paints_no_highlight() {
            let cmd = sample(":wq", None, false);
            let theme = Theme::default();
            let mut surface = RecordingSurface::default();
            paint(
                &cmd,
                &mut surface,
                &theme,
                Rect::new(0.0, 0.0, 100.0, 20.0),
                8.0,
                None,
            );
            // Only the background fill — no selection, no cursor.
            assert_eq!(surface.fills.len(), 1);
        }

        /// Selection highlight painting: unifies behavior across backends.
        #[test]
        fn selection_paints_highlight_before_text() {
            let cmd = sample(":wq!", None, false);
            let theme = Theme::default();
            let mut surface = RecordingSurface::default();
            let rect = Rect::new(0.0, 0.0, 100.0, 20.0);
            let layout = paint(&cmd, &mut surface, &theme, rect, 8.0, Some((0, 3)));

            let sel_rect = layout.selection_bounds((0, 3)).unwrap();
            // Highlight fill (index 1) comes after the background (index
            // 0) and before the text run.
            assert_eq!(surface.fills.len(), 2);
            assert_eq!(surface.fills[1].0, sel_rect);
            assert_eq!(
                surface.fills[1].1,
                theme.selection.with_alpha(theme.selection_alpha as f64)
            );
        }

        #[test]
        fn empty_selection_range_paints_no_highlight() {
            let cmd = sample(":wq!", None, false);
            let theme = Theme::default();
            let mut surface = RecordingSurface::default();
            paint(
                &cmd,
                &mut surface,
                &theme,
                Rect::new(0.0, 0.0, 100.0, 20.0),
                8.0,
                Some((2, 2)),
            );
            assert_eq!(surface.fills.len(), 1);
        }

        /// Closes the other documented divergence: GTK anchored the
        /// cursor at a real glyph-prefix width ignoring right-align;
        /// this shared `paint` anchors at the shared
        /// `CommandLineLayout::char_bounds`, which *does* account for
        /// the right-align shift.
        #[test]
        fn cursor_respects_right_align_shift() {
            let cmd = sample("3/17", Some(2), true);
            let theme = Theme::default();
            let mut surface = RecordingSurface::default();
            let rect = Rect::new(0.0, 0.0, 20.0, 20.0);
            let layout = paint(&cmd, &mut surface, &theme, rect, 1.0, None);

            let cursor_rect = layout.char_bounds(2);
            assert!(
                cursor_rect.x > rect.x,
                "right-aligned text's cursor must shift right of the bar origin"
            );
            let cursor_fill = surface
                .fills
                .iter()
                .find(|(r, c)| *c == theme.cursor && r.x == cursor_rect.x)
                .expect("cursor should paint at char_bounds(2)");
            let _ = cursor_fill;
        }

        #[test]
        fn zero_size_rect_paints_nothing() {
            let cmd = sample(":wq", Some(1), false);
            let theme = Theme::default();
            let mut surface = RecordingSurface::default();
            paint(
                &cmd,
                &mut surface,
                &theme,
                Rect::new(0.0, 0.0, 0.0, 20.0),
                8.0,
                Some((0, 1)),
            );
            assert!(surface.fills.is_empty());
            assert!(surface.text_runs.is_empty());
            assert!(surface.clip_pushes.is_empty());
            assert_eq!(surface.clip_pops, 0);
        }

        #[test]
        fn clip_pushed_and_popped_once_around_paint() {
            let cmd = sample(":wq", None, false);
            let theme = Theme::default();
            let mut surface = RecordingSurface::default();
            let rect = Rect::new(3.0, 4.0, 50.0, 20.0);
            paint(&cmd, &mut surface, &theme, rect, 8.0, None);
            assert_eq!(surface.clip_pushes, vec![rect]);
            assert_eq!(surface.clip_pops, 1);
        }

        /// A `cursor_offset` landing mid-character must not panic: `char_bounds`
        /// snaps via `column_for_byte_offset`, not a raw string slice.
        #[test]
        fn multibyte_cursor_offset_does_not_panic() {
            let text = ":éditer";
            assert!(!text.is_char_boundary(2));
            let cmd = sample(text, Some(2), false);
            let theme = Theme::default();
            let mut surface = RecordingSurface::default();
            paint(
                &cmd,
                &mut surface,
                &theme,
                Rect::new(0.0, 0.0, 100.0, 20.0),
                8.0,
                None,
            );
        }
    }
}

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

    #[test]
    fn round_trip_serde() {
        let cmd = CommandLine {
            id: "cmd".into(),
            text: ":wq".into(),
            cursor_offset: Some(3),
            right_align: false,
        };
        let json = serde_json::to_string(&cmd).unwrap();
        let back: CommandLine = serde_json::from_str(&json).unwrap();
        assert_eq!(back.text, ":wq");
        assert_eq!(back.cursor_offset, Some(3));
    }

    #[test]
    fn defaults_via_serde() {
        let json = r#"{"id":"cmd","text":"hello"}"#;
        let cmd: CommandLine = serde_json::from_str(json).unwrap();
        assert_eq!(cmd.cursor_offset, None);
        assert!(!cmd.right_align);
    }

    // ── CommandLineLayout ───────────────────────────────────────────────

    fn cmd(text: &str, right_align: bool) -> CommandLine {
        CommandLine {
            id: WidgetId::new("cmd"),
            text: text.into(),
            cursor_offset: None,
            right_align,
        }
    }

    #[test]
    fn hit_test_maps_x_to_byte_offset_left_aligned() {
        let c = cmd(":wq", false);
        let layout = c.layout(Rect::new(0.0, 0.0, 20.0, 1.0), CommandLineMeasure::new(1.0));
        assert_eq!(layout.hit_test(0.0), 0); // before ':'
        assert_eq!(layout.hit_test(1.5), 1); // inside 'w'
        assert_eq!(layout.hit_test(100.0), 3); // past the end -> text.len()
    }

    #[test]
    fn hit_test_at_nonzero_origin() {
        // A LOCAL/ABSOLUTE mixup is invisible at rect.x == 0, so this
        // primitive-level test uses a nonzero origin too.
        let c = cmd(":wq", false);
        let layout = c.layout(
            Rect::new(10.0, 5.0, 20.0, 1.0),
            CommandLineMeasure::new(1.0),
        );
        // x < text_origin_x (10.0) clamps to the first column.
        assert_eq!(layout.hit_test(3.0), 0);
        assert_eq!(layout.hit_test(10.0), 0);
        assert_eq!(layout.hit_test(11.5), 1);
    }

    #[test]
    fn hit_test_accounts_for_multibyte_chars() {
        // ":éditer" — 'é' is 2 bytes; byte offsets after it must skip a byte per
        let c = cmd(":éditer", false);
        let layout = c.layout(Rect::new(0.0, 0.0, 20.0, 1.0), CommandLineMeasure::new(1.0));
        // Columns: ':' (0) 'é' (1) 'd' (2) 'i' (3) ...
        assert_eq!(layout.hit_test(0.5), 0);
        assert_eq!(layout.hit_test(1.5), 1); // start of 'é', byte offset 1
        assert_eq!(layout.hit_test(2.5), 3); // start of 'd' -> byte offset 3 (post 2-byte 'é')
    }

    #[test]
    fn hit_test_right_aligned_shifts_text_origin() {
        let c = cmd("3/17", true);
        let layout = c.layout(Rect::new(0.0, 0.0, 10.0, 1.0), CommandLineMeasure::new(1.0));
        // 4 chars in a 10-wide rect -> text starts at x = 6.
        assert_eq!(layout.text_origin_x, 6.0);
        assert_eq!(layout.hit_test(0.0), 0); // left of text -> first column
        assert_eq!(layout.hit_test(6.5), 0);
        assert_eq!(layout.hit_test(7.5), 1);
    }

    #[test]
    fn hit_test_empty_text_always_zero() {
        let c = cmd("", false);
        let layout = c.layout(Rect::new(0.0, 0.0, 10.0, 1.0), CommandLineMeasure::new(1.0));
        assert_eq!(layout.hit_test(0.0), 0);
        assert_eq!(layout.hit_test(9.0), 0);
    }

    #[test]
    fn selection_bounds_spans_the_selected_columns() {
        let c = cmd(":wq!", false);
        let layout = c.layout(Rect::new(0.0, 0.0, 20.0, 1.0), CommandLineMeasure::new(1.0));
        // Select ":wq" -> byte offsets 0..3.
        let r = layout.selection_bounds((0, 3)).unwrap();
        assert_eq!((r.x, r.width), (0.0, 3.0));
    }

    #[test]
    fn selection_bounds_order_independent() {
        let c = cmd(":wq!", false);
        let layout = c.layout(Rect::new(0.0, 0.0, 20.0, 1.0), CommandLineMeasure::new(1.0));
        assert_eq!(
            layout.selection_bounds((3, 0)),
            layout.selection_bounds((0, 3))
        );
    }

    #[test]
    fn selection_bounds_empty_range_is_none() {
        let c = cmd(":wq!", false);
        let layout = c.layout(Rect::new(0.0, 0.0, 20.0, 1.0), CommandLineMeasure::new(1.0));
        assert!(layout.selection_bounds((2, 2)).is_none());
    }

    #[test]
    fn selection_bounds_at_nonzero_origin() {
        let c = cmd(":wq!", false);
        let layout = c.layout(
            Rect::new(10.0, 5.0, 20.0, 1.0),
            CommandLineMeasure::new(1.0),
        );
        let r = layout.selection_bounds((0, 3)).unwrap();
        assert_eq!((r.x, r.y, r.width), (10.0, 5.0, 3.0));
    }

    #[test]
    fn char_bounds_snaps_mid_char_offset_to_its_column() {
        let c = cmd(":éditer", false);
        let layout = c.layout(Rect::new(0.0, 0.0, 20.0, 1.0), CommandLineMeasure::new(1.0));
        // Byte 2 sits inside 'é' (bytes 1..3); should snap to column 1.
        let snapped = layout.char_bounds(2);
        let exact = layout.char_bounds(1);
        assert_eq!(snapped, exact);
    }

    #[test]
    fn roundtrip_hit_test_then_selection_bounds() {
        // A host drags from x=1 to x=3 over ":wq!" and should get back a
        // selection rect covering exactly the "wq" columns.
        let c = cmd(":wq!", false);
        let layout = c.layout(Rect::new(0.0, 0.0, 20.0, 1.0), CommandLineMeasure::new(1.0));
        let start = layout.hit_test(1.0);
        let end = layout.hit_test(3.0);
        let r = layout.selection_bounds((start, end)).unwrap();
        assert_eq!((r.x, r.width), (1.0, 2.0));
    }
}