Skip to content

Commit

Permalink
minibrowser: implement HiDPI support (#30343)
Browse files Browse the repository at this point in the history
  • Loading branch information
delan committed Sep 14, 2023
1 parent d22d97f commit bb1a6c2
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 67 deletions.
73 changes: 53 additions & 20 deletions ports/servoshell/app.rs
Expand Up @@ -10,13 +10,14 @@ use std::rc::Rc;
use std::time::Instant;

use gleam::gl;
use log::{trace, warn};
use log::{info, trace, warn};
use servo::compositing::windowing::EmbedderEvent;
use servo::config::opts;
use servo::servo_config::pref;
use servo::Servo;
use surfman::GLApi;
use webxr::glwindow::GlWindowDiscovery;
use winit::event::WindowEvent;
use winit::event_loop::EventLoopWindowTarget;
use winit::window::WindowId;

Expand Down Expand Up @@ -95,9 +96,8 @@ impl App {
webrender_surfman.make_gl_context_current().unwrap();
debug_assert_eq!(webrender_gl.get_error(), gleam::gl::NO_ERROR);

// Set up egui context for minibrowser ui
// Adapted from https://github.com/emilk/egui/blob/9478e50d012c5138551c38cbee16b07bc1fcf283/crates/egui_glow/examples/pure_glow.rs
app.minibrowser = Some(Minibrowser::new(&webrender_surfman, &events_loop).into());
app.minibrowser =
Some(Minibrowser::new(&webrender_surfman, &events_loop, window.as_ref()).into());
}

if let Some(mut minibrowser) = app.minibrowser() {
Expand All @@ -119,15 +119,23 @@ impl App {
let t_start = Instant::now();
let mut t = t_start;
let ev_waker = events_loop.create_event_loop_waker();
events_loop.run_forever(move |e, w, control_flow| {
events_loop.run_forever(move |event, w, control_flow| {
let now = Instant::now();
match e {
// Uncomment to filter out logging of DeviceEvent, which can be very noisy.
match event {
// Uncomment to filter out logging of common events, which can be very noisy.
// winit::event::Event::DeviceEvent { .. } => {},
_ => trace!("@{:?} (+{:?}) {:?}", now - t_start, now - t, e),
// winit::event::Event::WindowEvent {
// event: WindowEvent::CursorMoved { .. },
// ..
// } => {},
// winit::event::Event::MainEventsCleared => {},
// winit::event::Event::RedrawEventsCleared => {},
// winit::event::Event::UserEvent(..) => {},
// winit::event::Event::NewEvents(..) => {},
_ => trace!("@{:?} (+{:?}) {:?}", now - t_start, now - t, event),
}
t = now;
match e {
match event {
winit::event::Event::NewEvents(winit::event::StartCause::Init) => {
let surfman = window.webrender_surfman();

Expand Down Expand Up @@ -182,7 +190,7 @@ impl App {
return;
}

if let winit::event::Event::RedrawRequested(_) = e {
if let winit::event::Event::RedrawRequested(_) = event {
// We need to redraw the window for some reason.
trace!("RedrawRequested");

Expand All @@ -207,20 +215,45 @@ impl App {
// Handle the event
let mut consumed = false;
if let Some(mut minibrowser) = app.minibrowser() {
if let winit::event::Event::WindowEvent { ref event, .. } = e {
let response = minibrowser.context.on_event(&event);
if response.repaint {
// Request a winit redraw event, so we can recomposite, update and paint the
// minibrowser, and present the new frame.
match event {
winit::event::Event::WindowEvent {
event: WindowEvent::ScaleFactorChanged { scale_factor, .. },
..
} => {
// Intercept any ScaleFactorChanged events away from EguiGlow::on_event, so
// we can use our own logic for calculating the scale factor and set egui’s
// scale factor to that value manually.
let effective_scale_factor = window.hidpi_factor().get();
info!(
"window scale factor changed to {}, setting scale factor to {}",
scale_factor, effective_scale_factor
);
minibrowser
.context
.egui_ctx
.set_pixels_per_point(effective_scale_factor);

// Request a winit redraw event, so we can recomposite, update and paint
// the minibrowser, and present the new frame.
window.winit_window().unwrap().request_redraw();
}

// TODO how do we handle the tab key? (see doc for consumed)
consumed = response.consumed;
},
winit::event::Event::WindowEvent { ref event, .. } => {
let response = minibrowser.context.on_event(&event);
if response.repaint {
// Request a winit redraw event, so we can recomposite, update and paint
// the minibrowser, and present the new frame.
window.winit_window().unwrap().request_redraw();
}

// TODO how do we handle the tab key? (see doc for consumed)
// Note that servo doesn’t yet support tabbing through links and inputs
consumed = response.consumed;
},
_ => {},
}
}
if !consumed {
app.queue_embedder_events_for_winit_event(e);
app.queue_embedder_events_for_winit_event(event);
}

let animating = app.is_animating();
Expand Down
54 changes: 25 additions & 29 deletions ports/servoshell/headed_window.rs
Expand Up @@ -8,7 +8,8 @@ use std::cell::{Cell, RefCell};
use std::collections::HashMap;
use std::rc::Rc;

use euclid::{Angle, Point2D, Rotation3D, Scale, Size2D, UnknownUnit, Vector2D, Vector3D};
use euclid::num::Zero;
use euclid::{Angle, Length, Point2D, Rotation3D, Scale, Size2D, UnknownUnit, Vector2D, Vector3D};
use log::{debug, info, trace};
use raw_window_handle::{HasRawDisplayHandle, HasRawWindowHandle};
use servo::compositing::windowing::{
Expand Down Expand Up @@ -48,7 +49,7 @@ pub struct Window {
webrender_surfman: WebrenderSurfman,
screen_size: Size2D<u32, DevicePixel>,
inner_size: Cell<Size2D<u32, DevicePixel>>,
toolbar_height: Cell<f32>,
toolbar_height: Cell<Length<f32, DeviceIndependentPixel>>,
mouse_down_button: Cell<Option<winit::event::MouseButton>>,
mouse_down_point: Cell<Point2D<i32, DevicePixel>>,
primary_monitor: winit::monitor::MonitorHandle,
Expand Down Expand Up @@ -155,7 +156,7 @@ impl Window {
device_pixel_ratio_override,
xr_window_poses: RefCell::new(vec![]),
modifiers_state: Cell::new(ModifiersState::empty()),
toolbar_height: Cell::new(0.0),
toolbar_height: Cell::new(Default::default()),
}
}

Expand Down Expand Up @@ -241,7 +242,7 @@ impl Window {
) {
use servo::script_traits::MouseButton;

let max_pixel_dist = 10.0 * self.servo_hidpi_factor().get();
let max_pixel_dist = 10.0 * self.hidpi_factor().get();
let mouse_button = match &button {
winit::event::MouseButton::Left => MouseButton::Left,
winit::event::MouseButton::Right => MouseButton::Right,
Expand Down Expand Up @@ -280,20 +281,6 @@ impl Window {
.borrow_mut()
.push(EmbedderEvent::MouseWindowEventClass(event));
}

fn device_hidpi_factor(&self) -> Scale<f32, DeviceIndependentPixel, DevicePixel> {
Scale::new(self.winit_window.scale_factor() as f32)
}

fn servo_hidpi_factor(&self) -> Scale<f32, DeviceIndependentPixel, DevicePixel> {
match self.device_pixel_ratio_override {
Some(override_value) => Scale::new(override_value),
_ => match opts::get().output_file {
Some(_) => Scale::new(1.0),
None => self.device_hidpi_factor(),
},
}
}
}

impl WindowPortsMethods for Window {
Expand All @@ -305,8 +292,18 @@ impl WindowPortsMethods for Window {
!self.event_queue.borrow().is_empty()
}

fn device_hidpi_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 page_height(&self) -> f32 {
let dpr = self.servo_hidpi_factor();
let dpr = self.hidpi_factor();
let size = self.winit_window.inner_size();
size.height as f32 * dpr.get()
}
Expand Down Expand Up @@ -406,14 +403,13 @@ impl WindowPortsMethods for Window {
}
},
winit::event::WindowEvent::CursorMoved { position, .. } => {
let (x, y): (f64, f64) = position.into();
let y = y - f64::from(self.toolbar_height.get());
self.mouse_pos.set(Point2D::new(x, y).to_i32());
let toolbar_height = self.toolbar_height.get() * self.hidpi_factor();
let mut position = winit_position_to_euclid_point(position).to_f32();
position -= Size2D::from_lengths(Length::zero(), toolbar_height);
self.mouse_pos.set(position.to_i32());
self.event_queue
.borrow_mut()
.push(EmbedderEvent::MouseWindowMoveEventClass(Point2D::new(
x as f32, y as f32,
)));
.push(EmbedderEvent::MouseWindowMoveEventClass(position.to_f32()));
},
winit::event::WindowEvent::MouseWheel { delta, phase, .. } => {
let (mut dx, mut dy, mode) = match delta {
Expand Down Expand Up @@ -512,7 +508,7 @@ impl WindowPortsMethods for Window {
Some(&self.winit_window)
}

fn set_toolbar_height(&self, height: f32) {
fn set_toolbar_height(&self, height: Length<f32, DeviceIndependentPixel>) {
self.toolbar_height.set(height);
}
}
Expand All @@ -533,8 +529,8 @@ impl WindowMethods for Window {
let inner_size = winit_size_to_euclid_size(self.winit_window.inner_size()).to_f32();

// Subtract the minibrowser toolbar height if any
let toolbar_height = self.toolbar_height.get();
let viewport_size = inner_size - Size2D::new(0f32, toolbar_height);
let toolbar_height = self.toolbar_height.get() * self.hidpi_factor();
let viewport_size = inner_size - Size2D::from_lengths(Length::zero(), toolbar_height);

let viewport_origin = DeviceIntPoint::zero(); // bottom left
let viewport = DeviceIntRect::new(viewport_origin, viewport_size.to_i32());
Expand All @@ -547,7 +543,7 @@ impl WindowMethods for Window {
screen,
// FIXME: Winit doesn't have API for available size. Fallback to screen size
screen_avail: screen,
hidpi_factor: self.servo_hidpi_factor(),
hidpi_factor: self.hidpi_factor(),
}
}

Expand Down
25 changes: 14 additions & 11 deletions ports/servoshell/headless_window.rs
Expand Up @@ -7,7 +7,7 @@
use std::cell::Cell;
use std::rc::Rc;

use euclid::{Point2D, Rotation3D, Scale, Size2D, UnknownUnit, Vector3D};
use euclid::{Length, Point2D, Rotation3D, Scale, Size2D, UnknownUnit, Vector3D};
use servo::compositing::windowing::{
AnimationState, EmbedderCoordinates, EmbedderEvent, WindowMethods,
};
Expand Down Expand Up @@ -52,13 +52,6 @@ impl Window {

Rc::new(window)
}

fn servo_hidpi_factor(&self) -> Scale<f32, DeviceIndependentPixel, DevicePixel> {
match self.device_pixel_ratio_override {
Some(override_value) => Scale::new(override_value),
_ => Scale::new(1.0),
}
}
}

impl WindowPortsMethods for Window {
Expand All @@ -74,14 +67,24 @@ impl WindowPortsMethods for Window {
unsafe { winit::window::WindowId::dummy() }
}

fn device_hidpi_factor(&self) -> Scale<f32, DeviceIndependentPixel, DevicePixel> {
Scale::new(1.0)
}

fn device_pixel_ratio_override(
&self,
) -> Option<Scale<f32, DeviceIndependentPixel, DevicePixel>> {
self.device_pixel_ratio_override.map(Scale::new)
}

fn page_height(&self) -> f32 {
let height = self
.webrender_surfman
.context_surface_info()
.unwrap_or(None)
.map(|info| info.size.height)
.unwrap_or(0);
let dpr = self.servo_hidpi_factor();
let dpr = self.hidpi_factor();
height as f32 * dpr.get()
}

Expand Down Expand Up @@ -112,14 +115,14 @@ impl WindowPortsMethods for Window {
None
}

fn set_toolbar_height(&self, _height: f32) {
fn set_toolbar_height(&self, _height: Length<f32, DeviceIndependentPixel>) {
unimplemented!("headless Window only")
}
}

impl WindowMethods for Window {
fn get_coordinates(&self) -> EmbedderCoordinates {
let dpr = self.servo_hidpi_factor();
let dpr = self.hidpi_factor();
let size = self
.webrender_surfman
.context_surface_info()
Expand Down
22 changes: 17 additions & 5 deletions ports/servoshell/minibrowser.rs
Expand Up @@ -7,8 +7,10 @@ use std::sync::Arc;
use std::time::Instant;

use egui::{Key, Modifiers, TopBottomPanel};
use euclid::Length;
use log::{trace, warn};
use servo::compositing::windowing::EmbedderEvent;
use servo::servo_geometry::DeviceIndependentPixel;
use servo::servo_url::ServoUrl;
use servo::webrender_surfman::WebrenderSurfman;

Expand All @@ -20,7 +22,7 @@ use crate::window_trait::WindowPortsMethods;
pub struct Minibrowser {
pub context: EguiGlow,
pub event_queue: RefCell<Vec<MinibrowserEvent>>,
pub toolbar_height: Cell<f32>,
pub toolbar_height: Cell<Length<f32, DeviceIndependentPixel>>,
last_update: Instant,
location: RefCell<String>,

Expand All @@ -34,15 +36,25 @@ pub enum MinibrowserEvent {
}

impl Minibrowser {
pub fn new(webrender_surfman: &WebrenderSurfman, events_loop: &EventsLoop) -> Self {
pub fn new(
webrender_surfman: &WebrenderSurfman,
events_loop: &EventsLoop,
window: &dyn WindowPortsMethods,
) -> Self {
let gl = unsafe {
glow::Context::from_loader_function(|s| webrender_surfman.get_proc_address(s))
};

// Adapted from https://github.com/emilk/egui/blob/9478e50d012c5138551c38cbee16b07bc1fcf283/crates/egui_glow/examples/pure_glow.rs
let context = EguiGlow::new(events_loop.as_winit(), Arc::new(gl), None);
context
.egui_ctx
.set_pixels_per_point(window.hidpi_factor().get());

Self {
context: EguiGlow::new(events_loop.as_winit(), Arc::new(gl), None),
context,
event_queue: RefCell::new(vec![]),
toolbar_height: 0f32.into(),
toolbar_height: Default::default(),
last_update: Instant::now(),
location: RefCell::new(String::default()),
location_dirty: false.into(),
Expand Down Expand Up @@ -96,7 +108,7 @@ impl Minibrowser {
);
});

toolbar_height.set(ctx.used_rect().height());
toolbar_height.set(Length::new(ctx.used_rect().height()));
*last_update = now;
});
}
Expand Down
6 changes: 5 additions & 1 deletion ports/servoshell/platform/windows/servo.exe.manifest
Expand Up @@ -17,7 +17,11 @@

<asmv3:application>
<asmv3:windowsSettings xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">
<dpiAware>true</dpiAware>
<!-- enable per-monitor dpi awareness where possible -->
<!-- https://learn.microsoft.com/en-us/windows/win32/hidpi/setting-the-default-dpi-awareness-for-a-process -->
<!-- https://stackoverflow.com/q/23551112 -->
<dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">true/pm</dpiAware>
<dpiAwareness xmlns="http://schemas.microsoft.com/SMI/2016/WindowsSettings">PerMonitorV2</dpiAwareness>
</asmv3:windowsSettings>
</asmv3:application>
</assembly>

0 comments on commit bb1a6c2

Please sign in to comment.