Refactor macOS to use new objc2 features (#2465)

* Remove UnownedWindow::inner_rect

* Refactor custom view to use much less `unsafe`

The compiler fence is safe to get rid of now since `interpretKeyEvents` takes `&mut self`

* Refactor Window to use much less unsafe

* Refactor NSApplication usage to have much less unsafe

* Remove cocoa dependency

* Enable `deny(unsafe_op_in_unsafe_fn)` on macOS

Also re-enable clippy `let_unit_value` lint

* Remove #[macro_use] on macOS

* Refactor window delegate to use much less unsafe
This commit is contained in:
Mads Marquart 2022-09-08 16:45:29 +02:00 committed by GitHub
parent 05dd31b8ea
commit 340f951d10
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
34 changed files with 2756 additions and 2335 deletions

View file

@ -1,5 +1,4 @@
#![cfg(target_os = "macos")]
#![allow(clippy::let_unit_value)]
#![deny(unsafe_op_in_unsafe_fn)]
#[macro_use]
mod util;
@ -18,19 +17,21 @@ mod view;
mod window;
mod window_delegate;
use std::{fmt, ops::Deref, sync::Arc};
use std::{fmt, ops::Deref};
use self::window::WinitWindow;
use self::window_delegate::WinitWindowDelegate;
pub(crate) use self::{
event_loop::{
EventLoop, EventLoopProxy, EventLoopWindowTarget, PlatformSpecificEventLoopAttributes,
},
monitor::{MonitorHandle, VideoMode},
window::{PlatformSpecificWindowBuilderAttributes, UnownedWindow, WindowId},
window::{PlatformSpecificWindowBuilderAttributes, WindowId},
};
use crate::{
error::OsError as RootOsError, event::DeviceId as RootDeviceId, window::WindowAttributes,
};
use objc::rc::autoreleasepool;
use objc2::rc::{autoreleasepool, Id, Shared};
pub(crate) use crate::icon::NoIcon as PlatformIcon;
@ -46,10 +47,17 @@ impl DeviceId {
// Constant device ID; to be removed when if backend is updated to report real device IDs.
pub(crate) const DEVICE_ID: RootDeviceId = RootDeviceId(DeviceId);
pub struct Window {
window: Arc<UnownedWindow>,
pub(crate) struct Window {
pub(crate) window: Id<WinitWindow, Shared>,
// We keep this around so that it doesn't get dropped until the window does.
_delegate: util::IdRef,
_delegate: Id<WinitWindowDelegate, Shared>,
}
impl Drop for Window {
fn drop(&mut self) {
// Ensure the window is closed
util::close_async(Id::into_super(self.window.clone()));
}
}
#[derive(Debug)]
@ -62,7 +70,7 @@ unsafe impl Send for Window {}
unsafe impl Sync for Window {}
impl Deref for Window {
type Target = UnownedWindow;
type Target = WinitWindow;
#[inline]
fn deref(&self) -> &Self::Target {
&*self.window
@ -75,7 +83,7 @@ impl Window {
attributes: WindowAttributes,
pl_attribs: PlatformSpecificWindowBuilderAttributes,
) -> Result<Self, RootOsError> {
let (window, _delegate) = autoreleasepool(|_| UnownedWindow::new(attributes, pl_attribs))?;
let (window, _delegate) = autoreleasepool(|_| WinitWindow::new(attributes, pl_attribs))?;
Ok(Window { window, _delegate })
}
}