Skip to content

Commit

Permalink
Don't use struct for window delegate
Browse files Browse the repository at this point in the history
  • Loading branch information
francesca64 committed Dec 21, 2018
1 parent c6853b0 commit 8e9fc01
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 102 deletions.
3 changes: 1 addition & 2 deletions src/platform_impl/macos/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ pub use self::{
Id as WindowId, PlatformSpecificWindowBuilderAttributes, UnownedWindow,
},
};
use self::window_delegate::WindowDelegate;

#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct DeviceId;
Expand All @@ -36,7 +35,7 @@ pub(crate) const DEVICE_ID: RootDeviceId = RootDeviceId(DeviceId);
pub struct Window {
window: Arc<UnownedWindow>,
// We keep this around so that it doesn't get dropped until the window does.
_delegate: WindowDelegate,
_delegate: util::IdRef,
}

unsafe impl Send for Window {}
Expand Down
20 changes: 10 additions & 10 deletions src/platform_impl/macos/util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,16 @@ pub const EMPTY_RANGE: ffi::NSRange = ffi::NSRange {
pub struct IdRef(id);

impl IdRef {
pub fn new(i: id) -> IdRef {
IdRef(i)
pub fn new(inner: id) -> IdRef {
IdRef(inner)
}

#[allow(dead_code)]
pub fn retain(i: id) -> IdRef {
if i != nil {
let _: id = unsafe { msg_send![i, retain] };
pub fn retain(inner: id) -> IdRef {
if inner != nil {
let () = unsafe { msg_send![inner, retain] };
}
IdRef(i)
IdRef(inner)
}

pub fn non_nil(self) -> Option<IdRef> {
Expand All @@ -45,9 +45,9 @@ impl Drop for IdRef {
fn drop(&mut self) {
if self.0 != nil {
unsafe {
let autoreleasepool = NSAutoreleasePool::new(nil);
let _ : () = msg_send![self.0, release];
let _ : () = msg_send![autoreleasepool, release];
let pool = NSAutoreleasePool::new(nil);
let () = msg_send![self.0, release];
pool.drain();
};
}
}
Expand Down Expand Up @@ -89,7 +89,7 @@ pub unsafe fn create_input_context(view: id) -> IdRef {

#[allow(dead_code)]
pub unsafe fn open_emoji_picker() {
let _: () = msg_send![NSApp(), orderFrontCharacterPalette:nil];
let () = msg_send![NSApp(), orderFrontCharacterPalette:nil];
}

pub extern fn yes(_: &Object, _: Sel) -> BOOL {
Expand Down
20 changes: 9 additions & 11 deletions src/platform_impl/macos/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use platform::macos::{ActivationPolicy, WindowExtMacOS};
use platform_impl::platform::{
app_state::AppState, ffi, monitor::{self, MonitorHandle},
util::{self, IdRef}, view::{self, new_view},
window_delegate::{WindowDelegate, WindowDelegateState},
window_delegate::new_delegate,
};

#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
Expand Down Expand Up @@ -93,7 +93,7 @@ fn create_window(
pl_attrs: &PlatformSpecificWindowBuilderAttributes,
) -> Option<IdRef> {
unsafe {
let autoreleasepool = NSAutoreleasePool::new(nil);
let pool = NSAutoreleasePool::new(nil);
let screen = match attrs.fullscreen {
Some(ref monitor_id) => {
let monitor_screen = monitor_id.inner.get_nsscreen();
Expand Down Expand Up @@ -185,7 +185,7 @@ fn create_window(
nswindow.center();
nswindow
});
let _: () = msg_send![autoreleasepool, drain];
pool.drain();
res
}
}
Expand Down Expand Up @@ -247,7 +247,7 @@ impl UnownedWindow {
pub fn new(
mut win_attribs: WindowAttributes,
pl_attribs: PlatformSpecificWindowBuilderAttributes,
) -> Result<(Arc<Self>, WindowDelegate), CreationError> {
) -> Result<(Arc<Self>, IdRef), CreationError> {
unsafe {
if !msg_send![class!(NSThread), isMainThread] {
panic!("Windows can only be created on the main thread on macOS");
Expand Down Expand Up @@ -286,7 +286,7 @@ impl UnownedWindow {

use cocoa::foundation::NSArray;
// register for drag and drop operations.
let _: () = msg_send![*nswindow, registerForDraggedTypes:NSArray::arrayWithObject(
let () = msg_send![*nswindow, registerForDraggedTypes:NSArray::arrayWithObject(
nil,
appkit::NSFilenamesPboardType,
)];
Expand All @@ -312,10 +312,7 @@ impl UnownedWindow {
cursor_hidden: Default::default(),
});

let delegate = WindowDelegate::new(WindowDelegateState::new(
&window,
fullscreen.is_some(),
));
let delegate = new_delegate(&window, fullscreen.is_some());

// Set fullscreen mode after we setup everything
if let Some(monitor) = fullscreen {
Expand Down Expand Up @@ -344,7 +341,7 @@ impl UnownedWindow {
window.set_maximized(maximized);
}

let _: () = unsafe { msg_send![pool, drain] };
unsafe { pool.drain() };

Ok((window, delegate))
}
Expand Down Expand Up @@ -550,7 +547,7 @@ impl UnownedWindow {

// Roll back temp styles
if needs_temp_mask {
self.set_style_mask_sync(curr_mask);
self.set_style_mask_async(curr_mask);
}

is_zoomed != 0
Expand Down Expand Up @@ -617,6 +614,7 @@ impl UnownedWindow {
NSSize::new(800.0, 600.0),
))
};
// This probably isn't thread-safe!
self.nswindow.setFrame_display_(new_rect, 0);
}
}
Expand Down
Loading

0 comments on commit 8e9fc01

Please sign in to comment.