From f64814f2494dcc14046bd712b482e44a38a4ec06 Mon Sep 17 00:00:00 2001 From: Josh Matthews Date: Sun, 25 Dec 2022 16:41:58 -0500 Subject: [PATCH] Fix macOS extensions in winit. --- ports/winit/app.rs | 2 +- ports/winit/events_loop.rs | 22 +++++++++++++++++++--- ports/winit/headed_window.rs | 25 ++----------------------- 3 files changed, 22 insertions(+), 27 deletions(-) diff --git a/ports/winit/app.rs b/ports/winit/app.rs index 582ddb94783..4832e7d3bb6 100644 --- a/ports/winit/app.rs +++ b/ports/winit/app.rs @@ -40,7 +40,7 @@ impl App { device_pixels_per_px: Option, user_agent: Option, ) { - let events_loop = EventsLoop::new(opts::get().headless); + let events_loop = EventsLoop::new(opts::get().headless, opts::get().output_file.is_some()); // Implements window methods, used by compositor. let window = if opts::get().headless { diff --git a/ports/winit/events_loop.rs b/ports/winit/events_loop.rs index 6988dc85a82..2448e91e584 100644 --- a/ports/winit/events_loop.rs +++ b/ports/winit/events_loop.rs @@ -8,6 +8,8 @@ use servo::embedder_traits::EventLoopWaker; use std::sync::{Arc, Condvar, Mutex}; use std::time; use winit; +#[cfg(target_os = "macos")] +use winit::platform::macos::{ActivationPolicy, EventLoopBuilderExtMacOS}; #[derive(Debug)] pub enum ServoEvent { @@ -30,17 +32,31 @@ impl EventsLoop { // Ideally, we could use the winit event loop in both modes, // but on Linux, the event loop requires a X11 server. #[cfg(not(any(target_os = "linux", target_os = "macos")))] - pub fn new(_headless: bool) -> EventsLoop { + pub fn new(_headless: bool, _has_output_file: bool) -> EventsLoop { EventsLoop(EventLoop::Winit(Some(winit::event_loop::EventLoopBuilder::with_user_event().build()))) } - #[cfg(any(target_os = "linux", target_os = "macos"))] - pub fn new(headless: bool) -> EventsLoop { + #[cfg(target_os = "linux")] + pub fn new(headless: bool, _has_output_file: bool) -> EventsLoop { EventsLoop(if headless { EventLoop::Headless(Arc::new((Mutex::new(false), Condvar::new()))) } else { EventLoop::Winit(Some(winit::event_loop::EventLoopBuilder::with_user_event().build())) }) } + #[cfg(target_os = "macos")] + pub fn new(headless: bool, _has_output_file: bool) -> EventsLoop { + EventsLoop(if headless { + EventLoop::Headless(Arc::new((Mutex::new(false), Condvar::new()))) + } else { + let mut event_loop_builder = winit::event_loop::EventLoopBuilder::with_user_event(); + if _has_output_file { + // Prevent the window from showing in Dock.app, stealing focus, + // when generating an output file. + event_loop_builder.with_activation_policy(ActivationPolicy::Prohibited); + } + EventLoop::Winit(Some(event_loop_builder.build())) + }) + } } impl EventsLoop { diff --git a/ports/winit/headed_window.rs b/ports/winit/headed_window.rs index ef7e4bc4bdb..c12eb5b5e99 100644 --- a/ports/winit/headed_window.rs +++ b/ports/winit/headed_window.rs @@ -11,8 +11,6 @@ use euclid::{ Angle, Point2D, Rotation3D, Scale, Size2D, UnknownUnit, Vector2D, Vector3D, }; -#[cfg(target_os = "macos")] -use winit::platform::macos::{ActivationPolicy, WindowBuilderExtMacOS}; #[cfg(any(target_os = "linux", target_os = "windows"))] use winit::window::Icon; use winit::event::{ElementState, KeyboardInput, MouseButton, MouseScrollDelta, TouchPhase, VirtualKeyCode}; @@ -50,21 +48,6 @@ use winapi; use winit::dpi::{LogicalPosition, PhysicalPosition, PhysicalSize}; use winit::event::ModifiersState; -#[cfg(target_os = "macos")] -fn builder_with_platform_options(mut builder: winit::window::WindowBuilder) -> winit::window::WindowBuilder { - if opts::get().output_file.is_some() { - // Prevent the window from showing in Dock.app, stealing focus, - // when generating an output file. - builder = builder.with_activation_policy(ActivationPolicy::Prohibited) - } - builder -} - -#[cfg(not(target_os = "macos"))] -fn builder_with_platform_options(builder: winit::window::WindowBuilder) -> winit::window::WindowBuilder { - builder -} - pub struct Window { winit_window: winit::window::Window, webrender_surfman: WebrenderSurfman, @@ -117,15 +100,13 @@ impl Window { let width = win_size.to_untyped().width; let height = win_size.to_untyped().height; - let mut window_builder = winit::window::WindowBuilder::new() + let window_builder = winit::window::WindowBuilder::new() .with_title("Servo".to_string()) .with_decorations(!no_native_titlebar) .with_transparent(no_native_titlebar) .with_inner_size(PhysicalSize::new(width as f64, height as f64)) .with_visible(visible); - window_builder = builder_with_platform_options(window_builder); - let winit_window = window_builder.build(events_loop.as_winit()).expect("Failed to create window."); #[cfg(any(target_os = "linux", target_os = "windows"))] @@ -508,13 +489,11 @@ impl WindowPortsMethods for Window { ) -> Box { let size = self.winit_window.outer_size(); - let mut window_builder = winit::window::WindowBuilder::new() + let window_builder = winit::window::WindowBuilder::new() .with_title("Servo XR".to_string()) .with_inner_size(size) .with_visible(true); - window_builder = builder_with_platform_options(window_builder); - let winit_window = window_builder.build(event_loop) .expect("Failed to create window.");