agg-gui 0.4.0

Immediate-mode Rust GUI library with AGG rendering, Y-up layout, widgets, text, SVG, and native/WASM adapters
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
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
//! Unit tests for the interactive rich-text editor: the logical [`RichEditCore`]
//! (typing / pending style / toggle-over-selection / undo coalescing / clipboard
//! round-trip) and the widget's caret hit-testing across mixed font sizes.

use std::sync::Arc;

use crate::color::Color;
use crate::event::{Event, Key, Modifiers, MouseButton};
use crate::geometry::{Point, Size};
use crate::text::Font;
use crate::widget::Widget;
use crate::widgets::rich_text::commands::RichCommand;
use crate::widgets::rich_text::model::{Block, DocPos, InlineStyle, ListKind, RichDoc, TextRun};
use crate::widgets::rich_text::view::SharedResolver;
use crate::widgets::text_area::TextHAlign;

use super::core::RichEditCore;
use super::RichTextEdit;

const FONT_BYTES: &[u8] = include_bytes!("../../../../../demo/assets/Arial-Regular.ttf");

fn font() -> Arc<Font> {
    Arc::new(Font::from_slice(FONT_BYTES).expect("test font loads"))
}

fn resolver() -> SharedResolver {
    let f = font();
    std::rc::Rc::new(move |_: &InlineStyle| Arc::clone(&f))
}

fn bold_style() -> InlineStyle {
    InlineStyle {
        bold: true,
        ..Default::default()
    }
}

fn sized(text: &str, size: f64) -> TextRun {
    TextRun::new(
        text,
        InlineStyle {
            font_size: Some(size),
            ..Default::default()
        },
    )
}

// ── Logical core tests ────────────────────────────────────────────────────

#[test]
fn typing_inherits_style_at_caret() {
    // Caret at the end of a bold run: the next char must inherit bold.
    let doc = RichDoc::from_blocks(vec![Block::from_run(TextRun::new("X", bold_style()))]);
    let mut core = RichEditCore::new(doc, 16.0);
    core.set_caret(DocPos::new(0, 1), false);
    core.insert("y");
    let block = &core.doc().blocks[0];
    assert_eq!(block.text(), "Xy");
    // Normalised into a single bold run.
    assert_eq!(block.runs.len(), 1);
    assert!(block.runs[0].style.bold);
}

#[test]
fn pending_style_applies_to_next_insert() {
    let doc = RichDoc::from_blocks(vec![Block::plain("hi")]);
    let mut core = RichEditCore::new(doc, 16.0);
    core.set_caret(DocPos::new(0, 2), false);
    // Toggle bold with a collapsed selection: arms a pending style, no mutation.
    core.exec(&RichCommand::ToggleBold);
    assert_eq!(core.doc().blocks[0].text(), "hi");
    assert!(core.pending_style().is_some());
    // The next typed char is bold; the plain prefix is untouched.
    core.insert("Z");
    let block = &core.doc().blocks[0];
    assert_eq!(block.text(), "hiZ");
    let last = block.runs.last().unwrap();
    assert_eq!(last.text, "Z");
    assert!(last.style.bold);
    // Pending style consumed after typing.
    assert!(core.pending_style().is_none());
}

#[test]
fn moving_caret_clears_pending_style() {
    let doc = RichDoc::from_blocks(vec![Block::plain("hi")]);
    let mut core = RichEditCore::new(doc, 16.0);
    core.set_caret(DocPos::new(0, 2), false);
    core.exec(&RichCommand::ToggleBold);
    assert!(core.pending_style().is_some());
    core.set_caret(DocPos::new(0, 0), false);
    assert!(core.pending_style().is_none());
}

#[test]
fn toggle_bold_over_selection_through_exec() {
    let doc = RichDoc::from_blocks(vec![Block::plain("hello")]);
    let mut core = RichEditCore::new(doc, 16.0);
    core.select_all();
    core.exec(&RichCommand::ToggleBold);
    assert_eq!(core.common_style_of_selection().bold, Some(true));
    // A second toggle clears it.
    core.exec(&RichCommand::ToggleBold);
    assert_eq!(core.common_style_of_selection().bold, Some(false));
}

#[test]
fn pending_style_keeps_block_align_and_list_active() {
    // Caret in a left-aligned bullet block; arm bold with a collapsed caret.
    // The inline pending style must not blank out the block-level toolbar
    // state: alignment and list stay reported from the caret's block.
    let doc = RichDoc::from_blocks(vec![Block {
        runs: vec![TextRun::plain("item")],
        align: TextHAlign::Left,
        list: ListKind::Bullet,
        indent: 0,
    }]);
    let mut core = RichEditCore::new(doc, 16.0);
    core.set_caret(DocPos::new(0, 4), false);
    core.exec(&RichCommand::ToggleBold);
    assert!(core.pending_style().is_some(), "bold armed at the caret");

    let cs = core.common_style_of_selection();
    assert_eq!(cs.bold, Some(true), "pending bold is reported");
    assert_eq!(
        cs.align,
        Some(TextHAlign::Left),
        "block alignment must survive an armed pending style"
    );
    assert_eq!(
        cs.list,
        Some(ListKind::Bullet),
        "block list kind must survive an armed pending style"
    );
}

#[test]
fn undo_coalesces_rapid_typing_into_one_step() {
    let doc = RichDoc::from_blocks(vec![Block::plain("Hi")]);
    let mut core = RichEditCore::new(doc, 16.0);
    core.set_caret(core.doc().end_pos(), false);
    // Baseline snapshot.
    core.feed_undo(0.0);

    // Rapid typing within stable_time — must not each become an undo point.
    core.insert("a");
    core.feed_undo(0.1);
    core.insert("b");
    core.feed_undo(0.2);
    core.insert("c");
    core.feed_undo(0.3);
    assert_eq!(core.doc().blocks[0].text(), "Hiabc");

    // Hold steady past stable_time (default 1.0s) → exactly one undo point.
    core.feed_undo(0.4);
    core.feed_undo(1.6);
    assert!(core.can_undo());

    // A single undo reverts the whole coalesced run back to the baseline.
    assert!(core.undo());
    assert_eq!(core.doc().blocks[0].text(), "Hi");
    assert!(!core.can_undo(), "typing coalesced into one undo step");
}

// ── Live colour preview: undo hygiene ─────────────────────────────────────
//
// A colour dialog previews by exec-ing `SetTextColor` every drag frame. The
// preview session (`begin_preview` → `commit_preview` / `cancel_preview`)
// suspends undo feeding so the drag collapses into ONE undo step on commit and
// leaves NO stray entry on cancel.

fn colored(text: &str, color: crate::color::Color) -> TextRun {
    TextRun::new(
        text,
        InlineStyle {
            text_color: Some(color),
            ..Default::default()
        },
    )
}

#[test]
fn preview_commit_collapses_drag_into_one_undo_step() {
    let red = crate::color::Color::rgb(1.0, 0.0, 0.0);
    let blue = crate::color::Color::rgb(0.0, 0.0, 1.0);
    let green = crate::color::Color::rgb(0.0, 1.0, 0.0);
    let doc = RichDoc::from_blocks(vec![Block::from_run(colored("hello", red))]);
    let mut core = RichEditCore::new(doc, 16.0);
    core.select_all();
    core.feed_undo(0.0); // baseline undo point (red)
    assert!(!core.can_undo());

    // Dialog opens: capture the committed state + suspend feeding.
    core.begin_preview();
    assert!(core.is_previewing());

    // Drag: rapid live previews. Feeding is suspended, so none snapshot.
    core.exec(&RichCommand::SetTextColor(blue));
    core.feed_undo(0.1);
    core.exec(&RichCommand::SetTextColor(green));
    core.feed_undo(0.2);

    // Select: commit + resume, then settle into exactly one undo point.
    core.commit_preview();
    assert!(!core.is_previewing());
    core.feed_undo(3.0);
    core.feed_undo(4.5);
    assert!(core.can_undo(), "the committed colour change is undoable");

    // A single undo reverts the whole drag back to the original colour, and
    // there is nothing left to undo — the drag collapsed into one step.
    assert!(core.undo());
    assert_eq!(core.common_style_of_selection().text_color, Some(Some(red)));
    assert!(!core.can_undo(), "the drag must collapse into a single undo step");
}

#[test]
fn preview_cancel_restores_and_leaves_no_undo_residue() {
    let red = crate::color::Color::rgb(1.0, 0.0, 0.0);
    let blue = crate::color::Color::rgb(0.0, 0.0, 1.0);
    let doc = RichDoc::from_blocks(vec![Block::from_run(colored("hello", red))]);
    let mut core = RichEditCore::new(doc, 16.0);
    core.select_all();
    core.feed_undo(0.0); // baseline undo point (red)
    assert!(!core.can_undo());

    core.begin_preview();
    // Preview to a different colour, feeding at times that WOULD stabilise into
    // an undo point (gap ≥ stable_time) if suspension were broken.
    core.exec(&RichCommand::SetTextColor(blue));
    core.feed_undo(0.1);
    core.feed_undo(2.0);
    assert_eq!(
        core.common_style_of_selection().text_color,
        Some(Some(blue)),
        "the preview updates the document live"
    );

    // Cancel: restore the captured snapshot + resume.
    core.cancel_preview();
    assert!(!core.is_previewing());
    assert_eq!(
        core.common_style_of_selection().text_color,
        Some(Some(red)),
        "cancel restores the original colour"
    );

    // Resume feeding: the restored state equals the committed baseline, so no
    // stray undo entry survives the cancelled preview.
    core.feed_undo(3.0);
    core.feed_undo(4.5);
    assert!(
        !core.can_undo(),
        "a cancelled preview must leave no undo residue"
    );
}

#[test]
fn clipboard_round_trip_across_blocks() {
    // Multi-block selection flattens to text with `\n`; re-inserting it rebuilds
    // the paragraph split. This exercises the plain-text bridge Copy/Paste use.
    let doc = RichDoc::from_blocks(vec![Block::plain("alpha"), Block::plain("beta")]);
    let mut core = RichEditCore::new(doc, 16.0);
    core.select_all();
    let copied = core.selected_plain_text();
    assert_eq!(copied, "alpha\nbeta");

    let mut fresh = RichEditCore::new(RichDoc::new(), 16.0);
    fresh.insert(&copied);
    assert_eq!(fresh.doc().blocks.len(), 2);
    assert_eq!(fresh.doc().blocks[0].text(), "alpha");
    assert_eq!(fresh.doc().blocks[1].text(), "beta");
}

#[test]
fn enter_on_empty_list_item_exits_list_at_indent_zero() {
    // An empty bullet item + Enter becomes a plain paragraph (no new bullet).
    let doc = RichDoc::from_blocks(vec![Block {
        runs: vec![],
        list: ListKind::Bullet,
        indent: 0,
        ..Block::new()
    }]);
    let mut core = RichEditCore::new(doc, 16.0);
    core.set_caret(DocPos::new(0, 0), false);
    core.split();
    assert_eq!(core.doc().blocks.len(), 1, "no split occurred");
    assert_eq!(core.doc().blocks[0].list, ListKind::None);
    assert_eq!(core.doc().blocks[0].indent, 0);
}

#[test]
fn enter_on_empty_nested_list_item_outdents() {
    // An empty nested item + Enter decreases indent and keeps the list.
    let doc = RichDoc::from_blocks(vec![Block {
        runs: vec![],
        list: ListKind::Ordered,
        indent: 2,
        ..Block::new()
    }]);
    let mut core = RichEditCore::new(doc, 16.0);
    core.set_caret(DocPos::new(0, 0), false);
    core.split();
    assert_eq!(core.doc().blocks.len(), 1, "no split occurred");
    assert_eq!(core.doc().blocks[0].list, ListKind::Ordered);
    assert_eq!(core.doc().blocks[0].indent, 1);
}

#[test]
fn enter_on_non_empty_list_item_splits_normally() {
    let doc = RichDoc::from_blocks(vec![Block {
        runs: vec![TextRun::plain("item")],
        list: ListKind::Bullet,
        ..Block::new()
    }]);
    let mut core = RichEditCore::new(doc, 16.0);
    core.set_caret(core.doc().end_pos(), false);
    core.split();
    assert_eq!(core.doc().blocks.len(), 2, "non-empty item splits");
    // The new block inherits the list kind (an empty continuation bullet).
    assert_eq!(core.doc().blocks[1].list, ListKind::Bullet);
    assert_eq!(core.doc().blocks[1].text(), "");
}

// Programmatic handle-API tests live in `handle_api_tests.rs` (declared from
// `editor.rs`) to keep this file under the 800-line cap.

// ── Styled clipboard (rich Copy / Cut / Paste) ─────────────────────────────

fn ctrl(c: char) -> Event {
    Event::KeyDown {
        key: Key::Char(c),
        modifiers: Modifiers {
            ctrl: true,
            ..Default::default()
        },
    }
}

fn red_24_bold() -> InlineStyle {
    InlineStyle {
        bold: true,
        font_size: Some(24.0),
        text_color: Some(Color::from_rgb8(255, 0, 0)),
        ..Default::default()
    }
}

/// Copy a styled, bulleted line and paste it into a fresh editor: the bold /
/// 24pt / red run and the list decoration must all survive the round trip
/// through the in-process rich clipboard slot.
#[test]
fn styled_run_and_list_block_survive_copy_paste() {
    crate::widgets::rich_text::rich_clipboard::clear();
    let doc = RichDoc::from_blocks(vec![Block {
        runs: vec![TextRun::new("Hi", red_24_bold())],
        list: ListKind::Bullet,
        ..Block::new()
    }]);
    let mut src = laid_out_editor(doc, 400.0, 200.0);
    src.on_event(&Event::FocusGained);
    src.core.borrow_mut().select_all();
    src.on_event(&ctrl('c'));

    // Cross-instance paste into a blank editor.
    let mut dst = laid_out_editor(RichDoc::new(), 400.0, 200.0);
    dst.on_event(&Event::FocusGained);
    dst.on_event(&ctrl('v'));

    let doc = dst.core.borrow().doc().clone();
    assert_eq!(doc.blocks.len(), 1);
    assert_eq!(doc.blocks[0].list, ListKind::Bullet, "list decoration survives");
    let run = &doc.blocks[0].runs[0];
    assert_eq!(run.text, "Hi");
    assert!(run.style.bold, "bold survives");
    assert_eq!(run.style.font_size, Some(24.0), "point size survives");
    assert_eq!(
        run.style.text_color,
        Some(Color::from_rgb8(255, 0, 0)),
        "colour survives"
    );
}

/// A native clipboard (arboard on Windows) may hand back `\r\n` for text we
/// copied with `\n`. The fingerprint match normalizes line endings, so a styled
/// multi-line copy still pastes styled after that round trip.
#[test]
fn crlf_mangled_clipboard_still_matches_fingerprint() {
    crate::widgets::rich_text::rich_clipboard::clear();
    let doc = RichDoc::from_blocks(vec![
        Block::from_run(TextRun::new("one", red_24_bold())),
        Block::from_run(TextRun::new("two", red_24_bold())),
    ]);
    let mut src = laid_out_editor(doc, 400.0, 200.0);
    src.on_event(&Event::FocusGained);
    src.core.borrow_mut().select_all();
    src.on_event(&ctrl('c'));

    // Simulate the OS clipboard normalizing "one\ntwo" to CRLF line endings.
    crate::clipboard::set_text("one\r\ntwo");

    let mut dst = laid_out_editor(RichDoc::new(), 400.0, 200.0);
    dst.on_event(&Event::FocusGained);
    dst.on_event(&ctrl('v'));

    let doc = dst.core.borrow().doc().clone();
    assert_eq!(doc.blocks.len(), 2);
    assert!(doc.blocks[0].runs[0].style.bold, "styled fragment reused");
    assert!(doc.blocks[1].runs[0].style.bold);
}

/// When the system clipboard text no longer matches our stored fingerprint
/// (something was copied elsewhere in between), paste falls back to inserting
/// the external plain text in the caret's inherited style.
#[test]
fn fingerprint_mismatch_falls_back_to_plain_text() {
    crate::widgets::rich_text::rich_clipboard::clear();
    let doc = RichDoc::from_blocks(vec![Block::from_run(TextRun::new("Hi", red_24_bold()))]);
    let mut src = laid_out_editor(doc, 400.0, 200.0);
    src.on_event(&Event::FocusGained);
    src.core.borrow_mut().select_all();
    src.on_event(&ctrl('c'));

    // Simulate an external app overwriting the system clipboard: the rich slot
    // still holds the styled fragment, but its fingerprint no longer matches.
    crate::clipboard::set_text("external");

    let mut dst = laid_out_editor(RichDoc::new(), 400.0, 200.0);
    dst.on_event(&Event::FocusGained);
    dst.on_event(&ctrl('v'));

    let doc = dst.core.borrow().doc().clone();
    assert_eq!(doc.blocks[0].text(), "external");
    // Inserted as plain text — no bold carried over from the stale fragment.
    assert!(!doc.blocks[0].runs[0].style.bold);
}

/// External plain text (never copied from a RichTextEdit) pastes as plain text.
#[test]
fn external_plain_text_pastes_unstyled() {
    crate::widgets::rich_text::rich_clipboard::clear();
    crate::clipboard::set_text("hello world");
    let mut dst = laid_out_editor(RichDoc::new(), 400.0, 200.0);
    dst.on_event(&Event::FocusGained);
    dst.on_event(&ctrl('v'));
    let doc = dst.core.borrow().doc().clone();
    assert_eq!(doc.blocks[0].text(), "hello world");
    assert!(!doc.blocks[0].runs[0].style.bold);
}

/// Cut removes the styled selection and still makes it available for a styled
/// paste elsewhere.
#[test]
fn cut_removes_styled_content_and_keeps_it_for_paste() {
    crate::widgets::rich_text::rich_clipboard::clear();
    let doc = RichDoc::from_blocks(vec![Block {
        runs: vec![TextRun::new("keep ", InlineStyle::default()), TextRun::new("cutme", red_24_bold())],
        ..Block::new()
    }]);
    let mut src = laid_out_editor(doc, 400.0, 200.0);
    src.on_event(&Event::FocusGained);
    // Select just "cutme".
    src.core.borrow_mut().set_selection(DocPos::new(0, 5), DocPos::new(0, 10));
    src.on_event(&ctrl('x'));
    assert_eq!(src.core.borrow().doc().blocks[0].text(), "keep ");

    let mut dst = laid_out_editor(RichDoc::new(), 400.0, 200.0);
    dst.on_event(&Event::FocusGained);
    dst.on_event(&ctrl('v'));
    let doc = dst.core.borrow().doc().clone();
    assert_eq!(doc.blocks[0].text(), "cutme");
    assert!(doc.blocks[0].runs[0].style.bold);
}

/// A styled paste is a single undo step, matching plain paste.
#[test]
fn styled_paste_is_one_undo_step() {
    crate::widgets::rich_text::rich_clipboard::clear();
    let doc = RichDoc::from_blocks(vec![Block::from_run(TextRun::new("AB", red_24_bold()))]);
    let mut src = laid_out_editor(doc, 400.0, 200.0);
    src.on_event(&Event::FocusGained);
    src.core.borrow_mut().select_all();
    src.on_event(&ctrl('c'));

    let mut dst = laid_out_editor(RichDoc::new(), 400.0, 200.0);
    dst.on_event(&Event::FocusGained);
    // Baseline undo snapshot for the empty doc, then paste and let it settle.
    dst.core.borrow_mut().feed_undo(0.0);
    dst.on_event(&ctrl('v'));
    dst.core.borrow_mut().feed_undo(0.1);
    dst.core.borrow_mut().feed_undo(1.5);
    assert_eq!(dst.core.borrow().doc().blocks[0].text(), "AB");

    assert!(dst.core.borrow_mut().undo(), "one undo reverts the paste");
    assert_eq!(dst.core.borrow().doc().blocks[0].text(), "");
    assert!(!dst.core.borrow().can_undo(), "paste was a single undo step");
}

// ── Widget geometry (caret hit-testing) ───────────────────────────────────

fn laid_out_editor(doc: RichDoc, w: f64, h: f64) -> RichTextEdit {
    let mut ed = RichTextEdit::new(doc, resolver()).with_font_size(16.0);
    ed.layout(Size::new(w, h));
    ed
}

#[test]
fn caret_hit_test_round_trips_at_mixed_sizes() {
    // One line mixing a 12px run and a 32px run. Hit-testing at a byte's own
    // caret x must return that byte, proving per-fragment metrics are honoured.
    let doc = RichDoc::from_blocks(vec![Block {
        runs: vec![sized("small ", 12.0), sized("BIG", 32.0)],
        ..Block::new()
    }]);
    let ed = laid_out_editor(doc, 400.0, 120.0);

    for byte in [0usize, 3, 6, 7, 9] {
        let pos = DocPos::new(0, byte);
        let geom = ed
            .caret_geometry(pos)
            .expect("caret geometry after layout");
        // Sample just inside the caret column, on the caret's line.
        let probe = Point::new(geom.x + 0.5, geom.y_bottom + geom.height * 0.5);
        let hit = ed.hit_test_pos(probe);
        assert_eq!(hit.block, 0);
        assert_eq!(
            hit.byte, byte,
            "hit-test at caret x for byte {byte} returned {}",
            hit.byte
        );
    }
}

#[test]
fn click_before_first_char_is_byte_zero() {
    let doc = RichDoc::from_blocks(vec![Block::from_run(sized("hello", 16.0))]);
    let ed = laid_out_editor(doc, 400.0, 120.0);
    let geom = ed.caret_geometry(DocPos::new(0, 0)).unwrap();
    // Far to the left of the text still clamps to the line start.
    let hit = ed.hit_test_pos(Point::new(geom.x - 50.0, geom.y_bottom + 2.0));
    assert_eq!(hit, DocPos::new(0, 0));
}

// ── Double-click word / triple-click block selection ──────────────────────

fn plain_doc(blocks: &[&str]) -> RichDoc {
    RichDoc::from_blocks(blocks.iter().map(|s| Block::plain(*s)).collect())
}

#[test]
fn double_click_selects_word() {
    let mut ed = laid_out_editor(plain_doc(&["hello world"]), 400.0, 120.0);
    // DocPos byte 8 is inside "world".
    ed.begin_pointer_selection(DocPos::new(0, 8), 2, false);
    assert_eq!(ed.core.borrow().selected_plain_text(), "world");
}

#[test]
fn double_click_stops_at_punctuation() {
    let mut ed = laid_out_editor(plain_doc(&["foo.bar"]), 400.0, 120.0);
    ed.begin_pointer_selection(DocPos::new(0, 1), 2, false);
    assert_eq!(ed.core.borrow().selected_plain_text(), "foo");
    ed.begin_pointer_selection(DocPos::new(0, 5), 2, false);
    assert_eq!(ed.core.borrow().selected_plain_text(), "bar");
}

#[test]
fn triple_click_selects_block() {
    let mut ed = laid_out_editor(plain_doc(&["first block", "second block"]), 400.0, 200.0);
    // Triple-click in the second block selects that whole block.
    ed.begin_pointer_selection(DocPos::new(1, 4), 3, false);
    assert_eq!(ed.core.borrow().selected_plain_text(), "second block");
}

#[test]
fn word_drag_extends_by_whole_words() {
    let mut ed = laid_out_editor(plain_doc(&["alpha beta gamma"]), 400.0, 120.0);
    ed.begin_pointer_selection(DocPos::new(0, 7), 2, false); // "beta"
    assert_eq!(ed.core.borrow().selected_plain_text(), "beta");
    ed.extend_selection_drag(DocPos::new(0, 13)); // drag into "gamma"
    assert_eq!(ed.core.borrow().selected_plain_text(), "beta gamma");
    ed.extend_selection_drag(DocPos::new(0, 2)); // drag back into "alpha"
    assert_eq!(ed.core.borrow().selected_plain_text(), "alpha beta");
}

/// A real double `MouseDown` at the same point selects the word, proving the
/// multi-click counter is wired through the event handler.
#[test]
fn double_mouse_down_event_selects_word() {
    let mut ed = laid_out_editor(plain_doc(&["hello world"]), 400.0, 120.0);
    ed.on_event(&Event::FocusGained);
    let geom = ed.caret_geometry(DocPos::new(0, 8)).unwrap();
    let click = Point::new(geom.x + 0.5, geom.y_bottom + geom.height * 0.5);
    let down = Event::MouseDown {
        pos: click,
        button: MouseButton::Left,
        modifiers: Modifiers::default(),
    };
    ed.on_event(&down);
    ed.on_event(&Event::MouseUp {
        pos: click,
        button: MouseButton::Left,
        modifiers: Modifiers::default(),
    });
    ed.on_event(&down);
    assert_eq!(ed.core.borrow().selected_plain_text(), "world");
}

// ── Text-rendering pipeline: LCD routing ───────────────────────────────────

/// The editor must follow the System LCD setting exactly like `Label` /
/// `TextField` / `TextArea`: an `LcdCoverage` backbuffer when LCD is on, a
/// grayscale `Rgba` backbuffer when it is off.
#[test]
fn backbuffer_mode_follows_lcd_setting() {
    use crate::widget::BackbufferMode;
    // Standard density so the high-scale hard cap in `lcd_enabled` doesn't mask
    // the explicit override under test.
    crate::device_scale::set_device_scale(1.0);
    crate::ux_scale::set_ux_scale(1.0);

    let doc = RichDoc::from_blocks(vec![Block {
        runs: vec![sized("hello", 16.0)],
        ..Block::new()
    }]);
    let ed = laid_out_editor(doc, 400.0, 120.0);

    crate::font_settings::set_lcd_enabled(true);
    assert_eq!(ed.backbuffer_mode(), BackbufferMode::LcdCoverage);

    crate::font_settings::set_lcd_enabled(false);
    assert_eq!(ed.backbuffer_mode(), BackbufferMode::Rgba);

    crate::font_settings::clear_lcd_enabled_override();
}

/// An async font arrival reshapes the layout via `invalidate_layout` WITHOUT
/// advancing `core.rev()`, so the paint signature must fold in the layout
/// generation or the backbuffer would keep blitting the stale fallback-font
/// bitmap. Bumping the generation must change the sig.
#[test]
fn invalidate_layout_changes_cache_sig() {
    let doc = RichDoc::from_blocks(vec![Block {
        runs: vec![sized("hello", 16.0)],
        ..Block::new()
    }]);
    let mut ed = laid_out_editor(doc, 400.0, 120.0);

    let before = ed.cache_sig();
    // No document/caret change — `core.rev()` is untouched.
    let rev_before = ed.core.borrow().rev();
    ed.invalidate_layout();
    assert_eq!(ed.core.borrow().rev(), rev_before, "rev must not change");
    let after = ed.cache_sig();
    assert_ne!(
        before, after,
        "invalidate_layout must change the cache sig so an async font arrival re-rasters"
    );
}

// ── Caret-blink redraw cadence ──────────────────────────────────────────────

/// A focused, idle editor must not spin the render loop: `needs_draw` stays
/// `false` between 500 ms blink boundaries and `next_draw_deadline` reports the
/// exact next boundary so the host wakes twice a second instead.
#[test]
fn focused_idle_editor_does_not_redraw_every_frame() {
    use std::time::Duration;
    use web_time::Instant;

    let mut ed = laid_out_editor(plain_doc(&["hello"]), 400.0, 120.0);
    ed.focused = true;
    let now = Instant::now();
    ed.focus_time = Some(now);
    ed.blink_last_phase.set(0);

    assert!(
        !ed.needs_draw(),
        "idle focused editor should not report a per-frame draw need"
    );
    let deadline = ed
        .next_draw_deadline()
        .expect("focused editor schedules a blink wake");
    let remaining = deadline.saturating_duration_since(now);
    assert!(
        remaining > Duration::ZERO && remaining <= Duration::from_millis(500),
        "next blink deadline should be within one 500 ms interval, got {remaining:?}"
    );
}

#[test]
fn blink_boundary_requests_one_draw_then_reschedules() {
    use std::time::Duration;
    use web_time::Instant;

    let mut ed = laid_out_editor(plain_doc(&["hello"]), 400.0, 120.0);
    ed.focused = true;
    ed.focus_time = Some(Instant::now() - Duration::from_millis(600));
    ed.blink_last_phase.set(0);
    assert!(
        ed.needs_draw(),
        "a crossed blink boundary should request exactly one draw"
    );
    ed.blink_last_phase.set(1);
    assert!(
        !ed.needs_draw(),
        "after painting the new phase the editor should go idle again"
    );
}

#[test]
fn unfocused_editor_is_quiet() {
    let ed = laid_out_editor(plain_doc(&["hello"]), 400.0, 120.0);
    assert!(!ed.needs_draw(), "unfocused editor must not request draws");
    assert!(
        ed.next_draw_deadline().is_none(),
        "unfocused editor schedules no wake"
    );
}

// ── Wheel scrolling speed ───────────────────────────────────────────────────

/// One wheel notch scrolls ≈ 3 visual-line heights (Windows convention), not a
/// single pixel.
#[test]
fn one_wheel_notch_scrolls_about_three_lines() {
    let blocks: Vec<String> = (0..40).map(|i| format!("line {i}")).collect();
    let refs: Vec<&str> = blocks.iter().map(String::as_str).collect();
    let mut ed = laid_out_editor(plain_doc(&refs), 400.0, 100.0);
    assert!(ed.max_scroll_y() > 0.0, "content should overflow the viewport");

    let line_h = ed
        .visual_lines()
        .first()
        .map(|&(_, _, _, h)| h)
        .expect("laid-out editor has visual lines");
    assert!(line_h > 0.0);

    let before = ed.scroll_offset();
    // One notch downward: negative delta_y increases the offset.
    assert!(ed.scroll_by_wheel(-1.0), "a notch should move the offset");
    let moved = ed.scroll_offset() - before;
    let expected = 3.0 * line_h;
    assert!(
        (moved - expected).abs() < 0.5,
        "one notch should scroll ~3 lines: moved {moved}, expected {expected}"
    );
}

/// A `RichTextEdit` added **directly** as a tree child — with no host wrapper
/// forwarding `backbuffer_cache_mut` — must still engage the cached LCD/RGBA
/// backbuffer path when painted through the framework. The widget forwards the
/// backbuffer hooks itself, so `paint_subtree` takes the backbuffered branch and
/// populates the cache pixels. This mirrors the demo host test
/// (`host_engages_editor_backbuffer_cache`) but proves an unwrapped editor needs
/// no framework precondition the host was compensating for.
#[test]
fn direct_embed_engages_backbuffer_cache() {
    // Standard density so LCD is available; either mode still populates pixels
    // (the assertion only cares that the cached path ran, not which mode).
    crate::device_scale::set_device_scale(1.0);
    crate::ux_scale::set_ux_scale(1.0);

    let doc = RichDoc::from_blocks(vec![Block {
        runs: vec![sized("hello world", 16.0)],
        ..Block::new()
    }]);
    let mut ed = RichTextEdit::new(doc, resolver()).with_font_size(16.0);

    // The widget exposes its own cache — no host forwarding involved.
    assert!(
        ed.backbuffer_cache_mut().is_some(),
        "RichTextEdit must expose its backbuffer cache directly"
    );

    ed.layout(Size::new(400.0, 300.0));

    let mut fb = crate::Framebuffer::new(400, 300);
    {
        let mut ctx = crate::GfxCtx::new(&mut fb);
        crate::widget::paint_subtree(&mut ed, &mut ctx);
    }

    let engaged = ed
        .backbuffer_cache_mut()
        .map(|c| c.pixels.is_some())
        .unwrap_or(false);
    assert!(
        engaged,
        "a directly-embedded RichTextEdit's backbuffer cache must populate after \
         a framework paint — the cached LCD/RGBA path did not engage"
    );
}