libservo: Allow embedders to signal when the cursor has left the WebView (#37317)

Currently, the hover state will stay when the mouse moves out of the
webview, this PR fixes it

Testing: Hover on the `About` on servo.org, and then move the mouse up
to the browser UI, see the hover state resets

Signed-off-by: Tony <legendmastertony@gmail.com>
This commit is contained in:
Tony 2025-06-18 19:59:11 +08:00 committed by GitHub
parent 0896341285
commit b9fcc95992
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 125 additions and 27 deletions

View file

@ -2113,6 +2113,49 @@ impl Document {
}
}
#[allow(unsafe_code)]
pub(crate) fn handle_mouse_leave_event(
&self,
hit_test_result: Option<CompositorHitTestResult>,
pressed_mouse_buttons: u16,
can_gc: CanGc,
) {
// Ignore all incoming events without a hit test.
let Some(hit_test_result) = hit_test_result else {
return;
};
self.window()
.send_to_embedder(EmbedderMsg::Status(self.webview_id(), None));
let node = unsafe { node::from_untrusted_node_address(hit_test_result.node) };
for element in node
.inclusive_ancestors(ShadowIncluding::No)
.filter_map(DomRoot::downcast::<Element>)
{
element.set_hover_state(false);
element.set_active_state(false);
}
self.fire_mouse_event(
hit_test_result.point_in_viewport,
node.upcast(),
FireMouseEventType::Out,
EventBubbles::Bubbles,
EventCancelable::Cancelable,
pressed_mouse_buttons,
can_gc,
);
self.handle_mouse_enter_leave_event(
hit_test_result.point_in_viewport,
FireMouseEventType::Leave,
None,
node,
pressed_mouse_buttons,
can_gc,
);
}
fn handle_mouse_enter_leave_event(
&self,
client_point: Point2D<f32>,

View file

@ -1127,6 +1127,14 @@ impl ScriptThread {
can_gc,
);
},
InputEvent::MouseLeave(_) => {
self.topmost_mouse_over_target.take();
document.handle_mouse_leave_event(
event.hit_test_result,
event.pressed_mouse_buttons,
can_gc,
);
},
InputEvent::Touch(touch_event) => {
let touch_result =
document.handle_touch_event(touch_event, event.hit_test_result, can_gc);