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

@ -2939,34 +2939,8 @@ where
}
}
let pipeline_id = match &hit_test_result {
Some(hit_test) => hit_test.pipeline_id,
None => {
// If there's no hit test, send to the focused browsing context of the given webview.
let Some(browsing_context_id) = self
.webviews
.get(webview_id)
.map(|webview| webview.focused_browsing_context_id)
else {
warn!("Handling InputEvent for an unknown webview: {webview_id}");
return;
};
let Some(pipeline_id) = self
.browsing_contexts
.get(&browsing_context_id)
.map(|context| context.pipeline_id)
else {
warn!("{browsing_context_id}: Got InputEvent for nonexistent browsing context");
return;
};
pipeline_id
},
};
let Some(pipeline) = self.pipelines.get(&pipeline_id) else {
debug!("Got event for pipeline ({pipeline_id}) after closure");
let Some(webview) = self.webviews.get_mut(webview_id) else {
warn!("Got input event for unknown WebViewId: {webview_id:?}");
return;
};
@ -2976,13 +2950,7 @@ where
active_keyboard_modifiers,
event,
};
if let Err(error) = pipeline
.event_loop
.send(ScriptThreadMessage::SendInputEvent(pipeline_id, event))
{
self.handle_send_error(pipeline_id, error);
}
webview.forward_input_event(event, &self.pipelines, &self.browsing_contexts);
}
#[servo_tracing::instrument(skip_all)]

View file

@ -2,9 +2,17 @@
* 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 base::id::BrowsingContextId;
use embedder_traits::Theme;
use std::collections::HashMap;
use base::id::{BrowsingContextId, PipelineId};
use embedder_traits::{InputEvent, MouseLeaveEvent, Theme};
use euclid::Point2D;
use log::warn;
use script_traits::{ConstellationInputEvent, ScriptThreadMessage};
use style_traits::CSSPixel;
use crate::browsingcontext::BrowsingContext;
use crate::pipeline::Pipeline;
use crate::session_history::JointSessionHistory;
/// The `Constellation`'s view of a `WebView` in the embedding layer. This tracks all of the
@ -15,6 +23,14 @@ pub(crate) struct ConstellationWebView {
/// context.
pub focused_browsing_context_id: BrowsingContextId,
/// The [`BrowsingContextId`] of the currently hovered browsing context, to use for
/// knowing which frame is currently receiving cursor events.
pub hovered_browsing_context_id: Option<BrowsingContextId>,
/// The last mouse move point in the coordinate space of the Pipeline that it
/// happened int.
pub last_mouse_move_point: Point2D<f32, CSSPixel>,
/// The joint session history for this webview.
pub session_history: JointSessionHistory,
@ -27,6 +43,8 @@ impl ConstellationWebView {
pub(crate) fn new(focused_browsing_context_id: BrowsingContextId) -> Self {
Self {
focused_browsing_context_id,
hovered_browsing_context_id: None,
last_mouse_move_point: Default::default(),
session_history: JointSessionHistory::new(),
theme: Theme::Light,
}
@ -42,4 +60,91 @@ impl ConstellationWebView {
pub(crate) fn theme(&self) -> Theme {
self.theme
}
fn target_pipeline_id_for_input_event(
&self,
event: &ConstellationInputEvent,
browsing_contexts: &HashMap<BrowsingContextId, BrowsingContext>,
) -> Option<PipelineId> {
if let Some(hit_test_result) = &event.hit_test_result {
return Some(hit_test_result.pipeline_id);
}
// If there's no hit test, send the event to either the hovered or focused browsing context,
// depending on the event type.
let browsing_context_id = if matches!(event.event, InputEvent::MouseLeave(_)) {
self.hovered_browsing_context_id
.unwrap_or(self.focused_browsing_context_id)
} else {
self.focused_browsing_context_id
};
Some(browsing_contexts.get(&browsing_context_id)?.pipeline_id)
}
pub(crate) fn forward_input_event(
&mut self,
event: ConstellationInputEvent,
pipelines: &HashMap<PipelineId, Pipeline>,
browsing_contexts: &HashMap<BrowsingContextId, BrowsingContext>,
) {
let Some(pipeline_id) = self.target_pipeline_id_for_input_event(&event, browsing_contexts)
else {
warn!("Unknown pipeline for input event. Ignoring.");
return;
};
let Some(pipeline) = pipelines.get(&pipeline_id) else {
warn!("Unknown pipeline id {pipeline_id:?} for input event. Ignoring.");
return;
};
let mut update_hovered_browsing_context = |newly_hovered_browsing_context_id| {
let old_hovered_context_id = std::mem::replace(
&mut self.hovered_browsing_context_id,
newly_hovered_browsing_context_id,
);
if old_hovered_context_id == newly_hovered_browsing_context_id {
return;
}
let Some(old_hovered_context_id) = old_hovered_context_id else {
return;
};
let Some(pipeline) = browsing_contexts
.get(&old_hovered_context_id)
.and_then(|browsing_context| pipelines.get(&browsing_context.pipeline_id))
else {
return;
};
let mut synthetic_mouse_leave_event = event.clone();
synthetic_mouse_leave_event.event = InputEvent::MouseLeave(MouseLeaveEvent {
focus_moving_to_another_iframe: true,
});
let _ = pipeline
.event_loop
.send(ScriptThreadMessage::SendInputEvent(
pipeline.id,
synthetic_mouse_leave_event,
));
};
if let InputEvent::MouseLeave(_) = &event.event {
update_hovered_browsing_context(None);
return;
}
if let InputEvent::MouseMove(_) = &event.event {
update_hovered_browsing_context(Some(pipeline.browsing_context_id));
self.last_mouse_move_point = event
.hit_test_result
.as_ref()
.expect("MouseMove events should always have hit tests.")
.point_in_viewport;
}
let _ = pipeline
.event_loop
.send(ScriptThreadMessage::SendInputEvent(pipeline.id, event));
}
}