Skip to content

Commit e749ee7

Browse files
committed
Fix ui interactions when cursor disappears suddenly (#3926)
On platforms like wasm (on mobile) the cursor can disappear suddenly (ex: the user releases their finger from the screen). This causes the undesirable behavior in #3752. These changes make the UI handler properly handle this case. Fixes #3752 Alternative to #3599
1 parent 5bb4201 commit e749ee7

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

crates/bevy_ui/src/focus.rs

+13-11
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,9 @@ pub fn ui_focus_system(
7272
Option<&CalculatedClip>,
7373
)>,
7474
) {
75-
let cursor_position = if let Some(cursor_position) = windows
75+
let cursor_position = windows
7676
.get_primary()
77-
.and_then(|window| window.cursor_position())
78-
{
79-
cursor_position
80-
} else {
81-
return;
82-
};
77+
.and_then(|window| window.cursor_position());
8378

8479
// reset entities that were both clicked and released in the last frame
8580
for entity in state.entities_to_reset.drain(..) {
@@ -120,13 +115,20 @@ pub fn ui_focus_system(
120115
}
121116
// if the current cursor position is within the bounds of the node, consider it for
122117
// clicking
123-
if (min.x..max.x).contains(&cursor_position.x)
124-
&& (min.y..max.y).contains(&cursor_position.y)
125-
{
118+
let contains_cursor = if let Some(cursor_position) = cursor_position {
119+
(min.x..max.x).contains(&cursor_position.x)
120+
&& (min.y..max.y).contains(&cursor_position.y)
121+
} else {
122+
false
123+
};
124+
125+
if contains_cursor {
126126
Some((entity, focus_policy, interaction, FloatOrd(position.z)))
127127
} else {
128128
if let Some(mut interaction) = interaction {
129-
if *interaction == Interaction::Hovered {
129+
if *interaction == Interaction::Hovered
130+
|| (cursor_position.is_none() && *interaction != Interaction::None)
131+
{
130132
*interaction = Interaction::None;
131133
}
132134
}

0 commit comments

Comments
 (0)