From a129e64d772e012504fa01c9e74566765384becf Mon Sep 17 00:00:00 2001 From: Jonathan Johnson Date: Wed, 9 Oct 2024 08:21:01 -0700 Subject: [PATCH] Fixed cursor hover events Broken in 4f3ef7d9edfa7c614574d9b1b7d819cd0a292b02. Thanks to @pepone42 for reporting this. --- src/context.rs | 12 +++++++++--- src/window.rs | 13 +++++++++++-- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/src/context.rs b/src/context.rs index 2334fe1..30b06de 100644 --- a/src/context.rs +++ b/src/context.rs @@ -21,7 +21,9 @@ use crate::styles::{ComponentDefinition, FontFamilyList, Styles, Theme, ThemePai use crate::tree::Tree; use crate::value::{IntoValue, Source, Value}; use crate::widget::{EventHandling, MountedWidget, RootBehavior, WidgetId, WidgetInstance}; -use crate::window::{CursorState, DeviceId, KeyEvent, PlatformWindow, ThemeMode}; +use crate::window::{ + CursorState, DeviceId, KeyEvent, PlatformWindow, ThemeMode, WidgetCursorState, +}; use crate::ConstraintLimit; /// A context to an event function. @@ -316,13 +318,17 @@ impl<'context> EventContext<'context> { continue; }; let relative = location - widget_layout.origin; + let widget_hover = Some(WidgetCursorState { + id: widget.id(), + last_hovered: location, + }); if widget_context.hit_test(relative) { - if current_hover != Some(widget.id()) { + if current_hover != widget_hover { widget_context.hover(location); } drop(widget_context); - self.cursor.widget = Some(widget.id()); + self.cursor.widget = widget_hover; break; } } diff --git a/src/window.rs b/src/window.rs index 246938e..c99f2cc 100644 --- a/src/window.rs +++ b/src/window.rs @@ -2378,7 +2378,10 @@ where if let (ElementState::Pressed, Some(location), Some(hovered)) = ( state, self.cursor.location, - self.cursor.widget.and_then(|id| self.tree.widget(id)), + self.cursor + .widget + .as_ref() + .and_then(|hover| self.tree.widget(hover.id)), ) { if let Some(handler) = recursively_handle_event( &mut EventContext::new( @@ -2943,7 +2946,13 @@ fn recursively_handle_event( #[derive(Default)] pub(crate) struct CursorState { pub(crate) location: Option>, - pub(crate) widget: Option, + pub(crate) widget: Option, +} + +#[derive(Eq, PartialEq)] +pub(crate) struct WidgetCursorState { + pub(crate) id: WidgetId, + pub(crate) last_hovered: Point, } pub(crate) mod sealed {