Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make winit completely platform-agnostic #2430

Draft
wants to merge 9 commits into
base: master
Choose a base branch
from
Prev Previous commit
Next Next commit
Simplify construction of MonitorHandles
madsmtm committed Aug 11, 2022
commit 45b98c4c27996d0436afad6400671488dd5e80d7
4 changes: 3 additions & 1 deletion src/event_loop.rs
Original file line number Diff line number Diff line change
@@ -312,7 +312,9 @@ impl<T> EventLoopWindowTarget<T> {
/// **Wayland:** Always returns `None`.
#[inline]
pub fn primary_monitor(&self) -> Option<MonitorHandle> {
self.p.primary_monitor()
self.p
.primary_monitor()
.map(|inner| MonitorHandle { inner })
}

/// Change [`DeviceEvent`] filter mode.
4 changes: 3 additions & 1 deletion src/monitor.rs
Original file line number Diff line number Diff line change
@@ -78,7 +78,9 @@ impl VideoMode {
/// a separate set of valid video modes.
#[inline]
pub fn monitor(&self) -> MonitorHandle {
self.video_mode.monitor()
MonitorHandle {
inner: self.video_mode.monitor(),
}
}
}

25 changes: 8 additions & 17 deletions src/platform_impl/android/mod.rs
Original file line number Diff line number Diff line change
@@ -23,7 +23,6 @@ use crate::{
error,
event::{self, VirtualKeyCode},
event_loop::{self, ControlFlow},
monitor,
window::{self, CursorGrabMode},
};

@@ -636,10 +635,8 @@ pub struct EventLoopWindowTarget<T: 'static> {
}

impl<T: 'static> EventLoopWindowTarget<T> {
pub fn primary_monitor(&self) -> Option<monitor::MonitorHandle> {
Some(monitor::MonitorHandle {
inner: MonitorHandle,
})
pub fn primary_monitor(&self) -> Option<MonitorHandle> {
Some(MonitorHandle)
}

pub fn available_monitors(&self) -> VecDeque<MonitorHandle> {
@@ -702,10 +699,8 @@ impl Window {
WindowId
}

pub fn primary_monitor(&self) -> Option<monitor::MonitorHandle> {
Some(monitor::MonitorHandle {
inner: MonitorHandle,
})
pub fn primary_monitor(&self) -> Option<MonitorHandle> {
Some(MonitorHandle)
}

pub fn available_monitors(&self) -> VecDeque<MonitorHandle> {
@@ -714,10 +709,8 @@ impl Window {
v
}

pub fn current_monitor(&self) -> Option<monitor::MonitorHandle> {
Some(monitor::MonitorHandle {
inner: MonitorHandle,
})
pub fn current_monitor(&self) -> Option<MonitorHandle> {
Some(MonitorHandle)
}

pub fn scale_factor(&self) -> f64 {
@@ -935,9 +928,7 @@ impl VideoMode {
self.refresh_rate_millihertz
}

pub fn monitor(&self) -> monitor::MonitorHandle {
monitor::MonitorHandle {
inner: self.monitor.clone(),
}
pub fn monitor(&self) -> MonitorHandle {
self.monitor.clone()
}
}
5 changes: 2 additions & 3 deletions src/platform_impl/ios/event_loop.rs
Original file line number Diff line number Diff line change
@@ -15,7 +15,6 @@ use crate::{
event_loop::{
ControlFlow, EventLoopClosed, EventLoopWindowTarget as RootEventLoopWindowTarget,
},
monitor::MonitorHandle as RootMonitorHandle,
platform::ios::Idiom,
};

@@ -59,11 +58,11 @@ impl<T: 'static> EventLoopWindowTarget<T> {
unsafe { monitor::uiscreens() }
}

pub fn primary_monitor(&self) -> Option<RootMonitorHandle> {
pub fn primary_monitor(&self) -> Option<MonitorHandle> {
// guaranteed to be on main thread
let monitor = unsafe { monitor::main_uiscreen() };

Some(RootMonitorHandle { inner: monitor })
Some(monitor)
}

pub fn raw_display_handle(&self) -> RawDisplayHandle {
8 changes: 3 additions & 5 deletions src/platform_impl/ios/monitor.rs
Original file line number Diff line number Diff line change
@@ -6,7 +6,7 @@ use std::{

use crate::{
dpi::{PhysicalPosition, PhysicalSize},
monitor::{MonitorHandle as RootMonitorHandle, VideoMode as RootVideoMode},
monitor::VideoMode as RootVideoMode,
platform_impl::platform::{
app_state,
ffi::{id, nil, CGFloat, CGRect, CGSize, NSInteger, NSUInteger},
@@ -84,10 +84,8 @@ impl VideoMode {
self.refresh_rate_millihertz
}

pub fn monitor(&self) -> RootMonitorHandle {
RootMonitorHandle {
inner: self.monitor.clone(),
}
pub fn monitor(&self) -> MonitorHandle {
self.monitor.clone()
}
}

25 changes: 13 additions & 12 deletions src/platform_impl/ios/window.rs
Original file line number Diff line number Diff line change
@@ -223,9 +223,10 @@ impl Inner {
msg_send![uiscreen, setCurrentMode: video_mode.video_mode.screen_mode.0];
uiscreen
}
Some(Fullscreen::Borderless(monitor)) => monitor
.unwrap_or_else(|| self.current_monitor_inner())
.ui_screen() as id,
Some(Fullscreen::Borderless(Some(monitor))) => monitor.ui_screen() as id,
Some(Fullscreen::Borderless(None)) => {
self.current_monitor_inner().ui_screen() as id
}
None => {
warn!("`Window::set_fullscreen(None)` ignored on iOS");
return;
@@ -254,7 +255,7 @@ impl Inner {
pub fn fullscreen(&self) -> Option<Fullscreen> {
unsafe {
let monitor = self.current_monitor_inner();
let uiscreen = monitor.inner.ui_screen();
let uiscreen = monitor.ui_screen();
let screen_space_bounds = self.screen_frame();
let screen_bounds: CGRect = msg_send![uiscreen, bounds];

@@ -264,7 +265,9 @@ impl Inner {
&& screen_space_bounds.size.width == screen_bounds.size.width
&& screen_space_bounds.size.height == screen_bounds.size.height
{
Some(Fullscreen::Borderless(Some(monitor)))
Some(Fullscreen::Borderless(Some(RootMonitorHandle {
inner: monitor,
})))
} else {
None
}
@@ -305,26 +308,24 @@ impl Inner {
}

// Allow directly accessing the current monitor internally without unwrapping.
fn current_monitor_inner(&self) -> RootMonitorHandle {
fn current_monitor_inner(&self) -> MonitorHandle {
unsafe {
let uiscreen: id = msg_send![self.window, screen];
RootMonitorHandle {
inner: MonitorHandle::retained_new(uiscreen),
}
MonitorHandle::retained_new(uiscreen)
}
}

pub fn current_monitor(&self) -> Option<RootMonitorHandle> {
pub fn current_monitor(&self) -> Option<MonitorHandle> {
Some(self.current_monitor_inner())
}

pub fn available_monitors(&self) -> VecDeque<MonitorHandle> {
unsafe { monitor::uiscreens() }
}

pub fn primary_monitor(&self) -> Option<RootMonitorHandle> {
pub fn primary_monitor(&self) -> Option<MonitorHandle> {
let monitor = unsafe { monitor::main_uiscreen() };
Some(RootMonitorHandle { inner: monitor })
Some(monitor)
}

pub fn id(&self) -> WindowId {
25 changes: 8 additions & 17 deletions src/platform_impl/linux/mod.rs
Original file line number Diff line number Diff line change
@@ -38,7 +38,6 @@ use crate::{
ControlFlow, DeviceEventFilter, EventLoopClosed, EventLoopWindowTarget as RootELW,
},
icon::Icon,
monitor::MonitorHandle as RootMonitorHandle,
window::{CursorGrabMode, CursorIcon, Fullscreen, UserAttentionType, WindowAttributes},
};

@@ -299,7 +298,7 @@ impl VideoMode {
}

#[inline]
pub fn monitor(&self) -> RootMonitorHandle {
pub fn monitor(&self) -> MonitorHandle {
x11_or_wayland!(match self; VideoMode(m) => m.monitor())
}
}
@@ -522,21 +521,17 @@ impl Window {
}

#[inline]
pub fn current_monitor(&self) -> Option<RootMonitorHandle> {
pub fn current_monitor(&self) -> Option<MonitorHandle> {
match self {
#[cfg(feature = "x11")]
Window::X(ref window) => {
let current_monitor = MonitorHandle::X(window.current_monitor());
Some(RootMonitorHandle {
inner: current_monitor,
})
Some(current_monitor)
}
#[cfg(feature = "wayland")]
Window::Wayland(ref window) => {
let current_monitor = MonitorHandle::Wayland(window.current_monitor()?);
Some(RootMonitorHandle {
inner: current_monitor,
})
Some(current_monitor)
}
}
}
@@ -560,14 +555,12 @@ impl Window {
}

#[inline]
pub fn primary_monitor(&self) -> Option<RootMonitorHandle> {
pub fn primary_monitor(&self) -> Option<MonitorHandle> {
match self {
#[cfg(feature = "x11")]
Window::X(ref window) => {
let primary_monitor = MonitorHandle::X(window.primary_monitor());
Some(RootMonitorHandle {
inner: primary_monitor,
})
Some(primary_monitor)
}
#[cfg(feature = "wayland")]
Window::Wayland(ref window) => window.primary_monitor(),
@@ -804,16 +797,14 @@ impl<T> EventLoopWindowTarget<T> {
}

#[inline]
pub fn primary_monitor(&self) -> Option<RootMonitorHandle> {
pub fn primary_monitor(&self) -> Option<MonitorHandle> {
match *self {
#[cfg(feature = "wayland")]
EventLoopWindowTarget::Wayland(ref evlp) => evlp.primary_monitor(),
#[cfg(feature = "x11")]
EventLoopWindowTarget::X(ref evlp) => {
let primary_monitor = MonitorHandle::X(evlp.x_connection().primary_monitor());
Some(RootMonitorHandle {
inner: primary_monitor,
})
Some(primary_monitor)
}
}
}
9 changes: 3 additions & 6 deletions src/platform_impl/linux/wayland/output.rs
Original file line number Diff line number Diff line change
@@ -8,7 +8,6 @@ use sctk::environment::Environment;
use sctk::output::OutputStatusListener;

use crate::dpi::{PhysicalPosition, PhysicalSize};
use crate::monitor::MonitorHandle as RootMonitorHandle;
use crate::platform_impl::platform::{
MonitorHandle as PlatformMonitorHandle, VideoMode as PlatformVideoMode,
};
@@ -224,10 +223,8 @@ impl VideoMode {
self.refresh_rate_millihertz
}

pub fn monitor(&self) -> RootMonitorHandle {
RootMonitorHandle {
inner: PlatformMonitorHandle::Wayland(self.monitor.clone()),
}
pub fn monitor(&self) -> PlatformMonitorHandle {
PlatformMonitorHandle::Wayland(self.monitor.clone())
}
}

@@ -243,7 +240,7 @@ impl<T> EventLoopWindowTarget<T> {
}

#[inline]
pub fn primary_monitor(&self) -> Option<RootMonitorHandle> {
pub fn primary_monitor(&self) -> Option<PlatformMonitorHandle> {
// There's no primary monitor on Wayland.
None
}
24 changes: 11 additions & 13 deletions src/platform_impl/linux/wayland/window/mod.rs
Original file line number Diff line number Diff line change
@@ -205,12 +205,11 @@ impl Window {
warn!("`Fullscreen::Exclusive` is ignored on Wayland")
}
Some(Fullscreen::Borderless(monitor)) => {
let monitor =
monitor.and_then(|RootMonitorHandle { inner: monitor }| match monitor {
PlatformMonitorHandle::Wayland(monitor) => Some(monitor.proxy),
#[cfg(feature = "x11")]
PlatformMonitorHandle::X(_) => None,
});
let monitor = monitor.and_then(|monitor| match monitor.inner {
PlatformMonitorHandle::Wayland(monitor) => Some(monitor.proxy),
#[cfg(feature = "x11")]
PlatformMonitorHandle::X(_) => None,
});

window.set_fullscreen(monitor.as_ref());
}
@@ -462,12 +461,11 @@ impl Window {
return;
}
Some(Fullscreen::Borderless(monitor)) => {
let monitor =
monitor.and_then(|RootMonitorHandle { inner: monitor }| match monitor {
PlatformMonitorHandle::Wayland(monitor) => Some(monitor.proxy),
#[cfg(feature = "x11")]
PlatformMonitorHandle::X(_) => None,
});
let monitor = monitor.and_then(|monitor| match monitor.inner {
PlatformMonitorHandle::Wayland(monitor) => Some(monitor.proxy),
#[cfg(feature = "x11")]
PlatformMonitorHandle::X(_) => None,
});

WindowRequest::Fullscreen(monitor)
}
@@ -576,7 +574,7 @@ impl Window {
}

#[inline]
pub fn primary_monitor(&self) -> Option<RootMonitorHandle> {
pub fn primary_monitor(&self) -> Option<PlatformMonitorHandle> {
None
}

7 changes: 2 additions & 5 deletions src/platform_impl/linux/x11/monitor.rs
Original file line number Diff line number Diff line change
@@ -13,7 +13,6 @@ use super::{
};
use crate::{
dpi::{PhysicalPosition, PhysicalSize},
monitor::MonitorHandle as RootMonitorHandle,
platform_impl::{MonitorHandle as PlatformMonitorHandle, VideoMode as PlatformVideoMode},
};

@@ -53,10 +52,8 @@ impl VideoMode {
}

#[inline]
pub fn monitor(&self) -> RootMonitorHandle {
RootMonitorHandle {
inner: PlatformMonitorHandle::X(self.monitor.clone().unwrap()),
}
pub fn monitor(&self) -> PlatformMonitorHandle {
PlatformMonitorHandle::X(self.monitor.clone().unwrap())
}
}

5 changes: 2 additions & 3 deletions src/platform_impl/macos/event_loop.rs
Original file line number Diff line number Diff line change
@@ -22,7 +22,6 @@ use raw_window_handle::{AppKitDisplayHandle, RawDisplayHandle};
use crate::{
event::Event,
event_loop::{ControlFlow, EventLoopClosed, EventLoopWindowTarget as RootWindowTarget},
monitor::MonitorHandle as RootMonitorHandle,
platform::macos::ActivationPolicy,
platform_impl::{
get_aux_state_mut,
@@ -84,9 +83,9 @@ impl<T: 'static> EventLoopWindowTarget<T> {
}

#[inline]
pub fn primary_monitor(&self) -> Option<RootMonitorHandle> {
pub fn primary_monitor(&self) -> Option<MonitorHandle> {
let monitor = monitor::primary_monitor();
Some(RootMonitorHandle { inner: monitor })
Some(monitor)
}

#[inline]
Loading