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
//! Bevy plugin providing a scrollbar.
//!
//! # Getting started
//!
//! First add `bevy_scrollbar` to your dependencies and the [`ScrollbarPlugin`] to your app.
//!
//! # Making a scrollbar
//!
//! The two pieces of a scrollbar are referred to as the _track_ and the _thumb_. You can turn an entity into a scrollbar (track) by adding [`Scrollbar { scrollable }`](Scrollbar) to it, where `scrollable` is the entity Id of another node with overflowing content. This spawns the thumb as the child of the track along with three observers. See [`Scrollbar`] for more details.
//!
//! # Example 1
//!
//! ```no_run
//! use bevy::{
//! ecs::spawn::{SpawnIter, SpawnWith},
//! prelude::*,
//! };
//! use bevy_scrollbar::{Scrollbar, ScrollbarPlugin};
//!
//! fn main() {
//! App::new()
//! .add_plugins((DefaultPlugins, ScrollbarPlugin))
//! .add_systems(Startup, setup)
//! .run();
//! }
//!
//! fn setup(mut commands: Commands) {
//! commands.spawn(Camera2d);
//!
//! commands.spawn((
//! // Container node for the overflowed node and its scrollbar that are usually siblings.
//! Node {
//! width: Val::Percent(100.0),
//! height: Val::Percent(100.0),
//! justify_content: JustifyContent::Center,
//! align_items: AlignItems::Center,
//! ..default()
//! },
//! Children::spawn(SpawnWith(|container: &mut ChildSpawner| {
//! // Overflowed node
//! let scrollable = container
//! .spawn((
//! Node {
//! height: Val::Percent(80.0),
//! border: UiRect::all(Val::Px(5.0)).with_right(Val::Px(2.5)),
//! // You can omit the overflow field for a vertical scrollbar
//! overflow: Overflow::scroll_y(),
//! flex_direction: FlexDirection::Column,
//! ..default()
//! },
//! BorderColor::all(Color::BLACK),
//! Children::spawn(SpawnIter(
//! (0..100).map(|i| Text::new(format!(" Scrolling {i}! "))),
//! )),
//! ))
//! .id();
//!
//! // Scrollbar
//! container.spawn((
//! Scrollbar { scrollable },
//! Node {
//! width: Val::Percent(1.5),
//! // Same height as the scrollable node
//! height: Val::Percent(80.0),
//! border: UiRect::all(Val::Px(5.0)).with_left(Val::Px(2.5)),
//! ..default()
//! },
//! BorderColor::all(Color::BLACK),
//! ));
//! })),
//! ));
//! }
//! ```
//!
//! # The [`Scrollbar`] / [`Scrollable`] relationship
//!
//! The [`Scrollbar`] component implements `Relationship` with target [`Scrollable`]. This relates the [`Scrollbar`] node to the overflowed node to which [`Scrollable`] is added. This means [`Scrollable`] can be used to spawn a scrollbar (the same way `Children` can be used to spawn children). See [example-2](crate#example-2).
//!
//! # The [`Scrollable`] content
//!
//! The [`Scrollable`] content responds to mouse `Scroll` triggers. You can configure how fast the content scrolls by adding [`ScrollSpeed`] to the [`Scrollable`] node. See [example-2](crate#example-2).
//!
//! # Thumb customization
//!
//! Color and `Drag` speed of the thumb can be configured by adding [`ThumbColor`] and [`DragSpeed`] to the [`Scrollbar`]. See [example-2](crate#example-2).
//!
//! # Example 2
//!
//!```no_run
//! use bevy::{ecs::spawn::SpawnIter, prelude::*};
//! use bevy_scrollbar::{
//! Scrollable, ScrollSpeed, ScrollbarPlugin, ThumbColor, DragSpeed,
//! };
//!
//! fn main() {
//! App::new()
//! .add_plugins((DefaultPlugins, ScrollbarPlugin))
//! .add_systems(Startup, setup)
//! .run();
//! }
//!
//! fn setup(mut commands: Commands) {
//! commands.spawn(Camera2d);
//!
//! // Container of the scrollable content and its scrollbar
//! let mut container = commands.spawn(Node {
//! width: Val::Percent(100.0),
//! height: Val::Percent(100.0),
//! justify_content: JustifyContent::Center,
//! align_items: AlignItems::Center,
//! ..default()
//! });
//! let container_id = container.id();
//!
//! // Spawn the scrollable node
//! container.with_child((
//! Node {
//! width: Val::Percent(80.0),
//! height: Val::Percent(80.0),
//! border: UiRect::all(Val::Px(5.0)),
//! flex_wrap: FlexWrap::Wrap,
//! // Ommitting the overflow field because the scrollbar is vertical
//! ..default()
//! },
//! BorderColor::all(Color::BLACK),
//! Children::spawn(SpawnIter((0..100).map(|i| {
//! (
//! Node {
//! width: Val::Percent(20.0),
//! height: Val::Percent(20.0),
//! justify_content: JustifyContent::Center,
//! align_items: AlignItems::Center,
//! ..default()
//! },
//! Children::spawn_one(Text::new(format!("Scroll {i}!"))),
//! )
//! }))),
//! // Customize scroll speed of the content
//! ScrollSpeed(2.0),
//! // Spawn the scrollbar
//! Scrollable::spawn_one((
//! // Add the scrollbar as a child of the container
//! ChildOf(container_id),
//! Node {
//! width: Val::Percent(1.5),
//! // Same height as the scrollable node
//! height: Val::Percent(80.0),
//! margin: UiRect::left(Val::Px(5.0)),
//! border: UiRect::all(Val::Px(5.0)),
//! ..default()
//! },
//! BorderColor::all(Color::BLACK),
//! // The thumb will be spawned with the same border radius
//! BorderRadius::all(Val::Px(10.0)),
//! // Customize color of the thumb
//! ThumbColor(Color::srgb(0.0, 0.0, 1.0)),
//! // Customize drag speed of the thumb
//! DragSpeed(4.0),
//! )),
//! ));
//! }
//!```
use ;
use debug;
pub use ;
pub use ;
/// Plugin scheduling [`ScrollbarSystems`] after `UiSystem::Layout` in `PostUpdate`.
;
/// `SystemSet` containing the system updating the thumb of a [`Scrollbar`].
;
/// Clamps [`ScrollPosition`] and updates the length and position of the thumb.
///
/// Bevy computes layout and `Transform` of UI nodes in `UiSystems::Layout`. This system runs in `PostUpdate` after `UiSystems::Layout` and uses change detection on the [`Scrollable`] node. Graphically, the thumb is updated on the frame following the change. This allows us to use the computation done by `UiSystems::Layout`.
/// Clamps [`ScrollPosition`] and updates the position of the thumb.