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