Fix scrolling in UI example (#8069)
Co-authored-by: Carter Anderson <mcanders1@gmail.com>
This commit is contained in:
parent
f6c72a7442
commit
1d4910a1e3
@ -124,7 +124,6 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
|
|||||||
NodeBundle {
|
NodeBundle {
|
||||||
style: Style {
|
style: Style {
|
||||||
flex_direction: FlexDirection::Column,
|
flex_direction: FlexDirection::Column,
|
||||||
flex_grow: 1.0,
|
|
||||||
align_items: AlignItems::Center,
|
align_items: AlignItems::Center,
|
||||||
..default()
|
..default()
|
||||||
},
|
},
|
||||||
@ -145,12 +144,7 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
|
|||||||
font_size: 20.,
|
font_size: 20.,
|
||||||
color: Color::WHITE,
|
color: Color::WHITE,
|
||||||
},
|
},
|
||||||
)
|
),
|
||||||
.with_style(Style {
|
|
||||||
flex_shrink: 0.,
|
|
||||||
size: Size::height(Val::Px(20.)),
|
|
||||||
..default()
|
|
||||||
}),
|
|
||||||
Label,
|
Label,
|
||||||
AccessibilityNode(NodeBuilder::new(Role::ListItem)),
|
AccessibilityNode(NodeBuilder::new(Role::ListItem)),
|
||||||
));
|
));
|
||||||
@ -291,21 +285,21 @@ struct ScrollingList {
|
|||||||
|
|
||||||
fn mouse_scroll(
|
fn mouse_scroll(
|
||||||
mut mouse_wheel_events: EventReader<MouseWheel>,
|
mut mouse_wheel_events: EventReader<MouseWheel>,
|
||||||
mut query_list: Query<(&mut ScrollingList, &mut Style, &Children, &Node)>,
|
mut query_list: Query<(&mut ScrollingList, &mut Style, &Parent, &Node)>,
|
||||||
query_item: Query<&Node>,
|
query_node: Query<&Node>,
|
||||||
) {
|
) {
|
||||||
for mouse_wheel_event in mouse_wheel_events.iter() {
|
for mouse_wheel_event in mouse_wheel_events.iter() {
|
||||||
for (mut scrolling_list, mut style, children, uinode) in &mut query_list {
|
for (mut scrolling_list, mut style, parent, list_node) in &mut query_list {
|
||||||
let items_height: f32 = children
|
let items_height = list_node.size().y;
|
||||||
.iter()
|
let container_height = query_node.get(parent.get()).unwrap().size().y;
|
||||||
.map(|entity| query_item.get(*entity).unwrap().size().y)
|
|
||||||
.sum();
|
let max_scroll = (items_height - container_height).max(0.);
|
||||||
let panel_height = uinode.size().y;
|
|
||||||
let max_scroll = (items_height - panel_height).max(0.);
|
|
||||||
let dy = match mouse_wheel_event.unit {
|
let dy = match mouse_wheel_event.unit {
|
||||||
MouseScrollUnit::Line => mouse_wheel_event.y * 20.,
|
MouseScrollUnit::Line => mouse_wheel_event.y * 20.,
|
||||||
MouseScrollUnit::Pixel => mouse_wheel_event.y,
|
MouseScrollUnit::Pixel => mouse_wheel_event.y,
|
||||||
};
|
};
|
||||||
|
|
||||||
scrolling_list.position += dy;
|
scrolling_list.position += dy;
|
||||||
scrolling_list.position = scrolling_list.position.clamp(-max_scroll, 0.);
|
scrolling_list.position = scrolling_list.position.clamp(-max_scroll, 0.);
|
||||||
style.top = Val::Px(scrolling_list.position);
|
style.top = Val::Px(scrolling_list.position);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user