Skip to content

Commit 5e8aa79

Browse files
Newtyped ScrollPosition (#19881)
# Objective Change `ScrollPosition` to newtype `Vec2`. It's easier to work with a `Vec2` wrapper than individual fields. I'm not sure why this wasn't newtyped to start with. Maybe the intent was to support responsive coordinates eventually but that probably isn't very useful or straightforward to implement. And even if we do want to support responsive coords in the future, it can newtype `Val2`. ## Solution Change `ScrollPosition` to newtype `Vec2`. Also added some extra details to the doc comments. ## Testing Try the `scroll` example. --------- Co-authored-by: Alice Cecile <alice.i.cecile@gmail.com>
1 parent b980d4a commit 5e8aa79

File tree

7 files changed

+34
-59
lines changed

7 files changed

+34
-59
lines changed

crates/bevy_core_widgets/src/core_scrollbar.rs

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -136,23 +136,13 @@ fn scrollbar_on_pointer_down(
136136
ControlOrientation::Horizontal => {
137137
if node.size().x > 0. {
138138
let click_pos = local_pos.x * content_size.x / node.size().x;
139-
adjust_scroll_pos(
140-
&mut scroll_pos.offset_x,
141-
click_pos,
142-
visible_size.x,
143-
max_range.x,
144-
);
139+
adjust_scroll_pos(&mut scroll_pos.x, click_pos, visible_size.x, max_range.x);
145140
}
146141
}
147142
ControlOrientation::Vertical => {
148143
if node.size().y > 0. {
149144
let click_pos = local_pos.y * content_size.y / node.size().y;
150-
adjust_scroll_pos(
151-
&mut scroll_pos.offset_y,
152-
click_pos,
153-
visible_size.y,
154-
max_range.y,
155-
);
145+
adjust_scroll_pos(&mut scroll_pos.y, click_pos, visible_size.y, max_range.y);
156146
}
157147
}
158148
}
@@ -171,8 +161,8 @@ fn scrollbar_on_drag_start(
171161
if let Ok(scroll_area) = q_scroll_area.get(scrollbar.target) {
172162
drag.dragging = true;
173163
drag.drag_origin = match scrollbar.orientation {
174-
ControlOrientation::Horizontal => scroll_area.offset_x,
175-
ControlOrientation::Vertical => scroll_area.offset_y,
164+
ControlOrientation::Horizontal => scroll_area.x,
165+
ControlOrientation::Vertical => scroll_area.y,
176166
};
177167
}
178168
}
@@ -204,13 +194,13 @@ fn scrollbar_on_drag(
204194
match scrollbar.orientation {
205195
ControlOrientation::Horizontal => {
206196
let range = (content_size.x - visible_size.x).max(0.);
207-
scroll_pos.offset_x = (drag.drag_origin
197+
scroll_pos.x = (drag.drag_origin
208198
+ (distance.x * content_size.x) / scrollbar_size.x)
209199
.clamp(0., range);
210200
}
211201
ControlOrientation::Vertical => {
212202
let range = (content_size.y - visible_size.y).max(0.);
213-
scroll_pos.offset_y = (drag.drag_origin
203+
scroll_pos.y = (drag.drag_origin
214204
+ (distance.y * content_size.y) / scrollbar_size.y)
215205
.clamp(0., range);
216206
}
@@ -296,7 +286,7 @@ fn update_scrollbar_thumb(
296286
visible_size.x,
297287
track_length.x,
298288
scrollbar.min_thumb_length,
299-
scroll_area.0.offset_x,
289+
scroll_area.0.x,
300290
);
301291

302292
thumb.top = Val::Px(0.);
@@ -310,7 +300,7 @@ fn update_scrollbar_thumb(
310300
visible_size.y,
311301
track_length.y,
312302
scrollbar.min_thumb_length,
313-
scroll_area.0.offset_y,
303+
scroll_area.0.y,
314304
);
315305

316306
thumb.left = Val::Px(0.);

crates/bevy_ui/src/layout/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -311,12 +311,12 @@ with UI components as a child of an entity without UI components, your UI layout
311311
.map(|scroll_pos| {
312312
Vec2::new(
313313
if style.overflow.x == OverflowAxis::Scroll {
314-
scroll_pos.offset_x
314+
scroll_pos.x
315315
} else {
316316
0.0
317317
},
318318
if style.overflow.y == OverflowAxis::Scroll {
319-
scroll_pos.offset_y
319+
scroll_pos.y
320320
} else {
321321
0.0
322322
},
@@ -333,7 +333,7 @@ with UI components as a child of an entity without UI components, your UI layout
333333
if clamped_scroll_position != scroll_position {
334334
commands
335335
.entity(entity)
336-
.insert(ScrollPosition::from(clamped_scroll_position));
336+
.insert(ScrollPosition(clamped_scroll_position));
337337
}
338338

339339
let physical_scroll_position =

crates/bevy_ui/src/ui_node.rs

Lines changed: 12 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -321,45 +321,27 @@ impl Default for ComputedNode {
321321
}
322322
}
323323

324-
/// The scroll position of the node.
324+
/// The scroll position of the node. Values are in logical pixels, increasing from top-left to bottom-right.
325325
///
326-
/// Updating the values of `ScrollPosition` will reposition the children of the node by the offset amount.
326+
/// Increasing the x-coordinate causes the scrolled content to visibly move left on the screen, while increasing the y-coordinate causes the scrolled content to move up.
327+
/// This might seem backwards, however what's really happening is that
328+
/// the scroll position is moving the visible "window" in the local coordinate system of the scrolled content -
329+
/// moving the window down causes the content to move up.
330+
///
331+
/// Updating the values of `ScrollPosition` will reposition the children of the node by the offset amount in logical pixels.
327332
/// `ScrollPosition` may be updated by the layout system when a layout change makes a previously valid `ScrollPosition` invalid.
328333
/// Changing this does nothing on a `Node` without setting at least one `OverflowAxis` to `OverflowAxis::Scroll`.
329-
#[derive(Component, Debug, Clone, Reflect)]
334+
#[derive(Component, Debug, Clone, Default, Deref, DerefMut, Reflect)]
330335
#[reflect(Component, Default, Clone)]
331-
pub struct ScrollPosition {
332-
/// How far across the node is scrolled, in logical pixels. (0 = not scrolled / scrolled to right)
333-
pub offset_x: f32,
334-
/// How far down the node is scrolled, in logical pixels. (0 = not scrolled / scrolled to top)
335-
pub offset_y: f32,
336-
}
336+
pub struct ScrollPosition(pub Vec2);
337337

338338
impl ScrollPosition {
339-
pub const DEFAULT: Self = Self {
340-
offset_x: 0.0,
341-
offset_y: 0.0,
342-
};
343-
}
344-
345-
impl Default for ScrollPosition {
346-
fn default() -> Self {
347-
Self::DEFAULT
348-
}
349-
}
350-
351-
impl From<&ScrollPosition> for Vec2 {
352-
fn from(scroll_pos: &ScrollPosition) -> Self {
353-
Vec2::new(scroll_pos.offset_x, scroll_pos.offset_y)
354-
}
339+
pub const DEFAULT: Self = Self(Vec2::ZERO);
355340
}
356341

357342
impl From<Vec2> for ScrollPosition {
358-
fn from(vec: Vec2) -> Self {
359-
ScrollPosition {
360-
offset_x: vec.x,
361-
offset_y: vec.y,
362-
}
343+
fn from(value: Vec2) -> Self {
344+
Self(value)
363345
}
364346
}
365347

examples/testbed/full_ui.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -454,8 +454,8 @@ pub fn update_scroll_position(
454454
for (_pointer, pointer_map) in hover_map.iter() {
455455
for (entity, _hit) in pointer_map.iter() {
456456
if let Ok(mut scroll_position) = scrolled_node_query.get_mut(*entity) {
457-
scroll_position.offset_x -= dx;
458-
scroll_position.offset_y -= dy;
457+
scroll_position.x -= dx;
458+
scroll_position.y -= dy;
459459
}
460460
}
461461
}

examples/ui/scroll.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -338,8 +338,8 @@ pub fn update_scroll_position(
338338
for (_pointer, pointer_map) in hover_map.iter() {
339339
for (entity, _hit) in pointer_map.iter() {
340340
if let Ok(mut scroll_position) = scrolled_node_query.get_mut(*entity) {
341-
scroll_position.offset_x -= dx;
342-
scroll_position.offset_y -= dy;
341+
scroll_position.x -= dx;
342+
scroll_position.y -= dy;
343343
}
344344
}
345345
}

examples/ui/scrollbars.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,7 @@ fn scroll_area_demo() -> impl Bundle {
8686
..default()
8787
},
8888
BackgroundColor(colors::GRAY1.into()),
89-
ScrollPosition {
90-
offset_x: 0.0,
91-
offset_y: 10.0,
92-
},
89+
ScrollPosition(Vec2::new(0.0, 10.0)),
9390
Children::spawn((
9491
// The actual content of the scrolling area
9592
Spawn(text_row("Alpha Wolf")),
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
title: Make `ScrollPosition` newtype `Vec2`
3+
pull_requests: [19881]
4+
---
5+
6+
`ScrollPosition` now newtypes `Vec2`, its `offset_x` and `offset_y` fields have been removed.

0 commit comments

Comments
 (0)