diff --git a/CHANGELOG.md b/CHANGELOG.md index d28b32c3..2fb9be9e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,6 @@ # Unreleased +- On all platforms, `available_monitors` and `primary_monitor` are now on `EventLoopWindowTarget` rather than `EventLoop` to list monitors event in the event loop. - On Unix, X11 and Wayland are now optional features (enabled by default) - On X11, fix deadlock when calling `set_fullscreen_inner`. - On Web, prevent the webpage from scrolling when the user is focused on a winit canvas diff --git a/src/event_loop.rs b/src/event_loop.rs index 224292a5..a523c01a 100644 --- a/src/event_loop.rs +++ b/src/event_loop.rs @@ -155,11 +155,20 @@ impl EventLoop { event_loop_proxy: self.event_loop.create_proxy(), } } +} +impl Deref for EventLoop { + type Target = EventLoopWindowTarget; + fn deref(&self) -> &EventLoopWindowTarget { + self.event_loop.window_target() + } +} + +impl EventLoopWindowTarget { /// Returns the list of all the monitors available on the system. #[inline] pub fn available_monitors(&self) -> impl Iterator { - self.event_loop + self.p .available_monitors() .into_iter() .map(|inner| MonitorHandle { inner }) @@ -169,18 +178,11 @@ impl EventLoop { #[inline] pub fn primary_monitor(&self) -> MonitorHandle { MonitorHandle { - inner: self.event_loop.primary_monitor(), + inner: self.p.primary_monitor(), } } } -impl Deref for EventLoop { - type Target = EventLoopWindowTarget; - fn deref(&self) -> &EventLoopWindowTarget { - self.event_loop.window_target() - } -} - /// Used to send custom events to `EventLoop`. pub struct EventLoopProxy { event_loop_proxy: platform_impl::EventLoopProxy, diff --git a/src/monitor.rs b/src/monitor.rs index 8977c11a..126b03e2 100644 --- a/src/monitor.rs +++ b/src/monitor.rs @@ -3,11 +3,11 @@ //! If you want to get basic information about a monitor, you can use the [`MonitorHandle`][monitor_handle] //! type. This is retreived from one of the following methods, which return an iterator of //! [`MonitorHandle`][monitor_handle]: -//! - [`EventLoop::available_monitors`][loop_get] +//! - [`EventLoopWindowTarget::available_monitors`][loop_get] //! - [`Window::available_monitors`][window_get]. //! //! [monitor_handle]: crate::monitor::MonitorHandle -//! [loop_get]: crate::event_loop::EventLoop::available_monitors +//! [loop_get]: crate::event_loop::EventLoopWindowTarget::available_monitors //! [window_get]: crate::window::Window::available_monitors use crate::{ dpi::{PhysicalPosition, PhysicalSize}, diff --git a/src/platform_impl/android/mod.rs b/src/platform_impl/android/mod.rs index 8a5c6940..d42b9420 100644 --- a/src/platform_impl/android/mod.rs +++ b/src/platform_impl/android/mod.rs @@ -295,16 +295,6 @@ impl EventLoop { &self.window_target } - pub fn primary_monitor(&self) -> MonitorHandle { - MonitorHandle - } - - pub fn available_monitors(&self) -> VecDeque { - let mut v = VecDeque::with_capacity(1); - v.push_back(self.primary_monitor()); - v - } - pub fn create_proxy(&self) -> EventLoopProxy { EventLoopProxy { queue: self.user_queue.clone(), @@ -339,6 +329,18 @@ pub struct EventLoopWindowTarget { _marker: std::marker::PhantomData, } +impl EventLoopWindowTarget { + pub fn primary_monitor(&self) -> MonitorHandle { + MonitorHandle + } + + pub fn available_monitors(&self) -> VecDeque { + let mut v = VecDeque::with_capacity(1); + v.push_back(self.primary_monitor()); + v + } +} + #[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)] pub struct WindowId; diff --git a/src/platform_impl/ios/event_loop.rs b/src/platform_impl/ios/event_loop.rs index 61b389d4..2e7c64d2 100644 --- a/src/platform_impl/ios/event_loop.rs +++ b/src/platform_impl/ios/event_loop.rs @@ -49,6 +49,18 @@ pub struct EventLoopWindowTarget { sender_to_clone: Sender, } +impl EventLoopWindowTarget { + pub fn available_monitors(&self) -> VecDeque { + // guaranteed to be on main thread + unsafe { monitor::uiscreens() } + } + + pub fn primary_monitor(&self) -> MonitorHandle { + // guaranteed to be on main thread + unsafe { monitor::main_uiscreen() } + } +} + pub struct EventLoop { window_target: RootEventLoopWindowTarget, } @@ -115,16 +127,6 @@ impl EventLoop { EventLoopProxy::new(self.window_target.p.sender_to_clone.clone()) } - pub fn available_monitors(&self) -> VecDeque { - // guaranteed to be on main thread - unsafe { monitor::uiscreens() } - } - - pub fn primary_monitor(&self) -> MonitorHandle { - // guaranteed to be on main thread - unsafe { monitor::main_uiscreen() } - } - pub fn window_target(&self) -> &RootEventLoopWindowTarget { &self.window_target } diff --git a/src/platform_impl/linux/mod.rs b/src/platform_impl/linux/mod.rs index 613d0e23..0fee6832 100644 --- a/src/platform_impl/linux/mod.rs +++ b/src/platform_impl/linux/mod.rs @@ -602,35 +602,6 @@ impl EventLoop { Ok(EventLoop::X(x11::EventLoop::new(xconn))) } - #[inline] - pub fn available_monitors(&self) -> VecDeque { - match *self { - #[cfg(feature = "wayland")] - EventLoop::Wayland(ref evlp) => evlp - .available_monitors() - .into_iter() - .map(MonitorHandle::Wayland) - .collect(), - #[cfg(feature = "x11")] - EventLoop::X(ref evlp) => evlp - .x_connection() - .available_monitors() - .into_iter() - .map(MonitorHandle::X) - .collect(), - } - } - - #[inline] - pub fn primary_monitor(&self) -> MonitorHandle { - match *self { - #[cfg(feature = "wayland")] - EventLoop::Wayland(ref evlp) => MonitorHandle::Wayland(evlp.primary_monitor()), - #[cfg(feature = "x11")] - EventLoop::X(ref evlp) => MonitorHandle::X(evlp.x_connection().primary_monitor()), - } - } - pub fn create_proxy(&self) -> EventLoopProxy { x11_or_wayland!(match self; EventLoop(evlp) => evlp.create_proxy(); as EventLoopProxy) } @@ -677,6 +648,39 @@ impl EventLoopWindowTarget { _ => false, } } + + #[inline] + pub fn available_monitors(&self) -> VecDeque { + match *self { + #[cfg(feature = "wayland")] + EventLoopWindowTarget::Wayland(ref evlp) => evlp + .available_monitors() + .into_iter() + .map(MonitorHandle::Wayland) + .collect(), + #[cfg(feature = "x11")] + EventLoopWindowTarget::X(ref evlp) => evlp + .x_connection() + .available_monitors() + .into_iter() + .map(MonitorHandle::X) + .collect(), + } + } + + #[inline] + pub fn primary_monitor(&self) -> MonitorHandle { + match *self { + #[cfg(feature = "wayland")] + EventLoopWindowTarget::Wayland(ref evlp) => { + MonitorHandle::Wayland(evlp.primary_monitor()) + } + #[cfg(feature = "x11")] + EventLoopWindowTarget::X(ref evlp) => { + MonitorHandle::X(evlp.x_connection().primary_monitor()) + } + } + } } fn sticky_exit_callback( diff --git a/src/platform_impl/linux/wayland/event_loop.rs b/src/platform_impl/linux/wayland/event_loop.rs index 737fcc78..77215b9c 100644 --- a/src/platform_impl/linux/wayland/event_loop.rs +++ b/src/platform_impl/linux/wayland/event_loop.rs @@ -245,8 +245,6 @@ pub struct EventLoop { poll: Poll, // The wayland display pub display: Arc, - // The output manager - pub outputs: OutputMgr, // The cursor manager cursor_manager: Arc>, kbd_channel: Receiver>, @@ -277,6 +275,8 @@ pub struct EventLoopWindowTarget { pub display: Arc, // The list of seats pub seats: Arc>>, + // The output manager + pub outputs: OutputMgr, _marker: ::std::marker::PhantomData, } @@ -418,10 +418,10 @@ impl EventLoop { .unwrap(); let cursor_manager_clone = cursor_manager.clone(); + let outputs = env.outputs.clone(); Ok(EventLoop { poll, display: display.clone(), - outputs: env.outputs.clone(), user_sender, user_channel, kbd_channel, @@ -435,6 +435,7 @@ impl EventLoop { cleanup_needed: Arc::new(Mutex::new(false)), seats, display, + outputs, _marker: ::std::marker::PhantomData, }), _marker: ::std::marker::PhantomData, @@ -633,14 +634,6 @@ impl EventLoop { callback(Event::LoopDestroyed, &self.window_target, &mut control_flow); } - pub fn primary_monitor(&self) -> MonitorHandle { - primary_monitor(&self.outputs) - } - - pub fn available_monitors(&self) -> VecDeque { - available_monitors(&self.outputs) - } - pub fn window_target(&self) -> &RootELW { &self.window_target } @@ -650,6 +643,14 @@ impl EventLoopWindowTarget { pub fn display(&self) -> &Display { &*self.display } + + pub fn available_monitors(&self) -> VecDeque { + available_monitors(&self.outputs) + } + + pub fn primary_monitor(&self) -> MonitorHandle { + primary_monitor(&self.outputs) + } } /* diff --git a/src/platform_impl/linux/x11/mod.rs b/src/platform_impl/linux/x11/mod.rs index 186e9c01..0849c0f9 100644 --- a/src/platform_impl/linux/x11/mod.rs +++ b/src/platform_impl/linux/x11/mod.rs @@ -258,10 +258,6 @@ impl EventLoop { &self.target } - pub(crate) fn x_connection(&self) -> &Arc { - get_xtarget(&self.target).x_connection() - } - pub fn run_return(&mut self, mut callback: F) where F: FnMut(Event<'_, T>, &RootELW, &mut ControlFlow), diff --git a/src/platform_impl/macos/event_loop.rs b/src/platform_impl/macos/event_loop.rs index 01437b84..0d88d979 100644 --- a/src/platform_impl/macos/event_loop.rs +++ b/src/platform_impl/macos/event_loop.rs @@ -34,6 +34,18 @@ impl Default for EventLoopWindowTarget { } } +impl EventLoopWindowTarget { + #[inline] + pub fn available_monitors(&self) -> VecDeque { + monitor::available_monitors() + } + + #[inline] + pub fn primary_monitor(&self) -> MonitorHandle { + monitor::primary_monitor() + } +} + pub struct EventLoop { window_target: Rc>, _delegate: IdRef, @@ -68,16 +80,6 @@ impl EventLoop { } } - #[inline] - pub fn available_monitors(&self) -> VecDeque { - monitor::available_monitors() - } - - #[inline] - pub fn primary_monitor(&self) -> MonitorHandle { - monitor::primary_monitor() - } - pub fn window_target(&self) -> &RootWindowTarget { &self.window_target } diff --git a/src/platform_impl/web/event_loop/mod.rs b/src/platform_impl/web/event_loop/mod.rs index 50640a1f..fa01b5df 100644 --- a/src/platform_impl/web/event_loop/mod.rs +++ b/src/platform_impl/web/event_loop/mod.rs @@ -27,14 +27,6 @@ impl EventLoop { } } - pub fn available_monitors(&self) -> VecDequeIter { - VecDeque::new().into_iter() - } - - pub fn primary_monitor(&self) -> monitor::Handle { - monitor::Handle - } - pub fn run(self, mut event_handler: F) -> ! where F: 'static diff --git a/src/platform_impl/web/event_loop/window_target.rs b/src/platform_impl/web/event_loop/window_target.rs index db485a16..3ba5e1d2 100644 --- a/src/platform_impl/web/event_loop/window_target.rs +++ b/src/platform_impl/web/event_loop/window_target.rs @@ -1,9 +1,10 @@ -use super::{backend, device, proxy::Proxy, runner, window}; +use super::{super::monitor, backend, device, proxy::Proxy, runner, window}; use crate::dpi::{PhysicalSize, Size}; use crate::event::{DeviceId, ElementState, Event, KeyboardInput, TouchPhase, WindowEvent}; use crate::event_loop::ControlFlow; use crate::window::{Theme, WindowId}; use std::clone::Clone; +use std::collections::{vec_deque::IntoIter as VecDequeIter, VecDeque}; pub struct WindowTarget { pub(crate) runner: runner::Shared, @@ -213,4 +214,12 @@ impl WindowTarget { }); }); } + + pub fn available_monitors(&self) -> VecDequeIter { + VecDeque::new().into_iter() + } + + pub fn primary_monitor(&self) -> monitor::Handle { + monitor::Handle + } } diff --git a/src/platform_impl/windows/event_loop.rs b/src/platform_impl/windows/event_loop.rs index 61ec167f..eb0a6576 100644 --- a/src/platform_impl/windows/event_loop.rs +++ b/src/platform_impl/windows/event_loop.rs @@ -4,6 +4,7 @@ mod runner; use parking_lot::Mutex; use std::{ + collections::VecDeque, marker::PhantomData, mem, panic, ptr, rc::Rc, @@ -38,7 +39,8 @@ use crate::{ dpi::{become_dpi_aware, dpi_to_scale_factor, enable_non_client_dpi_scaling}, drop_handler::FileDropHandler, event::{self, handle_extended_keys, process_key_params, vkey_to_winit_vkey}, - monitor, raw_input, util, + monitor::{self, MonitorHandle}, + raw_input, util, window_state::{CursorFlags, WindowFlags, WindowState}, wrap_device_id, WindowId, DEVICE_ID, }, @@ -247,6 +249,15 @@ impl EventLoopWindowTarget { target_window: self.thread_msg_target, } } + + // TODO: Investigate opportunities for caching + pub fn available_monitors(&self) -> VecDeque { + monitor::available_monitors() + } + + pub fn primary_monitor(&self) -> MonitorHandle { + monitor::primary_monitor() + } } fn main_thread_id() -> DWORD { @@ -851,7 +862,7 @@ unsafe extern "system" fn public_window_callback( window_pos.cy = new_monitor_rect.bottom - new_monitor_rect.top; } *fullscreen_monitor = crate::monitor::MonitorHandle { - inner: monitor::MonitorHandle::new(new_monitor), + inner: MonitorHandle::new(new_monitor), }; } } diff --git a/src/platform_impl/windows/monitor.rs b/src/platform_impl/windows/monitor.rs index 7136f35d..7fa4c73d 100644 --- a/src/platform_impl/windows/monitor.rs +++ b/src/platform_impl/windows/monitor.rs @@ -11,7 +11,7 @@ use std::{ io, mem, ptr, }; -use super::{util, EventLoop}; +use super::util; use crate::{ dpi::{PhysicalPosition, PhysicalSize}, monitor::{MonitorHandle as RootMonitorHandle, VideoMode as RootVideoMode}, @@ -126,17 +126,6 @@ pub fn current_monitor(hwnd: HWND) -> MonitorHandle { MonitorHandle::new(hmonitor) } -impl EventLoop { - // TODO: Investigate opportunities for caching - pub fn available_monitors(&self) -> VecDeque { - available_monitors() - } - - pub fn primary_monitor(&self) -> MonitorHandle { - primary_monitor() - } -} - impl Window { pub fn available_monitors(&self) -> VecDeque { available_monitors() diff --git a/src/window.rs b/src/window.rs index 113e7c04..000adef7 100644 --- a/src/window.rs +++ b/src/window.rs @@ -758,7 +758,7 @@ impl Window { /// Returns the list of all the monitors available on the system. /// - /// This is the same as `EventLoop::available_monitors`, and is provided for convenience. + /// This is the same as `EventLoopWindowTarget::available_monitors`, and is provided for convenience. /// /// ## Platform-specific /// @@ -773,7 +773,7 @@ impl Window { /// Returns the primary monitor of the system. /// - /// This is the same as `EventLoop::primary_monitor`, and is provided for convenience. + /// This is the same as `EventLoopWindowTarget::primary_monitor`, and is provided for convenience. /// /// ## Platform-specific ///