use embedder_traits::user_contents::UserContentManagerId;
use embedder_traits::{GenericEmbedderProxy, InputEvent, MouseLeftViewportEvent, Theme};
use euclid::Point2D;
use log::warn;
use rustc_hash::FxHashMap;
use script_traits::{ConstellationInputEvent, ScriptThreadMessage};
use servo_base::id::{BrowsingContextId, PipelineId, WebViewId};
use style_traits::CSSPixel;
use super::embedder::ConstellationToEmbedderMsg;
use crate::browsingcontext::BrowsingContext;
use crate::pipeline::Pipeline;
use crate::session_history::JointSessionHistory;
pub(crate) struct ConstellationWebView {
webview_id: WebViewId,
pub active_top_level_pipeline_id: PipelineId,
pub focused_browsing_context_id: BrowsingContextId,
pub hovered_browsing_context_id: Option<BrowsingContextId>,
pub last_mouse_move_point: Point2D<f32, CSSPixel>,
pub session_history: JointSessionHistory,
pub user_content_manager_id: Option<UserContentManagerId>,
theme: Theme,
pub accessibility_active: bool,
}
impl ConstellationWebView {
pub(crate) fn new(
embedder_proxy: &GenericEmbedderProxy<ConstellationToEmbedderMsg>,
webview_id: WebViewId,
active_top_level_pipeline_id: PipelineId,
focused_browsing_context_id: BrowsingContextId,
user_content_manager_id: Option<UserContentManagerId>,
) -> Self {
embedder_proxy.send(ConstellationToEmbedderMsg::AccessibilityTreeIdChanged(
webview_id,
active_top_level_pipeline_id.into(),
));
Self {
webview_id,
user_content_manager_id,
active_top_level_pipeline_id,
focused_browsing_context_id,
hovered_browsing_context_id: None,
last_mouse_move_point: Default::default(),
session_history: JointSessionHistory::new(),
theme: Theme::Light,
accessibility_active: false,
}
}
pub(crate) fn set_theme(&mut self, new_theme: Theme) -> bool {
let old_theme = std::mem::replace(&mut self.theme, new_theme);
old_theme != self.theme
}
pub(crate) fn theme(&self) -> Theme {
self.theme
}
fn target_pipeline_id_for_input_event(
&self,
event: &ConstellationInputEvent,
browsing_contexts: &FxHashMap<BrowsingContextId, BrowsingContext>,
) -> Option<PipelineId> {
if let Some(hit_test_result) = &event.hit_test_result {
return Some(hit_test_result.pipeline_id);
}
let browsing_context_id = if matches!(event.event.event, InputEvent::MouseLeftViewport(_)) {
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: &FxHashMap<PipelineId, Pipeline>,
browsing_contexts: &FxHashMap<BrowsingContextId, BrowsingContext>,
) -> bool {
let Some(pipeline_id) = self.target_pipeline_id_for_input_event(&event, browsing_contexts)
else {
warn!("Unknown pipeline for input event. Ignoring.");
return false;
};
let Some(pipeline) = pipelines.get(&pipeline_id) else {
warn!("Unknown pipeline id {pipeline_id:?} for input event. Ignoring.");
return false;
};
let mut update_hovered_browsing_context =
|newly_hovered_browsing_context_id, focus_moving_to_another_iframe: bool| {
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.event =
InputEvent::MouseLeftViewport(MouseLeftViewportEvent {
focus_moving_to_another_iframe,
});
let _ = pipeline
.event_loop
.send(ScriptThreadMessage::SendInputEvent(
self.webview_id,
pipeline.id,
synthetic_mouse_leave_event,
));
};
if let InputEvent::MouseLeftViewport(_) = &event.event.event {
update_hovered_browsing_context(None, false);
return true;
}
if let InputEvent::MouseMove(_) = &event.event.event {
update_hovered_browsing_context(Some(pipeline.browsing_context_id), true);
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(
self.webview_id,
pipeline.id,
event,
));
true
}
}