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

@ -25,7 +25,6 @@ use super::appkit::{NSApp, NSApplicationActivationPolicy, NSEvent};
use crate::{
event::Event,
event_loop::{ControlFlow, EventLoopClosed, EventLoopWindowTarget as RootWindowTarget},
monitor::MonitorHandle as RootMonitorHandle,
platform::macos::ActivationPolicy,
platform_impl::platform::{
app::WinitApplication,
@ -83,9 +82,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]

View file

@ -34,6 +34,7 @@ use crate::{
use objc2::rc::{autoreleasepool, Id, Shared};
pub(crate) use crate::icon::NoIcon as PlatformIcon;
pub(self) use crate::platform_impl::Fullscreen;
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct DeviceId;

View file

@ -10,10 +10,7 @@ use objc2::rc::{Id, Shared};
use super::appkit::NSScreen;
use super::ffi;
use crate::{
dpi::{PhysicalPosition, PhysicalSize},
monitor::{MonitorHandle as RootMonitorHandle, VideoMode as RootVideoMode},
};
use crate::dpi::{PhysicalPosition, PhysicalSize};
#[derive(Clone)]
pub struct VideoMode {
@ -89,10 +86,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()
}
}
@ -234,7 +229,7 @@ impl MonitorHandle {
}
}
pub fn video_modes(&self) -> impl Iterator<Item = RootVideoMode> {
pub fn video_modes(&self) -> impl Iterator<Item = VideoMode> {
let refresh_rate_millihertz = self.refresh_rate_millihertz().unwrap_or(0);
let monitor = self.clone();
@ -279,7 +274,7 @@ impl MonitorHandle {
unimplemented!()
};
let video_mode = VideoMode {
VideoMode {
size: (
ffi::CGDisplayModeGetPixelWidth(mode) as u32,
ffi::CGDisplayModeGetPixelHeight(mode) as u32,
@ -288,9 +283,7 @@ impl MonitorHandle {
bit_depth,
monitor: monitor.clone(),
native_mode: NativeDisplayMode(mode),
};
RootVideoMode { video_mode }
}
})
}
}

View file

@ -18,7 +18,6 @@ use crate::{
},
error::{ExternalError, NotSupportedError, OsError as RootOsError},
icon::Icon,
monitor::{MonitorHandle as RootMonitorHandle, VideoMode as RootVideoMode},
platform::macos::WindowExtMacOS,
platform_impl::platform::{
app_state::AppState,
@ -27,11 +26,10 @@ use crate::{
util,
view::WinitView,
window_delegate::WinitWindowDelegate,
OsError,
Fullscreen, OsError,
},
window::{
CursorGrabMode, CursorIcon, Fullscreen, UserAttentionType, WindowAttributes,
WindowId as RootWindowId,
CursorGrabMode, CursorIcon, UserAttentionType, WindowAttributes, WindowId as RootWindowId,
},
};
use core_graphics::display::{CGDisplay, CGPoint};
@ -133,14 +131,14 @@ pub struct SharedState {
pub resizable: bool,
/// This field tracks the current fullscreen state of the window
/// (as seen by `WindowDelegate`).
pub fullscreen: Option<Fullscreen>,
pub(crate) fullscreen: Option<Fullscreen>,
// This is true between windowWillEnterFullScreen and windowDidEnterFullScreen
// or windowWillExitFullScreen and windowDidExitFullScreen.
// We must not toggle fullscreen when this is true.
pub in_fullscreen_transition: bool,
// If it is attempted to toggle fullscreen when in_fullscreen_transition is true,
// Set target_fullscreen and do after fullscreen transition is end.
pub target_fullscreen: Option<Option<Fullscreen>>,
pub(crate) target_fullscreen: Option<Option<Fullscreen>>,
pub maximized: bool,
pub standard_frame: Option<NSRect>,
is_simple_fullscreen: bool,
@ -211,11 +209,11 @@ impl WinitWindow {
}
let this = autoreleasepool(|_| {
let screen = match attrs.fullscreen {
Some(Fullscreen::Borderless(Some(RootMonitorHandle { inner: ref monitor })))
| Some(Fullscreen::Exclusive(RootVideoMode {
video_mode: VideoMode { ref monitor, .. },
})) => monitor.ns_screen().or_else(NSScreen::main),
let screen = match &attrs.fullscreen {
Some(Fullscreen::Borderless(Some(monitor)))
| Some(Fullscreen::Exclusive(VideoMode { monitor, .. })) => {
monitor.ns_screen().or_else(NSScreen::main)
}
Some(Fullscreen::Borderless(None)) => NSScreen::main(),
None => None,
};
@ -758,7 +756,7 @@ impl WinitWindow {
}
#[inline]
pub fn fullscreen(&self) -> Option<Fullscreen> {
pub(crate) fn fullscreen(&self) -> Option<Fullscreen> {
let shared_state_lock = self.lock_shared_state("fullscreen");
shared_state_lock.fullscreen.clone()
}
@ -769,7 +767,7 @@ impl WinitWindow {
}
#[inline]
pub fn set_fullscreen(&self, fullscreen: Option<Fullscreen>) {
pub(crate) fn set_fullscreen(&self, fullscreen: Option<Fullscreen>) {
let mut shared_state_lock = self.lock_shared_state("set_fullscreen");
if shared_state_lock.is_simple_fullscreen {
return;
@ -791,15 +789,9 @@ impl WinitWindow {
// does not take a screen parameter, but uses the current screen)
if let Some(ref fullscreen) = fullscreen {
let new_screen = match fullscreen {
Fullscreen::Borderless(borderless) => {
let RootMonitorHandle { inner: monitor } = borderless
.clone()
.unwrap_or_else(|| self.current_monitor_inner());
monitor
}
Fullscreen::Exclusive(RootVideoMode {
video_mode: VideoMode { ref monitor, .. },
}) => monitor.clone(),
Fullscreen::Borderless(Some(monitor)) => monitor.clone(),
Fullscreen::Borderless(None) => self.current_monitor_inner(),
Fullscreen::Exclusive(video_mode) => video_mode.monitor(),
}
.ns_screen()
.unwrap();
@ -827,7 +819,7 @@ impl WinitWindow {
// parameter, which is not consistent with the docs saying that it
// takes a `NSDictionary`..
let display_id = video_mode.monitor().inner.native_identifier();
let display_id = video_mode.monitor().native_identifier();
let mut fade_token = ffi::kCGDisplayFadeReservationInvalidToken;
@ -861,7 +853,7 @@ impl WinitWindow {
unsafe {
let result = ffi::CGDisplaySetDisplayMode(
display_id,
video_mode.video_mode.native_mode.0,
video_mode.native_mode.0,
std::ptr::null(),
);
assert!(result == ffi::kCGErrorSuccess, "failed to set video mode");
@ -903,9 +895,9 @@ impl WinitWindow {
Arc::downgrade(&*self.shared_state),
);
}
(&Some(Fullscreen::Exclusive(RootVideoMode { ref video_mode })), &None) => {
(&Some(Fullscreen::Exclusive(ref video_mode)), &None) => {
unsafe {
util::restore_display_mode_async(video_mode.monitor().inner.native_identifier())
util::restore_display_mode_async(video_mode.monitor().native_identifier())
};
// Rest of the state is restored by `window_did_exit_fullscreen`
util::toggle_full_screen_async(
@ -937,10 +929,7 @@ impl WinitWindow {
let _: () = msg_send![self, setLevel: ffi::CGShieldingWindowLevel() + 1];
}
}
(
&Some(Fullscreen::Exclusive(RootVideoMode { ref video_mode })),
&Some(Fullscreen::Borderless(_)),
) => {
(&Some(Fullscreen::Exclusive(ref video_mode)), &Some(Fullscreen::Borderless(_))) => {
let presentation_options =
shared_state_lock.save_presentation_opts.unwrap_or_else(|| {
NSApplicationPresentationOptions::NSApplicationPresentationFullScreen
@ -950,7 +939,7 @@ impl WinitWindow {
NSApp().setPresentationOptions(presentation_options);
unsafe {
util::restore_display_mode_async(video_mode.monitor().inner.native_identifier())
util::restore_display_mode_async(video_mode.monitor().native_identifier())
};
// Restore the normal window level following the Borderless fullscreen
@ -1064,15 +1053,13 @@ impl WinitWindow {
#[inline]
// Allow directly accessing the current monitor internally without unwrapping.
pub(crate) fn current_monitor_inner(&self) -> RootMonitorHandle {
pub(crate) fn current_monitor_inner(&self) -> MonitorHandle {
let display_id = self.screen().expect("expected screen").display_id();
RootMonitorHandle {
inner: MonitorHandle::new(display_id),
}
MonitorHandle::new(display_id)
}
#[inline]
pub fn current_monitor(&self) -> Option<RootMonitorHandle> {
pub fn current_monitor(&self) -> Option<MonitorHandle> {
Some(self.current_monitor_inner())
}
@ -1082,9 +1069,9 @@ impl WinitWindow {
}
#[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]

View file

@ -17,8 +17,9 @@ use crate::{
event::{EventProxy, EventWrapper},
util,
window::WinitWindow,
Fullscreen,
},
window::{Fullscreen, WindowId},
window::WindowId,
};
declare_class!(