Created
December 25, 2018 11:45
-
-
Save monomadic/6e41104cf4d4350a3769363aa7019967 to your computer and use it in GitHub Desktop.
Window with NSEvent delegate in rust.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extern crate cocoa; | |
#[macro_use] | |
extern crate objc; | |
use cocoa::appkit::*; | |
use cocoa::base::*; | |
use cocoa::foundation::*; | |
use objc::runtime::*; | |
use objc::declare::*; | |
fn print_nsrect(rect: NSRect) { | |
println!("x:{} y:{} h:{} w:{}", rect.origin.y, rect.origin.x, rect.size.height, rect.size.width); | |
} | |
fn main() { | |
unsafe { | |
let _pool = NSAutoreleasePool::new(nil); | |
let app = NSApp(); | |
app.setActivationPolicy_(NSApplicationActivationPolicyRegular); | |
// create Window | |
let window = NSWindow::alloc(nil) | |
.initWithContentRect_styleMask_backing_defer_(NSRect::new(NSPoint::new(0., 0.), | |
NSSize::new(200., 200.)), | |
NSWindowStyleMask::NSTitledWindowMask, | |
NSBackingStoreBuffered, | |
NO) | |
.autorelease(); | |
window.cascadeTopLeftFromPoint_(NSPoint::new(20., 20.)); | |
window.center(); | |
let title = NSString::alloc(nil).init_str("Hello World!"); | |
NSWindow::setTitle_(window, title); | |
window.makeKeyAndOrderFront_(nil); | |
window.setAcceptsMouseMovedEvents_(YES); | |
let current_app = NSRunningApplication::currentApplication(nil); | |
current_app.activateWithOptions_(NSApplicationActivateIgnoringOtherApps); | |
let mut decl = ClassDecl::new("MainView", class!(NSView)).unwrap(); | |
decl.add_method(sel!(acceptsFirstResponder), | |
accepts_first_responder as extern "C" fn(&Object, Sel) -> BOOL); | |
extern "C" fn accepts_first_responder(_this: &Object, _sel: Sel) -> BOOL { println!("acceptsFirstResponder"); YES } | |
decl.add_method(sel!(scrollWheel:), do_nsevent as extern "C" fn(&Object, Sel, id)); | |
decl.add_method(sel!(mouseDown:), do_nsevent as extern "C" fn(&Object, Sel, id)); | |
decl.add_method(sel!(mouseUp:), do_nsevent as extern "C" fn(&Object, Sel, id)); | |
decl.add_method(sel!(mouseMoved:), do_nsevent as extern "C" fn(&Object, Sel, id)); | |
decl.add_method(sel!(keyDown:), do_nsevent as extern "C" fn(&Object, Sel, id)); | |
decl.add_method(sel!(keyUp:), do_nsevent as extern "C" fn(&Object, Sel, id)); | |
extern "C" fn do_nsevent(_this: &Object, _: Sel, event: id) { | |
println!("NSEvent type: {:?}", unsafe { NSEvent::eventType(event) }); | |
} | |
decl.add_method(sel!(applicationDidFinishLaunching:), | |
did_finish_launching as extern "C" fn(&Object, Sel, id)); | |
extern "C" fn did_finish_launching(_this: &Object, _sel: Sel, _notification: id) { | |
println!("applicationDidFinishLaunching"); | |
} | |
let delegate_class = decl.register(); | |
// let delegate_object = msg_send![delegate_class, new]; | |
let delegate_object:id = msg_send![delegate_class, alloc]; | |
let main_view:id = msg_send![delegate_object, init]; | |
let red = msg_send![class!(NSColor), redColor]; | |
let view:id = msg_send![class!(MainView), new]; | |
view.setBackgroundColor_(red); | |
let content_view = window.contentView(); | |
let content_frame:NSRect = NSView::frame(content_view as id); | |
NSView::initWithFrame_(delegate_object, content_frame); | |
msg_send![view, setFrame:content_frame]; | |
content_view.addSubview_(view); | |
NSWindow::setDelegate_(window, main_view); | |
app.run(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment