presentar-terminal 0.3.5

Terminal backend for Presentar UI framework with zero-allocation rendering
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
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
//! Scrollbar widget with position indicator and arrow buttons.
//!
//! Provides vertical and horizontal scrollbars for scrollable content.
//! Based on btop scrollbar patterns.

use presentar_core::{
    Brick, BrickAssertion, BrickBudget, BrickVerification, Canvas, Color, Constraints, Event,
    LayoutResult, Point, Rect, Size, TextStyle, TypeId, Widget,
};
use std::any::Any;
use std::time::Duration;

/// Scrollbar orientation.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum ScrollOrientation {
    /// Vertical scrollbar (default).
    #[default]
    Vertical,
    /// Horizontal scrollbar.
    Horizontal,
}

/// Characters used for scrollbar rendering.
#[derive(Debug, Clone)]
pub struct ScrollbarChars {
    /// Track/background character.
    pub track: char,
    /// Thumb/position indicator.
    pub thumb: char,
    /// Up arrow (vertical) or left arrow (horizontal).
    pub arrow_start: char,
    /// Down arrow (vertical) or right arrow (horizontal).
    pub arrow_end: char,
}

impl Default for ScrollbarChars {
    fn default() -> Self {
        Self::unicode()
    }
}

impl ScrollbarChars {
    /// Unicode box-drawing characters.
    #[must_use]
    pub fn unicode() -> Self {
        Self {
            track: '',
            thumb: '',
            arrow_start: '',
            arrow_end: '',
        }
    }

    /// Unicode horizontal variant.
    #[must_use]
    pub fn unicode_horizontal() -> Self {
        Self {
            track: '',
            thumb: '',
            arrow_start: '',
            arrow_end: '',
        }
    }

    /// ASCII-only characters.
    #[must_use]
    pub fn ascii() -> Self {
        Self {
            track: '-',
            thumb: '#',
            arrow_start: '^',
            arrow_end: 'v',
        }
    }

    /// Minimal style (no arrows).
    #[must_use]
    pub fn minimal() -> Self {
        Self {
            track: '',
            thumb: '',
            arrow_start: '',
            arrow_end: '',
        }
    }
}

/// Scrollbar widget with position indicator and optional arrow buttons.
#[derive(Debug, Clone)]
pub struct Scrollbar {
    /// Scrollbar orientation.
    orientation: ScrollOrientation,
    /// Total content length (items or lines).
    content_length: usize,
    /// Visible viewport length.
    viewport_length: usize,
    /// Current scroll offset (0-based).
    offset: usize,
    /// Show arrow buttons at ends.
    show_arrows: bool,
    /// Characters for rendering.
    chars: ScrollbarChars,
    /// Track color.
    track_color: Color,
    /// Thumb color.
    thumb_color: Color,
    /// Arrow color.
    arrow_color: Color,
    /// Cached bounds.
    bounds: Rect,
}

impl Default for Scrollbar {
    fn default() -> Self {
        Self::vertical(100, 10)
    }
}

impl Scrollbar {
    /// Create a vertical scrollbar.
    #[must_use]
    pub fn vertical(content_length: usize, viewport_length: usize) -> Self {
        Self {
            orientation: ScrollOrientation::Vertical,
            content_length,
            viewport_length,
            offset: 0,
            show_arrows: true,
            chars: ScrollbarChars::unicode(),
            track_color: Color::new(0.3, 0.3, 0.3, 1.0),
            thumb_color: Color::new(0.7, 0.7, 0.7, 1.0),
            arrow_color: Color::new(0.5, 0.5, 0.5, 1.0),
            bounds: Rect::default(),
        }
    }

    /// Create a horizontal scrollbar.
    #[must_use]
    pub fn horizontal(content_length: usize, viewport_length: usize) -> Self {
        Self {
            orientation: ScrollOrientation::Horizontal,
            content_length,
            viewport_length,
            offset: 0,
            show_arrows: true,
            chars: ScrollbarChars::unicode_horizontal(),
            track_color: Color::new(0.3, 0.3, 0.3, 1.0),
            thumb_color: Color::new(0.7, 0.7, 0.7, 1.0),
            arrow_color: Color::new(0.5, 0.5, 0.5, 1.0),
            bounds: Rect::default(),
        }
    }

    /// Set whether to show arrow buttons.
    #[must_use]
    pub fn with_arrows(mut self, show: bool) -> Self {
        self.show_arrows = show;
        self
    }

    /// Set custom characters.
    #[must_use]
    pub fn with_chars(mut self, chars: ScrollbarChars) -> Self {
        self.chars = chars;
        self
    }

    /// Set track color.
    #[must_use]
    pub fn with_track_color(mut self, color: Color) -> Self {
        self.track_color = color;
        self
    }

    /// Set thumb color.
    #[must_use]
    pub fn with_thumb_color(mut self, color: Color) -> Self {
        self.thumb_color = color;
        self
    }

    /// Set arrow color.
    #[must_use]
    pub fn with_arrow_color(mut self, color: Color) -> Self {
        self.arrow_color = color;
        self
    }

    /// Get current offset.
    #[must_use]
    pub fn offset(&self) -> usize {
        self.offset
    }

    /// Set scroll offset.
    pub fn set_offset(&mut self, offset: usize) {
        self.offset = offset.min(self.max_offset());
    }

    /// Get scroll position as fraction (0.0-1.0).
    #[must_use]
    pub fn position(&self) -> f64 {
        let max = self.max_offset();
        if max == 0 {
            0.0
        } else {
            self.offset as f64 / max as f64
        }
    }

    /// Get thumb size as fraction of viewport (0.0-1.0).
    #[must_use]
    pub fn thumb_size(&self) -> f64 {
        if self.content_length == 0 {
            1.0
        } else {
            (self.viewport_length as f64 / self.content_length as f64).min(1.0)
        }
    }

    /// Get maximum scroll offset.
    #[must_use]
    pub fn max_offset(&self) -> usize {
        self.content_length.saturating_sub(self.viewport_length)
    }

    /// Check if content is scrollable.
    #[must_use]
    pub fn is_scrollable(&self) -> bool {
        self.content_length > self.viewport_length
    }

    /// Scroll by delta (positive = down/right, negative = up/left).
    pub fn scroll(&mut self, delta: i32) {
        if delta >= 0 {
            self.offset = (self.offset + delta as usize).min(self.max_offset());
        } else {
            self.offset = self.offset.saturating_sub((-delta) as usize);
        }
    }

    /// Scroll up/left by one unit.
    pub fn scroll_start(&mut self) {
        self.scroll(-1);
    }

    /// Scroll down/right by one unit.
    pub fn scroll_end(&mut self) {
        self.scroll(1);
    }

    /// Page up/left.
    pub fn page_start(&mut self) {
        let page = self.viewport_length.max(1);
        self.offset = self.offset.saturating_sub(page);
    }

    /// Page down/right.
    pub fn page_end(&mut self) {
        let page = self.viewport_length.max(1);
        self.offset = (self.offset + page).min(self.max_offset());
    }

    /// Jump to position (0.0-1.0).
    pub fn jump_to(&mut self, position: f64) {
        let pos = position.clamp(0.0, 1.0);
        self.offset = (pos * self.max_offset() as f64).round() as usize;
    }

    /// Jump to top/left.
    pub fn jump_start(&mut self) {
        self.offset = 0;
    }

    /// Jump to bottom/right.
    pub fn jump_end(&mut self) {
        self.offset = self.max_offset();
    }

    /// Update content and viewport lengths.
    pub fn update_lengths(&mut self, content_length: usize, viewport_length: usize) {
        self.content_length = content_length;
        self.viewport_length = viewport_length;
        self.offset = self.offset.min(self.max_offset());
    }

    /// Get the orientation.
    #[must_use]
    pub fn orientation(&self) -> ScrollOrientation {
        self.orientation
    }

    /// Get content length.
    #[must_use]
    pub fn content_length(&self) -> usize {
        self.content_length
    }

    /// Get viewport length.
    #[must_use]
    pub fn viewport_length(&self) -> usize {
        self.viewport_length
    }
}

impl Brick for Scrollbar {
    fn brick_name(&self) -> &'static str {
        "scrollbar"
    }

    fn assertions(&self) -> &[BrickAssertion] {
        static ASSERTIONS: &[BrickAssertion] = &[BrickAssertion::max_latency_ms(16)];
        ASSERTIONS
    }

    fn budget(&self) -> BrickBudget {
        BrickBudget::uniform(8)
    }

    fn verify(&self) -> BrickVerification {
        BrickVerification {
            passed: self.assertions().to_vec(),
            failed: vec![],
            verification_time: Duration::from_micros(5),
        }
    }

    fn to_html(&self) -> String {
        String::new()
    }

    fn to_css(&self) -> String {
        String::new()
    }
}

impl Widget for Scrollbar {
    fn type_id(&self) -> TypeId {
        TypeId::of::<Self>()
    }

    fn measure(&self, constraints: Constraints) -> Size {
        match self.orientation {
            ScrollOrientation::Vertical => Size::new(1.0, constraints.max_height.clamp(3.0, 20.0)),
            ScrollOrientation::Horizontal => Size::new(constraints.max_width.clamp(3.0, 20.0), 1.0),
        }
    }

    fn layout(&mut self, bounds: Rect) -> LayoutResult {
        self.bounds = bounds;
        LayoutResult {
            size: Size::new(bounds.width, bounds.height),
        }
    }

    fn paint(&self, canvas: &mut dyn Canvas) {
        let track_style = TextStyle {
            color: self.track_color,
            ..Default::default()
        };
        let thumb_style = TextStyle {
            color: self.thumb_color,
            ..Default::default()
        };
        let arrow_style = TextStyle {
            color: self.arrow_color,
            ..Default::default()
        };

        match self.orientation {
            ScrollOrientation::Vertical => {
                self.paint_vertical(canvas, &track_style, &thumb_style, &arrow_style);
            }
            ScrollOrientation::Horizontal => {
                self.paint_horizontal(canvas, &track_style, &thumb_style, &arrow_style);
            }
        }
    }

    fn event(&mut self, _event: &Event) -> Option<Box<dyn Any + Send>> {
        None
    }

    fn children(&self) -> &[Box<dyn Widget>] {
        &[]
    }

    fn children_mut(&mut self) -> &mut [Box<dyn Widget>] {
        &mut []
    }
}

impl Scrollbar {
    fn paint_vertical(
        &self,
        canvas: &mut dyn Canvas,
        track_style: &TextStyle,
        thumb_style: &TextStyle,
        arrow_style: &TextStyle,
    ) {
        let height = self.bounds.height as usize;
        if height < 3 {
            return;
        }

        let arrow_offset = usize::from(self.show_arrows);
        let track_start = arrow_offset;
        let track_end = height.saturating_sub(arrow_offset);
        let track_len = track_end.saturating_sub(track_start);

        if track_len == 0 {
            return;
        }

        // Draw arrows
        if self.show_arrows {
            canvas.draw_text(
                &self.chars.arrow_start.to_string(),
                Point::new(self.bounds.x, self.bounds.y),
                arrow_style,
            );
            canvas.draw_text(
                &self.chars.arrow_end.to_string(),
                Point::new(self.bounds.x, self.bounds.y + (height - 1) as f32),
                arrow_style,
            );
        }

        // Calculate thumb position and size
        let thumb_size = ((self.thumb_size() * track_len as f64).round() as usize).max(1);
        let thumb_pos = if self.is_scrollable() {
            (self.position() * (track_len.saturating_sub(thumb_size)) as f64).round() as usize
        } else {
            0
        };

        // Draw track and thumb
        for i in 0..track_len {
            let y = track_start + i;
            let in_thumb = i >= thumb_pos && i < thumb_pos + thumb_size;
            let ch = if in_thumb {
                self.chars.thumb
            } else {
                self.chars.track
            };
            let style = if in_thumb { thumb_style } else { track_style };
            canvas.draw_text(
                &ch.to_string(),
                Point::new(self.bounds.x, self.bounds.y + y as f32),
                style,
            );
        }
    }

    fn paint_horizontal(
        &self,
        canvas: &mut dyn Canvas,
        track_style: &TextStyle,
        thumb_style: &TextStyle,
        arrow_style: &TextStyle,
    ) {
        let width = self.bounds.width as usize;
        if width < 3 {
            return;
        }

        let arrow_offset = usize::from(self.show_arrows);
        let track_start = arrow_offset;
        let track_end = width.saturating_sub(arrow_offset);
        let track_len = track_end.saturating_sub(track_start);

        if track_len == 0 {
            return;
        }

        // Draw arrows
        if self.show_arrows {
            canvas.draw_text(
                &self.chars.arrow_start.to_string(),
                Point::new(self.bounds.x, self.bounds.y),
                arrow_style,
            );
            canvas.draw_text(
                &self.chars.arrow_end.to_string(),
                Point::new(self.bounds.x + (width - 1) as f32, self.bounds.y),
                arrow_style,
            );
        }

        // Calculate thumb position and size
        let thumb_size = ((self.thumb_size() * track_len as f64).round() as usize).max(1);
        let thumb_pos = if self.is_scrollable() {
            (self.position() * (track_len.saturating_sub(thumb_size)) as f64).round() as usize
        } else {
            0
        };

        // Draw track and thumb
        for i in 0..track_len {
            let x = track_start + i;
            let in_thumb = i >= thumb_pos && i < thumb_pos + thumb_size;
            let ch = if in_thumb {
                self.chars.thumb
            } else {
                self.chars.track
            };
            let style = if in_thumb { thumb_style } else { track_style };
            canvas.draw_text(
                &ch.to_string(),
                Point::new(self.bounds.x + x as f32, self.bounds.y),
                style,
            );
        }
    }
}

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

    struct MockCanvas {
        texts: Vec<(String, Point)>,
    }

    impl MockCanvas {
        fn new() -> Self {
            Self { texts: vec![] }
        }
    }

    impl Canvas for MockCanvas {
        fn fill_rect(&mut self, _rect: Rect, _color: Color) {}
        fn stroke_rect(&mut self, _rect: Rect, _color: Color, _width: f32) {}
        fn draw_text(&mut self, text: &str, position: Point, _style: &TextStyle) {
            self.texts.push((text.to_string(), position));
        }
        fn draw_line(&mut self, _from: Point, _to: Point, _color: Color, _width: f32) {}
        fn fill_circle(&mut self, _center: Point, _radius: f32, _color: Color) {}
        fn stroke_circle(&mut self, _center: Point, _radius: f32, _color: Color, _width: f32) {}
        fn fill_arc(&mut self, _c: Point, _r: f32, _s: f32, _e: f32, _color: Color) {}
        fn draw_path(&mut self, _points: &[Point], _color: Color, _width: f32) {}
        fn fill_polygon(&mut self, _points: &[Point], _color: Color) {}
        fn push_clip(&mut self, _rect: Rect) {}
        fn pop_clip(&mut self) {}
        fn push_transform(&mut self, _transform: presentar_core::Transform2D) {}
        fn pop_transform(&mut self) {}
    }

    // =====================================================
    // Construction Tests
    // =====================================================

    #[test]
    fn test_vertical_scrollbar_creation() {
        let sb = Scrollbar::vertical(100, 10);
        assert_eq!(sb.orientation(), ScrollOrientation::Vertical);
        assert_eq!(sb.content_length(), 100);
        assert_eq!(sb.viewport_length(), 10);
    }

    #[test]
    fn test_horizontal_scrollbar_creation() {
        let sb = Scrollbar::horizontal(100, 20);
        assert_eq!(sb.orientation(), ScrollOrientation::Horizontal);
        assert_eq!(sb.content_length(), 100);
        assert_eq!(sb.viewport_length(), 20);
    }

    #[test]
    fn test_scrollbar_default() {
        let sb = Scrollbar::default();
        assert_eq!(sb.orientation(), ScrollOrientation::Vertical);
        assert_eq!(sb.offset(), 0);
    }

    // =====================================================
    // Builder Pattern Tests
    // =====================================================

    #[test]
    fn test_with_arrows() {
        let sb = Scrollbar::vertical(100, 10).with_arrows(false);
        assert!(!sb.show_arrows);
    }

    #[test]
    fn test_with_chars() {
        let chars = ScrollbarChars::ascii();
        let sb = Scrollbar::vertical(100, 10).with_chars(chars.clone());
        assert_eq!(sb.chars.track, '-');
    }

    #[test]
    fn test_with_track_color() {
        let sb = Scrollbar::vertical(100, 10).with_track_color(Color::RED);
        assert_eq!(sb.track_color, Color::RED);
    }

    #[test]
    fn test_with_thumb_color() {
        let sb = Scrollbar::vertical(100, 10).with_thumb_color(Color::GREEN);
        assert_eq!(sb.thumb_color, Color::GREEN);
    }

    #[test]
    fn test_with_arrow_color() {
        let sb = Scrollbar::vertical(100, 10).with_arrow_color(Color::BLUE);
        assert_eq!(sb.arrow_color, Color::BLUE);
    }

    // =====================================================
    // ScrollbarChars Tests
    // =====================================================

    #[test]
    fn test_chars_unicode() {
        let chars = ScrollbarChars::unicode();
        assert_eq!(chars.track, '');
        assert_eq!(chars.thumb, '');
        assert_eq!(chars.arrow_start, '');
        assert_eq!(chars.arrow_end, '');
    }

    #[test]
    fn test_chars_unicode_horizontal() {
        let chars = ScrollbarChars::unicode_horizontal();
        assert_eq!(chars.arrow_start, '');
        assert_eq!(chars.arrow_end, '');
    }

    #[test]
    fn test_chars_ascii() {
        let chars = ScrollbarChars::ascii();
        assert_eq!(chars.track, '-');
        assert_eq!(chars.thumb, '#');
    }

    #[test]
    fn test_chars_minimal() {
        let chars = ScrollbarChars::minimal();
        assert_eq!(chars.track, '');
        assert_eq!(chars.thumb, '');
    }

    #[test]
    fn test_chars_default() {
        let chars = ScrollbarChars::default();
        assert_eq!(chars, ScrollbarChars::unicode());
    }

    // =====================================================
    // Offset/Position Tests
    // =====================================================

    #[test]
    fn test_offset_initial() {
        let sb = Scrollbar::vertical(100, 10);
        assert_eq!(sb.offset(), 0);
    }

    #[test]
    fn test_set_offset() {
        let mut sb = Scrollbar::vertical(100, 10);
        sb.set_offset(50);
        assert_eq!(sb.offset(), 50);
    }

    #[test]
    fn test_set_offset_clamps() {
        let mut sb = Scrollbar::vertical(100, 10);
        sb.set_offset(1000);
        assert_eq!(sb.offset(), 90); // max_offset = 100 - 10
    }

    #[test]
    fn test_position_zero() {
        let sb = Scrollbar::vertical(100, 10);
        assert!((sb.position() - 0.0).abs() < f64::EPSILON);
    }

    #[test]
    fn test_position_mid() {
        let mut sb = Scrollbar::vertical(100, 10);
        sb.set_offset(45);
        assert!((sb.position() - 0.5).abs() < 0.01);
    }

    #[test]
    fn test_position_end() {
        let mut sb = Scrollbar::vertical(100, 10);
        sb.set_offset(90);
        assert!((sb.position() - 1.0).abs() < f64::EPSILON);
    }

    #[test]
    fn test_position_no_scroll() {
        let sb = Scrollbar::vertical(10, 20);
        assert!((sb.position() - 0.0).abs() < f64::EPSILON);
    }

    // =====================================================
    // Thumb Size Tests
    // =====================================================

    #[test]
    fn test_thumb_size_small_viewport() {
        let sb = Scrollbar::vertical(100, 10);
        assert!((sb.thumb_size() - 0.1).abs() < f64::EPSILON);
    }

    #[test]
    fn test_thumb_size_large_viewport() {
        let sb = Scrollbar::vertical(100, 50);
        assert!((sb.thumb_size() - 0.5).abs() < f64::EPSILON);
    }

    #[test]
    fn test_thumb_size_viewport_exceeds() {
        let sb = Scrollbar::vertical(50, 100);
        assert!((sb.thumb_size() - 1.0).abs() < f64::EPSILON);
    }

    #[test]
    fn test_thumb_size_empty_content() {
        let sb = Scrollbar::vertical(0, 10);
        assert!((sb.thumb_size() - 1.0).abs() < f64::EPSILON);
    }

    // =====================================================
    // Max Offset Tests
    // =====================================================

    #[test]
    fn test_max_offset() {
        let sb = Scrollbar::vertical(100, 10);
        assert_eq!(sb.max_offset(), 90);
    }

    #[test]
    fn test_max_offset_no_scroll() {
        let sb = Scrollbar::vertical(10, 20);
        assert_eq!(sb.max_offset(), 0);
    }

    #[test]
    fn test_is_scrollable_true() {
        let sb = Scrollbar::vertical(100, 10);
        assert!(sb.is_scrollable());
    }

    #[test]
    fn test_is_scrollable_false() {
        let sb = Scrollbar::vertical(10, 20);
        assert!(!sb.is_scrollable());
    }

    // =====================================================
    // Scroll Methods Tests
    // =====================================================

    #[test]
    fn test_scroll_positive() {
        let mut sb = Scrollbar::vertical(100, 10);
        sb.scroll(5);
        assert_eq!(sb.offset(), 5);
    }

    #[test]
    fn test_scroll_negative() {
        let mut sb = Scrollbar::vertical(100, 10);
        sb.set_offset(10);
        sb.scroll(-3);
        assert_eq!(sb.offset(), 7);
    }

    #[test]
    fn test_scroll_clamps_max() {
        let mut sb = Scrollbar::vertical(100, 10);
        sb.scroll(1000);
        assert_eq!(sb.offset(), 90);
    }

    #[test]
    fn test_scroll_clamps_min() {
        let mut sb = Scrollbar::vertical(100, 10);
        sb.scroll(-100);
        assert_eq!(sb.offset(), 0);
    }

    #[test]
    fn test_scroll_start() {
        let mut sb = Scrollbar::vertical(100, 10);
        sb.set_offset(10);
        sb.scroll_start();
        assert_eq!(sb.offset(), 9);
    }

    #[test]
    fn test_scroll_end() {
        let mut sb = Scrollbar::vertical(100, 10);
        sb.scroll_end();
        assert_eq!(sb.offset(), 1);
    }

    #[test]
    fn test_page_start() {
        let mut sb = Scrollbar::vertical(100, 10);
        sb.set_offset(50);
        sb.page_start();
        assert_eq!(sb.offset(), 40);
    }

    #[test]
    fn test_page_end() {
        let mut sb = Scrollbar::vertical(100, 10);
        sb.page_end();
        assert_eq!(sb.offset(), 10);
    }

    // =====================================================
    // Jump Methods Tests
    // =====================================================

    #[test]
    fn test_jump_to_mid() {
        let mut sb = Scrollbar::vertical(100, 10);
        sb.jump_to(0.5);
        assert_eq!(sb.offset(), 45);
    }

    #[test]
    fn test_jump_to_start() {
        let mut sb = Scrollbar::vertical(100, 10);
        sb.set_offset(50);
        sb.jump_to(0.0);
        assert_eq!(sb.offset(), 0);
    }

    #[test]
    fn test_jump_to_end() {
        let mut sb = Scrollbar::vertical(100, 10);
        sb.jump_to(1.0);
        assert_eq!(sb.offset(), 90);
    }

    #[test]
    fn test_jump_to_clamps() {
        let mut sb = Scrollbar::vertical(100, 10);
        sb.jump_to(2.0);
        assert_eq!(sb.offset(), 90);
        sb.jump_to(-1.0);
        assert_eq!(sb.offset(), 0);
    }

    #[test]
    fn test_jump_start() {
        let mut sb = Scrollbar::vertical(100, 10);
        sb.set_offset(50);
        sb.jump_start();
        assert_eq!(sb.offset(), 0);
    }

    #[test]
    fn test_jump_end() {
        let mut sb = Scrollbar::vertical(100, 10);
        sb.jump_end();
        assert_eq!(sb.offset(), 90);
    }

    // =====================================================
    // Update Lengths Tests
    // =====================================================

    #[test]
    fn test_update_lengths() {
        let mut sb = Scrollbar::vertical(100, 10);
        sb.update_lengths(200, 20);
        assert_eq!(sb.content_length(), 200);
        assert_eq!(sb.viewport_length(), 20);
    }

    #[test]
    fn test_update_lengths_clamps_offset() {
        let mut sb = Scrollbar::vertical(100, 10);
        sb.set_offset(80);
        sb.update_lengths(50, 10);
        assert_eq!(sb.offset(), 40); // new max_offset
    }

    // =====================================================
    // Brick Trait Tests
    // =====================================================

    #[test]
    fn test_brick_name() {
        let sb = Scrollbar::vertical(100, 10);
        assert_eq!(sb.brick_name(), "scrollbar");
    }

    #[test]
    fn test_assertions_not_empty() {
        let sb = Scrollbar::vertical(100, 10);
        assert!(!sb.assertions().is_empty());
    }

    #[test]
    fn test_budget() {
        let sb = Scrollbar::vertical(100, 10);
        let budget = sb.budget();
        assert!(budget.paint_ms > 0);
    }

    #[test]
    fn test_verify() {
        let sb = Scrollbar::vertical(100, 10);
        assert!(sb.verify().is_valid());
    }

    #[test]
    fn test_to_html() {
        let sb = Scrollbar::vertical(100, 10);
        assert!(sb.to_html().is_empty());
    }

    #[test]
    fn test_to_css() {
        let sb = Scrollbar::vertical(100, 10);
        assert!(sb.to_css().is_empty());
    }

    // =====================================================
    // Widget Trait Tests
    // =====================================================

    #[test]
    fn test_type_id() {
        let sb = Scrollbar::vertical(100, 10);
        assert_eq!(Widget::type_id(&sb), TypeId::of::<Scrollbar>());
    }

    #[test]
    fn test_measure_vertical() {
        let sb = Scrollbar::vertical(100, 10);
        let size = sb.measure(Constraints::loose(Size::new(100.0, 50.0)));
        assert_eq!(size.width, 1.0);
        assert!(size.height >= 3.0);
    }

    #[test]
    fn test_measure_horizontal() {
        let sb = Scrollbar::horizontal(100, 10);
        let size = sb.measure(Constraints::loose(Size::new(50.0, 100.0)));
        assert_eq!(size.height, 1.0);
        assert!(size.width >= 3.0);
    }

    #[test]
    fn test_layout() {
        let mut sb = Scrollbar::vertical(100, 10);
        let bounds = Rect::new(5.0, 10.0, 1.0, 20.0);
        let result = sb.layout(bounds);
        assert_eq!(result.size.height, 20.0);
        assert_eq!(sb.bounds, bounds);
    }

    #[test]
    fn test_children() {
        let sb = Scrollbar::vertical(100, 10);
        assert!(sb.children().is_empty());
    }

    #[test]
    fn test_children_mut() {
        let mut sb = Scrollbar::vertical(100, 10);
        assert!(sb.children_mut().is_empty());
    }

    #[test]
    fn test_event() {
        let mut sb = Scrollbar::vertical(100, 10);
        let event = Event::KeyDown {
            key: presentar_core::Key::Enter,
        };
        assert!(sb.event(&event).is_none());
    }

    // =====================================================
    // Paint Tests
    // =====================================================

    #[test]
    fn test_paint_vertical() {
        let mut sb = Scrollbar::vertical(100, 10);
        sb.bounds = Rect::new(0.0, 0.0, 1.0, 10.0);
        let mut canvas = MockCanvas::new();
        sb.paint(&mut canvas);
        assert!(!canvas.texts.is_empty());
    }

    #[test]
    fn test_paint_vertical_no_arrows() {
        let mut sb = Scrollbar::vertical(100, 10).with_arrows(false);
        sb.bounds = Rect::new(0.0, 0.0, 1.0, 10.0);
        let mut canvas = MockCanvas::new();
        sb.paint(&mut canvas);
        // Should not contain arrow characters
        let has_arrow = canvas.texts.iter().any(|(t, _)| t == "" || t == "");
        assert!(!has_arrow);
    }

    #[test]
    fn test_paint_horizontal() {
        let mut sb = Scrollbar::horizontal(100, 10);
        sb.bounds = Rect::new(0.0, 0.0, 10.0, 1.0);
        let mut canvas = MockCanvas::new();
        sb.paint(&mut canvas);
        assert!(!canvas.texts.is_empty());
    }

    #[test]
    fn test_paint_small_bounds() {
        let mut sb = Scrollbar::vertical(100, 10);
        sb.bounds = Rect::new(0.0, 0.0, 1.0, 2.0);
        let mut canvas = MockCanvas::new();
        sb.paint(&mut canvas);
        // Should handle small bounds gracefully
    }

    #[test]
    fn test_paint_with_offset() {
        let mut sb = Scrollbar::vertical(100, 10);
        sb.set_offset(50);
        sb.bounds = Rect::new(0.0, 0.0, 1.0, 10.0);
        let mut canvas = MockCanvas::new();
        sb.paint(&mut canvas);
        assert!(!canvas.texts.is_empty());
    }

    // =====================================================
    // Orientation Enum Tests
    // =====================================================

    #[test]
    fn test_orientation_default() {
        assert_eq!(ScrollOrientation::default(), ScrollOrientation::Vertical);
    }

    #[test]
    fn test_orientation_eq() {
        assert_eq!(ScrollOrientation::Vertical, ScrollOrientation::Vertical);
        assert_ne!(ScrollOrientation::Vertical, ScrollOrientation::Horizontal);
    }
}

impl PartialEq for ScrollbarChars {
    fn eq(&self, other: &Self) -> bool {
        self.track == other.track
            && self.thumb == other.thumb
            && self.arrow_start == other.arrow_start
            && self.arrow_end == other.arrow_end
    }
}