Skip to content

Commit a50cd8d

Browse files
authored
Fix dragging bug in drag_to_scroll example (#21752)
# Objective Fixes #21675. The issue was that the position the drag started on was never saved so new drags always forced the grid to its initial position. The original entity hierarchy consists of a `ScrollableNode`, with a child node (grid parent), which itself has a bunch of nodes as children (grid tiles). Only the grid node has a `Pickable` component. There are a few issues in the original code: * Since grid tiles have `should_block_lower = false` and `is_hoverable = true`, they will allow drag events to propagate to the grid parent. Since the grid parent does not have `Pickable`, it blocks further propagation but does ding a scroll event. This creates two scroll events observed by the `ScrollableNode`. * However, neither of these are the same entity as the `ScrollableNode`, so both events fail the check originally on line 59 (`drag_start.entity != drag_start.original_event_target()`). ## Solution I tried to fix both issues. * Add `Pickable` to the grid parent with `should_block_lower = true` and `is_hoverable = false`. This way it does not ding an event nor does it propagate further. This ensures there is only one scroll event ever going through. * Remove the line 59 entity check as it is no longer necessary. ## Testing Tested on macOS, seems to work fine.
1 parent 3a86269 commit a50cd8d

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

examples/ui/drag_to_scroll.rs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -51,27 +51,30 @@ fn setup(mut commands: Commands) {
5151
},
5252
)
5353
.observe(
54-
|drag_start: On<Pointer<DragStart>>,
54+
|_: On<Pointer<DragStart>>,
5555
mut scroll_position_query: Query<
5656
(&ComputedNode, &mut ScrollStart),
5757
With<ScrollableNode>,
5858
>| {
59-
if drag_start.entity != drag_start.original_event_target() {
60-
return;
61-
}
6259
if let Ok((computed_node, mut start)) = scroll_position_query.single_mut() {
6360
start.0 = computed_node.scroll_position * computed_node.inverse_scale_factor;
6461
}
6562
},
6663
)
6764
.with_children(|commands| {
6865
commands
69-
.spawn(Node {
70-
display: Display::Grid,
71-
grid_template_rows: RepeatedGridTrack::px(w as i32, 100.),
72-
grid_template_columns: RepeatedGridTrack::px(h as i32, 100.),
73-
..default()
74-
})
66+
.spawn((
67+
Node {
68+
display: Display::Grid,
69+
grid_template_rows: RepeatedGridTrack::px(w as i32, 100.),
70+
grid_template_columns: RepeatedGridTrack::px(h as i32, 100.),
71+
..default()
72+
},
73+
Pickable {
74+
is_hoverable: false,
75+
should_block_lower: true,
76+
}
77+
))
7578
.with_children(|commands| {
7679
for y in 0..h {
7780
for x in 0..w {

0 commit comments

Comments
 (0)