Skip to content

Commit

Permalink
Fix scrolling in UI example (#8069)
Browse files Browse the repository at this point in the history
Co-authored-by: Carter Anderson <[email protected]>
  • Loading branch information
rparrett and cart authored Mar 15, 2023
1 parent f6c72a7 commit 1d4910a
Showing 1 changed file with 10 additions and 16 deletions.
26 changes: 10 additions & 16 deletions examples/ui/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,6 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
NodeBundle {
style: Style {
flex_direction: FlexDirection::Column,
flex_grow: 1.0,
align_items: AlignItems::Center,
..default()
},
Expand All @@ -145,12 +144,7 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
font_size: 20.,
color: Color::WHITE,
},
)
.with_style(Style {
flex_shrink: 0.,
size: Size::height(Val::Px(20.)),
..default()
}),
),
Label,
AccessibilityNode(NodeBuilder::new(Role::ListItem)),
));
Expand Down Expand Up @@ -291,21 +285,21 @@ struct ScrollingList {

fn mouse_scroll(
mut mouse_wheel_events: EventReader<MouseWheel>,
mut query_list: Query<(&mut ScrollingList, &mut Style, &Children, &Node)>,
query_item: Query<&Node>,
mut query_list: Query<(&mut ScrollingList, &mut Style, &Parent, &Node)>,
query_node: Query<&Node>,
) {
for mouse_wheel_event in mouse_wheel_events.iter() {
for (mut scrolling_list, mut style, children, uinode) in &mut query_list {
let items_height: f32 = children
.iter()
.map(|entity| query_item.get(*entity).unwrap().size().y)
.sum();
let panel_height = uinode.size().y;
let max_scroll = (items_height - panel_height).max(0.);
for (mut scrolling_list, mut style, parent, list_node) in &mut query_list {
let items_height = list_node.size().y;
let container_height = query_node.get(parent.get()).unwrap().size().y;

let max_scroll = (items_height - container_height).max(0.);

let dy = match mouse_wheel_event.unit {
MouseScrollUnit::Line => mouse_wheel_event.y * 20.,
MouseScrollUnit::Pixel => mouse_wheel_event.y,
};

scrolling_list.position += dy;
scrolling_list.position = scrolling_list.position.clamp(-max_scroll, 0.);
style.top = Val::Px(scrolling_list.position);
Expand Down

0 comments on commit 1d4910a

Please sign in to comment.