Make ns identifiers use snake_case on macOS (#904)

This makes the macOS API style consistent with that of iOS.
This commit is contained in:
Nikolai Vazquez 2019-06-11 01:09:38 +02:00 committed by Osspial
parent 07356b9634
commit f256ff7d58
7 changed files with 206 additions and 205 deletions

View file

@ -60,26 +60,26 @@ pub struct PlatformSpecificWindowBuilderAttributes {
fn create_app(activation_policy: ActivationPolicy) -> Option<id> {
unsafe {
let nsapp = NSApp();
if nsapp == nil {
let ns_app = NSApp();
if ns_app == nil {
None
} else {
use self::NSApplicationActivationPolicy::*;
nsapp.setActivationPolicy_(match activation_policy {
ns_app.setActivationPolicy_(match activation_policy {
ActivationPolicy::Regular => NSApplicationActivationPolicyRegular,
ActivationPolicy::Accessory => NSApplicationActivationPolicyAccessory,
ActivationPolicy::Prohibited => NSApplicationActivationPolicyProhibited,
});
nsapp.finishLaunching();
Some(nsapp)
ns_app.finishLaunching();
Some(ns_app)
}
}
}
unsafe fn create_view(nswindow: id) -> Option<(IdRef, Weak<Mutex<util::Cursor>>)> {
let (nsview, cursor) = new_view(nswindow);
nsview.non_nil().map(|nsview| {
nsview.setWantsBestResolutionOpenGLSurface_(YES);
unsafe fn create_view(ns_window: id) -> Option<(IdRef, Weak<Mutex<util::Cursor>>)> {
let (ns_view, cursor) = new_view(ns_window);
ns_view.non_nil().map(|ns_view| {
ns_view.setWantsBestResolutionOpenGLSurface_(YES);
// On Mojave, views automatically become layer-backed shortly after being added to
// a window. Changing the layer-backedness of a view breaks the association between
@ -87,12 +87,12 @@ unsafe fn create_view(nswindow: id) -> Option<(IdRef, Weak<Mutex<util::Cursor>>)
// 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 {
nsview.setWantsLayer(YES);
ns_view.setWantsLayer(YES);
}
nswindow.setContentView_(*nsview);
nswindow.makeFirstResponder_(*nsview);
(nsview, cursor)
ns_window.setContentView_(*ns_view);
ns_window.makeFirstResponder_(*ns_view);
(ns_view, cursor)
})
}
@ -104,7 +104,7 @@ fn create_window(
let pool = NSAutoreleasePool::new(nil);
let screen = match attrs.fullscreen {
Some(ref monitor_id) => {
let monitor_screen = monitor_id.inner.nsscreen();
let monitor_screen = monitor_id.inner.ns_screen();
Some(monitor_screen.unwrap_or(appkit::NSScreen::mainScreen(nil)))
},
_ => None,
@ -144,24 +144,24 @@ fn create_window(
masks |= NSWindowStyleMask::NSFullSizeContentViewWindowMask;
}
let nswindow: id = msg_send![WINDOW_CLASS.0, alloc];
let nswindow = IdRef::new(nswindow.initWithContentRect_styleMask_backing_defer_(
let ns_window: id = msg_send![WINDOW_CLASS.0, alloc];
let ns_window = IdRef::new(ns_window.initWithContentRect_styleMask_backing_defer_(
frame,
masks,
appkit::NSBackingStoreBuffered,
NO,
));
let res = nswindow.non_nil().map(|nswindow| {
let res = ns_window.non_nil().map(|ns_window| {
let title = IdRef::new(NSString::alloc(nil).init_str(&attrs.title));
nswindow.setReleasedWhenClosed_(NO);
nswindow.setTitle_(*title);
nswindow.setAcceptsMouseMovedEvents_(YES);
ns_window.setReleasedWhenClosed_(NO);
ns_window.setTitle_(*title);
ns_window.setAcceptsMouseMovedEvents_(YES);
if pl_attrs.titlebar_transparent {
nswindow.setTitlebarAppearsTransparent_(YES);
ns_window.setTitlebarAppearsTransparent_(YES);
}
if pl_attrs.title_hidden {
nswindow.setTitleVisibility_(appkit::NSWindowTitleVisibility::NSWindowTitleHidden);
ns_window.setTitleVisibility_(appkit::NSWindowTitleVisibility::NSWindowTitleHidden);
}
if pl_attrs.titlebar_buttons_hidden {
for titlebar_button in &[
@ -170,28 +170,28 @@ fn create_window(
NSWindowButton::NSWindowCloseButton,
NSWindowButton::NSWindowZoomButton,
] {
let button = nswindow.standardWindowButton_(*titlebar_button);
let button = ns_window.standardWindowButton_(*titlebar_button);
let _: () = msg_send![button, setHidden:YES];
}
}
if pl_attrs.movable_by_window_background {
nswindow.setMovableByWindowBackground_(YES);
ns_window.setMovableByWindowBackground_(YES);
}
if attrs.always_on_top {
let _: () = msg_send![*nswindow, setLevel:ffi::NSWindowLevel::NSFloatingWindowLevel];
let _: () = msg_send![*ns_window, setLevel:ffi::NSWindowLevel::NSFloatingWindowLevel];
}
if let Some(increments) = pl_attrs.resize_increments {
let (x, y) = (increments.width, increments.height);
if x >= 1.0 && y >= 1.0 {
let size = NSSize::new(x as CGFloat, y as CGFloat);
nswindow.setResizeIncrements_(size);
ns_window.setResizeIncrements_(size);
}
}
nswindow.center();
nswindow
ns_window.center();
ns_window
});
pool.drain();
res
@ -241,8 +241,8 @@ impl From<WindowAttributes> for SharedState {
}
pub struct UnownedWindow {
pub nswindow: IdRef, // never changes
pub nsview: IdRef, // never changes
pub ns_window: IdRef, // never changes
pub ns_view: IdRef, // never changes
input_context: IdRef, // never changes
pub shared_state: Arc<Mutex<SharedState>>,
decorations: AtomicBool,
@ -266,37 +266,37 @@ impl UnownedWindow {
let pool = unsafe { NSAutoreleasePool::new(nil) };
let nsapp = create_app(pl_attribs.activation_policy).ok_or_else(|| {
let ns_app = create_app(pl_attribs.activation_policy).ok_or_else(|| {
unsafe { pool.drain() };
os_error!(OsError::CreationError("Couldn't create `NSApplication`"))
})?;
let nswindow = create_window(&win_attribs, &pl_attribs).ok_or_else(|| {
let ns_window = create_window(&win_attribs, &pl_attribs).ok_or_else(|| {
unsafe { pool.drain() };
os_error!(OsError::CreationError("Couldn't create `NSWindow`"))
})?;
let (nsview, cursor) = unsafe { create_view(*nswindow) }.ok_or_else(|| {
let (ns_view, cursor) = unsafe { create_view(*ns_window) }.ok_or_else(|| {
unsafe { pool.drain() };
os_error!(OsError::CreationError("Couldn't create `NSView`"))
})?;
let input_context = unsafe { util::create_input_context(*nsview) };
let input_context = unsafe { util::create_input_context(*ns_view) };
unsafe {
if win_attribs.transparent {
nswindow.setOpaque_(NO);
nswindow.setBackgroundColor_(NSColor::clearColor(nil));
ns_window.setOpaque_(NO);
ns_window.setBackgroundColor_(NSColor::clearColor(nil));
}
nsapp.activateIgnoringOtherApps_(YES);
ns_app.activateIgnoringOtherApps_(YES);
win_attribs.min_inner_size.map(|dim| set_min_inner_size(*nswindow, dim));
win_attribs.max_inner_size.map(|dim| set_max_inner_size(*nswindow, dim));
win_attribs.min_inner_size.map(|dim| set_min_inner_size(*ns_window, dim));
win_attribs.max_inner_size.map(|dim| set_max_inner_size(*ns_window, dim));
use cocoa::foundation::NSArray;
// register for drag and drop operations.
let () = msg_send![*nswindow, registerForDraggedTypes:NSArray::arrayWithObject(
let () = msg_send![*ns_window, registerForDraggedTypes:NSArray::arrayWithObject(
nil,
appkit::NSFilenamesPboardType,
)];
@ -313,8 +313,8 @@ impl UnownedWindow {
let decorations = win_attribs.decorations;
let window = Arc::new(UnownedWindow {
nsview,
nswindow,
ns_view,
ns_window,
input_context,
shared_state: Arc::new(Mutex::new(win_attribs.into())),
decorations: AtomicBool::new(decorations),
@ -341,9 +341,9 @@ impl UnownedWindow {
// before it transitions.
unsafe {
if visible {
window.nswindow.makeKeyAndOrderFront_(nil);
window.ns_window.makeKeyAndOrderFront_(nil);
} else {
window.nswindow.makeKeyWindow();
window.ns_window.makeKeyWindow();
}
}
@ -358,35 +358,35 @@ impl UnownedWindow {
fn set_style_mask_async(&self, mask: NSWindowStyleMask) {
unsafe { util::set_style_mask_async(
*self.nswindow,
*self.nsview,
*self.ns_window,
*self.ns_view,
mask,
) };
}
fn set_style_mask_sync(&self, mask: NSWindowStyleMask) {
unsafe { util::set_style_mask_sync(
*self.nswindow,
*self.nsview,
*self.ns_window,
*self.ns_view,
mask,
) };
}
pub fn id(&self) -> Id {
get_window_id(*self.nswindow)
get_window_id(*self.ns_window)
}
pub fn set_title(&self, title: &str) {
unsafe {
let title = IdRef::new(NSString::alloc(nil).init_str(title));
self.nswindow.setTitle_(*title);
self.ns_window.setTitle_(*title);
}
}
pub fn set_visible(&self, visible: bool) {
match visible {
true => unsafe { util::make_key_and_order_front_async(*self.nswindow) },
false => unsafe { util::order_out_async(*self.nswindow) },
true => unsafe { util::make_key_and_order_front_async(*self.ns_window) },
false => unsafe { util::order_out_async(*self.ns_window) },
}
}
@ -395,7 +395,7 @@ impl UnownedWindow {
}
pub fn outer_position(&self) -> Result<LogicalPosition, NotSupportedError> {
let frame_rect = unsafe { NSWindow::frame(*self.nswindow) };
let frame_rect = unsafe { NSWindow::frame(*self.ns_window) };
Ok((
frame_rect.origin.x as f64,
util::bottom_left_to_top_left(frame_rect),
@ -405,8 +405,8 @@ impl UnownedWindow {
pub fn inner_position(&self) -> Result<LogicalPosition, NotSupportedError> {
let content_rect = unsafe {
NSWindow::contentRectForFrameRect_(
*self.nswindow,
NSWindow::frame(*self.nswindow),
*self.ns_window,
NSWindow::frame(*self.ns_window),
)
};
Ok((
@ -426,40 +426,40 @@ impl UnownedWindow {
NSSize::new(0f64, 0f64),
);
unsafe {
util::set_frame_top_left_point_async(*self.nswindow, dummy.origin);
util::set_frame_top_left_point_async(*self.ns_window, dummy.origin);
}
}
#[inline]
pub fn inner_size(&self) -> LogicalSize {
let view_frame = unsafe { NSView::frame(*self.nsview) };
let view_frame = unsafe { NSView::frame(*self.ns_view) };
(view_frame.size.width as f64, view_frame.size.height as f64).into()
}
#[inline]
pub fn outer_size(&self) -> LogicalSize {
let view_frame = unsafe { NSWindow::frame(*self.nswindow) };
let view_frame = unsafe { NSWindow::frame(*self.ns_window) };
(view_frame.size.width as f64, view_frame.size.height as f64).into()
}
#[inline]
pub fn set_inner_size(&self, size: LogicalSize) {
unsafe {
util::set_content_size_async(*self.nswindow, size);
util::set_content_size_async(*self.ns_window, size);
}
}
pub fn set_min_inner_size(&self, dimensions: Option<LogicalSize>) {
unsafe {
let dimensions = dimensions.unwrap_or_else(|| (0, 0).into());
set_min_inner_size(*self.nswindow, dimensions);
set_min_inner_size(*self.ns_window, dimensions);
}
}
pub fn set_max_inner_size(&self, dimensions: Option<LogicalSize>) {
unsafe {
let dimensions = dimensions.unwrap_or_else(|| (!0, !0).into());
set_max_inner_size(*self.nswindow, dimensions);
set_max_inner_size(*self.ns_window, dimensions);
}
}
@ -473,7 +473,7 @@ impl UnownedWindow {
shared_state_lock.fullscreen.is_some()
};
if !fullscreen {
let mut mask = unsafe { self.nswindow.styleMask() };
let mut mask = unsafe { self.ns_window.styleMask() };
if resizable {
mask |= NSWindowStyleMask::NSResizableWindowMask;
} else {
@ -489,8 +489,8 @@ impl UnownedWindow {
*cursor_access.lock().unwrap() = cursor;
}
unsafe {
let _: () = msg_send![*self.nswindow,
invalidateCursorRectsForView:*self.nsview
let _: () = msg_send![*self.ns_window,
invalidateCursorRectsForView:*self.ns_view
];
}
}
@ -519,7 +519,7 @@ impl UnownedWindow {
#[inline]
pub fn hidpi_factor(&self) -> f64 {
unsafe { NSWindow::backingScaleFactor(*self.nswindow) as _ }
unsafe { NSWindow::backingScaleFactor(*self.ns_window) as _ }
}
#[inline]
@ -540,7 +540,7 @@ impl UnownedWindow {
pub(crate) fn is_zoomed(&self) -> bool {
// because `isZoomed` doesn't work if the window's borderless,
// we make it resizable temporalily.
let curr_mask = unsafe { self.nswindow.styleMask() };
let curr_mask = unsafe { self.ns_window.styleMask() };
let required = NSWindowStyleMask::NSTitledWindowMask
| NSWindowStyleMask::NSResizableWindowMask;
@ -549,7 +549,7 @@ impl UnownedWindow {
self.set_style_mask_sync(required);
}
let is_zoomed: BOOL = unsafe { msg_send![*self.nswindow, isZoomed] };
let is_zoomed: BOOL = unsafe { msg_send![*self.ns_window, isZoomed] };
// Roll back temp styles
if needs_temp_mask {
@ -562,7 +562,7 @@ impl UnownedWindow {
fn saved_style(&self, shared_state: &mut SharedState) -> NSWindowStyleMask {
let base_mask = shared_state.saved_style
.take()
.unwrap_or_else(|| unsafe { self.nswindow.styleMask() });
.unwrap_or_else(|| unsafe { self.ns_window.styleMask() });
if shared_state.resizable {
base_mask | NSWindowStyleMask::NSResizableWindowMask
} else {
@ -604,19 +604,19 @@ impl UnownedWindow {
// Save the standard frame sized if it is not zoomed
if !is_zoomed {
unsafe {
shared_state_lock.standard_frame = Some(NSWindow::frame(*self.nswindow));
shared_state_lock.standard_frame = Some(NSWindow::frame(*self.ns_window));
}
}
shared_state_lock.maximized = maximized;
let curr_mask = unsafe { self.nswindow.styleMask() };
let curr_mask = unsafe { self.ns_window.styleMask() };
if shared_state_lock.fullscreen.is_some() {
// Handle it in window_did_exit_fullscreen
return;
} else if curr_mask.contains(NSWindowStyleMask::NSResizableWindowMask) {
// Just use the native zoom if resizable
unsafe { self.nswindow.zoom_(nil) };
unsafe { self.ns_window.zoom_(nil) };
} else {
// if it's not resizable, we set the frame directly
unsafe {
@ -627,7 +627,7 @@ impl UnownedWindow {
Self::saved_standard_frame(&mut *shared_state_lock)
};
// This probably isn't thread-safe!
self.nswindow.setFrame_display_(new_rect, 0);
self.ns_window.setFrame_display_(new_rect, 0);
}
}
@ -666,8 +666,8 @@ impl UnownedWindow {
};
unsafe { util::toggle_full_screen_async(
*self.nswindow,
*self.nsview,
*self.ns_window,
*self.ns_view,
not_fullscreen,
Arc::downgrade(&self.shared_state),
) };
@ -718,7 +718,7 @@ impl UnownedWindow {
} else {
ffi::NSWindowLevel::NSNormalWindowLevel
};
unsafe { util::set_level_async(*self.nswindow, level) };
unsafe { util::set_level_async(*self.ns_window, level) };
}
#[inline]
@ -737,7 +737,7 @@ impl UnownedWindow {
pub fn set_ime_position(&self, logical_spot: LogicalPosition) {
unsafe {
view::set_ime_position(
*self.nsview,
*self.ns_view,
*self.input_context,
logical_spot.x,
logical_spot.y,
@ -748,7 +748,7 @@ impl UnownedWindow {
#[inline]
pub fn current_monitor(&self) -> RootMonitorHandle {
unsafe {
let screen: id = msg_send![*self.nswindow, screen];
let screen: id = msg_send![*self.ns_window, screen];
let desc = NSScreen::deviceDescription(screen);
let key = IdRef::new(NSString::alloc(nil).init_str("NSScreenNumber"));
let value = NSDictionary::valueForKey_(desc, *key);
@ -770,13 +770,13 @@ impl UnownedWindow {
impl WindowExtMacOS for UnownedWindow {
#[inline]
fn nswindow(&self) -> *mut c_void {
*self.nswindow as *mut _
fn ns_window(&self) -> *mut c_void {
*self.ns_window as *mut _
}
#[inline]
fn nsview(&self) -> *mut c_void {
*self.nsview as *mut _
fn ns_view(&self) -> *mut c_void {
*self.ns_view as *mut _
}
#[inline]
@ -811,8 +811,8 @@ impl WindowExtMacOS for UnownedWindow {
if fullscreen {
// Remember the original window's settings
shared_state_lock.standard_frame = Some(NSWindow::frame(*self.nswindow));
shared_state_lock.saved_style = Some(self.nswindow.styleMask());
shared_state_lock.standard_frame = Some(NSWindow::frame(*self.ns_window));
shared_state_lock.saved_style = Some(self.ns_window.styleMask());
shared_state_lock.save_presentation_opts = Some(app.presentationOptions_());
// Tell our window's state that we're in fullscreen
@ -825,17 +825,17 @@ impl WindowExtMacOS for UnownedWindow {
app.setPresentationOptions_(presentation_options);
// Hide the titlebar
util::toggle_style_mask(*self.nswindow, *self.nsview, NSWindowStyleMask::NSTitledWindowMask, false);
util::toggle_style_mask(*self.ns_window, *self.ns_view, NSWindowStyleMask::NSTitledWindowMask, false);
// Set the window frame to the screen frame size
let screen = self.nswindow.screen();
let screen = self.ns_window.screen();
let screen_frame = NSScreen::frame(screen);
NSWindow::setFrame_display_(*self.nswindow, screen_frame, YES);
NSWindow::setFrame_display_(*self.ns_window, screen_frame, YES);
// Fullscreen windows can't be resized, minimized, or moved
util::toggle_style_mask(*self.nswindow, *self.nsview, NSWindowStyleMask::NSMiniaturizableWindowMask, false);
util::toggle_style_mask(*self.nswindow, *self.nsview, NSWindowStyleMask::NSResizableWindowMask, false);
NSWindow::setMovable_(*self.nswindow, NO);
util::toggle_style_mask(*self.ns_window, *self.ns_view, NSWindowStyleMask::NSMiniaturizableWindowMask, false);
util::toggle_style_mask(*self.ns_window, *self.ns_view, NSWindowStyleMask::NSResizableWindowMask, false);
NSWindow::setMovable_(*self.ns_window, NO);
true
} else {
@ -848,8 +848,8 @@ impl WindowExtMacOS for UnownedWindow {
}
let frame = Self::saved_standard_frame(&mut *shared_state_lock);
NSWindow::setFrame_display_(*self.nswindow, frame, YES);
NSWindow::setMovable_(*self.nswindow, YES);
NSWindow::setFrame_display_(*self.ns_window, frame, YES);
NSWindow::setMovable_(*self.ns_window, YES);
true
}
@ -861,8 +861,8 @@ impl Drop for UnownedWindow {
fn drop(&mut self) {
trace!("Dropping `UnownedWindow` ({:?})", self as *mut _);
// Close the window if it has not yet been closed.
if *self.nswindow != nil {
unsafe { util::close_async(*self.nswindow) };
if *self.ns_window != nil {
unsafe { util::close_async(*self.ns_window) };
}
}
}