ScrollView
A scrollable viewport that clips and pans its content. Content larger than the viewport can be scrolled via mouse wheel (desktop) or touch drag (mobile).
Header
cpp
#include <neoflux/widget/scroll_view.h>Construction
cpp
auto scroll = std::make_shared<ScrollView>();Methods
SetContent()
cpp
void SetContent(std::shared_ptr<Widget> content);Sets the scrollable content. Replaces any existing content. The content is laid out at its natural size and clipped to the viewport.
ScrollTo()
cpp
void ScrollTo(float x, float y) noexcept;Sets the scroll offset in pixels. Clamped to the valid range.
GetScrollOffset()
cpp
[[nodiscard]] Point GetScrollOffset() const noexcept;Returns the current scroll offset.
GetContentSize()
cpp
[[nodiscard]] Size GetContentSize() const noexcept;Returns the content size after layout.
Layout Behavior
ScrollViewsetsflex_grow = 1andflex_shrink = 1to fill available space in its parent.- Content is set to
flex_shrink = 0andalign_self = flex-startso it keeps its natural size. overflow = hiddenclips content to the viewport.
Example
cpp
auto scroll = std::make_shared<ScrollView>();
auto content = std::make_shared<Container>();
content->SetFlexDirection(FlexDirection::kColumn);
for (int i = 0; i < 20; ++i) {
auto item = std::make_shared<Text>("Item " + std::to_string(i));
content->AddChild(item);
}
scroll->SetContent(content);
parent->AddChild(scroll);Painting
ScrollView::Paint():
- Saves the current transform/clip state.
- Applies a clip rect to the viewport bounds.
- Translates by the negative scroll offset.
- Paints children.
- Restores the previous state.
Comments