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
//! Scroll Demo
//!
//! Demonstrates Material UI scroll containers and scrollbar orientations.
//!
//! ## Simple Scroll Pattern
//!
//! Creating a scrollable container requires three components:
//!
//! 1. **ScrollContainerBuilder** - Configure scroll direction and behavior
//! - `.vertical()`, `.horizontal()`, or `.both()`
//! - `.with_scrollbars(true)` - Scrollbars spawn automatically (default: true)
//! - `.sensitivity(40.0)` - Mouse wheel scroll speed
//!
//! 2. **ScrollPosition::default()** - Tracks current scroll offset
//!
//! 3. **Node with overflow** - Defines the scrollable area
//! - `overflow: Overflow::scroll()` - **Both axes must be Scroll for Bevy's scroll system**
//! - The `ScrollContainer.direction` field controls which direction actually scrolls
//! - For vertical-only: use `.vertical()` + `Overflow::scroll()`
//! - For horizontal-only: use `.horizontal()` + `Overflow::scroll()`
//! - Both overflow and size (width/height) are required
//!
//! The ScrollPlugin automatically:
//! - Creates an internal ScrollContent wrapper
//! - Spawns scrollbars (when show_scrollbars=true)
//! - Syncs scroll position
//! - Handles mouse wheel and scrollbar dragging
//!
//! No manual scrollbar spawning needed!
use bevy::prelude::*;
use bevy_material_ui::prelude::*;
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_plugins(MaterialUiPlugin)
.add_plugins(TelemetryPlugin)
.add_systems(Startup, setup)
.run();
}
fn setup(mut commands: Commands, theme: Res<MaterialTheme>, telemetry: Res<TelemetryConfig>) {
commands.spawn(Camera2d);
commands
.spawn((
Node {
width: Val::Percent(100.0),
height: Val::Percent(100.0),
flex_direction: FlexDirection::Column,
row_gap: Val::Px(24.0),
padding: UiRect::all(Val::Px(24.0)),
..default()
},
BackgroundColor(theme.surface),
))
.insert_test_id("scroll_demo/root", &telemetry)
.with_children(|root| {
root.spawn((
Text::new("Scroll Containers"),
TextFont {
font_size: 24.0,
..default()
},
TextColor(theme.on_surface),
));
// Vertical scroller
root.spawn(Node {
flex_direction: FlexDirection::Column,
row_gap: Val::Px(8.0),
..default()
})
.with_children(|section| {
section.spawn((
Text::new("Vertical scrollbar"),
TextFont {
font_size: 14.0,
..default()
},
TextColor(theme.on_surface_variant),
));
section
.spawn((
// 1. Configure scroll direction and behavior
// Scrollbars spawn automatically (default: with_scrollbars=true)
ScrollContainerBuilder::new().vertical().build(),
// 2. Track scroll position
ScrollPosition::default(),
// 3. Define scrollable area with overflow and size
Node {
width: Val::Px(420.0),
height: Val::Px(160.0),
overflow: Overflow::scroll(), // Both axes must be Scroll
padding: UiRect::all(Val::Px(12.0)),
flex_direction: FlexDirection::Column,
row_gap: Val::Px(8.0),
..default()
},
BackgroundColor(theme.surface_container_low),
BorderRadius::all(Val::Px(12.0)),
))
.insert_test_id("scroll_demo/scroll/vertical", &telemetry)
.with_children(|content| {
for i in 1..=30 {
content.spawn((
Text::new(format!("Item {i}")),
TextFont {
font_size: 14.0,
..default()
},
TextColor(theme.on_surface),
));
}
});
});
// Horizontal scroller
root.spawn(Node {
flex_direction: FlexDirection::Column,
row_gap: Val::Px(8.0),
..default()
})
.with_children(|section| {
section.spawn((
Text::new("Horizontal scrollbar"),
TextFont {
font_size: 14.0,
..default()
},
TextColor(theme.on_surface_variant),
));
section
.spawn((
ScrollContainerBuilder::new().horizontal().build(),
ScrollPosition::default(),
Node {
width: Val::Px(420.0),
height: Val::Px(120.0),
// Both axes must be Scroll for Bevy's scroll system
// ScrollContainer.direction controls which direction actually scrolls
overflow: Overflow::scroll(),
padding: UiRect::all(Val::Px(12.0)),
flex_direction: FlexDirection::Row,
column_gap: Val::Px(12.0),
..default()
},
BackgroundColor(theme.surface_container_low),
BorderRadius::all(Val::Px(12.0)),
))
.insert_test_id("scroll_demo/scroll/horizontal", &telemetry)
.with_children(|content| {
for i in 1..=20 {
content.spawn((
Node {
width: Val::Px(96.0),
height: Val::Px(72.0),
..default()
},
BackgroundColor(if i % 2 == 0 {
theme.secondary_container
} else {
theme.primary_container
}),
BorderRadius::all(Val::Px(12.0)),
));
}
});
});
});
}