1
0
Fork 0

adapt macos backend to new api

This commit is contained in:
Micah Johnston 2020-09-07 02:43:06 -05:00 committed by micah
parent e78598954f
commit bb6a5eeeac

View file

@ -6,22 +6,23 @@ use cocoa::appkit::{
NSApplicationActivationPolicyRegular, NSBackingStoreBuffered, NSRunningApplication, NSView, NSApplicationActivationPolicyRegular, NSBackingStoreBuffered, NSRunningApplication, NSView,
NSWindow, NSWindowStyleMask, NSWindow, NSWindowStyleMask,
}; };
use cocoa::base::{nil, NO}; use cocoa::base::{id, nil, NO};
use cocoa::foundation::{NSAutoreleasePool, NSPoint, NSRect, NSSize, NSString}; use cocoa::foundation::{NSAutoreleasePool, NSPoint, NSRect, NSSize, NSString};
use raw_window_handle::{macos::MacOSHandle, HasRawWindowHandle, RawWindowHandle}; use raw_window_handle::{macos::MacOSHandle, HasRawWindowHandle, RawWindowHandle};
use crate::{ use crate::{AppWindow, MouseScroll, WindowOpenOptions};
AppWindow, Event, MouseButtonID, MouseScroll, RawWindow, WindowInfo, WindowOpenOptions,
};
pub struct Window<A: AppWindow> { pub struct Window {
app_window: A, ns_window: id,
app_message_rx: mpsc::Receiver<A::AppMessage>, ns_view: id,
} }
impl<A: AppWindow> Window<A> { impl Window {
pub fn open(options: WindowOpenOptions, app_message_rx: mpsc::Receiver<A::AppMessage>) -> Self { pub fn open<A: AppWindow>(
options: WindowOpenOptions,
app_message_rx: mpsc::Receiver<A::AppMessage>,
) {
unsafe { unsafe {
let _pool = NSAutoreleasePool::new(nil); let _pool = NSAutoreleasePool::new(nil);
@ -33,7 +34,7 @@ impl<A: AppWindow> Window<A> {
NSSize::new(options.width as f64, options.height as f64), NSSize::new(options.width as f64, options.height as f64),
); );
let window = NSWindow::alloc(nil) let ns_window = NSWindow::alloc(nil)
.initWithContentRect_styleMask_backing_defer_( .initWithContentRect_styleMask_backing_defer_(
rect, rect,
NSWindowStyleMask::NSTitledWindowMask, NSWindowStyleMask::NSTitledWindowMask,
@ -41,37 +42,33 @@ impl<A: AppWindow> Window<A> {
NO, NO,
) )
.autorelease(); .autorelease();
window.center(); ns_window.center();
window.setTitle_(NSString::alloc(nil).init_str(options.title)); ns_window.setTitle_(NSString::alloc(nil).init_str(options.title));
window.makeKeyAndOrderFront_(nil); ns_window.makeKeyAndOrderFront_(nil);
let view = NSView::alloc(nil).init(); let ns_view = NSView::alloc(nil).init();
window.setContentView_(view); ns_window.setContentView_(ns_view);
let raw_window = RawWindow { let mut window = Window {
raw_window_handle: RawWindowHandle::MacOS(MacOSHandle { ns_window,
ns_window: window as *mut c_void, ns_view,
ns_view: app as *mut c_void,
..raw_window_handle::macos::MacOSHandle::empty()
}),
}; };
let window_info = WindowInfo { let app_window = A::build(&mut window);
width: options.width as u32,
height: options.height as u32,
scale: 1.0,
};
let app_window = A::build(raw_window, &window_info);
let current_app = NSRunningApplication::currentApplication(nil); let current_app = NSRunningApplication::currentApplication(nil);
current_app.activateWithOptions_(NSApplicationActivateIgnoringOtherApps); current_app.activateWithOptions_(NSApplicationActivateIgnoringOtherApps);
app.run(); app.run();
Window {
app_window,
app_message_rx,
}
} }
} }
} }
unsafe impl HasRawWindowHandle for Window {
fn raw_window_handle(&self) -> RawWindowHandle {
RawWindowHandle::MacOS(MacOSHandle {
ns_window: self.ns_window as *mut c_void,
ns_view: self.ns_view as *mut c_void,
..MacOSHandle::empty()
})
}
}