protextinator 0.5.2

Text management, made simple
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
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
use crate::math::Size;
use crate::style::{
    FontColor, FontFamily, FontSize, HorizontalTextAlignment, LineHeight, TextStyle, TextWrap,
    VerticalTextAlignment, Weight,
};
use crate::tests::mono_style_test;
use crate::{Action, Point, TextContext, TextState};
use cosmic_text::Color;

// Helper functions for creating styles with different alignments
fn mono_style_with_alignment(
    h_align: HorizontalTextAlignment,
    v_align: VerticalTextAlignment,
) -> TextStyle {
    TextStyle {
        font_size: FontSize(14.0),
        line_height: LineHeight(1.0),
        font_color: FontColor(Color::rgb(0, 0, 0)),
        horizontal_alignment: h_align,
        vertical_alignment: v_align,
        wrap: Some(TextWrap::NoWrap), // No wrapping to ensure a single line
        font_family: FontFamily::Monospace,
        weight: Weight::NORMAL,
        letter_spacing: None,
    }
}

fn mono_style_with_wrap(
    h_align: HorizontalTextAlignment,
    v_align: VerticalTextAlignment,
    wrap: Option<TextWrap>,
) -> TextStyle {
    TextStyle {
        font_size: FontSize(14.0),
        line_height: LineHeight(1.0),
        font_color: FontColor(Color::rgb(0, 0, 0)),
        horizontal_alignment: h_align,
        vertical_alignment: v_align,
        wrap,
        font_family: FontFamily::Monospace,
        weight: Weight::NORMAL,
        letter_spacing: None,
    }
}

#[test]
pub fn test_set_text() {
    let mut ctx = TextContext::default();
    let initial_text = "Hello World".to_string();

    let mut text_state = TextState::new_with_text(initial_text, &mut ctx.font_system, ());
    text_state.set_style(&mono_style_test());
    text_state.set_outer_size(&Size::new(200.0, 25.0));
    text_state.recalculate(&mut ctx);

    // Test initial text
    assert_eq!(text_state.text(), "Hello World");
    assert_eq!(text_state.text_char_len(), 11);

    // Test setting new text
    text_state.set_text("New text");
    text_state.recalculate(&mut ctx);
    assert_eq!(text_state.text(), "New text");
    assert_eq!(text_state.text_char_len(), 8);

    // Test setting empty text
    text_state.set_text("");
    text_state.recalculate(&mut ctx);
    assert_eq!(text_state.text(), "");
    assert_eq!(text_state.text_char_len(), 0);
}

#[test]
pub fn test_set_style() {
    let mut ctx = TextContext::default();
    let initial_text = "Hello World".to_string();

    let mut text_state = TextState::new_with_text(initial_text, &mut ctx.font_system, ());

    // Initial style (not used in this test but kept for documentation)
    let _initial_style = text_state.style().clone();

    // Create a new style
    let new_style = TextStyle::new(20.0, Color::rgb(255, 0, 0))
        .with_horizontal_alignment(HorizontalTextAlignment::Center)
        .with_vertical_alignment(VerticalTextAlignment::Center);

    // Set the new style
    text_state.set_style(&new_style);

    // Verify style was updated
    let updated_style = text_state.style();
    assert_eq!(updated_style.font_size.value(), 20.0);
    assert_eq!(updated_style.font_color.0, Color::rgb(255, 0, 0));
    assert!(matches!(
        updated_style.horizontal_alignment,
        HorizontalTextAlignment::Center
    ));
    assert!(matches!(
        updated_style.vertical_alignment,
        VerticalTextAlignment::Center
    ));
}

#[test]
pub fn test_set_outer_size() {
    let mut ctx = TextContext::default();
    let initial_text = "Hello World".to_string();

    let mut text_state = TextState::new_with_text(initial_text, &mut ctx.font_system, ());

    // Initial size should be zero
    assert_eq!(text_state.outer_size(), Size::ZERO);

    // Set a new size
    let new_size = Size::new(300.0, 150.0);
    text_state.set_outer_size(&new_size);

    // Verify size was updated
    assert_eq!(text_state.outer_size(), new_size);
}

#[test]
pub fn test_inner_size() {
    let mut ctx = TextContext::default();
    let initial_text = "Hello World".to_string();

    let mut text_state = TextState::new_with_text(initial_text, &mut ctx.font_system, ());
    text_state.set_style(&mono_style_test());
    text_state.set_outer_size(&Size::new(200.0, 25.0));

    // Before recalculation, inner size should be zero
    assert_eq!(text_state.inner_size(), Size::ZERO);

    // After recalculation, inner size should reflect text content
    text_state.recalculate(&mut ctx);
    let inner_size = text_state.inner_size();

    // Inner size should be non-zero after recalculation
    assert!(inner_size.x > 0.0);
    assert!(inner_size.y > 0.0);

    // Adding more text should increase inner size
    text_state.set_text("Hello World with more text to increase the size");
    text_state.recalculate(&mut ctx);
    let new_inner_size = text_state.inner_size();

    // New inner size should be larger than before
    assert!(new_inner_size.x > inner_size.x);
}

#[test]
pub fn test_buffer_metadata() {
    let mut ctx = TextContext::default();
    let initial_text = "Hello World".to_string();

    let mut text_state = TextState::new_with_text(initial_text, &mut ctx.font_system, ());

    // Default metadata should be 0
    assert_eq!(text_state.buffer_metadata(), 0);

    // Set new metadata
    text_state.set_buffer_metadata(42);

    // Verify metadata was updated
    assert_eq!(text_state.buffer_metadata(), 42);
}

#[test]
pub fn test_caret_width() {
    let mut ctx = TextContext::default();
    let initial_text = "Hello World".to_string();

    let mut text_state = TextState::new_with_text(initial_text, &mut ctx.font_system, ());

    // Default caret width should be 3.0
    assert_eq!(text_state.caret_width(), 3.0);

    // Set new caret width
    text_state.set_caret_width(5.0);

    // Verify caret width was updated
    assert_eq!(text_state.caret_width(), 5.0);
}

#[test]
pub fn test_selection_methods() {
    let mut ctx = TextContext::default();
    let initial_text = "Hello World".to_string();

    let mut text_state = TextState::new_with_text(initial_text, &mut ctx.font_system, ());
    text_state.set_style(&mono_style_test());
    text_state.set_outer_size(&Size::new(200.0, 25.0));
    text_state.is_editable = true;
    text_state.is_selectable = true;
    text_state.are_actions_enabled = true;
    text_state.recalculate(&mut ctx);

    // Initially no text should be selected
    assert!(!text_state.is_text_selected());
    assert!(text_state.selected_text().is_none());

    // Select all text
    text_state.apply_action(&mut ctx, &Action::SelectAll);

    // Verify text is selected
    assert!(text_state.is_text_selected());
    assert_eq!(text_state.selected_text(), Some("Hello World"));

    // Reset selection
    text_state.reset_selection();

    // Verify selection was reset
    assert!(!text_state.is_text_selected());
    assert!(text_state.selected_text().is_none());
}

#[test]
pub fn test_cursor_char_index() {
    let mut ctx = TextContext::default();
    let initial_text = "Hello World".to_string();

    let mut text_state = TextState::new_with_text(initial_text, &mut ctx.font_system, ());
    text_state.set_style(&mono_style_test());
    text_state.set_outer_size(&Size::new(200.0, 25.0));
    text_state.is_editable = true;
    text_state.is_editing = true;
    text_state.is_selectable = true;
    text_state.are_actions_enabled = true;
    text_state.recalculate(&mut ctx);

    // Initial cursor position should be at the beginning
    assert_eq!(text_state.cursor_char_index(), Some(0));

    // Move cursor right
    text_state.apply_action(&mut ctx, &Action::MoveCursorRight);
    assert_eq!(text_state.cursor_char_index(), Some(1));

    // Move cursor right again
    text_state.apply_action(&mut ctx, &Action::MoveCursorRight);
    assert_eq!(text_state.cursor_char_index(), Some(2));

    // Move cursor left
    text_state.apply_action(&mut ctx, &Action::MoveCursorLeft);
    assert_eq!(text_state.cursor_char_index(), Some(1));
}

#[test]
pub fn test_absolute_scroll() {
    let mut ctx = TextContext::default();
    // Create a multi-line text to test scrolling
    let initial_text = "Line 1\nLine 2\nLine 3\nLine 4\nLine 5".to_string();

    let mut text_state = TextState::new_with_text(initial_text, &mut ctx.font_system, ());
    // Use a style with vertical alignment set to None to allow vertical scrolling
    text_state.set_style(&mono_style_with_alignment(
        HorizontalTextAlignment::Left,
        VerticalTextAlignment::None,
    ));
    text_state.set_outer_size(&Size::new(200.0, 50.0)); // Small height to ensure scrolling is needed
    text_state.recalculate(&mut ctx);

    // Initial scroll should be at the top
    let initial_scroll = text_state.absolute_scroll();
    assert_eq!(initial_scroll.x, 0.0);
    assert_eq!(initial_scroll.y, 0.0);

    // Set scroll position
    let new_scroll = Point::new(0.0, 20.0);
    text_state.set_absolute_scroll(new_scroll);

    // Verify scroll position was updated
    let updated_scroll = text_state.absolute_scroll();
    assert!(updated_scroll.y > 0.0); // Should have scrolled down
}

#[test]
pub fn test_handle_press() {
    let mut ctx = TextContext::default();
    let initial_text = "Hello World".to_string();

    let mut text_state = TextState::new_with_text(initial_text, &mut ctx.font_system, ());
    text_state.set_style(&mono_style_test());
    text_state.set_outer_size(&Size::new(200.0, 25.0));
    text_state.is_editable = true;
    text_state.is_selectable = true;
    text_state.recalculate(&mut ctx);

    // Initial cursor position
    assert_eq!(text_state.cursor_char_index(), Some(0));

    // Handle press in the middle of the text
    text_state.handle_press(&mut ctx, Point::new(30.0, 10.0));

    // Cursor should have moved
    assert!(text_state.cursor_char_index().unwrap() > 0);
}

#[test]
pub fn test_horizontal_alignment() {
    let mut ctx = TextContext::default();
    let text = "Hello World".to_string();
    let container_width = 200.0;
    let container_height = 25.0;

    // Test left alignment (Start)
    let mut text_state = TextState::new_with_text(text.clone(), &mut ctx.font_system, ());
    text_state.set_style(&mono_style_with_alignment(
        HorizontalTextAlignment::Left,
        VerticalTextAlignment::Start,
    ));
    text_state.set_outer_size(&Size::new(container_width, container_height));
    text_state.recalculate(&mut ctx);

    // Get the first glyph position (should be at the left edge)
    let first_glyph_x = text_state.first_glyph().unwrap().x;
    assert!(
        first_glyph_x < 5.0,
        "Left-aligned text should start near the left edge"
    );

    // Test center alignment
    let mut text_state = TextState::new_with_text(text.clone(), &mut ctx.font_system, ());
    text_state.set_style(&mono_style_with_alignment(
        HorizontalTextAlignment::Center,
        VerticalTextAlignment::Start,
    ));
    text_state.set_outer_size(&Size::new(container_width, container_height));
    text_state.recalculate(&mut ctx);

    // Get text width and then the first glyph position
    let text_width = text_state.inner_size().x;
    let first_glyph_x = text_state.first_glyph().unwrap().x;
    let expected_x = (container_width - text_width) / 2.0;
    assert!(
        (first_glyph_x - expected_x).abs() < 5.0,
        "Center-aligned text should start near the center. Expected: {expected_x}, Actual: {first_glyph_x}"
    );

    // Test right alignment
    let mut text_state = TextState::new_with_text(text.clone(), &mut ctx.font_system, ());
    text_state.set_style(&mono_style_with_alignment(
        HorizontalTextAlignment::Right,
        VerticalTextAlignment::Start,
    ));
    text_state.set_outer_size(&Size::new(container_width, container_height));
    text_state.recalculate(&mut ctx);

    // Get text width and then the first glyph position
    let text_width = text_state.inner_size().x;
    let first_glyph_x = text_state.first_glyph().unwrap().x;
    let expected_x = container_width - text_width;
    assert!((first_glyph_x - expected_x).abs() < 5.0,
            "Right-aligned text should start near the right edge minus text width. Expected: {expected_x}, Actual: {first_glyph_x}");
}

#[test]
pub fn test_vertical_alignment() {
    let mut ctx = TextContext::default();
    let text = "Hello World".to_string();
    let container_width = 200.0;
    let container_height = 100.0; // Taller container to see vertical alignment effects

    // Test top alignment (Start)
    let mut text_state = TextState::new_with_text(text.clone(), &mut ctx.font_system, ());
    text_state.set_style(&mono_style_with_alignment(
        HorizontalTextAlignment::Left,
        VerticalTextAlignment::Start,
    ));
    text_state.set_outer_size(&Size::new(container_width, container_height));
    text_state.recalculate(&mut ctx);

    // Verify the style is set correctly
    assert!(
        matches!(
            text_state.style().vertical_alignment,
            VerticalTextAlignment::Start
        ),
        "Vertical alignment should be Start"
    );

    // Test center vertical alignment
    let mut text_state = TextState::new_with_text(text.clone(), &mut ctx.font_system, ());
    text_state.set_style(&mono_style_with_alignment(
        HorizontalTextAlignment::Left,
        VerticalTextAlignment::Center,
    ));
    text_state.set_outer_size(&Size::new(container_width, container_height));
    text_state.recalculate(&mut ctx);

    // Verify the style is set correctly
    assert!(
        matches!(
            text_state.style().vertical_alignment,
            VerticalTextAlignment::Center
        ),
        "Vertical alignment should be Center"
    );

    // Test bottom alignment (End)
    let mut text_state = TextState::new_with_text(text.clone(), &mut ctx.font_system, ());
    text_state.set_style(&mono_style_with_alignment(
        HorizontalTextAlignment::Left,
        VerticalTextAlignment::End,
    ));
    text_state.set_outer_size(&Size::new(container_width, container_height));
    text_state.recalculate(&mut ctx);

    // Verify the style is set correctly
    assert!(
        matches!(
            text_state.style().vertical_alignment,
            VerticalTextAlignment::End
        ),
        "Vertical alignment should be End"
    );
}

#[test]
pub fn test_combined_alignment() {
    let mut ctx = TextContext::default();
    let text = "Hello World".to_string();
    let container_width = 200.0;
    let container_height = 100.0;

    // Test center-center alignment
    let mut text_state = TextState::new_with_text(text.clone(), &mut ctx.font_system, ());
    text_state.set_style(&mono_style_with_alignment(
        HorizontalTextAlignment::Center,
        VerticalTextAlignment::Center,
    ));
    text_state.set_outer_size(&Size::new(container_width, container_height));
    text_state.recalculate(&mut ctx);

    // Verify the style is set correctly
    assert!(
        matches!(
            text_state.style().horizontal_alignment,
            HorizontalTextAlignment::Center
        ),
        "Horizontal alignment should be Center"
    );
    assert!(
        matches!(
            text_state.style().vertical_alignment,
            VerticalTextAlignment::Center
        ),
        "Vertical alignment should be Center"
    );

    // Get text width and first glyph position to check horizontal centering
    let text_width = text_state.inner_size().x;
    let first_glyph_x = text_state.first_glyph().unwrap().x;
    let expected_x = (container_width - text_width) / 2.0;
    assert!(
        (first_glyph_x - expected_x).abs() < 5.0,
        "Horizontally centered text should start at the right position. Expected: {expected_x}, Actual: {first_glyph_x}"
    );

    // Test right-bottom alignment
    let mut text_state = TextState::new_with_text(text.clone(), &mut ctx.font_system, ());
    text_state.set_style(&mono_style_with_alignment(
        HorizontalTextAlignment::Right,
        VerticalTextAlignment::End,
    ));
    text_state.set_outer_size(&Size::new(container_width, container_height));
    text_state.recalculate(&mut ctx);

    // Verify the style is set correctly
    assert!(
        matches!(
            text_state.style().horizontal_alignment,
            HorizontalTextAlignment::Right
        ),
        "Horizontal alignment should be Right"
    );
    assert!(
        matches!(
            text_state.style().vertical_alignment,
            VerticalTextAlignment::End
        ),
        "Vertical alignment should be End"
    );

    // Get text width and first glyph position to check right alignment
    let text_width = text_state.inner_size().x;
    let first_glyph_x = text_state.first_glyph().unwrap().x;
    let expected_x = container_width - text_width;
    assert!(
        (first_glyph_x - expected_x).abs() < 5.0,
        "Right-aligned text should start at the right position. Expected: {expected_x}, Actual: {first_glyph_x}"
    );
}

#[test]
pub fn test_horizontal_overflow() {
    let mut ctx = TextContext::default();
    let long_text =
        "This is a very long text that should overflow the container horizontally".to_string();
    let container_width = 100.0; // Small width to ensure overflow
    let container_height = 50.0;

    // Test with no wrapping (should overflow)
    let mut text_state = TextState::new_with_text(long_text.clone(), &mut ctx.font_system, ());
    text_state.set_style(&mono_style_with_wrap(
        HorizontalTextAlignment::Left,
        VerticalTextAlignment::Start,
        Some(TextWrap::NoWrap), // No wrapping
    ));
    text_state.set_outer_size(&Size::new(container_width, container_height));
    text_state.recalculate(&mut ctx);

    // Check that text width exceeds container width
    let text_width = text_state.inner_size().x;
    assert!(
        text_width > container_width,
        "Text should overflow horizontally. Text width: {text_width}, Container width: {container_width}"
    );

    // Test with wrapping (should not overflow horizontally)
    let mut text_state = TextState::new_with_text(long_text.clone(), &mut ctx.font_system, ());
    text_state.set_style(&mono_style_with_wrap(
        HorizontalTextAlignment::Left,
        VerticalTextAlignment::Start,
        Some(TextWrap::Wrap), // With wrapping
    ));
    text_state.set_outer_size(&Size::new(container_width, container_height));
    text_state.recalculate(&mut ctx);

    // Check that text width is close to container width (allow small margin of error)
    let text_width = text_state.inner_size().x;
    assert!(text_width <= container_width + 2.0,
            "Text should not overflow horizontally with wrapping by much. Text width: {text_width}, Container width: {container_width}");
}

#[test]
pub fn test_vertical_overflow() {
    let mut ctx = TextContext::default();
    let multi_line_text =
        "Line 1\nLine 2\nLine 3\nLine 4\nLine 5\nLine 6\nLine 7\nLine 8\nLine 9\nLine 10"
            .to_string();
    let container_width = 200.0;
    let container_height = 50.0; // Small height to ensure overflow

    // Test vertical overflow
    let mut text_state =
        TextState::new_with_text(multi_line_text.clone(), &mut ctx.font_system, ());
    text_state.set_style(&mono_style_test());
    text_state.set_outer_size(&Size::new(container_width, container_height));
    text_state.recalculate(&mut ctx);

    // Check that text height exceeds container height
    let text_height = text_state.inner_size().y;
    assert!(
        text_height > container_height,
        "Text should overflow vertically. Text height: {text_height}, Container height: {container_height}"
    );
}

#[test]
pub fn test_both_overflow() {
    let mut ctx = TextContext::default();
    // Create text that will overflow both horizontally and vertically
    let long_multi_line_text =
        "This is a very long line that should overflow horizontally\n".repeat(10);
    let container_width = 100.0; // Small width to ensure horizontal overflow
    let container_height = 50.0; // Small height to ensure vertical overflow

    // Test both horizontal and vertical overflow
    let mut text_state =
        TextState::new_with_text(long_multi_line_text.clone(), &mut ctx.font_system, ());
    text_state.set_style(&mono_style_with_wrap(
        HorizontalTextAlignment::Left,
        VerticalTextAlignment::Start,
        Some(TextWrap::NoWrap), // No wrapping to ensure horizontal overflow
    ));
    text_state.set_outer_size(&Size::new(container_width, container_height));
    text_state.recalculate(&mut ctx);

    // Check that text dimensions exceed container dimensions
    let text_size = text_state.inner_size();
    assert!(
        text_size.x > container_width,
        "Text should overflow horizontally. Text width: {}, Container width: {}",
        text_size.x,
        container_width
    );
    assert!(
        text_size.y > container_height,
        "Text should overflow vertically. Text height: {}, Container height: {}",
        text_size.y,
        container_height
    );
}

#[test]
pub fn test_vertical_scroll_with_alignment() {
    let mut ctx = TextContext::default();
    // Create a multi-line text to test scrolling
    let initial_text = "Line 1\nLine 2\nLine 3\nLine 4\nLine 5".to_string();
    let container_width = 200.0;
    let container_height = 50.0; // Small height to ensure scrolling is needed

    // Test with vertical alignment None (should allow scrolling)
    let mut text_state = TextState::new_with_text(initial_text.clone(), &mut ctx.font_system, ());
    text_state.set_style(&mono_style_with_alignment(
        HorizontalTextAlignment::Left,
        VerticalTextAlignment::None,
    ));
    text_state.set_outer_size(&Size::new(container_width, container_height));
    text_state.recalculate(&mut ctx);

    // Initial scroll should be at the top
    let initial_scroll = text_state.absolute_scroll();
    assert_eq!(initial_scroll.y, 0.0);

    // Set vertical scroll position
    let new_scroll = Point::new(0.0, 20.0);
    text_state.set_absolute_scroll(new_scroll);

    // Verify scroll position was updated (should work with VerticalTextAlignment::None)
    let updated_scroll = text_state.absolute_scroll();
    assert!(
        updated_scroll.y > 0.0,
        "Vertical scrolling should work with VerticalTextAlignment::None"
    );

    // Test with vertical alignment Start (should not allow scrolling)
    let mut text_state = TextState::new_with_text(initial_text.clone(), &mut ctx.font_system, ());
    text_state.set_style(&mono_style_with_alignment(
        HorizontalTextAlignment::Left,
        VerticalTextAlignment::Start,
    ));
    text_state.set_outer_size(&Size::new(container_width, container_height));
    text_state.recalculate(&mut ctx);

    // Get the initial scroll position
    // Note: We don't assert that it's 0.0 because absolute_scroll() might return
    // non-zero values even after setting scroll to 0.0 due to internal calculations
    text_state.set_absolute_scroll(Point::new(0.0, 0.0));
    let initial_scroll = text_state.absolute_scroll();

    // Try to set vertical scroll position
    let new_scroll = Point::new(0.0, 20.0);
    text_state.set_absolute_scroll(new_scroll);

    // Verify scroll position was not updated (should not work with VerticalTextAlignment::Start)
    let updated_scroll = text_state.absolute_scroll();
    assert_eq!(
        updated_scroll.y, initial_scroll.y,
        "Vertical scrolling should not work with VerticalTextAlignment::Start"
    );

    // Test with vertical alignment Center (should not allow scrolling)
    let mut text_state = TextState::new_with_text(initial_text.clone(), &mut ctx.font_system, ());
    text_state.set_style(&mono_style_with_alignment(
        HorizontalTextAlignment::Left,
        VerticalTextAlignment::Center,
    ));
    text_state.set_outer_size(&Size::new(container_width, container_height));
    text_state.recalculate(&mut ctx);

    // Get the initial scroll position
    // Note: We don't assert that it's 0.0 because absolute_scroll() might return
    // non-zero values even after setting scroll to 0.0 due to internal calculations
    text_state.set_absolute_scroll(Point::new(0.0, 0.0));
    let initial_scroll = text_state.absolute_scroll();

    // Try to set vertical scroll position
    text_state.set_absolute_scroll(new_scroll);

    // Verify scroll position was not updated (should not work with VerticalTextAlignment::Center)
    let updated_scroll = text_state.absolute_scroll();
    assert_eq!(
        updated_scroll.y, initial_scroll.y,
        "Vertical scrolling should not work with VerticalTextAlignment::Center"
    );

    // Test with vertical alignment End (should not allow scrolling)
    let mut text_state = TextState::new_with_text(initial_text.clone(), &mut ctx.font_system, ());
    text_state.set_style(&mono_style_with_alignment(
        HorizontalTextAlignment::Left,
        VerticalTextAlignment::End,
    ));
    text_state.set_outer_size(&Size::new(container_width, container_height));
    text_state.recalculate(&mut ctx);

    // Get the initial scroll position
    // Note: We don't assert that it's 0.0 because absolute_scroll() might return
    // non-zero values even after setting scroll to 0.0 due to internal calculations
    text_state.set_absolute_scroll(Point::new(0.0, 0.0));
    let initial_scroll = text_state.absolute_scroll();

    // Try to set vertical scroll position
    text_state.set_absolute_scroll(new_scroll);

    // Verify scroll position was not updated (should not work with VerticalTextAlignment::End)
    let updated_scroll = text_state.absolute_scroll();
    assert_eq!(
        updated_scroll.y, initial_scroll.y,
        "Vertical scrolling should not work with VerticalTextAlignment::End"
    );
}

#[test]
pub fn test_combined_scroll_with_alignment() {
    let mut ctx = TextContext::default();
    // Create a long multi-line text to test both scrolling directions
    let initial_text = "This is a very long text that should overflow horizontally\nLine 2\nLine 3\nLine 4\nLine 5".to_string();
    let container_width = 100.0; // Small width to ensure horizontal scrolling is needed
    let container_height = 50.0; // Small height to ensure vertical scrolling is needed

    // Test with both alignments set to None (should allow both scrolling directions)
    let mut text_state = TextState::new_with_text(initial_text.clone(), &mut ctx.font_system, ());
    text_state.set_style(&mono_style_with_alignment(
        HorizontalTextAlignment::None,
        VerticalTextAlignment::None,
    ));
    text_state.set_outer_size(&Size::new(container_width, container_height));
    text_state.recalculate(&mut ctx);

    // Initial scroll should be at the top-left
    let initial_scroll = text_state.absolute_scroll();
    assert_eq!(initial_scroll.x, 0.0);
    assert_eq!(initial_scroll.y, 0.0);

    // Set both scroll positions
    let new_scroll = Point::new(20.0, 30.0);
    text_state.set_absolute_scroll(new_scroll);

    // Verify both scroll positions were updated
    let updated_scroll = text_state.absolute_scroll();
    assert!(
        updated_scroll.x > 0.0,
        "Horizontal scrolling should work with HorizontalTextAlignment::None"
    );
    assert!(
        updated_scroll.y > 0.0,
        "Vertical scrolling should work with VerticalTextAlignment::None"
    );

    // Test with horizontal alignment None but vertical alignment Start (should only allow horizontal scrolling)
    let mut text_state = TextState::new_with_text(initial_text.clone(), &mut ctx.font_system, ());
    text_state.set_style(&mono_style_with_alignment(
        HorizontalTextAlignment::None,
        VerticalTextAlignment::Start,
    ));
    text_state.set_outer_size(&Size::new(container_width, container_height));
    text_state.recalculate(&mut ctx);

    // Set both scroll positions
    text_state.set_absolute_scroll(new_scroll);

    // Verify only horizontal scroll was updated
    let updated_scroll = text_state.absolute_scroll();
    assert!(
        updated_scroll.x > 0.0,
        "Horizontal scrolling should work with HorizontalTextAlignment::None"
    );
    assert_eq!(
        updated_scroll.y, 0.0,
        "Vertical scrolling should not work with VerticalTextAlignment::Start"
    );
}

#[test]
pub fn test_scale_factor_consistency() {
    let mut ctx = TextContext::default();
    let text = "Line 1\nLine 2\nLine 3".to_string();

    // Start with vertical centering to introduce a non-zero vertical alignment offset
    let mut text_state = TextState::new_with_text(text.clone(), &mut ctx.font_system, ());
    text_state.set_style(&mono_style_with_alignment(
        HorizontalTextAlignment::Left,
        VerticalTextAlignment::Center,
    ));
    text_state.set_outer_size(&Size::new(200.0, 100.0));

    // Base scale 1.0
    text_state.set_scale_factor(1.0);
    text_state.recalculate(&mut ctx);
    let scroll1 = text_state.absolute_scroll(); // logical

    // Increase scale factor; logical scroll should remain the same
    text_state.set_scale_factor(2.0);
    text_state.recalculate(&mut ctx);
    let scroll2 = text_state.absolute_scroll(); // logical

    assert!(scroll1.approx_eq(&scroll2, 0.75), "Absolute scroll should be stable in logical pixels when scale changes. Before: {:?}, After: {:?}", scroll1, scroll2);

    // Hit-test should also be stable in logical coords
    text_state.is_selectable = true;
    text_state.is_editable = true;
    text_state.are_actions_enabled = true;
    // Click near the start of the first visible line
    text_state.handle_press(&mut ctx, Point::new(2.0, 5.0));
    let cursor_idx_scale2 = text_state.cursor_char_index().unwrap_or(0);

    // Switch back to scale 1.0 and click the same logical position
    text_state.set_scale_factor(1.0);
    text_state.recalculate(&mut ctx);
    text_state.handle_press(&mut ctx, Point::new(2.0, 5.0));
    let cursor_idx_scale1 = text_state.cursor_char_index().unwrap_or(0);

    assert_eq!(
        cursor_idx_scale1, cursor_idx_scale2,
        "Hit-test should map the same logical coords to the same character across scales"
    );

    // Selection bounds are exposed in logical px; they should be comparable across scales
    // Use the public action API to select all
    text_state.apply_action(&mut ctx, &Action::SelectAll);
    let lines_scale1 = text_state.selection().lines().to_vec();

    text_state.set_scale_factor(2.0);
    text_state.recalculate(&mut ctx);
    let lines_scale2 = text_state.selection().lines().to_vec();

    // Compare first selection line heights (allow small epsilon due to layout/rounding)
    if let (Some(l1), Some(l2)) = (lines_scale1.first(), lines_scale2.first()) {
        let h1 = (l1.end_y_pt.unwrap_or(0.0) - l1.start_y_pt.unwrap_or(0.0)).abs();
        let h2 = (l2.end_y_pt.unwrap_or(0.0) - l2.start_y_pt.unwrap_or(0.0)).abs();
        assert!(
            (h1 - h2).abs() < 1.0,
            "Selection line height should be stable in logical px across scales: h1={} h2={}",
            h1,
            h2
        );
    }
}