From 951ba2a595c3496dee2279be5bcfaae691ca3bdb Mon Sep 17 00:00:00 2001 From: Micah Johnston Date: Mon, 25 May 2020 16:14:42 -0500 Subject: [PATCH] initial macos code --- Cargo.toml | 7 +++++-- examples/macos.rs | 10 ++++++++++ src/lib.rs | 3 +++ src/macos.rs | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 64 insertions(+), 2 deletions(-) create mode 100644 examples/macos.rs create mode 100644 src/macos.rs diff --git a/Cargo.toml b/Cargo.toml index d27fcb2..02ca731 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,7 +4,8 @@ version = "0.1.0" authors = [ "William Light ", "Charles Saracco ", - "Mirko Covizzi " + "Mirko Covizzi ", + "Micah Johnston ", ] edition = "2018" @@ -16,4 +17,6 @@ xcb = { version = "0.9", features = ["thread", "xlib_xcb", "dri2"] } [target.'cfg(target_os="windows")'.dependencies] winapi = { version = "0.3.8", features = ["libloaderapi", "winuser", "windef", "minwindef", "guiddef", "combaseapi"] } -# [target.'cfg(target_os="macos")'.dependencies] +[target.'cfg(target_os="macos")'.dependencies] +cocoa = "0.20.1" +objc = "0.2.7" diff --git a/examples/macos.rs b/examples/macos.rs new file mode 100644 index 0000000..924a382 --- /dev/null +++ b/examples/macos.rs @@ -0,0 +1,10 @@ +fn main() { + let window_open_options = baseview::WindowOpenOptions { + title: "baseview", + width: 512, + height: 512, + parent: baseview::Parent::None, + }; + + baseview::Window::open(window_open_options); +} diff --git a/src/lib.rs b/src/lib.rs index 1f469d7..aacc02a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -6,6 +6,9 @@ use std::ffi::c_void; mod x11; +mod macos; +pub use macos::Window; + pub enum Parent { None, AsIfParented, diff --git a/src/macos.rs b/src/macos.rs new file mode 100644 index 0000000..64ca90d --- /dev/null +++ b/src/macos.rs @@ -0,0 +1,46 @@ +use cocoa::appkit::{ + NSApp, NSApplication, NSApplicationActivateIgnoringOtherApps, + NSApplicationActivationPolicyRegular, NSBackingStoreBuffered, NSRunningApplication, NSView, + NSWindow, NSWindowStyleMask, +}; +use cocoa::base::{nil, NO}; +use cocoa::foundation::{NSAutoreleasePool, NSPoint, NSRect, NSSize, NSString}; + +use crate::WindowOpenOptions; + +pub struct Window; + +impl Window { + pub fn open(options: WindowOpenOptions) { + unsafe { + let _pool = NSAutoreleasePool::new(nil); + + let app = NSApp(); + app.setActivationPolicy_(NSApplicationActivationPolicyRegular); + + let rect = NSRect::new( + NSPoint::new(0.0, 0.0), + NSSize::new(options.width as f64, options.height as f64), + ); + + let window = NSWindow::alloc(nil) + .initWithContentRect_styleMask_backing_defer_( + rect, + NSWindowStyleMask::NSTitledWindowMask, + NSBackingStoreBuffered, + NO, + ) + .autorelease(); + window.center(); + window.setTitle_(NSString::alloc(nil).init_str(options.title)); + window.makeKeyAndOrderFront_(nil); + + let view = NSView::alloc(nil).init(); + window.setContentView_(view); + + let current_app = NSRunningApplication::currentApplication(nil); + current_app.activateWithOptions_(NSApplicationActivateIgnoringOtherApps); + app.run(); + } + } +}