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
This commit is contained in:
Carter Anderson 2022-02-13 01:49:34 +00:00
parent 523e7af739
commit 3ae96dd307

View File

@ -72,14 +72,9 @@ pub fn ui_focus_system(
Option<&CalculatedClip>, Option<&CalculatedClip>,
)>, )>,
) { ) {
let cursor_position = if let Some(cursor_position) = windows let cursor_position = windows
.get_primary() .get_primary()
.and_then(|window| window.cursor_position()) .and_then(|window| window.cursor_position());
{
cursor_position
} else {
return;
};
// reset entities that were both clicked and released in the last frame // reset entities that were both clicked and released in the last frame
for entity in state.entities_to_reset.drain(..) { for entity in state.entities_to_reset.drain(..) {
@ -120,13 +115,20 @@ pub fn ui_focus_system(
} }
// if the current cursor position is within the bounds of the node, consider it for // if the current cursor position is within the bounds of the node, consider it for
// clicking // clicking
if (min.x..max.x).contains(&cursor_position.x) 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) && (min.y..max.y).contains(&cursor_position.y)
{ } else {
false
};
if contains_cursor {
Some((entity, focus_policy, interaction, FloatOrd(position.z))) Some((entity, focus_policy, interaction, FloatOrd(position.z)))
} else { } else {
if let Some(mut interaction) = interaction { if let Some(mut interaction) = interaction {
if *interaction == Interaction::Hovered { if *interaction == Interaction::Hovered
|| (cursor_position.is_none() && *interaction != Interaction::None)
{
*interaction = Interaction::None; *interaction = Interaction::None;
} }
} }