ratatui-interact 0.5.3

Interactive TUI components for ratatui with focus management and mouse support
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
//! Scrollable content component
//!
//! A scrollable text pane with focus support, keyboard navigation, and mouse scrolling.
//! Ideal for displaying log output, help text, or any scrollable content.
//!
//! # Example
//!
//! ```rust,ignore
//! use ratatui_interact::components::{
//!     ScrollableContent, ScrollableContentState, ScrollableContentStyle,
//!     handle_scrollable_content_key, handle_scrollable_content_mouse,
//! };
//! use ratatui::prelude::*;
//!
//! // Create state with content
//! let mut state = ScrollableContentState::new(vec![
//!     "Line 1".to_string(),
//!     "Line 2".to_string(),
//!     "Line 3".to_string(),
//! ]);
//! state.set_focused(true);
//!
//! // In render:
//! let content = ScrollableContent::new(&state)
//!     .title("My Content")
//!     .style(ScrollableContentStyle::default());
//! content.render(area, buf);
//!
//! // Handle events:
//! handle_scrollable_content_key(&mut state, &key_event, visible_height);
//! handle_scrollable_content_mouse(&mut state, &mouse_event, content_area);
//! ```

use ratatui::{
    buffer::Buffer,
    layout::Rect,
    style::{Color, Modifier, Style},
    text::Line,
    widgets::{Block, Borders, Paragraph, Widget},
};

/// Actions that can result from scrollable content interaction
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ScrollableContentAction {
    /// Content was scrolled up
    ScrollUp,
    /// Content was scrolled down
    ScrollDown,
    /// Scrolled to top
    ScrollToTop,
    /// Scrolled to bottom
    ScrollToBottom,
    /// Page up
    PageUp,
    /// Page down
    PageDown,
    /// Toggle fullscreen
    ToggleFullscreen,
}

/// State for the ScrollableContent component
#[derive(Debug, Clone)]
pub struct ScrollableContentState {
    /// Content lines to display
    lines: Vec<String>,
    /// Current scroll position (line offset from top)
    scroll_offset: usize,
    /// Whether this pane is focused
    focused: bool,
    /// Whether this pane is in fullscreen mode
    fullscreen: bool,
    /// Title for the content pane
    title: Option<String>,
}

impl ScrollableContentState {
    /// Create a new state with the given content lines
    pub fn new(lines: Vec<String>) -> Self {
        Self {
            lines,
            scroll_offset: 0,
            focused: false,
            fullscreen: false,
            title: None,
        }
    }

    /// Create an empty state
    pub fn empty() -> Self {
        Self::new(Vec::new())
    }

    /// Set the content lines
    pub fn set_lines(&mut self, lines: Vec<String>) {
        self.lines = lines;
        // Clamp scroll offset to valid range
        if !self.lines.is_empty() {
            self.scroll_offset = self.scroll_offset.min(self.lines.len() - 1);
        } else {
            self.scroll_offset = 0;
        }
    }

    /// Get the content lines
    pub fn lines(&self) -> &[String] {
        &self.lines
    }

    /// Push a line to the content
    pub fn push_line(&mut self, line: impl Into<String>) {
        self.lines.push(line.into());
    }

    /// Clear all content
    pub fn clear(&mut self) {
        self.lines.clear();
        self.scroll_offset = 0;
    }

    /// Get the number of lines
    pub fn line_count(&self) -> usize {
        self.lines.len()
    }

    /// Get the current scroll offset
    pub fn scroll_offset(&self) -> usize {
        self.scroll_offset
    }

    /// Set the scroll offset
    pub fn set_scroll_offset(&mut self, offset: usize) {
        if !self.lines.is_empty() {
            self.scroll_offset = offset.min(self.lines.len() - 1);
        } else {
            self.scroll_offset = 0;
        }
    }

    /// Check if focused
    pub fn is_focused(&self) -> bool {
        self.focused
    }

    /// Set focus state
    pub fn set_focused(&mut self, focused: bool) {
        self.focused = focused;
    }

    /// Check if in fullscreen mode
    pub fn is_fullscreen(&self) -> bool {
        self.fullscreen
    }

    /// Set fullscreen mode
    pub fn set_fullscreen(&mut self, fullscreen: bool) {
        self.fullscreen = fullscreen;
    }

    /// Toggle fullscreen mode
    pub fn toggle_fullscreen(&mut self) -> bool {
        self.fullscreen = !self.fullscreen;
        self.fullscreen
    }

    /// Set the title
    pub fn set_title(&mut self, title: impl Into<String>) {
        self.title = Some(title.into());
    }

    /// Get the title
    pub fn title(&self) -> Option<&str> {
        self.title.as_deref()
    }

    /// Scroll up by the given number of lines
    pub fn scroll_up(&mut self, lines: usize) {
        self.scroll_offset = self.scroll_offset.saturating_sub(lines);
    }

    /// Scroll down by the given number of lines
    pub fn scroll_down(&mut self, lines: usize, visible_height: usize) {
        if self.lines.is_empty() {
            return;
        }
        let max_offset = self.lines.len().saturating_sub(visible_height);
        self.scroll_offset = (self.scroll_offset + lines).min(max_offset);
    }

    /// Scroll to the top
    pub fn scroll_to_top(&mut self) {
        self.scroll_offset = 0;
    }

    /// Scroll to the bottom
    pub fn scroll_to_bottom(&mut self, visible_height: usize) {
        if self.lines.is_empty() {
            return;
        }
        self.scroll_offset = self.lines.len().saturating_sub(visible_height);
    }

    /// Page up
    pub fn page_up(&mut self, visible_height: usize) {
        self.scroll_up(visible_height.saturating_sub(1));
    }

    /// Page down
    pub fn page_down(&mut self, visible_height: usize) {
        self.scroll_down(visible_height.saturating_sub(1), visible_height);
    }

    /// Get a slice of visible lines based on current scroll position and height
    pub fn visible_lines(&self, height: usize) -> &[String] {
        if self.lines.is_empty() {
            return &[];
        }
        let start = self.scroll_offset.min(self.lines.len() - 1);
        let end = (start + height).min(self.lines.len());
        &self.lines[start..end]
    }

    /// Check if scrolled to top
    pub fn is_at_top(&self) -> bool {
        self.scroll_offset == 0
    }

    /// Check if scrolled to bottom (given visible height)
    pub fn is_at_bottom(&self, visible_height: usize) -> bool {
        if self.lines.is_empty() {
            return true;
        }
        self.scroll_offset >= self.lines.len().saturating_sub(visible_height)
    }

    /// Get the content as a single string (for clipboard copy)
    pub fn content_as_string(&self) -> String {
        self.lines.join("\n")
    }
}

impl Default for ScrollableContentState {
    fn default() -> Self {
        Self::empty()
    }
}

/// Style configuration for ScrollableContent
#[derive(Debug, Clone)]
pub struct ScrollableContentStyle {
    /// Border style when not focused
    pub border_style: Style,
    /// Border style when focused
    pub focused_border_style: Style,
    /// Text style
    pub text_style: Style,
    /// Whether to show borders
    pub show_borders: bool,
    /// Whether to show scroll indicators
    pub show_scroll_indicators: bool,
}

impl Default for ScrollableContentStyle {
    fn default() -> Self {
        Self {
            border_style: Style::default().fg(Color::DarkGray),
            focused_border_style: Style::default().fg(Color::Cyan),
            text_style: Style::default().fg(Color::White),
            show_borders: true,
            show_scroll_indicators: true,
        }
    }
}

impl From<&crate::theme::Theme> for ScrollableContentStyle {
    fn from(theme: &crate::theme::Theme) -> Self {
        let p = &theme.palette;
        Self {
            border_style: Style::default().fg(p.border_disabled),
            focused_border_style: Style::default().fg(p.border_accent),
            text_style: Style::default().fg(p.text),
            show_borders: true,
            show_scroll_indicators: true,
        }
    }
}

impl ScrollableContentStyle {
    /// Create a minimal style without borders
    pub fn borderless() -> Self {
        Self {
            show_borders: false,
            ..Default::default()
        }
    }

    /// Create a style with custom focus color
    pub fn with_focus_color(mut self, color: Color) -> Self {
        self.focused_border_style = Style::default().fg(color);
        self
    }

    /// Set the text style
    pub fn text_style(mut self, style: Style) -> Self {
        self.text_style = style;
        self
    }
}

/// Scrollable content widget
///
/// A scrollable text pane that displays content with optional borders
/// and scroll indicators. Highlights when focused.
pub struct ScrollableContent<'a> {
    state: &'a ScrollableContentState,
    style: ScrollableContentStyle,
    title: Option<&'a str>,
}

impl<'a> ScrollableContent<'a> {
    /// Create a new scrollable content widget
    pub fn new(state: &'a ScrollableContentState) -> Self {
        Self {
            state,
            style: ScrollableContentStyle::default(),
            title: state.title.as_deref(),
        }
    }

    /// Set the style
    pub fn style(mut self, style: ScrollableContentStyle) -> Self {
        self.style = style;
        self
    }

    /// Apply a theme to derive the style
    pub fn theme(self, theme: &crate::theme::Theme) -> Self {
        self.style(ScrollableContentStyle::from(theme))
    }

    /// Set the title (overrides state title)
    pub fn title(mut self, title: &'a str) -> Self {
        self.title = Some(title);
        self
    }

    /// Calculate the inner area (content area without borders)
    pub fn inner_area(&self, area: Rect) -> Rect {
        if self.style.show_borders {
            Rect {
                x: area.x + 1,
                y: area.y + 1,
                width: area.width.saturating_sub(2),
                height: area.height.saturating_sub(2),
            }
        } else {
            area
        }
    }
}

impl Widget for ScrollableContent<'_> {
    fn render(self, area: Rect, buf: &mut Buffer) {
        if area.width == 0 || area.height == 0 {
            return;
        }

        let border_style = if self.state.focused {
            self.style.focused_border_style
        } else {
            self.style.border_style
        };

        // Create block with optional title
        let mut block = Block::default().border_style(border_style);
        if self.style.show_borders {
            block = block.borders(Borders::ALL);
        }
        if let Some(title) = self.title {
            let title_style = if self.state.focused {
                border_style.add_modifier(Modifier::BOLD)
            } else {
                border_style
            };
            block = block.title(format!(" {} ", title)).title_style(title_style);
        }

        let inner = block.inner(area);
        block.render(area, buf);

        // Render content
        let visible_height = inner.height as usize;
        let visible_lines = self.state.visible_lines(visible_height);

        let lines: Vec<Line> = visible_lines
            .iter()
            .map(|s| Line::from(s.as_str()).style(self.style.text_style))
            .collect();

        let paragraph = Paragraph::new(lines);
        paragraph.render(inner, buf);

        // Render scroll indicators if enabled
        if self.style.show_scroll_indicators && self.style.show_borders {
            let has_content_above = !self.state.is_at_top();
            let has_content_below = !self.state.is_at_bottom(visible_height);

            if has_content_above && area.height > 2 {
                buf.set_string(
                    area.x + area.width - 2,
                    area.y,
                    "â–²",
                    Style::default().fg(Color::DarkGray),
                );
            }
            if has_content_below && area.height > 2 {
                buf.set_string(
                    area.x + area.width - 2,
                    area.y + area.height - 1,
                    "â–¼",
                    Style::default().fg(Color::DarkGray),
                );
            }
        }
    }
}

/// Handle keyboard input for scrollable content
///
/// Supports:
/// - Up/Down or j/k: Scroll by one line
/// - PageUp/PageDown: Scroll by page
/// - Home/End: Scroll to top/bottom
/// - F10/Enter: Toggle fullscreen
///
/// Returns the action taken, if any.
pub fn handle_scrollable_content_key(
    state: &mut ScrollableContentState,
    key: &crossterm::event::KeyEvent,
    visible_height: usize,
) -> Option<ScrollableContentAction> {
    use crossterm::event::KeyCode;

    match key.code {
        KeyCode::Up | KeyCode::Char('k') => {
            state.scroll_up(1);
            Some(ScrollableContentAction::ScrollUp)
        }
        KeyCode::Down | KeyCode::Char('j') => {
            state.scroll_down(1, visible_height);
            Some(ScrollableContentAction::ScrollDown)
        }
        KeyCode::PageUp => {
            state.page_up(visible_height);
            Some(ScrollableContentAction::PageUp)
        }
        KeyCode::PageDown => {
            state.page_down(visible_height);
            Some(ScrollableContentAction::PageDown)
        }
        KeyCode::Home => {
            state.scroll_to_top();
            Some(ScrollableContentAction::ScrollToTop)
        }
        KeyCode::End => {
            state.scroll_to_bottom(visible_height);
            Some(ScrollableContentAction::ScrollToBottom)
        }
        KeyCode::F(10) | KeyCode::Enter => {
            state.toggle_fullscreen();
            Some(ScrollableContentAction::ToggleFullscreen)
        }
        _ => None,
    }
}

/// Handle mouse input for scrollable content
///
/// Supports scroll wheel for scrolling.
///
/// Returns the action taken, if any.
pub fn handle_scrollable_content_mouse(
    state: &mut ScrollableContentState,
    mouse: &crossterm::event::MouseEvent,
    content_area: Rect,
    visible_height: usize,
) -> Option<ScrollableContentAction> {
    use crossterm::event::MouseEventKind;

    // Check if mouse is within content area
    if mouse.column < content_area.x
        || mouse.column >= content_area.x + content_area.width
        || mouse.row < content_area.y
        || mouse.row >= content_area.y + content_area.height
    {
        return None;
    }

    match mouse.kind {
        MouseEventKind::ScrollUp => {
            state.scroll_up(3);
            Some(ScrollableContentAction::ScrollUp)
        }
        MouseEventKind::ScrollDown => {
            state.scroll_down(3, visible_height);
            Some(ScrollableContentAction::ScrollDown)
        }
        _ => None,
    }
}

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

    fn sample_lines() -> Vec<String> {
        (1..=100).map(|i| format!("Line {}", i)).collect()
    }

    #[test]
    fn test_state_new() {
        let lines = vec!["a".to_string(), "b".to_string()];
        let state = ScrollableContentState::new(lines.clone());
        assert_eq!(state.lines(), &lines);
        assert_eq!(state.scroll_offset(), 0);
        assert!(!state.is_focused());
        assert!(!state.is_fullscreen());
    }

    #[test]
    fn test_state_empty() {
        let state = ScrollableContentState::empty();
        assert!(state.lines().is_empty());
        assert_eq!(state.line_count(), 0);
    }

    #[test]
    fn test_scroll_up() {
        let mut state = ScrollableContentState::new(sample_lines());
        state.set_scroll_offset(50);
        assert_eq!(state.scroll_offset(), 50);

        state.scroll_up(10);
        assert_eq!(state.scroll_offset(), 40);

        state.scroll_up(100); // Should clamp to 0
        assert_eq!(state.scroll_offset(), 0);
    }

    #[test]
    fn test_scroll_down() {
        let mut state = ScrollableContentState::new(sample_lines());
        let visible_height = 20;

        state.scroll_down(10, visible_height);
        assert_eq!(state.scroll_offset(), 10);

        state.scroll_down(1000, visible_height); // Should clamp to max
        assert_eq!(state.scroll_offset(), 100 - visible_height);
    }

    #[test]
    fn test_scroll_to_top_bottom() {
        let mut state = ScrollableContentState::new(sample_lines());
        let visible_height = 20;

        state.scroll_to_bottom(visible_height);
        assert_eq!(state.scroll_offset(), 80);
        assert!(state.is_at_bottom(visible_height));

        state.scroll_to_top();
        assert_eq!(state.scroll_offset(), 0);
        assert!(state.is_at_top());
    }

    #[test]
    fn test_page_up_down() {
        let mut state = ScrollableContentState::new(sample_lines());
        let visible_height = 20;

        state.page_down(visible_height);
        assert_eq!(state.scroll_offset(), 19); // visible_height - 1

        state.page_up(visible_height);
        assert_eq!(state.scroll_offset(), 0);
    }

    #[test]
    fn test_visible_lines() {
        let state = ScrollableContentState::new(sample_lines());
        let visible = state.visible_lines(5);
        assert_eq!(visible.len(), 5);
        assert_eq!(visible[0], "Line 1");
        assert_eq!(visible[4], "Line 5");
    }

    #[test]
    fn test_focus_and_fullscreen() {
        let mut state = ScrollableContentState::empty();

        assert!(!state.is_focused());
        state.set_focused(true);
        assert!(state.is_focused());

        assert!(!state.is_fullscreen());
        assert!(state.toggle_fullscreen());
        assert!(state.is_fullscreen());
        assert!(!state.toggle_fullscreen());
        assert!(!state.is_fullscreen());
    }

    #[test]
    fn test_content_as_string() {
        let lines = vec!["a".to_string(), "b".to_string(), "c".to_string()];
        let state = ScrollableContentState::new(lines);
        assert_eq!(state.content_as_string(), "a\nb\nc");
    }

    #[test]
    fn test_set_lines_clamps_scroll() {
        let mut state = ScrollableContentState::new(sample_lines());
        state.set_scroll_offset(50);

        // Set shorter content
        state.set_lines(vec!["a".to_string(), "b".to_string()]);
        assert_eq!(state.scroll_offset(), 1); // Clamped to max valid offset
    }

    #[test]
    fn test_style_default() {
        let style = ScrollableContentStyle::default();
        assert!(style.show_borders);
        assert!(style.show_scroll_indicators);
    }

    #[test]
    fn test_style_borderless() {
        let style = ScrollableContentStyle::borderless();
        assert!(!style.show_borders);
    }

    #[test]
    fn test_handle_key_scroll() {
        use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};

        let mut state = ScrollableContentState::new(sample_lines());
        let visible_height = 20;

        // Down arrow
        let key = KeyEvent::new(KeyCode::Down, KeyModifiers::NONE);
        let action = handle_scrollable_content_key(&mut state, &key, visible_height);
        assert_eq!(action, Some(ScrollableContentAction::ScrollDown));
        assert_eq!(state.scroll_offset(), 1);

        // j key
        let key = KeyEvent::new(KeyCode::Char('j'), KeyModifiers::NONE);
        handle_scrollable_content_key(&mut state, &key, visible_height);
        assert_eq!(state.scroll_offset(), 2);

        // Up arrow
        let key = KeyEvent::new(KeyCode::Up, KeyModifiers::NONE);
        let action = handle_scrollable_content_key(&mut state, &key, visible_height);
        assert_eq!(action, Some(ScrollableContentAction::ScrollUp));
        assert_eq!(state.scroll_offset(), 1);

        // k key
        let key = KeyEvent::new(KeyCode::Char('k'), KeyModifiers::NONE);
        handle_scrollable_content_key(&mut state, &key, visible_height);
        assert_eq!(state.scroll_offset(), 0);

        // Home
        state.set_scroll_offset(50);
        let key = KeyEvent::new(KeyCode::Home, KeyModifiers::NONE);
        let action = handle_scrollable_content_key(&mut state, &key, visible_height);
        assert_eq!(action, Some(ScrollableContentAction::ScrollToTop));
        assert_eq!(state.scroll_offset(), 0);

        // End
        let key = KeyEvent::new(KeyCode::End, KeyModifiers::NONE);
        let action = handle_scrollable_content_key(&mut state, &key, visible_height);
        assert_eq!(action, Some(ScrollableContentAction::ScrollToBottom));
        assert_eq!(state.scroll_offset(), 80);
    }

    #[test]
    fn test_handle_key_fullscreen() {
        use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};

        let mut state = ScrollableContentState::new(sample_lines());
        let visible_height = 20;

        // F10
        let key = KeyEvent::new(KeyCode::F(10), KeyModifiers::NONE);
        let action = handle_scrollable_content_key(&mut state, &key, visible_height);
        assert_eq!(action, Some(ScrollableContentAction::ToggleFullscreen));
        assert!(state.is_fullscreen());

        // Enter
        let key = KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE);
        handle_scrollable_content_key(&mut state, &key, visible_height);
        assert!(!state.is_fullscreen());
    }

    #[test]
    fn test_widget_render() {
        let state = ScrollableContentState::new(vec![
            "Line 1".to_string(),
            "Line 2".to_string(),
            "Line 3".to_string(),
        ]);
        let widget = ScrollableContent::new(&state).title("Test");
        let mut buf = Buffer::empty(Rect::new(0, 0, 20, 10));

        widget.render(Rect::new(0, 0, 20, 10), &mut buf);

        // Check that content was rendered
        let content: String = buf.content.iter().map(|c| c.symbol()).collect();
        assert!(content.contains("Line 1"));
    }

    #[test]
    fn test_inner_area() {
        let state = ScrollableContentState::empty();
        let content = ScrollableContent::new(&state);
        let area = Rect::new(0, 0, 20, 10);

        let inner = content.inner_area(area);
        assert_eq!(inner.x, 1);
        assert_eq!(inner.y, 1);
        assert_eq!(inner.width, 18);
        assert_eq!(inner.height, 8);
    }

    #[test]
    fn test_title() {
        let mut state = ScrollableContentState::empty();
        state.set_title("My Title");
        assert_eq!(state.title(), Some("My Title"));

        let widget = ScrollableContent::new(&state);
        assert_eq!(widget.title, Some("My Title"));

        // Override with widget title
        let widget = ScrollableContent::new(&state).title("Override");
        assert_eq!(widget.title, Some("Override"));
    }
}