Scroll View Widget
Overview
The ScrollView widget provides a scrollable container for content that exceeds the available viewport size. It supports both vertical and horizontal scrolling with customizable scrollbars and smooth navigation.
Properties
content: The content widget to be scrolledcontent_width: Width of the content areacontent_height: Height of the content areascroll_x: Horizontal scroll offsetscroll_y: Vertical scroll offsetshow_vertical_scrollbar: Whether to display the vertical scrollbarshow_horizontal_scrollbar: Whether to display the horizontal scrollbarscrollbar_track: Character used for scrollbar trackscrollbar_thumb: Character used for scrollbar thumbscrollbar_style: Style configuration for scrollbarsbase: Base widget state
Methods
init(content: ContentType): Creates a new scroll view with the given contentwithContentSize(width: u16, height: u16): Sets the content dimensionswithHorizontalScrollbar(): Enables horizontal scrollbar displayhideVerticalScrollbar(): Hides the vertical scrollbarscrollBy(dx: i16, dy: i16): Scrolls by the specified amountscrollTo(x: u16, y: u16): Scrolls to the specified positionscrollToTop(): Scrolls to the top of the contentscrollToBottom(): Scrolls to the bottom of the contentgetViewportWidth(): Returns the width of the visible viewportgetViewportHeight(): Returns the height of the visible viewportrender(ctx: *RenderContext): Renders the scroll view and its contenthandleEvent(event: Event): Handles keyboard and mouse eventsisFocusable(): Returns true as scroll views are focusablesizeHint(): Returns size hint for layout
Events
- Keyboard Navigation: Arrow keys for scrolling, Page Up/Down, Home/End
- Mouse Scrolling: Mouse wheel events for vertical scrolling
Examples
Basic Usage
zig
const tui = @import("tui");
const ScrollView = tui.widgets.ScrollView;
const Text = tui.widgets.Text;
var text = Text.init("This is a long text that will be scrollable...");
var scroll_view = ScrollView(Text).init(text)
.withContentSize(100, 50);With Custom Scrollbars
zig
var scroll_view = ScrollView(Text).init(text)
.withContentSize(100, 50)
.withHorizontalScrollbar();