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:
parent
05dd31b8ea
commit
340f951d10
34 changed files with 2756 additions and 2335 deletions
|
|
@ -1,23 +1,16 @@
|
|||
use std::{
|
||||
ops::Deref,
|
||||
sync::{Mutex, Weak},
|
||||
};
|
||||
use std::mem;
|
||||
use std::ops::Deref;
|
||||
use std::sync::{Mutex, Weak};
|
||||
|
||||
use cocoa::{
|
||||
appkit::{CGFloat, NSScreen, NSWindow, NSWindowStyleMask},
|
||||
base::{id, nil},
|
||||
foundation::{NSPoint, NSSize, NSString},
|
||||
};
|
||||
use dispatch::Queue;
|
||||
use objc::foundation::is_main_thread;
|
||||
use objc::rc::autoreleasepool;
|
||||
use objc::runtime::Bool;
|
||||
use objc2::foundation::{is_main_thread, CGFloat, NSPoint, NSSize, NSString};
|
||||
use objc2::rc::{autoreleasepool, Id, Shared};
|
||||
|
||||
use crate::{
|
||||
dpi::LogicalSize,
|
||||
platform_impl::platform::{
|
||||
appkit::{NSScreen, NSWindow, NSWindowLevel, NSWindowStyleMask},
|
||||
ffi,
|
||||
util::IdRef,
|
||||
window::{SharedState, SharedStateMutexGuard},
|
||||
},
|
||||
};
|
||||
|
|
@ -37,91 +30,88 @@ impl<T> Deref for MainThreadSafe<T> {
|
|||
}
|
||||
}
|
||||
|
||||
unsafe fn set_style_mask(ns_window: id, ns_view: id, mask: NSWindowStyleMask) {
|
||||
ns_window.setStyleMask_(mask);
|
||||
fn set_style_mask(window: &NSWindow, mask: NSWindowStyleMask) {
|
||||
window.setStyleMask(mask);
|
||||
// If we don't do this, key handling will break
|
||||
// (at least until the window is clicked again/etc.)
|
||||
ns_window.makeFirstResponder_(ns_view);
|
||||
let _ = window.makeFirstResponder(Some(&window.contentView()));
|
||||
}
|
||||
|
||||
// Always use this function instead of trying to modify `styleMask` directly!
|
||||
// `setStyleMask:` isn't thread-safe, so we have to use Grand Central Dispatch.
|
||||
// Otherwise, this would vomit out errors about not being on the main thread
|
||||
// and fail to do anything.
|
||||
pub unsafe fn set_style_mask_async(ns_window: id, ns_view: id, mask: NSWindowStyleMask) {
|
||||
let ns_window = MainThreadSafe(ns_window);
|
||||
let ns_view = MainThreadSafe(ns_view);
|
||||
pub(crate) fn set_style_mask_async(window: &NSWindow, mask: NSWindowStyleMask) {
|
||||
// TODO(madsmtm): Remove this 'static hack!
|
||||
let window = unsafe { MainThreadSafe(mem::transmute::<&NSWindow, &'static NSWindow>(window)) };
|
||||
Queue::main().exec_async(move || {
|
||||
set_style_mask(*ns_window, *ns_view, mask);
|
||||
set_style_mask(*window, mask);
|
||||
});
|
||||
}
|
||||
pub unsafe fn set_style_mask_sync(ns_window: id, ns_view: id, mask: NSWindowStyleMask) {
|
||||
pub(crate) fn set_style_mask_sync(window: &NSWindow, mask: NSWindowStyleMask) {
|
||||
if is_main_thread() {
|
||||
set_style_mask(ns_window, ns_view, mask);
|
||||
set_style_mask(window, mask);
|
||||
} else {
|
||||
let ns_window = MainThreadSafe(ns_window);
|
||||
let ns_view = MainThreadSafe(ns_view);
|
||||
let window = MainThreadSafe(window);
|
||||
Queue::main().exec_sync(move || {
|
||||
set_style_mask(*ns_window, *ns_view, mask);
|
||||
set_style_mask(*window, mask);
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// `setContentSize:` isn't thread-safe either, though it doesn't log any errors
|
||||
// and just fails silently. Anyway, GCD to the rescue!
|
||||
pub unsafe fn set_content_size_async(ns_window: id, size: LogicalSize<f64>) {
|
||||
let ns_window = MainThreadSafe(ns_window);
|
||||
pub(crate) fn set_content_size_async(window: &NSWindow, size: LogicalSize<f64>) {
|
||||
let window = unsafe { MainThreadSafe(mem::transmute::<&NSWindow, &'static NSWindow>(window)) };
|
||||
Queue::main().exec_async(move || {
|
||||
ns_window.setContentSize_(NSSize::new(size.width as CGFloat, size.height as CGFloat));
|
||||
window.setContentSize(NSSize::new(size.width as CGFloat, size.height as CGFloat));
|
||||
});
|
||||
}
|
||||
|
||||
// `setFrameTopLeftPoint:` isn't thread-safe, but fortunately has the courtesy
|
||||
// to log errors.
|
||||
pub unsafe fn set_frame_top_left_point_async(ns_window: id, point: NSPoint) {
|
||||
let ns_window = MainThreadSafe(ns_window);
|
||||
pub(crate) fn set_frame_top_left_point_async(window: &NSWindow, point: NSPoint) {
|
||||
let window = unsafe { MainThreadSafe(mem::transmute::<&NSWindow, &'static NSWindow>(window)) };
|
||||
Queue::main().exec_async(move || {
|
||||
ns_window.setFrameTopLeftPoint_(point);
|
||||
window.setFrameTopLeftPoint(point);
|
||||
});
|
||||
}
|
||||
|
||||
// `setFrameTopLeftPoint:` isn't thread-safe, and fails silently.
|
||||
pub unsafe fn set_level_async(ns_window: id, level: ffi::NSWindowLevel) {
|
||||
let ns_window = MainThreadSafe(ns_window);
|
||||
pub(crate) fn set_level_async(window: &NSWindow, level: NSWindowLevel) {
|
||||
let window = unsafe { MainThreadSafe(mem::transmute::<&NSWindow, &'static NSWindow>(window)) };
|
||||
Queue::main().exec_async(move || {
|
||||
ns_window.setLevel_(level as _);
|
||||
window.setLevel(level);
|
||||
});
|
||||
}
|
||||
|
||||
// `setIgnoresMouseEvents_:` isn't thread-safe, and fails silently.
|
||||
pub unsafe fn set_ignore_mouse_events(ns_window: id, ignore: bool) {
|
||||
let ns_window = MainThreadSafe(ns_window);
|
||||
pub(crate) fn set_ignore_mouse_events(window: &NSWindow, ignore: bool) {
|
||||
let window = unsafe { MainThreadSafe(mem::transmute::<&NSWindow, &'static NSWindow>(window)) };
|
||||
Queue::main().exec_async(move || {
|
||||
ns_window.setIgnoresMouseEvents_(Bool::from(ignore).as_raw());
|
||||
window.setIgnoresMouseEvents(ignore);
|
||||
});
|
||||
}
|
||||
|
||||
// `toggleFullScreen` is thread-safe, but our additional logic to account for
|
||||
// window styles isn't.
|
||||
pub unsafe fn toggle_full_screen_async(
|
||||
ns_window: id,
|
||||
ns_view: id,
|
||||
pub(crate) fn toggle_full_screen_async(
|
||||
window: &NSWindow,
|
||||
not_fullscreen: bool,
|
||||
shared_state: Weak<Mutex<SharedState>>,
|
||||
) {
|
||||
let ns_window = MainThreadSafe(ns_window);
|
||||
let ns_view = MainThreadSafe(ns_view);
|
||||
let window = unsafe { MainThreadSafe(mem::transmute::<&NSWindow, &'static NSWindow>(window)) };
|
||||
let shared_state = MainThreadSafe(shared_state);
|
||||
Queue::main().exec_async(move || {
|
||||
// `toggleFullScreen` doesn't work if the `StyleMask` is none, so we
|
||||
// set a normal style temporarily. The previous state will be
|
||||
// restored in `WindowDelegate::window_did_exit_fullscreen`.
|
||||
if not_fullscreen {
|
||||
let curr_mask = ns_window.styleMask();
|
||||
let curr_mask = window.styleMask();
|
||||
let required =
|
||||
NSWindowStyleMask::NSTitledWindowMask | NSWindowStyleMask::NSResizableWindowMask;
|
||||
if !curr_mask.contains(required) {
|
||||
set_style_mask(*ns_window, *ns_view, required);
|
||||
set_style_mask(*window, required);
|
||||
if let Some(shared_state) = shared_state.upgrade() {
|
||||
let mut shared_state_lock = SharedStateMutexGuard::new(
|
||||
shared_state.lock().unwrap(),
|
||||
|
|
@ -134,26 +124,29 @@ pub unsafe fn toggle_full_screen_async(
|
|||
// Window level must be restored from `CGShieldingWindowLevel()
|
||||
// + 1` back to normal in order for `toggleFullScreen` to do
|
||||
// anything
|
||||
ns_window.setLevel_(0);
|
||||
ns_window.toggleFullScreen_(nil);
|
||||
window.setLevel(NSWindowLevel::Normal);
|
||||
window.toggleFullScreen(None);
|
||||
});
|
||||
}
|
||||
|
||||
pub unsafe fn restore_display_mode_async(ns_screen: u32) {
|
||||
pub(crate) unsafe fn restore_display_mode_async(ns_screen: u32) {
|
||||
Queue::main().exec_async(move || {
|
||||
ffi::CGRestorePermanentDisplayConfiguration();
|
||||
assert_eq!(ffi::CGDisplayRelease(ns_screen), ffi::kCGErrorSuccess);
|
||||
unsafe { ffi::CGRestorePermanentDisplayConfiguration() };
|
||||
assert_eq!(
|
||||
unsafe { ffi::CGDisplayRelease(ns_screen) },
|
||||
ffi::kCGErrorSuccess
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
// `setMaximized` is not thread-safe
|
||||
pub unsafe fn set_maximized_async(
|
||||
ns_window: id,
|
||||
pub(crate) fn set_maximized_async(
|
||||
window: &NSWindow,
|
||||
is_zoomed: bool,
|
||||
maximized: bool,
|
||||
shared_state: Weak<Mutex<SharedState>>,
|
||||
) {
|
||||
let ns_window = MainThreadSafe(ns_window);
|
||||
let window = unsafe { MainThreadSafe(mem::transmute::<&NSWindow, &'static NSWindow>(window)) };
|
||||
let shared_state = MainThreadSafe(shared_state);
|
||||
Queue::main().exec_async(move || {
|
||||
if let Some(shared_state) = shared_state.upgrade() {
|
||||
|
|
@ -162,7 +155,7 @@ pub unsafe fn set_maximized_async(
|
|||
|
||||
// Save the standard frame sized if it is not zoomed
|
||||
if !is_zoomed {
|
||||
shared_state_lock.standard_frame = Some(NSWindow::frame(*ns_window));
|
||||
shared_state_lock.standard_frame = Some(window.frame());
|
||||
}
|
||||
|
||||
shared_state_lock.maximized = maximized;
|
||||
|
|
@ -172,21 +165,21 @@ pub unsafe fn set_maximized_async(
|
|||
return;
|
||||
}
|
||||
|
||||
if ns_window
|
||||
if window
|
||||
.styleMask()
|
||||
.contains(NSWindowStyleMask::NSResizableWindowMask)
|
||||
{
|
||||
// Just use the native zoom if resizable
|
||||
ns_window.zoom_(nil);
|
||||
window.zoom(None);
|
||||
} else {
|
||||
// if it's not resizable, we set the frame directly
|
||||
let new_rect = if maximized {
|
||||
let screen = NSScreen::mainScreen(nil);
|
||||
NSScreen::visibleFrame(screen)
|
||||
let screen = NSScreen::main().expect("no screen found");
|
||||
screen.visibleFrame()
|
||||
} else {
|
||||
shared_state_lock.saved_standard_frame()
|
||||
};
|
||||
ns_window.setFrame_display_(new_rect, Bool::NO.as_raw());
|
||||
window.setFrame_display(new_rect, false);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
|
@ -194,30 +187,29 @@ pub unsafe fn set_maximized_async(
|
|||
|
||||
// `orderOut:` isn't thread-safe. Calling it from another thread actually works,
|
||||
// but with an odd delay.
|
||||
pub unsafe fn order_out_async(ns_window: id) {
|
||||
let ns_window = MainThreadSafe(ns_window);
|
||||
pub(crate) fn order_out_async(window: &NSWindow) {
|
||||
let window = unsafe { MainThreadSafe(mem::transmute::<&NSWindow, &'static NSWindow>(window)) };
|
||||
Queue::main().exec_async(move || {
|
||||
ns_window.orderOut_(nil);
|
||||
window.orderOut(None);
|
||||
});
|
||||
}
|
||||
|
||||
// `makeKeyAndOrderFront:` isn't thread-safe. Calling it from another thread
|
||||
// actually works, but with an odd delay.
|
||||
pub unsafe fn make_key_and_order_front_async(ns_window: id) {
|
||||
let ns_window = MainThreadSafe(ns_window);
|
||||
pub(crate) fn make_key_and_order_front_async(window: &NSWindow) {
|
||||
let window = unsafe { MainThreadSafe(mem::transmute::<&NSWindow, &'static NSWindow>(window)) };
|
||||
Queue::main().exec_async(move || {
|
||||
ns_window.makeKeyAndOrderFront_(nil);
|
||||
window.makeKeyAndOrderFront(None);
|
||||
});
|
||||
}
|
||||
|
||||
// `setTitle:` isn't thread-safe. Calling it from another thread invalidates the
|
||||
// window drag regions, which throws an exception when not done in the main
|
||||
// thread
|
||||
pub unsafe fn set_title_async(ns_window: id, title: String) {
|
||||
let ns_window = MainThreadSafe(ns_window);
|
||||
pub(crate) fn set_title_async(window: &NSWindow, title: String) {
|
||||
let window = unsafe { MainThreadSafe(mem::transmute::<&NSWindow, &'static NSWindow>(window)) };
|
||||
Queue::main().exec_async(move || {
|
||||
let title = IdRef::new(NSString::alloc(nil).init_str(&title));
|
||||
ns_window.setTitle_(*title);
|
||||
window.setTitle(&NSString::from_str(&title));
|
||||
});
|
||||
}
|
||||
|
||||
|
|
@ -225,12 +217,12 @@ pub unsafe fn set_title_async(ns_window: id, title: String) {
|
|||
// thread. Though, it's a good idea to look into that more...
|
||||
//
|
||||
// ArturKovacs: It's important that this operation keeps the underlying window alive
|
||||
// through the `IdRef` because otherwise it would dereference free'd memory
|
||||
pub unsafe fn close_async(ns_window: IdRef) {
|
||||
let ns_window = MainThreadSafe(ns_window);
|
||||
// through the `Id` because otherwise it would dereference free'd memory
|
||||
pub(crate) fn close_async(window: Id<NSWindow, Shared>) {
|
||||
let window = MainThreadSafe(window);
|
||||
Queue::main().exec_async(move || {
|
||||
autoreleasepool(move |_| {
|
||||
ns_window.close();
|
||||
window.close();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue