Merge pull request #792 from mitchmindtree/remove_glob_imports

Remove global imports from cocoa API in favour of explicitly importing traits and using namespacing for constants, types and functions.
This commit is contained in:
tomaka 2016-06-22 15:26:46 +02:00 committed by GitHub
commit c69b298724

View file

@ -22,9 +22,7 @@ use cgl::{CGLEnable, kCGLCECrashOnRemovedFunctions, CGLSetParameter, kCGLCPSurfa
use cocoa::base::{id, nil}; use cocoa::base::{id, nil};
use cocoa::foundation::{NSAutoreleasePool, NSDate, NSDefaultRunLoopMode, NSPoint, NSRect, NSSize, use cocoa::foundation::{NSAutoreleasePool, NSDate, NSDefaultRunLoopMode, NSPoint, NSRect, NSSize,
NSString, NSUInteger}; NSString, NSUInteger};
use cocoa::appkit; use cocoa::appkit::{self, NSApplication, NSEvent, NSOpenGLContext, NSOpenGLPixelFormat, NSView, NSWindow};
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;
@ -200,9 +198,9 @@ impl WindowProxy {
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), NSEventModifierFlags::empty(), nil, appkit::NSApplicationDefined, NSPoint::new(0.0, 0.0), appkit::NSEventModifierFlags::empty(),
0.0, 0, nil, NSApplicationActivatedEventType, 0, 0); 0.0, 0, nil, appkit::NSEventSubtype::NSApplicationActivatedEventType, 0, 0);
NSApp().postEvent_atStart_(event, NO); appkit::NSApp().postEvent_atStart_(event, NO);
pool.drain(); pool.drain();
} }
} }
@ -224,8 +222,8 @@ impl<'a> Iterator for PollEventsIterator<'a> {
unsafe { unsafe {
let pool = NSAutoreleasePool::new(nil); let pool = NSAutoreleasePool::new(nil);
let nsevent = NSApp().nextEventMatchingMask_untilDate_inMode_dequeue_( let nsevent = appkit::NSApp().nextEventMatchingMask_untilDate_inMode_dequeue_(
NSAnyEventMask.bits() | NSEventMaskPressure.bits(), appkit::NSAnyEventMask.bits() | appkit::NSEventMaskPressure.bits(),
NSDate::distantPast(nil), NSDate::distantPast(nil),
NSDefaultRunLoopMode, NSDefaultRunLoopMode,
YES); YES);
@ -253,8 +251,8 @@ impl<'a> Iterator for WaitEventsIterator<'a> {
unsafe { unsafe {
let pool = NSAutoreleasePool::new(nil); let pool = NSAutoreleasePool::new(nil);
let nsevent = NSApp().nextEventMatchingMask_untilDate_inMode_dequeue_( let nsevent = appkit::NSApp().nextEventMatchingMask_untilDate_inMode_dequeue_(
NSAnyEventMask.bits() | NSEventMaskPressure.bits(), appkit::NSAnyEventMask.bits() | appkit::NSEventMaskPressure.bits(),
NSDate::distantFuture(nil), NSDate::distantFuture(nil),
NSDefaultRunLoopMode, NSDefaultRunLoopMode,
YES); YES);
@ -359,7 +357,7 @@ impl Window {
fn create_app(activation_policy: ActivationPolicy) -> Option<id> { fn create_app(activation_policy: ActivationPolicy) -> Option<id> {
unsafe { unsafe {
let app = NSApp(); let app = appkit::NSApp();
if app == nil { if app == nil {
None None
} else { } else {
@ -379,13 +377,13 @@ impl Window {
_ => panic!("OS X monitors should always have a numeric native ID") _ => panic!("OS X monitors should always have a numeric native ID")
}; };
let matching_screen = { let matching_screen = {
let screens = NSScreen::screens(nil); let screens = appkit::NSScreen::screens(nil);
let count: NSUInteger = msg_send![screens, count]; let count: NSUInteger = msg_send![screens, count];
let key = IdRef::new(NSString::alloc(nil).init_str("NSScreenNumber")); let key = IdRef::new(NSString::alloc(nil).init_str("NSScreenNumber"));
let mut matching_screen: Option<id> = None; let mut matching_screen: Option<id> = None;
for i in 0..count { for i in 0..count {
let screen = msg_send![screens, objectAtIndex:i as NSUInteger]; let screen = msg_send![screens, objectAtIndex:i as NSUInteger];
let device_description = NSScreen::deviceDescription(screen); let device_description = appkit::NSScreen::deviceDescription(screen);
let value: id = msg_send![device_description, objectForKey:*key]; let value: id = msg_send![device_description, objectForKey:*key];
if value != nil { if value != nil {
let screen_number: NSUInteger = msg_send![value, unsignedIntegerValue]; let screen_number: NSUInteger = msg_send![value, unsignedIntegerValue];
@ -397,12 +395,12 @@ impl Window {
} }
matching_screen matching_screen
}; };
Some(matching_screen.unwrap_or(NSScreen::mainScreen(nil))) Some(matching_screen.unwrap_or(appkit::NSScreen::mainScreen(nil)))
}, },
None => None None => None
}; };
let frame = match screen { let frame = match screen {
Some(screen) => NSScreen::frame(screen), Some(screen) => appkit::NSScreen::frame(screen),
None => { None => {
let (width, height) = attrs.dimensions.unwrap_or((800, 600)); let (width, height) = attrs.dimensions.unwrap_or((800, 600));
NSRect::new(NSPoint::new(0., 0.), NSSize::new(width as f64, height as f64)) NSRect::new(NSPoint::new(0., 0.), NSSize::new(width as f64, height as f64))
@ -411,28 +409,28 @@ impl Window {
let masks = if screen.is_some() || attrs.transparent { let masks = if screen.is_some() || attrs.transparent {
// Fullscreen or transparent window // Fullscreen or transparent window
NSBorderlessWindowMask as NSUInteger | appkit::NSBorderlessWindowMask as NSUInteger |
NSResizableWindowMask as NSUInteger | appkit::NSResizableWindowMask as NSUInteger |
NSTitledWindowMask as NSUInteger appkit::NSTitledWindowMask as NSUInteger
} else if attrs.decorations { } else if attrs.decorations {
// Classic opaque window with titlebar // Classic opaque window with titlebar
NSClosableWindowMask as NSUInteger | appkit::NSClosableWindowMask as NSUInteger |
NSMiniaturizableWindowMask as NSUInteger | appkit::NSMiniaturizableWindowMask as NSUInteger |
NSResizableWindowMask as NSUInteger | appkit::NSResizableWindowMask as NSUInteger |
NSTitledWindowMask as NSUInteger appkit::NSTitledWindowMask as NSUInteger
} else { } else {
// Opaque window without a titlebar // Opaque window without a titlebar
NSClosableWindowMask as NSUInteger | appkit::NSClosableWindowMask as NSUInteger |
NSMiniaturizableWindowMask as NSUInteger | appkit::NSMiniaturizableWindowMask as NSUInteger |
NSResizableWindowMask as NSUInteger | appkit::NSResizableWindowMask as NSUInteger |
NSTitledWindowMask as NSUInteger | appkit::NSTitledWindowMask as NSUInteger |
NSFullSizeContentViewWindowMask as NSUInteger appkit::NSFullSizeContentViewWindowMask as NSUInteger
}; };
let window = IdRef::new(NSWindow::alloc(nil).initWithContentRect_styleMask_backing_defer_( let window = IdRef::new(NSWindow::alloc(nil).initWithContentRect_styleMask_backing_defer_(
frame, frame,
masks, masks,
NSBackingStoreBuffered, appkit::NSBackingStoreBuffered,
NO, NO,
)); ));
window.non_nil().map(|window| { window.non_nil().map(|window| {
@ -441,12 +439,12 @@ impl Window {
window.setAcceptsMouseMovedEvents_(YES); window.setAcceptsMouseMovedEvents_(YES);
if !attrs.decorations { if !attrs.decorations {
window.setTitleVisibility_(NSWindowTitleVisibility::NSWindowTitleHidden); window.setTitleVisibility_(appkit::NSWindowTitleVisibility::NSWindowTitleHidden);
window.setTitlebarAppearsTransparent_(YES); window.setTitlebarAppearsTransparent_(YES);
} }
if screen.is_some() { if screen.is_some() {
window.setLevel_(NSMainMenuWindowLevel as i64 + 1); window.setLevel_(appkit::NSMainMenuWindowLevel as i64 + 1);
} }
else { else {
window.center(); window.center();
@ -481,7 +479,7 @@ impl Window {
if let Some(cxt) = context.non_nil() { if let Some(cxt) = context.non_nil() {
let pf = { let pf = {
let get_attr = |attrib: NSOpenGLPixelFormatAttribute| -> i32 { let get_attr = |attrib: appkit::NSOpenGLPixelFormatAttribute| -> i32 {
let mut value = 0; let mut value = 0;
NSOpenGLPixelFormat::getValues_forAttribute_forVirtualScreen_( NSOpenGLPixelFormat::getValues_forAttribute_forVirtualScreen_(
@ -494,15 +492,15 @@ impl Window {
}; };
PixelFormat { PixelFormat {
hardware_accelerated: get_attr(NSOpenGLPFAAccelerated) != 0, hardware_accelerated: get_attr(appkit::NSOpenGLPFAAccelerated) != 0,
color_bits: (get_attr(NSOpenGLPFAColorSize) - get_attr(NSOpenGLPFAAlphaSize)) as u8, color_bits: (get_attr(appkit::NSOpenGLPFAColorSize) - get_attr(appkit::NSOpenGLPFAAlphaSize)) as u8,
alpha_bits: get_attr(NSOpenGLPFAAlphaSize) as u8, alpha_bits: get_attr(appkit::NSOpenGLPFAAlphaSize) as u8,
depth_bits: get_attr(NSOpenGLPFADepthSize) as u8, depth_bits: get_attr(appkit::NSOpenGLPFADepthSize) as u8,
stencil_bits: get_attr(NSOpenGLPFAStencilSize) as u8, stencil_bits: get_attr(appkit::NSOpenGLPFAStencilSize) as u8,
stereoscopy: get_attr(NSOpenGLPFAStereo) != 0, stereoscopy: get_attr(appkit::NSOpenGLPFAStereo) != 0,
double_buffer: get_attr(NSOpenGLPFADoubleBuffer) != 0, double_buffer: get_attr(appkit::NSOpenGLPFADoubleBuffer) != 0,
multisampling: if get_attr(NSOpenGLPFAMultisample) > 0 { multisampling: if get_attr(appkit::NSOpenGLPFAMultisample) > 0 {
Some(get_attr(NSOpenGLPFASamples) as u16) Some(get_attr(appkit::NSOpenGLPFASamples) as u16)
} else { } else {
None None
}, },
@ -512,7 +510,7 @@ impl Window {
cxt.setView_(view); cxt.setView_(view);
let value = if opengl.vsync { 1 } else { 0 }; let value = if opengl.vsync { 1 } else { 0 };
cxt.setValues_forParameter_(&value, NSOpenGLContextParameter::NSOpenGLCPSwapInterval); cxt.setValues_forParameter_(&value, appkit::NSOpenGLContextParameter::NSOpenGLCPSwapInterval);
CGLEnable(cxt.CGLContextObj() as *mut _, kCGLCECrashOnRemovedFunctions); CGLEnable(cxt.CGLContextObj() as *mut _, kCGLCECrashOnRemovedFunctions);
@ -615,7 +613,7 @@ impl Window {
} }
} }
unsafe fn modifier_event(event: id, keymask: NSEventModifierFlags, key: events::VirtualKeyCode, key_pressed: bool) -> Option<Event> { unsafe fn modifier_event(event: id, keymask: appkit::NSEventModifierFlags, key: events::VirtualKeyCode, key_pressed: bool) -> Option<Event> {
if !key_pressed && NSEvent::modifierFlags(event).contains(keymask) { if !key_pressed && NSEvent::modifierFlags(event).contains(keymask) {
return Some(Event::KeyboardInput(ElementState::Pressed, NSEvent::keyCode(event) as u8, Some(key))); return Some(Event::KeyboardInput(ElementState::Pressed, NSEvent::keyCode(event) as u8, Some(key)));
} else if key_pressed && !NSEvent::modifierFlags(event).contains(keymask) { } else if key_pressed && !NSEvent::modifierFlags(event).contains(keymask) {
@ -713,7 +711,10 @@ impl Window {
unsafe { unsafe {
// TODO: Check for errors. // TODO: Check for errors.
let _ = CGWarpMouseCursorPosition(CGPoint { x: cursor_x as CGFloat, y: cursor_y as CGFloat }); let _ = CGWarpMouseCursorPosition(appkit::CGPoint {
x: cursor_x as appkit::CGFloat,
y: cursor_y as appkit::CGFloat,
});
let _ = CGAssociateMouseAndMouseCursorPosition(true); let _ = CGAssociateMouseAndMouseCursorPosition(true);
} }
@ -824,17 +825,17 @@ unsafe fn NSEventToEvent(window: &Window, nsevent: id) -> Option<Event> {
if nsevent == nil { return None; } if nsevent == nil { return None; }
let event_type = nsevent.eventType(); let event_type = nsevent.eventType();
NSApp().sendEvent_(if let NSKeyDown = event_type { nil } else { nsevent }); appkit::NSApp().sendEvent_(if let appkit::NSKeyDown = event_type { nil } else { nsevent });
match event_type { match event_type {
NSLeftMouseDown => { Some(Event::MouseInput(ElementState::Pressed, MouseButton::Left)) }, appkit::NSLeftMouseDown => { Some(Event::MouseInput(ElementState::Pressed, MouseButton::Left)) },
NSLeftMouseUp => { Some(Event::MouseInput(ElementState::Released, MouseButton::Left)) }, appkit::NSLeftMouseUp => { Some(Event::MouseInput(ElementState::Released, MouseButton::Left)) },
NSRightMouseDown => { Some(Event::MouseInput(ElementState::Pressed, MouseButton::Right)) }, appkit::NSRightMouseDown => { Some(Event::MouseInput(ElementState::Pressed, MouseButton::Right)) },
NSRightMouseUp => { Some(Event::MouseInput(ElementState::Released, MouseButton::Right)) }, appkit::NSRightMouseUp => { Some(Event::MouseInput(ElementState::Released, MouseButton::Right)) },
NSMouseMoved | appkit::NSMouseMoved |
NSLeftMouseDragged | appkit::NSLeftMouseDragged |
NSOtherMouseDragged | appkit::NSOtherMouseDragged |
NSRightMouseDragged => { appkit::NSRightMouseDragged => {
let window_point = nsevent.locationInWindow(); let window_point = nsevent.locationInWindow();
let cWindow: id = msg_send![nsevent, window]; let cWindow: id = msg_send![nsevent, window];
let view_point = if cWindow == nil { let view_point = if cWindow == nil {
@ -849,7 +850,7 @@ unsafe fn NSEventToEvent(window: &Window, nsevent: id) -> Option<Event> {
Some(Event::MouseMoved((scale_factor * view_point.x as f32) as i32, Some(Event::MouseMoved((scale_factor * view_point.x as f32) as i32,
(scale_factor * (view_rect.size.height - view_point.y) as f32) as i32)) (scale_factor * (view_rect.size.height - view_point.y) as f32) as i32))
}, },
NSKeyDown => { appkit::NSKeyDown => {
let mut events = VecDeque::new(); let mut events = VecDeque::new();
let received_c_str = nsevent.characters().UTF8String(); let received_c_str = nsevent.characters().UTF8String();
let received_str = CStr::from_ptr(received_c_str); let received_str = CStr::from_ptr(received_c_str);
@ -863,12 +864,12 @@ unsafe fn NSEventToEvent(window: &Window, nsevent: id) -> Option<Event> {
window.delegate.state.pending_events.lock().unwrap().extend(events.into_iter()); window.delegate.state.pending_events.lock().unwrap().extend(events.into_iter());
event event
}, },
NSKeyUp => { appkit::NSKeyUp => {
let vkey = event::vkeycode_to_element(NSEvent::keyCode(nsevent)); let vkey = event::vkeycode_to_element(NSEvent::keyCode(nsevent));
Some(Event::KeyboardInput(ElementState::Released, NSEvent::keyCode(nsevent) as u8, vkey)) Some(Event::KeyboardInput(ElementState::Released, NSEvent::keyCode(nsevent) as u8, vkey))
}, },
NSFlagsChanged => { appkit::NSFlagsChanged => {
let mut events = VecDeque::new(); let mut events = VecDeque::new();
let shift_modifier = Window::modifier_event(nsevent, appkit::NSShiftKeyMask, events::VirtualKeyCode::LShift, shift_pressed); let shift_modifier = Window::modifier_event(nsevent, appkit::NSShiftKeyMask, events::VirtualKeyCode::LShift, shift_pressed);
if shift_modifier.is_some() { if shift_modifier.is_some() {
@ -894,7 +895,7 @@ unsafe fn NSEventToEvent(window: &Window, nsevent: id) -> Option<Event> {
window.delegate.state.pending_events.lock().unwrap().extend(events.into_iter()); window.delegate.state.pending_events.lock().unwrap().extend(events.into_iter());
event event
}, },
NSScrollWheel => { appkit::NSScrollWheel => {
use events::MouseScrollDelta::{LineDelta, PixelDelta}; use events::MouseScrollDelta::{LineDelta, PixelDelta};
let scale_factor = window.hidpi_factor(); let scale_factor = window.hidpi_factor();
let delta = if nsevent.hasPreciseScrollingDeltas() == YES { let delta = if nsevent.hasPreciseScrollingDeltas() == YES {
@ -905,13 +906,13 @@ unsafe fn NSEventToEvent(window: &Window, nsevent: id) -> Option<Event> {
scale_factor * nsevent.scrollingDeltaY() as f32) scale_factor * nsevent.scrollingDeltaY() as f32)
}; };
let phase = match nsevent.phase() { let phase = match nsevent.phase() {
NSEventPhaseMayBegin | NSEventPhaseBegan => TouchPhase::Started, appkit::NSEventPhaseMayBegin | appkit::NSEventPhaseBegan => TouchPhase::Started,
NSEventPhaseEnded => TouchPhase::Ended, appkit::NSEventPhaseEnded => TouchPhase::Ended,
_ => TouchPhase::Moved, _ => TouchPhase::Moved,
}; };
Some(Event::MouseWheel(delta, phase)) Some(Event::MouseWheel(delta, phase))
}, },
NSEventTypePressure => { appkit::NSEventTypePressure => {
Some(Event::TouchpadPressure(nsevent.pressure(), nsevent.stage())) Some(Event::TouchpadPressure(nsevent.pressure(), nsevent.stage()))
}, },
_ => { None }, _ => { None },