Fix OSX build

This commit is contained in:
Brendan Zabarauskas 2015-01-30 22:14:49 +11:00
parent 26c0c966d5
commit 9b530cdf40

View file

@ -12,6 +12,7 @@ use cocoa::base::{selector, msg_send, msg_send_stret, class_addMethod, class_add
use cocoa::base::{object_setInstanceVariable, object_getInstanceVariable}; use cocoa::base::{object_setInstanceVariable, object_getInstanceVariable};
use cocoa::appkit; use cocoa::appkit;
use cocoa::appkit::*; use cocoa::appkit::*;
use cocoa::appkit::NSEventSubtype::*;
use core_foundation::base::TCFType; use core_foundation::base::TCFType;
use core_foundation::string::CFString; use core_foundation::string::CFString;
@ -89,7 +90,7 @@ impl WindowDelegate {
if let Some(handler) = state.handler { if let Some(handler) = state.handler {
let rect = NSView::frame(state.view); let rect = NSView::frame(state.view);
let scale_factor = state.window.backingScaleFactor() as f32; let scale_factor = NSWindow::backingScaleFactor(state.window) as f32;
(handler)((scale_factor * rect.size.width as f32) as u32, (handler)((scale_factor * rect.size.width as f32) as u32,
(scale_factor * rect.size.height as f32) as u32); (scale_factor * rect.size.height as f32) as u32);
} }
@ -179,8 +180,9 @@ impl WindowProxy {
unsafe { unsafe {
let pool = NSAutoreleasePool::new(nil); let pool = NSAutoreleasePool::new(nil);
let event = let event =
NSEvent::otherEventWithType_location_modifierFlags_timestamp_windowNumber_context_subtype_data1_data2( NSEvent::otherEventWithType_location_modifierFlags_timestamp_windowNumber_context_subtype_data1_data2_(
nil, NSApplicationDefined, NSPoint::new(0.0, 0.0), 0, 0.0, 0, ptr::null_mut(), 0, 0, 0); nil, NSApplicationDefined, NSPoint::new(0.0, 0.0), NSEventModifierFlags::empty(),
0.0, 0, nil, NSApplicationActivatedEventType, 0, 0);
NSApp().postEvent_atStart_(event, YES); NSApp().postEvent_atStart_(event, YES);
pool.drain(); pool.drain();
} }
@ -364,10 +366,7 @@ impl Window {
pub fn get_position(&self) -> Option<(i32, i32)> { pub fn get_position(&self) -> Option<(i32, i32)> {
unsafe { unsafe {
// let content_rect = NSWindow::contentRectForFrameRect_(self.window, NSWindow::frame(self.window)); let content_rect = NSWindow::contentRectForFrameRect_(self.window, NSWindow::frame(self.window));
let content_rect: NSRect = msg_send_stret()(self.window,
selector("contentRectForFrameRect:"),
NSWindow::frame(self.window));
// NOTE: coordinate system might be inconsistent with other backends // NOTE: coordinate system might be inconsistent with other backends
Some((content_rect.origin.x as i32, content_rect.origin.y as i32)) Some((content_rect.origin.x as i32, content_rect.origin.y as i32))
} }
@ -410,7 +409,7 @@ impl Window {
loop { loop {
unsafe { unsafe {
let event = NSApp().nextEventMatchingMask_untilDate_inMode_dequeue_( let event = NSApp().nextEventMatchingMask_untilDate_inMode_dequeue_(
NSAnyEventMask as u64, NSAnyEventMask.bits(),
NSDate::distantPast(nil), NSDate::distantPast(nil),
NSDefaultRunLoopMode, NSDefaultRunLoopMode,
YES); YES);
@ -433,7 +432,7 @@ impl Window {
self.is_closed.set(ds.is_closed); self.is_closed.set(ds.is_closed);
} }
match event.get_type() { match msg_send()(event, selector("type")) {
NSLeftMouseDown => { events.push_back(MouseInput(Pressed, LeftMouseButton)); }, NSLeftMouseDown => { events.push_back(MouseInput(Pressed, LeftMouseButton)); },
NSLeftMouseUp => { events.push_back(MouseInput(Released, LeftMouseButton)); }, NSLeftMouseUp => { events.push_back(MouseInput(Released, LeftMouseButton)); },
NSRightMouseDown => { events.push_back(MouseInput(Pressed, RightMouseButton)); }, NSRightMouseDown => { events.push_back(MouseInput(Pressed, RightMouseButton)); },
@ -461,30 +460,30 @@ impl Window {
} }
} }
let vkey = event::vkeycode_to_element(event.keycode()); let vkey = event::vkeycode_to_element(NSEvent::keyCode(event));
events.push_back(KeyboardInput(Pressed, event.keycode() as u8, vkey)); events.push_back(KeyboardInput(Pressed, NSEvent::keyCode(event) as u8, vkey));
}, },
NSKeyUp => { NSKeyUp => {
let vkey = event::vkeycode_to_element(event.keycode()); let vkey = event::vkeycode_to_element(NSEvent::keyCode(event));
events.push_back(KeyboardInput(Released, event.keycode() as u8, vkey)); events.push_back(KeyboardInput(Released, NSEvent::keyCode(event) as u8, vkey));
}, },
NSFlagsChanged => { NSFlagsChanged => {
let shift_modifier = Window::modifier_event(event, appkit::NSShiftKeyMask as u64, events::VirtualKeyCode::LShift, shift_pressed); let shift_modifier = Window::modifier_event(event, appkit::NSShiftKeyMask, events::VirtualKeyCode::LShift, shift_pressed);
if shift_modifier.is_some() { if shift_modifier.is_some() {
shift_pressed = !shift_pressed; shift_pressed = !shift_pressed;
events.push_back(shift_modifier.unwrap()); events.push_back(shift_modifier.unwrap());
} }
let ctrl_modifier = Window::modifier_event(event, appkit::NSControlKeyMask as u64, events::VirtualKeyCode::LControl, ctrl_pressed); let ctrl_modifier = Window::modifier_event(event, appkit::NSControlKeyMask, events::VirtualKeyCode::LControl, ctrl_pressed);
if ctrl_modifier.is_some() { if ctrl_modifier.is_some() {
ctrl_pressed = !ctrl_pressed; ctrl_pressed = !ctrl_pressed;
events.push_back(ctrl_modifier.unwrap()); events.push_back(ctrl_modifier.unwrap());
} }
let win_modifier = Window::modifier_event(event, appkit::NSCommandKeyMask as u64, events::VirtualKeyCode::LWin, win_pressed); let win_modifier = Window::modifier_event(event, appkit::NSCommandKeyMask, events::VirtualKeyCode::LWin, win_pressed);
if win_modifier.is_some() { if win_modifier.is_some() {
win_pressed = !win_pressed; win_pressed = !win_pressed;
events.push_back(win_modifier.unwrap()); events.push_back(win_modifier.unwrap());
} }
let alt_modifier = Window::modifier_event(event, appkit::NSAlternateKeyMask as u64, events::VirtualKeyCode::LAlt, alt_pressed); let alt_modifier = Window::modifier_event(event, appkit::NSAlternateKeyMask, events::VirtualKeyCode::LAlt, alt_pressed);
if alt_modifier.is_some() { if alt_modifier.is_some() {
alt_pressed = !alt_pressed; alt_pressed = !alt_pressed;
events.push_back(alt_modifier.unwrap()); events.push_back(alt_modifier.unwrap());
@ -501,25 +500,20 @@ impl Window {
events events
} }
unsafe fn modifier_event(event: id, keymask: u64, key: events::VirtualKeyCode, key_pressed: bool) -> Option<Event> { unsafe fn modifier_event(event: id, keymask: NSEventModifierFlags, key: events::VirtualKeyCode, key_pressed: bool) -> Option<Event> {
if !key_pressed && Window::modifier_key_pressed(event, keymask) { if !key_pressed && NSEvent::modifierFlags(event).contains(keymask) {
return Some(KeyboardInput(Pressed, event.keycode() as u8, Some(key))); return Some(KeyboardInput(Pressed, NSEvent::keyCode(event) as u8, Some(key)));
} } else if key_pressed && !NSEvent::modifierFlags(event).contains(keymask) {
else if key_pressed && !Window::modifier_key_pressed(event, keymask) { return Some(KeyboardInput(Released, NSEvent::keyCode(event) as u8, Some(key)));
return Some(KeyboardInput(Released, event.keycode() as u8, Some(key)));
} }
return None; return None;
} }
unsafe fn modifier_key_pressed(event: id, modifier: u64) -> bool {
event.modifierFlags() & modifier != 0
}
pub fn wait_events(&self) -> RingBuf<Event> { pub fn wait_events(&self) -> RingBuf<Event> {
unsafe { unsafe {
let event = NSApp().nextEventMatchingMask_untilDate_inMode_dequeue_( let event = NSApp().nextEventMatchingMask_untilDate_inMode_dequeue_(
NSAnyEventMask as u64, NSAnyEventMask.bits(),
NSDate::distantFuture(nil), NSDate::distantFuture(nil),
NSDefaultRunLoopMode, NSDefaultRunLoopMode,
NO); NO);
@ -568,7 +562,7 @@ impl Window {
pub fn hidpi_factor(&self) -> f32 { pub fn hidpi_factor(&self) -> f32 {
unsafe { unsafe {
self.window.backingScaleFactor() as f32 NSWindow::backingScaleFactor(self.window) as f32
} }
} }
} }