Skip to content

Commit

Permalink
Fix ui interactions when cursor disappears suddenly (#3926)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
cart committed Feb 13, 2022
1 parent 5bb4201 commit e749ee7
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions crates/bevy_ui/src/focus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,9 @@ pub fn ui_focus_system(
Option<&CalculatedClip>,
)>,
) {
let cursor_position = if let Some(cursor_position) = windows
let cursor_position = windows
.get_primary()
.and_then(|window| window.cursor_position())
{
cursor_position
} else {
return;
};
.and_then(|window| window.cursor_position());

// reset entities that were both clicked and released in the last frame
for entity in state.entities_to_reset.drain(..) {
Expand Down Expand Up @@ -120,13 +115,20 @@ pub fn ui_focus_system(
}
// if the current cursor position is within the bounds of the node, consider it for
// clicking
if (min.x..max.x).contains(&cursor_position.x)
&& (min.y..max.y).contains(&cursor_position.y)
{
let contains_cursor = if let Some(cursor_position) = cursor_position {
(min.x..max.x).contains(&cursor_position.x)
&& (min.y..max.y).contains(&cursor_position.y)
} else {
false
};

if contains_cursor {
Some((entity, focus_policy, interaction, FloatOrd(position.z)))
} else {
if let Some(mut interaction) = interaction {
if *interaction == Interaction::Hovered {
if *interaction == Interaction::Hovered
|| (cursor_position.is_none() && *interaction != Interaction::None)
{
*interaction = Interaction::None;
}
}
Expand Down

0 comments on commit e749ee7

Please sign in to comment.