1
0
Fork 0

Merge pull request #4 from glowcoil/master

initial macos code
This commit is contained in:
william light 2020-05-25 23:20:13 +02:00 committed by GitHub
commit 2294fdb28d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 64 additions and 2 deletions

View file

@ -4,7 +4,8 @@ version = "0.1.0"
authors = [ authors = [
"William Light <git@wrl.lhiaudio.com>", "William Light <git@wrl.lhiaudio.com>",
"Charles Saracco <crsaracco@gmail.com>", "Charles Saracco <crsaracco@gmail.com>",
"Mirko Covizzi <mrkcvzz@gmail.com>" "Mirko Covizzi <mrkcvzz@gmail.com>",
"Micah Johnston <micah@glowcoil.com>",
] ]
edition = "2018" edition = "2018"
@ -16,4 +17,6 @@ xcb = { version = "0.9", features = ["thread", "xlib_xcb", "dri2"] }
[target.'cfg(target_os="windows")'.dependencies] [target.'cfg(target_os="windows")'.dependencies]
winapi = { version = "0.3.8", features = ["libloaderapi", "winuser", "windef", "minwindef", "guiddef", "combaseapi"] } 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"

10
examples/macos.rs Normal file
View file

@ -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);
}

View file

@ -6,6 +6,9 @@ use std::ffi::c_void;
mod x11; mod x11;
mod macos;
pub use macos::Window;
pub enum Parent { pub enum Parent {
None, None,
AsIfParented, AsIfParented,

46
src/macos.rs Normal file
View file

@ -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();
}
}
}