summary history branches tags files
commit:a38db83cf64aa50b01c1feb4567e44ad77d3195e
author:Trevor Bentley
committer:Trevor Bentley
date:Fri Jul 12 19:16:53 2019 +0200
parents:f0f12c55470bdb2331300a5ff42354c950036ee8
remove cocoa dependency
diff --git a/Cargo.toml b/Cargo.toml
line changes: +0/-1
index 0b5daa5..96f9bcc
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -25,7 +25,6 @@ log = "0.4"
 
 [target."cfg(target_os = \"macos\")".dependencies]
 libc = "0.2"
-cocoa = "0.18"
 objc-foundation = "0.1"
 objc_id = "0.1"
 

diff --git a/src/touchbar.rs b/src/touchbar.rs
line changes: +117/-9
index 5bf69b1..549b81c
--- a/src/touchbar.rs
+++ b/src/touchbar.rs
@@ -1,7 +1,6 @@
 extern crate objc;
 extern crate objc_foundation;
 extern crate objc_id;
-extern crate cocoa;
 
 use super::interface::*;
 
@@ -15,10 +14,6 @@ use objc::Message;
 use objc::declare::ClassDecl;
 use objc::runtime::{Class, Object, Sel};
 use self::objc_foundation::{INSObject, NSObject};
-use self::cocoa::base::{nil, YES, NO, SEL};
-use self::cocoa::foundation::NSString;
-use self::cocoa::foundation::{NSRect, NSPoint, NSSize};
-use self::cocoa::appkit::{NSApp, NSImage};
 use self::objc_id::Id;
 use self::objc_id::Shared;
 
@@ -60,6 +55,120 @@ const IDENT_PREFIX: &'static str = "com.trevorbentley.";
 /// ```
 pub type Touchbar = Box<RustTouchbarDelegateWrapper>;
 
+
+/////
+///// Simulate 'cocoa' crate
+/////
+///// Originally used 'cocoa' crate for some AppKit stuff.  To minimize
+///// dependencies, the things below are either copied from the cocoa
+///// crate, or minimally reimplemented
+/////
+#[allow(non_upper_case_globals)]
+const nil: *mut Object = 0 as *mut Object;
+const YES: i8 = 1i8;
+const NO: i8 = 0i8;
+
+
+#[allow(non_snake_case)]
+unsafe fn NSApp() -> *mut Object {
+    let cls = Class::get("NSApplication").unwrap();
+    msg_send![cls, sharedApplication]
+}
+
+//             let res = NSString::alloc(nil).init_str(name);
+struct NSString(*mut Object);
+impl NSString {
+    unsafe fn alloc(_: *mut Object) -> NSString {
+        let cls = Class::get("NSString").unwrap();
+        NSString(msg_send![cls, alloc])
+    }
+    unsafe fn init_str(self, s: &str) -> *mut Object {
+        msg_send![self.0, initWithBytes: s.as_ptr() length: s.len() encoding: 4]
+    }
+}
+
+// let objc_image = NSImage::alloc(nil).initWithContentsOfFile_(filename);
+struct NSImage(*mut Object);
+impl NSImage {
+    unsafe fn alloc(_: *mut Object) -> NSImage {
+        let cls = Class::get("NSImage").unwrap();
+        NSImage(msg_send![cls, alloc])
+    }
+    #[allow(non_snake_case)]
+    unsafe fn initWithContentsOfFile_(self, filename: *mut Object) -> *mut Object {
+        msg_send![self.0, initWithContentsOfFile: filename]
+    }
+}
+
+
+#[repr(C)]
+#[derive(Copy, Clone)]
+pub struct NSPoint {
+    pub x: f64,
+    pub y: f64,
+}
+
+impl NSPoint {
+    #[inline]
+    pub fn new(x: f64, y: f64) -> NSPoint {
+        NSPoint {
+            x: x,
+            y: y,
+        }
+    }
+}
+unsafe impl objc::Encode for NSPoint {
+    fn encode() -> objc::Encoding {
+        let encoding = format!("{{CGPoint={}{}}}",
+                               f64::encode().as_str(),
+                               f64::encode().as_str());
+        unsafe { objc::Encoding::from_str(&encoding) }
+    }
+}
+#[repr(C)]
+#[derive(Copy, Clone)]
+pub struct NSSize {
+    pub width: f64,
+    pub height: f64,
+}
+
+impl NSSize {
+    #[inline]
+    pub fn new(width: f64, height: f64) -> NSSize {
+        NSSize {
+            width: width,
+            height: height,
+        }
+    }
+}
+unsafe impl objc::Encode for NSSize {
+    fn encode() -> objc::Encoding {
+        let encoding = format!("{{CGSize={}{}}}",
+                               f64::encode().as_str(),
+                               f64::encode().as_str());
+        unsafe { objc::Encoding::from_str(&encoding) }
+    }
+}
+#[repr(C)]
+#[derive(Copy, Clone)]
+pub struct NSRect {
+    pub origin: NSPoint,
+    pub size: NSSize,
+}
+impl NSRect {
+    pub fn new(origin: NSPoint, size: NSSize) -> NSRect {
+        NSRect {
+            origin: origin,
+            size: size
+        }
+    }
+}
+
+/////
+///// End simulate 'cocoa' crate
+/////
+
+
 pub type Ident = u64;
 
 #[link(name = "DFRFoundation", kind = "framework")]
@@ -123,14 +232,13 @@ pub mod util {
     //! Mac environments.
 
     extern crate libc;
-    extern crate cocoa;
     use super::ItemId;
     use std::ptr;
     use std::ffi::CStr;
     use objc::runtime::Object;
     use objc::runtime::Class;
-    use self::cocoa::foundation::NSString;
-    use self::cocoa::base::nil;
+    use super::NSString;
+    use super::nil;
     use super::AppKitVersion;
 
     #[allow(dead_code)]
@@ -416,7 +524,7 @@ impl RustTouchbarDelegateWrapper {
         }
     }
     fn alloc_button(&mut self, image: Option<&TouchbarImage>, text: Option<&str>,
-                    target: *mut Object, sel: SEL) -> *mut Object {
+                    target: *mut Object, sel: Sel) -> *mut Object {
         unsafe {
             let text = match text {
                 Some(s) => NSString::alloc(nil).init_str(s),