Simplify internal type construction

This commit is contained in:
Mads Marquart 2022-09-21 10:04:28 +02:00 committed by GitHub
parent 25b129362f
commit fafdedfb7d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
30 changed files with 242 additions and 285 deletions

View file

@ -41,11 +41,11 @@ use crate::{
ControlFlow, DeviceEventFilter, EventLoopClosed, EventLoopWindowTarget as RootELW,
},
icon::Icon,
monitor::{MonitorHandle as RootMonitorHandle, VideoMode as RootVideoMode},
window::{CursorGrabMode, CursorIcon, Fullscreen, UserAttentionType, WindowAttributes},
window::{CursorGrabMode, CursorIcon, UserAttentionType, WindowAttributes},
};
pub(crate) use crate::icon::RgbaIcon as PlatformIcon;
pub(self) use crate::platform_impl::Fullscreen;
#[cfg(feature = "wayland")]
pub mod wayland;
@ -268,7 +268,7 @@ impl MonitorHandle {
}
#[inline]
pub fn video_modes(&self) -> Box<dyn Iterator<Item = RootVideoMode>> {
pub fn video_modes(&self) -> Box<dyn Iterator<Item = VideoMode>> {
x11_or_wayland!(match self; MonitorHandle(m) => Box::new(m.video_modes()))
}
}
@ -298,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())
}
}
@ -458,12 +458,12 @@ impl Window {
}
#[inline]
pub fn fullscreen(&self) -> Option<Fullscreen> {
pub(crate) fn fullscreen(&self) -> Option<Fullscreen> {
x11_or_wayland!(match self; Window(w) => w.fullscreen())
}
#[inline]
pub fn set_fullscreen(&self, monitor: Option<Fullscreen>) {
pub(crate) fn set_fullscreen(&self, monitor: Option<Fullscreen>) {
x11_or_wayland!(match self; Window(w) => w.set_fullscreen(monitor))
}
@ -531,21 +531,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)
}
}
}
@ -569,14 +565,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(),
@ -813,16 +807,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)
}
}
}

View file

@ -8,7 +8,6 @@ use sctk::environment::Environment;
use sctk::output::OutputStatusListener;
use crate::dpi::{PhysicalPosition, PhysicalSize};
use crate::monitor::{MonitorHandle as RootMonitorHandle, VideoMode as RootVideoMode};
use crate::platform_impl::platform::{
MonitorHandle as PlatformMonitorHandle, VideoMode as PlatformVideoMode,
};
@ -183,19 +182,19 @@ impl MonitorHandle {
}
#[inline]
pub fn video_modes(&self) -> impl Iterator<Item = RootVideoMode> {
pub fn video_modes(&self) -> impl Iterator<Item = PlatformVideoMode> {
let modes = sctk::output::with_output_info(&self.proxy, |info| info.modes.clone())
.unwrap_or_default();
let monitor = self.clone();
modes.into_iter().map(move |mode| RootVideoMode {
video_mode: PlatformVideoMode::Wayland(VideoMode {
modes.into_iter().map(move |mode| {
PlatformVideoMode::Wayland(VideoMode {
size: (mode.dimensions.0 as u32, mode.dimensions.1 as u32).into(),
refresh_rate_millihertz: mode.refresh_rate as u32,
bit_depth: 32,
monitor: monitor.clone(),
}),
})
})
}
}
@ -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
}

View file

@ -14,14 +14,11 @@ use sctk::window::Decorations;
use crate::dpi::{LogicalSize, PhysicalPosition, PhysicalSize, Position, Size};
use crate::error::{ExternalError, NotSupportedError, OsError as RootOsError};
use crate::monitor::MonitorHandle as RootMonitorHandle;
use crate::platform_impl::{
MonitorHandle as PlatformMonitorHandle, OsError,
Fullscreen, MonitorHandle as PlatformMonitorHandle, OsError,
PlatformSpecificWindowBuilderAttributes as PlatformAttributes,
};
use crate::window::{
CursorGrabMode, CursorIcon, Fullscreen, Theme, UserAttentionType, WindowAttributes,
};
use crate::window::{CursorGrabMode, CursorIcon, Theme, UserAttentionType, WindowAttributes};
use super::env::WindowingFeatures;
use super::event_loop::WinitState;
@ -218,12 +215,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 {
PlatformMonitorHandle::Wayland(monitor) => Some(monitor.proxy),
#[cfg(feature = "x11")]
PlatformMonitorHandle::X(_) => None,
});
window.set_fullscreen(monitor.as_ref());
}
@ -469,11 +465,9 @@ impl Window {
}
#[inline]
pub fn fullscreen(&self) -> Option<Fullscreen> {
pub(crate) fn fullscreen(&self) -> Option<Fullscreen> {
if self.fullscreen.load(Ordering::Relaxed) {
let current_monitor = self.current_monitor().map(|monitor| RootMonitorHandle {
inner: PlatformMonitorHandle::Wayland(monitor),
});
let current_monitor = self.current_monitor().map(PlatformMonitorHandle::Wayland);
Some(Fullscreen::Borderless(current_monitor))
} else {
@ -482,19 +476,18 @@ impl Window {
}
#[inline]
pub fn set_fullscreen(&self, fullscreen: Option<Fullscreen>) {
pub(crate) fn set_fullscreen(&self, fullscreen: Option<Fullscreen>) {
let fullscreen_request = match fullscreen {
Some(Fullscreen::Exclusive(_)) => {
warn!("`Fullscreen::Exclusive` is ignored on Wayland");
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 {
PlatformMonitorHandle::Wayland(monitor) => Some(monitor.proxy),
#[cfg(feature = "x11")]
PlatformMonitorHandle::X(_) => None,
});
WindowRequest::Fullscreen(monitor)
}
@ -603,7 +596,7 @@ impl Window {
}
#[inline]
pub fn primary_monitor(&self) -> Option<RootMonitorHandle> {
pub fn primary_monitor(&self) -> Option<PlatformMonitorHandle> {
None
}

View file

@ -13,7 +13,6 @@ use super::{
};
use crate::{
dpi::{PhysicalPosition, PhysicalSize},
monitor::{MonitorHandle as RootMonitorHandle, VideoMode as RootVideoMode},
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())
}
}
@ -199,13 +196,11 @@ impl MonitorHandle {
}
#[inline]
pub fn video_modes(&self) -> impl Iterator<Item = RootVideoMode> {
pub fn video_modes(&self) -> impl Iterator<Item = PlatformVideoMode> {
let monitor = self.clone();
self.video_modes.clone().into_iter().map(move |mut x| {
x.monitor = Some(monitor.clone());
RootVideoMode {
video_mode: PlatformVideoMode::X(x),
}
PlatformVideoMode::X(x)
})
}
}

View file

@ -15,13 +15,12 @@ use x11_dl::xlib::TrueColor;
use crate::{
dpi::{PhysicalPosition, PhysicalSize, Position, Size},
error::{ExternalError, NotSupportedError, OsError as RootOsError},
monitor::{MonitorHandle as RootMonitorHandle, VideoMode as RootVideoMode},
platform_impl::{
x11::{ime::ImeContextCreationError, MonitorHandle as X11MonitorHandle},
MonitorHandle as PlatformMonitorHandle, OsError, PlatformSpecificWindowBuilderAttributes,
VideoMode as PlatformVideoMode,
Fullscreen, MonitorHandle as PlatformMonitorHandle, OsError,
PlatformSpecificWindowBuilderAttributes, VideoMode as PlatformVideoMode,
},
window::{CursorGrabMode, CursorIcon, Fullscreen, Icon, UserAttentionType, WindowAttributes},
window::{CursorGrabMode, CursorIcon, Icon, UserAttentionType, WindowAttributes},
};
use super::{
@ -40,9 +39,9 @@ pub struct SharedState {
pub is_decorated: bool,
pub last_monitor: X11MonitorHandle,
pub dpi_adjusted: Option<(u32, u32)>,
pub fullscreen: Option<Fullscreen>,
pub(crate) fullscreen: Option<Fullscreen>,
// Set when application calls `set_fullscreen` when window is not visible
pub desired_fullscreen: Option<Option<Fullscreen>>,
pub(crate) desired_fullscreen: Option<Option<Fullscreen>>,
// Used to restore position after exiting fullscreen
pub restore_position: Option<(i32, i32)>,
// Used to restore video mode after exiting fullscreen
@ -638,17 +637,10 @@ impl UnownedWindow {
// fullscreen, so we can restore it upon exit, as XRandR does not
// provide a mechanism to set this per app-session or restore this
// to the desktop video mode as macOS and Windows do
(
&None,
&Some(Fullscreen::Exclusive(RootVideoMode {
video_mode: PlatformVideoMode::X(ref video_mode),
})),
)
(&None, &Some(Fullscreen::Exclusive(PlatformVideoMode::X(ref video_mode))))
| (
&Some(Fullscreen::Borderless(_)),
&Some(Fullscreen::Exclusive(RootVideoMode {
video_mode: PlatformVideoMode::X(ref video_mode),
})),
&Some(Fullscreen::Exclusive(PlatformVideoMode::X(ref video_mode))),
) => {
let monitor = video_mode.monitor.as_ref().unwrap();
shared_state_lock.desktop_video_mode =
@ -679,12 +671,12 @@ impl UnownedWindow {
}
Some(fullscreen) => {
let (video_mode, monitor) = match fullscreen {
Fullscreen::Exclusive(RootVideoMode {
video_mode: PlatformVideoMode::X(ref video_mode),
}) => (Some(video_mode), video_mode.monitor.clone().unwrap()),
Fullscreen::Borderless(Some(RootMonitorHandle {
inner: PlatformMonitorHandle::X(monitor),
})) => (None, monitor),
Fullscreen::Exclusive(PlatformVideoMode::X(ref video_mode)) => {
(Some(video_mode), video_mode.monitor.clone().unwrap())
}
Fullscreen::Borderless(Some(PlatformMonitorHandle::X(monitor))) => {
(None, monitor)
}
Fullscreen::Borderless(None) => (None, self.current_monitor()),
#[cfg(feature = "wayland")]
_ => unreachable!(),
@ -737,7 +729,7 @@ impl UnownedWindow {
}
#[inline]
pub fn fullscreen(&self) -> Option<Fullscreen> {
pub(crate) fn fullscreen(&self) -> Option<Fullscreen> {
let shared_state = self.shared_state_lock();
shared_state
@ -747,7 +739,7 @@ impl UnownedWindow {
}
#[inline]
pub fn set_fullscreen(&self, fullscreen: Option<Fullscreen>) {
pub(crate) fn set_fullscreen(&self, fullscreen: Option<Fullscreen>) {
if let Some(flusher) = self.set_fullscreen_inner(fullscreen) {
flusher
.sync()