Initial transition to objc2 (#2452)

* Use objc2

* Use objc2's NSInteger/NSUInteger/NSRange
This commit is contained in:
Mads Marquart 2022-09-02 15:48:02 +02:00 committed by GitHub
parent e0018d0710
commit 112965b4ff
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
23 changed files with 394 additions and 480 deletions

View file

@ -42,13 +42,14 @@ use cocoa::{
NSRequestUserAttentionType, NSScreen, NSView, NSWindow, NSWindowButton, NSWindowStyleMask,
},
base::{id, nil},
foundation::{NSDictionary, NSPoint, NSRect, NSSize, NSUInteger},
foundation::{NSDictionary, NSPoint, NSRect, NSSize},
};
use core_graphics::display::{CGDisplay, CGDisplayMode};
use objc::{
declare::ClassDecl,
declare::ClassBuilder,
foundation::{is_main_thread, NSUInteger},
rc::autoreleasepool,
runtime::{Class, Object, Sel, BOOL, NO, YES},
runtime::{Bool, Class, Object, Sel},
};
use once_cell::sync::Lazy;
@ -119,9 +120,9 @@ unsafe fn create_view(
// macos 10.14 and `true` after 10.15, we should set it to `YES` or `NO` to avoid
// always the default system value in favour of the user's code
if !pl_attribs.disallow_hidpi {
ns_view.setWantsBestResolutionOpenGLSurface_(YES);
ns_view.setWantsBestResolutionOpenGLSurface_(Bool::YES.as_raw());
} else {
ns_view.setWantsBestResolutionOpenGLSurface_(NO);
ns_view.setWantsBestResolutionOpenGLSurface_(Bool::NO.as_raw());
}
// On Mojave, views automatically become layer-backed shortly after being added to
@ -130,7 +131,7 @@ unsafe fn create_view(
// explicitly make the view layer-backed up front so that AppKit doesn't do it
// itself and break the association with its context.
if f64::floor(appkit::NSAppKitVersionNumber) > appkit::NSAppKitVersionNumber10_12 {
ns_view.setWantsLayer(YES);
ns_view.setWantsLayer(Bool::YES.as_raw());
}
(ns_view, cursor_state)
@ -141,7 +142,7 @@ fn create_window(
attrs: &WindowAttributes,
pl_attrs: &PlatformSpecificWindowBuilderAttributes,
) -> Option<IdRef> {
autoreleasepool(|| unsafe {
autoreleasepool(|_| unsafe {
let screen = match attrs.fullscreen {
Some(Fullscreen::Borderless(Some(RootMonitorHandle { inner: ref monitor })))
| Some(Fullscreen::Exclusive(RootVideoMode {
@ -208,17 +209,17 @@ fn create_window(
frame,
masks,
appkit::NSBackingStoreBuffered,
NO,
Bool::NO.as_raw(),
));
ns_window.non_nil().map(|ns_window| {
let title = util::ns_string_id_ref(&attrs.title);
ns_window.setReleasedWhenClosed_(NO);
ns_window.setReleasedWhenClosed_(Bool::NO.as_raw());
ns_window.setTitle_(*title);
ns_window.setAcceptsMouseMovedEvents_(YES);
ns_window.setAcceptsMouseMovedEvents_(Bool::YES.as_raw());
if pl_attrs.titlebar_transparent {
ns_window.setTitlebarAppearsTransparent_(YES);
ns_window.setTitlebarAppearsTransparent_(Bool::YES.as_raw());
}
if pl_attrs.title_hidden {
ns_window.setTitleVisibility_(appkit::NSWindowTitleVisibility::NSWindowTitleHidden);
@ -231,18 +232,15 @@ fn create_window(
NSWindowButton::NSWindowZoomButton,
] {
let button = ns_window.standardWindowButton_(*titlebar_button);
let _: () = msg_send![button, setHidden: YES];
let _: () = msg_send![button, setHidden: true];
}
}
if pl_attrs.movable_by_window_background {
ns_window.setMovableByWindowBackground_(YES);
ns_window.setMovableByWindowBackground_(Bool::YES.as_raw());
}
if attrs.always_on_top {
let _: () = msg_send![
*ns_window,
setLevel: ffi::NSWindowLevel::NSFloatingWindowLevel
];
let _: () = msg_send![*ns_window, setLevel: ffi::kCGFloatingWindowLevelKey];
}
if let Some(increments) = pl_attrs.resize_increments {
@ -254,7 +252,7 @@ fn create_window(
}
if !pl_attrs.has_shadow {
ns_window.setHasShadow_(NO);
ns_window.setHasShadow_(Bool::NO.as_raw());
}
if attrs.position.is_none() {
ns_window.center();
@ -270,25 +268,25 @@ unsafe impl Sync for WindowClass {}
static WINDOW_CLASS: Lazy<WindowClass> = Lazy::new(|| unsafe {
let window_superclass = class!(NSWindow);
let mut decl = ClassDecl::new("WinitWindow", window_superclass).unwrap();
let mut decl = ClassBuilder::new("WinitWindow", window_superclass).unwrap();
pub extern "C" fn can_become_main_window(_: &Object, _: Sel) -> BOOL {
pub extern "C" fn can_become_main_window(_: &Object, _: Sel) -> Bool {
trace_scope!("canBecomeMainWindow");
YES
Bool::YES
}
pub extern "C" fn can_become_key_window(_: &Object, _: Sel) -> BOOL {
pub extern "C" fn can_become_key_window(_: &Object, _: Sel) -> Bool {
trace_scope!("canBecomeKeyWindow");
YES
Bool::YES
}
decl.add_method(
sel!(canBecomeMainWindow),
can_become_main_window as extern "C" fn(&Object, Sel) -> BOOL,
can_become_main_window as extern "C" fn(_, _) -> _,
);
decl.add_method(
sel!(canBecomeKeyWindow),
can_become_key_window as extern "C" fn(&Object, Sel) -> BOOL,
can_become_key_window as extern "C" fn(_, _) -> _,
);
WindowClass(decl.register())
});
@ -396,11 +394,8 @@ impl UnownedWindow {
mut win_attribs: WindowAttributes,
pl_attribs: PlatformSpecificWindowBuilderAttributes,
) -> Result<(Arc<Self>, IdRef), RootOsError> {
unsafe {
let is_main_thread: BOOL = msg_send!(class!(NSThread), isMainThread);
if is_main_thread == NO {
panic!("Windows can only be created on the main thread on macOS");
}
if !is_main_thread() {
panic!("Windows can only be created on the main thread on macOS");
}
trace!("Creating new window");
@ -420,7 +415,7 @@ impl UnownedWindow {
unsafe {
if win_attribs.transparent {
ns_window.setOpaque_(NO);
ns_window.setOpaque_(Bool::NO.as_raw());
ns_window.setBackgroundColor_(NSColor::clearColor(nil));
}
@ -519,8 +514,8 @@ impl UnownedWindow {
#[inline]
pub fn is_visible(&self) -> Option<bool> {
let is_visible: BOOL = unsafe { msg_send![*self.ns_window, isVisible] };
Some(is_visible == YES)
let is_visible = unsafe { msg_send![*self.ns_window, isVisible] };
Some(is_visible)
}
pub fn request_redraw(&self) {
@ -625,8 +620,7 @@ impl UnownedWindow {
#[inline]
pub fn is_resizable(&self) -> bool {
let is_resizable: BOOL = unsafe { msg_send![*self.ns_window, isResizable] };
is_resizable == YES
unsafe { msg_send![*self.ns_window, isResizable] }
}
pub fn set_cursor_icon(&self, cursor: CursorIcon) {
@ -726,14 +720,14 @@ impl UnownedWindow {
self.set_style_mask_sync(required);
}
let is_zoomed: BOOL = unsafe { msg_send![*self.ns_window, isZoomed] };
let is_zoomed = unsafe { msg_send![*self.ns_window, isZoomed] };
// Roll back temp styles
if needs_temp_mask {
self.set_style_mask_async(curr_mask);
}
is_zoomed != NO
is_zoomed
}
fn saved_style(&self, shared_state: &mut SharedState) -> NSWindowStyleMask {
@ -767,8 +761,7 @@ impl UnownedWindow {
#[inline]
pub fn set_minimized(&self, minimized: bool) {
let is_minimized: BOOL = unsafe { msg_send![*self.ns_window, isMiniaturized] };
let is_minimized: bool = is_minimized == YES;
let is_minimized: bool = unsafe { msg_send![*self.ns_window, isMiniaturized] };
if is_minimized == minimized {
return;
}
@ -998,10 +991,7 @@ impl UnownedWindow {
// Restore the normal window level following the Borderless fullscreen
// `CGShieldingWindowLevel() + 1` hack.
let _: () = msg_send![
*self.ns_window,
setLevel: ffi::NSWindowLevel::NSNormalWindowLevel
];
let _: () = msg_send![*self.ns_window, setLevel: ffi::kCGBaseWindowLevelKey];
},
_ => {}
};
@ -1088,14 +1078,12 @@ impl UnownedWindow {
#[inline]
pub fn focus_window(&self) {
let is_minimized: BOOL = unsafe { msg_send![*self.ns_window, isMiniaturized] };
let is_minimized = is_minimized == YES;
let is_visible: BOOL = unsafe { msg_send![*self.ns_window, isVisible] };
let is_visible = is_visible == YES;
let is_minimized: bool = unsafe { msg_send![*self.ns_window, isMiniaturized] };
let is_visible: bool = unsafe { msg_send![*self.ns_window, isVisible] };
if !is_minimized && is_visible {
unsafe {
NSApp().activateIgnoringOtherApps_(YES);
NSApp().activateIgnoringOtherApps_(Bool::YES.as_raw());
util::make_key_and_order_front_async(*self.ns_window);
}
}
@ -1223,7 +1211,7 @@ impl WindowExtMacOS for UnownedWindow {
// Set the window frame to the screen frame size
let screen = self.ns_window.screen();
let screen_frame = NSScreen::frame(screen);
NSWindow::setFrame_display_(*self.ns_window, screen_frame, YES);
NSWindow::setFrame_display_(*self.ns_window, screen_frame, Bool::YES.as_raw());
// Fullscreen windows can't be resized, minimized, or moved
util::toggle_style_mask(
@ -1238,7 +1226,7 @@ impl WindowExtMacOS for UnownedWindow {
NSWindowStyleMask::NSResizableWindowMask,
false,
);
NSWindow::setMovable_(*self.ns_window, NO);
NSWindow::setMovable_(*self.ns_window, Bool::NO.as_raw());
true
} else {
@ -1251,8 +1239,8 @@ impl WindowExtMacOS for UnownedWindow {
}
let frame = shared_state_lock.saved_standard_frame();
NSWindow::setFrame_display_(*self.ns_window, frame, YES);
NSWindow::setMovable_(*self.ns_window, YES);
NSWindow::setFrame_display_(*self.ns_window, frame, Bool::YES.as_raw());
NSWindow::setMovable_(*self.ns_window, Bool::YES.as_raw());
true
}
@ -1261,15 +1249,12 @@ impl WindowExtMacOS for UnownedWindow {
#[inline]
fn has_shadow(&self) -> bool {
unsafe { self.ns_window.hasShadow() == YES }
unsafe { Bool::from_raw(self.ns_window.hasShadow()).as_bool() }
}
#[inline]
fn set_has_shadow(&self, has_shadow: bool) {
unsafe {
self.ns_window
.setHasShadow_(if has_shadow { YES } else { NO })
}
unsafe { self.ns_window.setHasShadow_(Bool::new(has_shadow).as_raw()) }
}
}
@ -1297,14 +1282,14 @@ unsafe fn set_min_inner_size<V: NSWindow + Copy>(window: V, mut min_size: Logica
// If necessary, resize the window to match constraint
if current_rect.size.width < min_size.width {
current_rect.size.width = min_size.width;
window.setFrame_display_(current_rect, NO)
window.setFrame_display_(current_rect, Bool::NO.as_raw())
}
if current_rect.size.height < min_size.height {
// The origin point of a rectangle is at its bottom left in Cocoa.
// To ensure the window's top-left point remains the same:
current_rect.origin.y += current_rect.size.height - min_size.height;
current_rect.size.height = min_size.height;
window.setFrame_display_(current_rect, NO)
window.setFrame_display_(current_rect, Bool::NO.as_raw())
}
}
@ -1322,13 +1307,13 @@ unsafe fn set_max_inner_size<V: NSWindow + Copy>(window: V, mut max_size: Logica
// If necessary, resize the window to match constraint
if current_rect.size.width > max_size.width {
current_rect.size.width = max_size.width;
window.setFrame_display_(current_rect, NO)
window.setFrame_display_(current_rect, Bool::NO.as_raw())
}
if current_rect.size.height > max_size.height {
// The origin point of a rectangle is at its bottom left in Cocoa.
// To ensure the window's top-left point remains the same:
current_rect.origin.y += current_rect.size.height - max_size.height;
current_rect.size.height = max_size.height;
window.setFrame_display_(current_rect, NO)
window.setFrame_display_(current_rect, Bool::NO.as_raw())
}
}