Use class macro (#605)

This commit is contained in:
Josh Groves 2018-07-19 10:02:33 -06:00 committed by Francesca Frangipane
parent 0cb5450999
commit 7ee46d80e6
8 changed files with 30 additions and 37 deletions

View file

@ -26,10 +26,10 @@ image = { version = "0.19", optional = true }
version = "0.2" version = "0.2"
[target.'cfg(target_os = "ios")'.dependencies] [target.'cfg(target_os = "ios")'.dependencies]
objc = "0.2" objc = "0.2.3"
[target.'cfg(target_os = "macos")'.dependencies] [target.'cfg(target_os = "macos")'.dependencies]
objc = "0.2" objc = "0.2.3"
cocoa = "0.15" cocoa = "0.15"
core-foundation = "0.6" core-foundation = "0.6"
core-graphics = "0.14" core-graphics = "0.14"

View file

@ -4,7 +4,7 @@ use std::ffi::CString;
use std::mem; use std::mem;
use std::os::raw::*; use std::os::raw::*;
use objc::runtime::{Class, Object}; use objc::runtime::Object;
pub type id = *mut Object; pub type id = *mut Object;
pub const nil: id = 0 as id; pub const nil: id = 0 as id;
@ -78,7 +78,7 @@ extern {
pub trait NSString: Sized { pub trait NSString: Sized {
unsafe fn alloc(_: Self) -> id { unsafe fn alloc(_: Self) -> id {
msg_send![class("NSString"), alloc] msg_send![class!(NSString), alloc]
} }
unsafe fn initWithUTF8String_(self, c_string: *const c_char) -> id; unsafe fn initWithUTF8String_(self, c_string: *const c_char) -> id;
@ -105,10 +105,3 @@ impl NSString for id {
msg_send![self, UTF8String] msg_send![self, UTF8String]
} }
} }
#[inline]
pub fn class(name: &str) -> *mut Class {
unsafe {
mem::transmute(Class::get(name))
}
}

View file

@ -170,7 +170,7 @@ impl fmt::Debug for MonitorId {
impl MonitorId { impl MonitorId {
#[inline] #[inline]
pub fn get_uiscreen(&self) -> id { pub fn get_uiscreen(&self) -> id {
let class = Class::get("UIScreen").expect("Failed to get class `UIScreen`"); let class = class!(UIScreen);
unsafe { msg_send![class, mainScreen] } unsafe { msg_send![class, mainScreen] }
} }
@ -209,7 +209,7 @@ impl EventsLoop {
pub fn new() -> EventsLoop { pub fn new() -> EventsLoop {
unsafe { unsafe {
if setjmp(mem::transmute(&mut JMPBUF)) != 0 { if setjmp(mem::transmute(&mut JMPBUF)) != 0 {
let app_class = Class::get("UIApplication").expect("Failed to get class `UIApplication`"); let app_class = class!(UIApplication);
let app: id = msg_send![app_class, sharedApplication]; let app: id = msg_send![app_class, sharedApplication];
let delegate: id = msg_send![app, delegate]; let delegate: id = msg_send![app, delegate];
let state: *mut c_void = *(&*delegate).get_ivar("winitState"); let state: *mut c_void = *(&*delegate).get_ivar("winitState");
@ -469,10 +469,10 @@ impl Window {
fn create_delegate_class() { fn create_delegate_class() {
extern fn did_finish_launching(this: &mut Object, _: Sel, _: id, _: id) -> BOOL { extern fn did_finish_launching(this: &mut Object, _: Sel, _: id, _: id) -> BOOL {
let screen_class = Class::get("UIScreen").expect("Failed to get class `UIScreen`"); let screen_class = class!(UIScreen);
let window_class = Class::get("UIWindow").expect("Failed to get class `UIWindow`"); let window_class = class!(UIWindow);
let controller_class = Class::get("MainViewController").expect("Failed to get class `MainViewController`"); let controller_class = class!(MainViewController);
let view_class = Class::get("MainView").expect("Failed to get class `MainView`"); let view_class = class!(MainView);
unsafe { unsafe {
let main_screen: id = msg_send![screen_class, mainScreen]; let main_screen: id = msg_send![screen_class, mainScreen];
let bounds: CGRect = msg_send![main_screen, bounds]; let bounds: CGRect = msg_send![main_screen, bounds];
@ -595,7 +595,7 @@ fn create_delegate_class() {
} }
} }
let ui_responder = Class::get("UIResponder").expect("Failed to get class `UIResponder`"); let ui_responder = class!(UIResponder);
let mut decl = ClassDecl::new("AppDelegate", ui_responder).expect("Failed to declare class `AppDelegate`"); let mut decl = ClassDecl::new("AppDelegate", ui_responder).expect("Failed to declare class `AppDelegate`");
unsafe { unsafe {
@ -642,7 +642,7 @@ fn create_delegate_class() {
// TODO: winit shouldn't contain GL-specfiic code // TODO: winit shouldn't contain GL-specfiic code
pub fn create_view_class() { pub fn create_view_class() {
let superclass = Class::get("UIViewController").expect("Failed to get class `UIViewController`"); let superclass = class!(UIViewController);
let decl = ClassDecl::new("MainViewController", superclass).expect("Failed to declare class `MainViewController`"); let decl = ClassDecl::new("MainViewController", superclass).expect("Failed to declare class `MainViewController`");
decl.register(); decl.register();
@ -663,10 +663,10 @@ pub fn create_view_class() {
} }
extern fn layer_class(_: &Class, _: Sel) -> *const Class { extern fn layer_class(_: &Class, _: Sel) -> *const Class {
unsafe { mem::transmute(Class::get("CAEAGLLayer").expect("Failed to get class `CAEAGLLayer`")) } class!(CAEAGLLayer)
} }
let superclass = Class::get("GLKView").expect("Failed to get class `GLKView`"); let superclass = class!(GLKView);
let mut decl = ClassDecl::new("MainView", superclass).expect("Failed to declare class `MainView`"); let mut decl = ClassDecl::new("MainView", superclass).expect("Failed to declare class `MainView`");
unsafe { unsafe {
decl.add_method(sel!(initForGl:), init_for_gl as extern fn(&Object, Sel, *const c_void) -> id); decl.add_method(sel!(initForGl:), init_for_gl as extern fn(&Object, Sel, *const c_void) -> id);

View file

@ -180,7 +180,7 @@ impl EventsLoop {
where F: FnMut(Event), where F: FnMut(Event),
{ {
unsafe { unsafe {
if !msg_send![cocoa::base::class("NSThread"), isMainThread] { if !msg_send![class!(NSThread), isMainThread] {
panic!("Events can only be polled from the main thread on macOS"); panic!("Events can only be polled from the main thread on macOS");
} }
} }
@ -221,7 +221,7 @@ impl EventsLoop {
where F: FnMut(Event) -> ControlFlow where F: FnMut(Event) -> ControlFlow
{ {
unsafe { unsafe {
if !msg_send![cocoa::base::class("NSThread"), isMainThread] { if !msg_send![class!(NSThread), isMainThread] {
panic!("Events can only be polled from the main thread on macOS"); panic!("Events can only be polled from the main thread on macOS");
} }
} }

View file

@ -35,7 +35,7 @@ unsafe impl objc::Encode for NSRange {
pub trait NSMutableAttributedString: Sized { pub trait NSMutableAttributedString: Sized {
unsafe fn alloc(_: Self) -> id { unsafe fn alloc(_: Self) -> id {
msg_send![class("NSMutableAttributedString"), alloc] msg_send![class!(NSMutableAttributedString), alloc]
} }
unsafe fn init(self) -> id; // *mut NSMutableAttributedString unsafe fn init(self) -> id; // *mut NSMutableAttributedString

View file

@ -26,13 +26,13 @@ pub unsafe fn set_style_mask(window: id, view: id, mask: NSWindowStyleMask) {
} }
pub unsafe fn create_input_context(view: id) -> IdRef { pub unsafe fn create_input_context(view: id) -> IdRef {
let input_context: id = msg_send![class("NSTextInputContext"), alloc]; let input_context: id = msg_send![class!(NSTextInputContext), alloc];
let input_context: id = msg_send![input_context, initWithClient:view]; let input_context: id = msg_send![input_context, initWithClient:view];
IdRef::new(input_context) IdRef::new(input_context)
} }
#[allow(dead_code)] #[allow(dead_code)]
pub unsafe fn open_emoji_picker() { pub unsafe fn open_emoji_picker() {
let app: id = msg_send![class("NSApplication"), sharedApplication]; let app: id = msg_send![class!(NSApplication), sharedApplication];
let _: () = msg_send![app, orderFrontCharacterPalette:nil]; let _: () = msg_send![app, orderFrontCharacterPalette:nil];
} }

View file

@ -64,7 +64,7 @@ unsafe impl Sync for ViewClass {}
lazy_static! { lazy_static! {
static ref VIEW_CLASS: ViewClass = unsafe { static ref VIEW_CLASS: ViewClass = unsafe {
let superclass = Class::get("NSView").unwrap(); let superclass = class!(NSView);
let mut decl = ClassDecl::new("WinitView", superclass).unwrap(); let mut decl = ClassDecl::new("WinitView", superclass).unwrap();
decl.add_method(sel!(dealloc), dealloc as extern fn(&Object, Sel)); decl.add_method(sel!(dealloc), dealloc as extern fn(&Object, Sel));
decl.add_method( decl.add_method(
@ -191,7 +191,7 @@ extern fn set_marked_text(
let marked_text_ref: &mut id = this.get_mut_ivar("markedText"); let marked_text_ref: &mut id = this.get_mut_ivar("markedText");
let _: () = msg_send![(*marked_text_ref), release]; let _: () = msg_send![(*marked_text_ref), release];
let marked_text = NSMutableAttributedString::alloc(nil); let marked_text = NSMutableAttributedString::alloc(nil);
let has_attr = msg_send![string, isKindOfClass:class("NSAttributedString")]; let has_attr = msg_send![string, isKindOfClass:class!(NSAttributedString)];
if has_attr { if has_attr {
marked_text.initWithAttributedString(string); marked_text.initWithAttributedString(string);
} else { } else {
@ -214,7 +214,7 @@ extern fn unmark_text(this: &Object, _sel: Sel) {
extern fn valid_attributes_for_marked_text(_this: &Object, _sel: Sel) -> id { extern fn valid_attributes_for_marked_text(_this: &Object, _sel: Sel) -> id {
//println!("validAttributesForMarkedText"); //println!("validAttributesForMarkedText");
unsafe { msg_send![class("NSArray"), array] } unsafe { msg_send![class!(NSArray), array] }
} }
extern fn attributed_substring_for_proposed_range( extern fn attributed_substring_for_proposed_range(
@ -265,7 +265,7 @@ extern fn insert_text(this: &Object, _sel: Sel, string: id, _replacement_range:
let state_ptr: *mut c_void = *this.get_ivar("winitState"); let state_ptr: *mut c_void = *this.get_ivar("winitState");
let state = &mut *(state_ptr as *mut ViewState); let state = &mut *(state_ptr as *mut ViewState);
let has_attr = msg_send![string, isKindOfClass:class("NSAttributedString")]; let has_attr = msg_send![string, isKindOfClass:class!(NSAttributedString)];
let characters = if has_attr { let characters = if has_attr {
// This is a *mut NSAttributedString // This is a *mut NSAttributedString
msg_send![string, string] msg_send![string, string]
@ -282,7 +282,7 @@ extern fn insert_text(this: &Object, _sel: Sel, string: id, _replacement_range:
state.last_insert = Some(string.to_owned()); state.last_insert = Some(string.to_owned());
// We don't need this now, but it's here if that changes. // We don't need this now, but it's here if that changes.
//let event: id = msg_send![class("NSApp"), currentEvent]; //let event: id = msg_send![class!(NSApp), currentEvent];
let mut events = VecDeque::with_capacity(characters.len()); let mut events = VecDeque::with_capacity(characters.len());
for character in string.chars() { for character in string.chars() {
@ -400,7 +400,7 @@ extern fn key_down(this: &Object, _sel: Sel, event: id) {
// Some keys (and only *some*, with no known reason) don't trigger `insertText`, while others do... // Some keys (and only *some*, with no known reason) don't trigger `insertText`, while others do...
// So, we don't give repeats the opportunity to trigger that, since otherwise our hack will cause some // So, we don't give repeats the opportunity to trigger that, since otherwise our hack will cause some
// keys to generate twice as many characters. // keys to generate twice as many characters.
let array: id = msg_send![class("NSArray"), arrayWithObject:event]; let array: id = msg_send![class!(NSArray), arrayWithObject:event];
let (): _ = msg_send![this, interpretKeyEvents:array]; let (): _ = msg_send![this, interpretKeyEvents:array];
} }
} }

View file

@ -421,7 +421,7 @@ impl WindowDelegate {
INIT.call_once(|| unsafe { INIT.call_once(|| unsafe {
// Create new NSWindowDelegate // Create new NSWindowDelegate
let superclass = Class::get("NSObject").unwrap(); let superclass = class!(NSObject);
let mut decl = ClassDecl::new("WinitWindowDelegate", superclass).unwrap(); let mut decl = ClassDecl::new("WinitWindowDelegate", superclass).unwrap();
// Add callback methods // Add callback methods
@ -591,7 +591,7 @@ impl Window2 {
pl_attribs: PlatformSpecificWindowBuilderAttributes, pl_attribs: PlatformSpecificWindowBuilderAttributes,
) -> Result<Window2, CreationError> { ) -> Result<Window2, CreationError> {
unsafe { unsafe {
if !msg_send![cocoa::base::class("NSThread"), isMainThread] { if !msg_send![class!(NSThread), isMainThread] {
panic!("Windows can only be created on the main thread on macOS"); panic!("Windows can only be created on the main thread on macOS");
} }
} }
@ -728,7 +728,7 @@ impl Window2 {
static INIT: std::sync::Once = std::sync::ONCE_INIT; static INIT: std::sync::Once = std::sync::ONCE_INIT;
INIT.call_once(|| unsafe { INIT.call_once(|| unsafe {
let window_superclass = Class::get("NSWindow").unwrap(); let window_superclass = class!(NSWindow);
let mut decl = ClassDecl::new("WinitWindow", window_superclass).unwrap(); let mut decl = ClassDecl::new("WinitWindow", window_superclass).unwrap();
decl.add_method(sel!(canBecomeMainWindow), yes as extern fn(&Object, Sel) -> BOOL); decl.add_method(sel!(canBecomeMainWindow), yes as extern fn(&Object, Sel) -> BOOL);
decl.add_method(sel!(canBecomeKeyWindow), yes as extern fn(&Object, Sel) -> BOOL); decl.add_method(sel!(canBecomeKeyWindow), yes as extern fn(&Object, Sel) -> BOOL);
@ -987,7 +987,7 @@ impl Window2 {
MouseCursor::ZoomOut => "arrowCursor", MouseCursor::ZoomOut => "arrowCursor",
}; };
let sel = Sel::register(cursor_name); let sel = Sel::register(cursor_name);
let cls = Class::get("NSCursor").unwrap(); let cls = class!(NSCursor);
unsafe { unsafe {
use objc::Message; use objc::Message;
let cursor: id = cls.send_message(sel, ()).unwrap(); let cursor: id = cls.send_message(sel, ()).unwrap();
@ -1004,7 +1004,7 @@ impl Window2 {
#[inline] #[inline]
pub fn hide_cursor(&self, hide: bool) { pub fn hide_cursor(&self, hide: bool) {
let cursor_class = Class::get("NSCursor").unwrap(); let cursor_class = class!(NSCursor);
// macOS uses a "hide counter" like Windows does, so we avoid incrementing it more than once. // macOS uses a "hide counter" like Windows does, so we avoid incrementing it more than once.
// (otherwise, `hide_cursor(false)` would need to be called n times!) // (otherwise, `hide_cursor(false)` would need to be called n times!)
if hide != self.cursor_hidden.get() { if hide != self.cursor_hidden.get() {