Fix scrolling in UI example (#8069)

Co-authored-by: Carter Anderson <mcanders1@gmail.com>
This commit is contained in:
Rob Parrett 2023-03-14 23:50:37 -07:00 committed by GitHub
parent f6c72a7442
commit 1d4910a1e3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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()
},
@ -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)),
));
@ -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);