libservo: Make zooming and HiDPI scaling work per-WebView (#36419)

libservo: Make zooming and HiDPI scaling work per-`WebView`

This change moves all zooming and HiDPI scaling to work per-`WebView` in
both libservo and Compositor. This means that you can pinch zoom one
`WebView` and it should now work independently of other `WebView`s.
This is accomplished by making each `WebView` in the WebRender scene
have its own scaling reference frame.

All WebViews are now expected to manage their HiDPI scaling factor and
this can be set independently of other WebViews. Perhaps in the future
this will become a Servo-wide setting.

This allows full removal of the `WindowMethods` trait from Servo.

Testing: There are not yet any tests for the WebView API, but I hope
to add those soon.

Co-authored-by: Shubham Gupta <shubham13297@gmail.com>
Signed-off-by: Martin Robinson <mrobinson@igalia.com>

Signed-off-by: Martin Robinson <mrobinson@igalia.com>
Co-authored-by: Shubham Gupta <shubham13297@gmail.com>
This commit is contained in:
Martin Robinson 2025-04-14 14:01:49 +02:00 committed by GitHub
parent f1417c4e75
commit c6dc7c83a8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
17 changed files with 415 additions and 385 deletions

View file

@ -14,7 +14,7 @@ use euclid::{Angle, Length, Point2D, Rotation3D, Scale, Size2D, UnknownUnit, Vec
use keyboard_types::{Modifiers, ShortcutMatcher};
use log::{debug, info};
use raw_window_handle::{HasDisplayHandle, HasWindowHandle, RawWindowHandle};
use servo::compositing::windowing::{WebRenderDebugOption, WindowMethods};
use servo::compositing::windowing::WebRenderDebugOption;
use servo::servo_config::pref;
use servo::servo_geometry::DeviceIndependentPixel;
use servo::webrender_api::ScrollLocation;
@ -252,7 +252,7 @@ impl Window {
/// Helper function to handle a click
fn handle_mouse(&self, webview: &WebView, button: MouseButton, action: ElementState) {
let max_pixel_dist = 10.0 * self.hidpi_factor().get();
let max_pixel_dist = 10.0 * self.hidpi_scale_factor().get();
let mouse_button = match &button {
MouseButton::Left => ServoMouseButton::Left,
MouseButton::Right => ServoMouseButton::Right,
@ -443,8 +443,11 @@ impl Window {
impl WindowPortsMethods for Window {
fn screen_geometry(&self) -> ScreenGeometry {
let hidpi_factor = self.hidpi_factor();
let toolbar_size = Size2D::new(0.0, (self.toolbar_height.get() * self.hidpi_factor()).0);
let hidpi_factor = self.hidpi_scale_factor();
let toolbar_size = Size2D::new(
0.0,
(self.toolbar_height.get() * self.hidpi_scale_factor()).0,
);
let screen_size = self.screen_size.to_f32() * hidpi_factor;
let available_screen_size = screen_size - toolbar_size;
@ -462,18 +465,18 @@ impl WindowPortsMethods for Window {
}
}
fn device_hidpi_factor(&self) -> Scale<f32, DeviceIndependentPixel, DevicePixel> {
fn device_hidpi_scale_factor(&self) -> Scale<f32, DeviceIndependentPixel, DevicePixel> {
Scale::new(self.winit_window.scale_factor() as f32)
}
fn device_pixel_ratio_override(
&self,
) -> Option<Scale<f32, DeviceIndependentPixel, DevicePixel>> {
self.device_pixel_ratio_override.map(Scale::new)
fn hidpi_scale_factor(&self) -> Scale<f32, DeviceIndependentPixel, DevicePixel> {
self.device_pixel_ratio_override
.map(Scale::new)
.unwrap_or_else(|| self.device_hidpi_scale_factor())
}
fn page_height(&self) -> f32 {
let dpr = self.hidpi_factor();
let dpr = self.hidpi_scale_factor();
let size = self.winit_window.inner_size();
size.height as f32 * dpr.get()
}
@ -483,7 +486,7 @@ impl WindowPortsMethods for Window {
}
fn request_resize(&self, _: &WebView, size: DeviceIntSize) -> Option<DeviceIntSize> {
let toolbar_height = self.toolbar_height() * self.hidpi_factor();
let toolbar_height = self.toolbar_height() * self.hidpi_scale_factor();
let toolbar_height = toolbar_height.get().ceil() as i32;
let total_size = PhysicalSize::new(size.width, size.height + toolbar_height);
self.winit_window
@ -587,7 +590,7 @@ impl WindowPortsMethods for Window {
},
WindowEvent::CursorMoved { position, .. } => {
let mut point = winit_position_to_euclid_point(position).to_f32();
point.y -= (self.toolbar_height() * self.hidpi_factor()).0;
point.y -= (self.toolbar_height() * self.hidpi_scale_factor()).0;
self.webview_relative_mouse_point.set(point);
webview.notify_input_event(InputEvent::MouseMove(MouseMoveEvent { point }));
@ -598,7 +601,7 @@ impl WindowPortsMethods for Window {
(dx as f64, (dy * LINE_HEIGHT) as f64, WheelMode::DeltaLine)
},
MouseScrollDelta::PixelDelta(position) => {
let scale_factor = self.device_hidpi_factor().inverse().get() as f64;
let scale_factor = self.device_hidpi_scale_factor().inverse().get() as f64;
let position = position.to_logical(scale_factor);
(position.x, position.y, WheelMode::DeltaPixel)
},
@ -728,7 +731,7 @@ impl WindowPortsMethods for Window {
// this prevents a crash in the compositor due to invalid surface size
self.winit_window.set_min_inner_size(Some(PhysicalSize::new(
1.0,
1.0 + (self.toolbar_height() * self.hidpi_factor()).0,
1.0 + (self.toolbar_height() * self.hidpi_scale_factor()).0,
)));
}
@ -761,13 +764,6 @@ impl WindowPortsMethods for Window {
}
}
impl WindowMethods for Window {
fn hidpi_factor(&self) -> Scale<f32, DeviceIndependentPixel, DevicePixel> {
self.device_pixel_ratio_override()
.unwrap_or_else(|| self.device_hidpi_factor())
}
}
fn winit_phase_to_touch_event_type(phase: TouchPhase) -> TouchEventType {
match phase {
TouchPhase::Started => TouchEventType::Down,