script/compositor: Send mouseleave events when cursor moves between <iframe>s (#38539)

Properly send `mouseleave` events when the cursor moves between
`<iframe>`s. This allows a better handling of cursor changes and status
text updates. Specifically, we do not need to continuously update the
cursor and the value can be cached in the `Document`. In addition,
status updates can now be sent properly when moving focus between
`<iframe>`s.

Note that style updates for `:hover` values are still broken, but less
so than before. Now the hover state on the `Node` is updated, but for
some
reason the restyle isn't taking place properly. This maintains the
status quo as far as behavior goes when hover moves between `<iframe>`s.

This change also adds a helper data structure to `Document` which will
eventually be responsible for event handling.

Testing: Cursor and status change are currently very hard to test as
the API test harness makes this difficult at the moment.

Signed-off-by: Martin Robinson <mrobinson@igalia.com>
Co-authored-by: Oriol Brufau <obrufau@igalia.com>
This commit is contained in:
Martin Robinson 2025-08-11 14:31:54 +02:00 committed by GitHub
parent 82ca2b92cd
commit b75c3feb97
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 307 additions and 210 deletions

View file

@ -30,8 +30,8 @@ use dom_struct::dom_struct;
use embedder_traits::{
AllowOrDeny, AnimationState, ContextMenuResult, Cursor, EditingActionEvent, EmbedderMsg,
FocusSequenceNumber, ImeEvent, InputEvent, LoadStatus, MouseButton, MouseButtonAction,
MouseButtonEvent, ScrollEvent, TouchEvent, TouchEventType, TouchId, UntrustedNodeAddress,
WheelEvent,
MouseButtonEvent, MouseLeaveEvent, ScrollEvent, TouchEvent, TouchEventType, TouchId,
UntrustedNodeAddress, WheelEvent,
};
use encoding_rs::{Encoding, UTF_8};
use euclid::Point2D;
@ -141,6 +141,7 @@ use crate::dom::cssstylesheet::CSSStyleSheet;
use crate::dom::customelementregistry::CustomElementDefinition;
use crate::dom::customevent::CustomEvent;
use crate::dom::datatransfer::DataTransfer;
use crate::dom::document_event_handler::DocumentEventHandler;
use crate::dom::documentfragment::DocumentFragment;
use crate::dom::documentorshadowroot::{
DocumentOrShadowRoot, ServoStylesheetInDocument, StylesheetSource,
@ -322,6 +323,8 @@ pub(crate) struct Document {
#[ignore_malloc_size_of = "defined in selectors"]
#[no_trace]
quirks_mode: Cell<QuirksMode>,
/// A helper used to process and store data related to input event handling.
event_handler: DocumentEventHandler,
/// Caches for the getElement methods
id_map: DomRefCell<HashMapTracedValues<Atom, Vec<Dom<Element>>>>,
name_map: DomRefCell<HashMapTracedValues<Atom, Vec<Dom<Element>>>>,
@ -2001,7 +2004,6 @@ impl Document {
pub(crate) unsafe fn handle_mouse_move_event(
&self,
input_event: &ConstellationInputEvent,
prev_mouse_over_target: &MutNullableDom<Element>,
can_gc: CanGc,
) {
// Ignore all incoming events without a hit test.
@ -2021,7 +2023,9 @@ impl Document {
return;
};
let target_has_changed = prev_mouse_over_target
let target_has_changed = self
.event_handler
.current_hover_target
.get()
.as_ref()
.is_none_or(|old_target| old_target != &new_target);
@ -2030,7 +2034,7 @@ impl Document {
// dispatch mouseout to the previous one, mouseover to the new one.
if target_has_changed {
// Dispatch mouseout and mouseleave to previous target.
if let Some(old_target) = prev_mouse_over_target.get() {
if let Some(old_target) = self.event_handler.current_hover_target.get() {
let old_target_is_ancestor_of_new_target = old_target
.upcast::<Node>()
.is_ancestor_of(new_target.upcast::<Node>());
@ -2078,9 +2082,6 @@ impl Document {
.inclusive_ancestors(ShadowIncluding::No)
.filter_map(DomRoot::downcast::<Element>)
{
if element.hover_state() {
break;
}
element.set_hover_state(true);
}
@ -2094,7 +2095,9 @@ impl Document {
can_gc,
);
let moving_from = prev_mouse_over_target
let moving_from = self
.event_handler
.current_hover_target
.get()
.map(|old_target| DomRoot::from_ref(old_target.upcast::<Node>()));
let event_target = DomRoot::from_ref(new_target.upcast::<Node>());
@ -2120,56 +2123,107 @@ impl Document {
can_gc,
);
// If the target has changed then store the current mouse over target for next frame.
if target_has_changed {
prev_mouse_over_target.set(Some(&new_target));
}
self.update_current_hover_target_and_status(Some(new_target));
}
pub(crate) fn set_cursor(&self, cursor: Cursor) {
self.send_to_embedder(EmbedderMsg::SetCursor(self.webview_id(), cursor));
fn update_current_hover_target_and_status(&self, new_hover_target: Option<DomRoot<Element>>) {
let previous_hover_target = self.event_handler.current_hover_target.get();
if previous_hover_target == new_hover_target {
return;
}
self.event_handler
.current_hover_target
.set(new_hover_target.as_deref());
// If the new hover target is an anchor with a status value, inform the embedder
// of the new value.
let window = self.window();
if let Some(anchor) = new_hover_target.and_then(|new_hover_target| {
new_hover_target
.upcast::<Node>()
.inclusive_ancestors(ShadowIncluding::No)
.filter_map(DomRoot::downcast::<HTMLAnchorElement>)
.next()
}) {
let status = anchor
.upcast::<Element>()
.get_attribute(&ns!(), &local_name!("href"))
.and_then(|href| {
let value = href.value();
let url = self.url();
url.join(&value).map(|url| url.to_string()).ok()
});
window.send_to_embedder(EmbedderMsg::Status(self.webview_id(), status));
return;
}
// No state was set above, which means that the new value of the status in the embedder
// should be `None`. Set that now. If `previous_hover_target` is `None` that means this
// is the first mouse move event we are seeing after getting the cursor. In that case,
// we also clear the status.
if previous_hover_target.is_none_or(|previous_hover_target| {
previous_hover_target
.upcast::<Node>()
.inclusive_ancestors(ShadowIncluding::No)
.filter_map(DomRoot::downcast::<HTMLAnchorElement>)
.next()
.is_some()
}) {
window.send_to_embedder(EmbedderMsg::Status(window.webview_id(), None));
}
}
#[allow(unsafe_code)]
pub(crate) fn handle_mouse_leave_event(
&self,
input_event: &ConstellationInputEvent,
mouse_leave_event: &MouseLeaveEvent,
can_gc: CanGc,
) {
// Ignore all incoming events without a hit test.
let Some(hit_test_result) = self.window.hit_test_from_input_event(input_event) else {
return;
};
if let Some(current_hover_target) = self.event_handler.current_hover_target.get() {
let current_hover_target = current_hover_target.upcast::<Node>();
for element in current_hover_target
.inclusive_ancestors(ShadowIncluding::No)
.filter_map(DomRoot::downcast::<Element>)
{
element.set_hover_state(false);
element.set_active_state(false);
}
self.window()
.send_to_embedder(EmbedderMsg::Status(self.webview_id(), None));
for element in hit_test_result
.node
.inclusive_ancestors(ShadowIncluding::No)
.filter_map(DomRoot::downcast::<Element>)
{
element.set_hover_state(false);
element.set_active_state(false);
if let Some(hit_test_result) = self
.window()
.hit_test_from_point_in_viewport(self.event_handler.most_recent_mousemove_point)
{
self.fire_mouse_event(
current_hover_target.upcast(),
FireMouseEventType::Out,
EventBubbles::Bubbles,
EventCancelable::Cancelable,
&hit_test_result,
input_event,
can_gc,
);
self.handle_mouse_enter_leave_event(
DomRoot::from_ref(current_hover_target),
None,
FireMouseEventType::Leave,
&hit_test_result,
input_event,
can_gc,
);
}
}
self.fire_mouse_event(
hit_test_result.node.upcast(),
FireMouseEventType::Out,
EventBubbles::Bubbles,
EventCancelable::Cancelable,
&hit_test_result,
input_event,
can_gc,
);
self.handle_mouse_enter_leave_event(
hit_test_result.node.clone(),
None,
FireMouseEventType::Leave,
&hit_test_result,
input_event,
can_gc,
);
self.event_handler.current_cursor.set(None);
self.event_handler.current_hover_target.set(None);
// If focus is moving to another frame, it will decide what the new status text is, but if
// this mouse leave event is leaving the WebView entirely, then clear it.
if !mouse_leave_event.focus_moving_to_another_iframe {
self.window()
.send_to_embedder(EmbedderMsg::Status(self.webview_id(), None));
}
}
fn handle_mouse_enter_leave_event(
@ -4123,6 +4177,14 @@ impl Document {
Ok(())
}
pub(crate) fn set_cursor(&self, cursor: Cursor) {
if Some(cursor) == self.event_handler.current_cursor.get() {
return;
}
self.event_handler.current_cursor.set(Some(cursor));
self.send_to_embedder(EmbedderMsg::SetCursor(self.webview_id(), cursor));
}
}
fn is_character_value_key(key: &Key) -> bool {
@ -4321,6 +4383,7 @@ impl Document {
url: DomRefCell::new(url),
// https://dom.spec.whatwg.org/#concept-document-quirks
quirks_mode: Cell::new(QuirksMode::NoQuirks),
event_handler: DocumentEventHandler::default(),
id_map: DomRefCell::new(HashMapTracedValues::new()),
name_map: DomRefCell::new(HashMapTracedValues::new()),
// https://dom.spec.whatwg.org/#concept-document-encoding
@ -4821,14 +4884,14 @@ impl Document {
})
}
pub(crate) fn element_state_will_change(&self, el: &Element) {
let mut entry = self.ensure_pending_restyle(el);
pub(crate) fn element_state_will_change(&self, element: &Element) {
let mut entry = self.ensure_pending_restyle(element);
if entry.snapshot.is_none() {
entry.snapshot = Some(Snapshot::new());
}
let snapshot = entry.snapshot.as_mut().unwrap();
if snapshot.state.is_none() {
snapshot.state = Some(el.state());
snapshot.state = Some(element.state());
}
}

View file

@ -0,0 +1,29 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
use std::cell::Cell;
use embedder_traits::Cursor;
use euclid::Point2D;
use style_traits::CSSPixel;
use crate::dom::bindings::root::MutNullableDom;
use crate::dom::types::Element;
/// The [`DocumentEventHandler`] is a structure responsible for handling events for
/// the [`crate::Document`] and storing data related to event handling. It exists to
/// decrease the size of the [`crate::Document`] structure.
#[derive(Default, JSTraceable, MallocSizeOf)]
#[cfg_attr(crown, crown::unrooted_must_root_lint::must_root)]
pub(crate) struct DocumentEventHandler {
/// The element that is currently hovered by the cursor.
pub(crate) current_hover_target: MutNullableDom<Element>,
/// The most recent mouse movement point, used for processing `mouseleave` events.
#[no_trace]
pub(crate) most_recent_mousemove_point: Point2D<f32, CSSPixel>,
/// The currently set [`Cursor`] or `None` if the `Document` isn't being hovered
/// by the cursor.
#[no_trace]
pub(crate) current_cursor: Cell<Option<Cursor>>,
}

View file

@ -5425,8 +5425,8 @@ impl Element {
}
pub(crate) fn set_focus_state(&self, value: bool) {
self.set_state(ElementState::FOCUS, value);
self.upcast::<Node>().dirty(NodeDamage::Other);
self.set_state(ElementState::FOCUS, value);
}
pub(crate) fn hover_state(&self) -> bool {

View file

@ -298,6 +298,7 @@ pub(crate) mod dissimilaroriginlocation;
pub(crate) mod dissimilaroriginwindow;
#[allow(dead_code)]
pub(crate) mod document;
pub(crate) mod document_event_handler;
pub(crate) mod documentfragment;
pub(crate) mod documentorshadowroot;
pub(crate) mod documenttype;

View file

@ -2599,21 +2599,25 @@ impl Window {
self.layout().query_elements_from_point(point, flags)
}
#[allow(unsafe_code)]
pub(crate) fn hit_test_from_input_event(
&self,
input_event: &ConstellationInputEvent,
) -> Option<HitTestResult> {
let compositor_hit_test_result = input_event.hit_test_result.as_ref()?;
self.hit_test_from_point_in_viewport(
input_event.hit_test_result.as_ref()?.point_in_viewport,
)
}
#[allow(unsafe_code)]
pub(crate) fn hit_test_from_point_in_viewport(
&self,
point_in_frame: Point2D<f32, CSSPixel>,
) -> Option<HitTestResult> {
let result = self
.elements_from_point_query(
compositor_hit_test_result.point_in_viewport.cast_unit(),
ElementsFromPointFlags::empty(),
)
.elements_from_point_query(point_in_frame.cast_unit(), ElementsFromPointFlags::empty())
.into_iter()
.nth(0)?;
let point_in_frame = compositor_hit_test_result.point_in_viewport;
let point_relative_to_initial_containing_block =
point_in_frame + self.scroll_offset().cast_unit();

View file

@ -49,15 +49,14 @@ use devtools_traits::{
};
use embedder_traits::user_content_manager::UserContentManager;
use embedder_traits::{
EmbedderMsg, FocusSequenceNumber, InputEvent, JavaScriptEvaluationError,
JavaScriptEvaluationId, MediaSessionActionType, MouseButton, MouseButtonAction,
MouseButtonEvent, Theme, ViewportDetails, WebDriverScriptCommand,
FocusSequenceNumber, InputEvent, JavaScriptEvaluationError, JavaScriptEvaluationId,
MediaSessionActionType, MouseButton, MouseButtonAction, MouseButtonEvent, Theme,
ViewportDetails, WebDriverScriptCommand,
};
use euclid::Point2D;
use euclid::default::Rect;
use fonts::{FontContext, SystemFontServiceProxy};
use headers::{HeaderMapExt, LastModified, ReferrerPolicy as ReferrerPolicyHeader};
use html5ever::{local_name, ns};
use http::header::REFRESH;
use hyper_serde::Serde;
use ipc_channel::ipc;
@ -115,9 +114,7 @@ use crate::dom::bindings::conversions::{
use crate::dom::bindings::inheritance::Castable;
use crate::dom::bindings::refcounted::Trusted;
use crate::dom::bindings::reflector::DomGlobal;
use crate::dom::bindings::root::{
Dom, DomRoot, MutNullableDom, RootCollection, ThreadLocalStackRoots,
};
use crate::dom::bindings::root::{Dom, DomRoot, RootCollection, ThreadLocalStackRoots};
use crate::dom::bindings::settings_stack::AutoEntryScript;
use crate::dom::bindings::str::DOMString;
use crate::dom::bindings::trace::{HashMapTracedValues, JSTraceable};
@ -130,11 +127,10 @@ use crate::dom::document::{
};
use crate::dom::element::Element;
use crate::dom::globalscope::GlobalScope;
use crate::dom::htmlanchorelement::HTMLAnchorElement;
use crate::dom::htmliframeelement::HTMLIFrameElement;
use crate::dom::htmlslotelement::HTMLSlotElement;
use crate::dom::mutationobserver::MutationObserver;
use crate::dom::node::{Node, NodeTraits, ShadowIncluding};
use crate::dom::node::NodeTraits;
use crate::dom::servoparser::{ParserContext, ServoParser};
use crate::dom::types::DebuggerGlobalScope;
#[cfg(feature = "webgpu")]
@ -253,9 +249,6 @@ pub struct ScriptThread {
/// The JavaScript runtime.
js_runtime: Rc<Runtime>,
/// The topmost element over the mouse.
topmost_mouse_over_target: MutNullableDom<Element>,
/// List of pipelines that have been owned and closed by this script thread.
#[no_trace]
closed_pipelines: DomRefCell<HashSet<PipelineId>>,
@ -978,7 +971,6 @@ impl ScriptThread {
timer_scheduler: Default::default(),
microtask_queue,
js_runtime,
topmost_mouse_over_target: MutNullableDom::new(Default::default()),
closed_pipelines: DomRefCell::new(HashSet::new()),
mutation_observer_microtask_queued: Default::default(),
mutation_observers: Default::default(),
@ -1053,59 +1045,7 @@ impl ScriptThread {
input_event: &ConstellationInputEvent,
can_gc: CanGc,
) {
// Get the previous target temporarily
let prev_mouse_over_target = self.topmost_mouse_over_target.get();
unsafe {
document.handle_mouse_move_event(input_event, &self.topmost_mouse_over_target, can_gc)
}
// Short-circuit if nothing changed
if self.topmost_mouse_over_target.get() == prev_mouse_over_target {
return;
}
let mut state_already_changed = false;
// Notify Constellation about the topmost anchor mouse over target.
let window = document.window();
if let Some(target) = self.topmost_mouse_over_target.get() {
if let Some(anchor) = target
.upcast::<Node>()
.inclusive_ancestors(ShadowIncluding::No)
.filter_map(DomRoot::downcast::<HTMLAnchorElement>)
.next()
{
let status = anchor
.upcast::<Element>()
.get_attribute(&ns!(), &local_name!("href"))
.and_then(|href| {
let value = href.value();
let url = document.url();
url.join(&value).map(|url| url.to_string()).ok()
});
let event = EmbedderMsg::Status(document.webview_id(), status);
window.send_to_embedder(event);
state_already_changed = true;
}
}
// We might have to reset the anchor state
if !state_already_changed {
if let Some(target) = prev_mouse_over_target {
if target
.upcast::<Node>()
.inclusive_ancestors(ShadowIncluding::No)
.filter_map(DomRoot::downcast::<HTMLAnchorElement>)
.next()
.is_some()
{
let event = EmbedderMsg::Status(window.webview_id(), None);
window.send_to_embedder(event);
}
}
}
unsafe { document.handle_mouse_move_event(input_event, can_gc) }
}
/// Process compositor events as part of a "update the rendering task".
@ -1131,12 +1071,10 @@ impl ScriptThread {
document.handle_mouse_button_event(mouse_button_event, &event, can_gc);
},
InputEvent::MouseMove(_) => {
// The event itself is unecessary here, because the point in the viewport is in the hit test.
self.process_mouse_move_event(&document, &event, can_gc);
},
InputEvent::MouseLeave(_) => {
self.topmost_mouse_over_target.take();
document.handle_mouse_leave_event(&event, can_gc);
InputEvent::MouseLeave(mouse_leave_event) => {
document.handle_mouse_leave_event(&event, &mouse_leave_event, can_gc);
},
InputEvent::Touch(touch_event) => {
let touch_result = document.handle_touch_event(touch_event, &event, can_gc);
@ -3062,13 +3000,6 @@ impl ScriptThread {
debug!("{id}: Clearing animations");
document.animations().clear();
// We don't want to dispatch `mouseout` event pointing to non-existing element
if let Some(target) = self.topmost_mouse_over_target.get() {
if target.upcast::<Node>().owner_doc() == document {
self.topmost_mouse_over_target.set(None);
}
}
// We discard the browsing context after requesting layout shut down,
// to avoid running layout on detached iframes.
let window = document.window();