diff --git a/Cargo.toml b/Cargo.toml index e212834..a338644 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,9 +9,9 @@ repository = "https://github.com/jmquigs/rs-barfly" [dependencies] [target.x86_64-apple-darwin.dependencies] -cocoa = "*" -objc = "*" -core-graphics = "*" -objc-foundation = "*" -objc_id = "*" -libc = "*" +cocoa = "0.8" +objc = "0.2" +core-graphics = "0.8" +objc-foundation = "0.3" +objc_id = "0.1" +libc = "0.2" -- 2.51.2 From abc84218be96427f3cb740a5e528dc8a31c739a4 Mon Sep 17 00:00:00 2001 From: Corey Farwell Date: Thu, 15 Jun 2017 17:41:20 -0400 Subject: [PATCH 02/13] Fix compilation with latest objc crate. --- src/osx/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/osx/mod.rs b/src/osx/mod.rs index f8acd90..0ae7582 100644 --- a/src/osx/mod.rs +++ b/src/osx/mod.rs @@ -150,7 +150,7 @@ impl INSObject for Callback { if klass.is_none() { println!("registering class for {}", cname); let superclass = NSObject::class(); - let mut decl = ClassDecl::new(superclass, &cname).unwrap(); + let mut decl = ClassDecl::new(&cname, superclass).unwrap(); decl.add_ivar::("_cbptr"); extern "C" fn barfly_callback_call(this: &Object, _cmd: Sel) { -- 2.51.2 From cf216af5fad00bf8549b61be0a0f5e831a0dd855 Mon Sep 17 00:00:00 2001 From: Corey Farwell Date: Fri, 16 Jun 2017 09:36:23 -0700 Subject: [PATCH 03/13] Fix objc-foundation version. --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index a338644..9bd85d9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,6 +12,6 @@ repository = "https://github.com/jmquigs/rs-barfly" cocoa = "0.8" objc = "0.2" core-graphics = "0.8" -objc-foundation = "0.3" +objc-foundation = "0.1" objc_id = "0.1" libc = "0.2" -- 2.51.2 From f6b012c1dfaea64f1104d6e07028786e57d7d697 Mon Sep 17 00:00:00 2001 From: Corey Farwell Date: Fri, 16 Jun 2017 09:38:43 -0700 Subject: [PATCH 04/13] Don't expose osx module. --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index e41f995..f414fdd 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,5 @@ #[cfg(target_os="macos")] -pub mod osx; +mod osx; #[cfg(target_os="macos")] #[macro_use] -- 2.51.2 From 7f47d95ad6531a3c79fe8eac323c7146b762d330 Mon Sep 17 00:00:00 2001 From: Corey Farwell Date: Fri, 16 Jun 2017 09:54:33 -0700 Subject: [PATCH 05/13] Use `NSStatusBar` from cocoa-rs. --- src/osx/mod.rs | 6 +++--- src/osx/objc_ext.rs | 14 -------------- 2 files changed, 3 insertions(+), 17 deletions(-) diff --git a/src/osx/mod.rs b/src/osx/mod.rs index 0ae7582..e02951d 100644 --- a/src/osx/mod.rs +++ b/src/osx/mod.rs @@ -9,7 +9,7 @@ pub use objc::Message; extern crate cocoa; pub use self::cocoa::base::{selector, nil, YES /* id, class, BOOL */}; pub use self::cocoa::appkit::{NSApp, NSApplication, NSWindow, NSMenu, NSMenuItem, NSRunningApplication, - NSApplicationActivateIgnoringOtherApps}; + NSApplicationActivateIgnoringOtherApps, NSStatusBar}; extern crate libc; pub use self::libc::c_void; @@ -20,7 +20,7 @@ extern crate objc_id; pub use self::objc_id::Id; mod objc_ext; -use self::objc_ext::{NSStatusBar, NSStatusItem}; +use self::objc_ext::{NSStatusItem}; extern crate objc_foundation; pub use self::cocoa::foundation::{NSAutoreleasePool, NSString}; @@ -85,7 +85,7 @@ impl Barfly for OsxBarfly { let app = NSApp(); app.activateIgnoringOtherApps_(YES); - let item = NSStatusBar::systemStatusBar(nil).statusItemWithLength(-1.0); + let item = NSStatusBar::systemStatusBar(nil).statusItemWithLength_(-1.0); item.setHighlightMode_(YES); let title = NSString::alloc(nil) .init_str(&self.name); diff --git a/src/osx/objc_ext.rs b/src/osx/objc_ext.rs index bf57652..e2f8a4b 100644 --- a/src/osx/objc_ext.rs +++ b/src/osx/objc_ext.rs @@ -4,20 +4,6 @@ use self::core_graphics::base::CGFloat; extern crate cocoa; use self::cocoa::base::{id, class, BOOL}; -pub trait NSStatusBar { - unsafe fn systemStatusBar(_: Self) -> id { - msg_send![class("NSStatusBar"), systemStatusBar] - } - - unsafe fn statusItemWithLength(self, len: CGFloat) -> id; -} - -impl NSStatusBar for id { - unsafe fn statusItemWithLength(self, len: CGFloat) -> id { - msg_send![self, statusItemWithLength:len] - } -} - pub trait NSStatusItem { unsafe fn setHighlightMode_(self, mode: BOOL); unsafe fn setMenu_(self, menu: id); -- 2.51.2 From e5ca2369940192da58238a583060161cbda22b21 Mon Sep 17 00:00:00 2001 From: Corey Farwell Date: Fri, 16 Jun 2017 10:03:20 -0700 Subject: [PATCH 06/13] rustfmt. --- examples/simple.rs | 44 +++++++++++++++++++++++++------------------- src/lib.rs | 8 ++++---- src/osx/mod.rs | 37 ++++++++++++++++++++----------------- src/osx/objc_ext.rs | 4 ++-- 4 files changed, 51 insertions(+), 42 deletions(-) diff --git a/examples/simple.rs b/examples/simple.rs index e3a1c00..b3f21dc 100644 --- a/examples/simple.rs +++ b/examples/simple.rs @@ -15,28 +15,34 @@ fn main() { let hm = Arc::new(RwLock::new(hm)); let phm = hm.clone(); - fly.add_item("Prefs", - Box::new(move || { - let mut hm = phm.write().unwrap(); - let size = hm.len(); - let k = format!("Prefs{}", size); - hm.insert(k, "Bar".to_owned()); - - println!("prefs selected, new hm {:?}", *hm); - })); + fly.add_item( + "Prefs", + Box::new(move || { + let mut hm = phm.write().unwrap(); + let size = hm.len(); + let k = format!("Prefs{}", size); + hm.insert(k, "Bar".to_owned()); + + println!("prefs selected, new hm {:?}", *hm); + }), + ); let fhm = hm.clone(); - fly.add_item("Summon Herb", - Box::new(move || { - let mut hm = fhm.write().unwrap(); - let size = hm.len(); - let k = format!("Herb{}", size); - hm.insert(k, "Bar".to_owned()); - - println!("Herb thinks you are a jerk and refuses to appear. By the way, \ + fly.add_item( + "Summon Herb", + Box::new(move || { + let mut hm = fhm.write().unwrap(); + let size = hm.len(); + let k = format!("Herb{}", size); + hm.insert(k, "Bar".to_owned()); + + println!( + "Herb thinks you are a jerk and refuses to appear. By the way, \ the hash map is: {:?}", - *hm); - })); + *hm + ); + }), + ); fly.add_quit_item("Quit"); diff --git a/src/lib.rs b/src/lib.rs index f414fdd..e61572c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,7 +1,7 @@ -#[cfg(target_os="macos")] +#[cfg(target_os = "macos")] mod osx; -#[cfg(target_os="macos")] +#[cfg(target_os = "macos")] #[macro_use] extern crate objc; @@ -15,8 +15,8 @@ pub trait Barfly { #[cfg(target_os = "macos")] pub type PlatformFly = osx::OsxBarfly; -pub fn new(name:&str) -> PlatformFly { - PlatformFly::new(name) +pub fn new(name: &str) -> PlatformFly { + PlatformFly::new(name) } #[test] diff --git a/src/osx/mod.rs b/src/osx/mod.rs index e02951d..bab30f3 100644 --- a/src/osx/mod.rs +++ b/src/osx/mod.rs @@ -8,8 +8,9 @@ pub use objc::Message; extern crate cocoa; pub use self::cocoa::base::{selector, nil, YES /* id, class, BOOL */}; -pub use self::cocoa::appkit::{NSApp, NSApplication, NSWindow, NSMenu, NSMenuItem, NSRunningApplication, - NSApplicationActivateIgnoringOtherApps, NSStatusBar}; +pub use self::cocoa::appkit::{NSApp, NSApplication, NSWindow, NSMenu, NSMenuItem, + NSRunningApplication, NSApplicationActivateIgnoringOtherApps, + NSStatusBar}; extern crate libc; pub use self::libc::c_void; @@ -20,7 +21,7 @@ extern crate objc_id; pub use self::objc_id::Id; mod objc_ext; -use self::objc_ext::{NSStatusItem}; +use self::objc_ext::NSStatusItem; extern crate objc_foundation; pub use self::cocoa::foundation::{NSAutoreleasePool, NSString}; @@ -39,7 +40,8 @@ impl Barfly for OsxBarfly { unsafe { OsxBarfly { name: name.to_owned(), - pool: NSAutoreleasePool::new(nil), /* TODO: not sure about the consequences of creating this here */ + /* TODO: not sure about the consequences of creating this here */ + pool: NSAutoreleasePool::new(nil), menu: NSMenu::new(nil).autorelease(), } } @@ -56,11 +58,9 @@ impl Barfly for OsxBarfly { let itemtitle = NSString::init_str(astring, menuItem); let action = sel!(call); let aitem = NSMenuItem::alloc(nil); - let item = NSMenuItem::initWithTitle_action_keyEquivalent_(aitem, - itemtitle, - action, - no_key); - let _: () = msg_send![item, setTarget:cb_obj]; + let item = + NSMenuItem::initWithTitle_action_keyEquivalent_(aitem, itemtitle, action, no_key); + let _: () = msg_send![item, setTarget: cb_obj]; NSMenu::addItem_(self.menu, item); } @@ -70,11 +70,13 @@ impl Barfly for OsxBarfly { fn add_quit_item(&mut self, label: &str) { unsafe { let no_key = NSString::alloc(nil).init_str(""); - let pref_item = NSString::alloc(nil) - .init_str(label); + let pref_item = NSString::alloc(nil).init_str(label); let pref_action = selector("terminate:"); - let menuitem = NSMenuItem::alloc(nil) - .initWithTitle_action_keyEquivalent_(pref_item, pref_action, no_key); + let menuitem = NSMenuItem::alloc(nil).initWithTitle_action_keyEquivalent_( + pref_item, + pref_action, + no_key, + ); self.menu.addItem_(menuitem); } @@ -87,8 +89,7 @@ impl Barfly for OsxBarfly { let item = NSStatusBar::systemStatusBar(nil).statusItemWithLength_(-1.0); item.setHighlightMode_(YES); - let title = NSString::alloc(nil) - .init_str(&self.name); + let title = NSString::alloc(nil).init_str(&self.name); item.setTitle_(title); item.setMenu_(self.menu); @@ -169,8 +170,10 @@ impl INSObject for Callback { } unsafe { - decl.add_method(sel!(call), - barfly_callback_call as extern "C" fn(&Object, Sel)); + decl.add_method( + sel!(call), + barfly_callback_call as extern "C" fn(&Object, Sel), + ); } decl.register(); diff --git a/src/osx/objc_ext.rs b/src/osx/objc_ext.rs index e2f8a4b..846bd38 100644 --- a/src/osx/objc_ext.rs +++ b/src/osx/objc_ext.rs @@ -12,12 +12,12 @@ pub trait NSStatusItem { impl NSStatusItem for id { unsafe fn setHighlightMode_(self, mode: BOOL) { - msg_send![self, setHighlightMode:mode] + msg_send![self, setHighlightMode: mode] } unsafe fn statusBar(self) -> id { msg_send![self, statusBar] } unsafe fn setMenu_(self, menu: id) { - msg_send![self, setMenu:menu] + msg_send![self, setMenu: menu] } } -- 2.51.2 From c6e41bb6f40f0afd8c3a010e48601259a71fafa1 Mon Sep 17 00:00:00 2001 From: Corey Farwell Date: Fri, 16 Jun 2017 10:16:59 -0700 Subject: [PATCH 07/13] Highlight mode is deprecated. --- src/osx/mod.rs | 1 - src/osx/objc_ext.rs | 4 ---- 2 files changed, 5 deletions(-) diff --git a/src/osx/mod.rs b/src/osx/mod.rs index bab30f3..e16bc83 100644 --- a/src/osx/mod.rs +++ b/src/osx/mod.rs @@ -88,7 +88,6 @@ impl Barfly for OsxBarfly { app.activateIgnoringOtherApps_(YES); let item = NSStatusBar::systemStatusBar(nil).statusItemWithLength_(-1.0); - item.setHighlightMode_(YES); let title = NSString::alloc(nil).init_str(&self.name); item.setTitle_(title); item.setMenu_(self.menu); diff --git a/src/osx/objc_ext.rs b/src/osx/objc_ext.rs index 846bd38..d9944b3 100644 --- a/src/osx/objc_ext.rs +++ b/src/osx/objc_ext.rs @@ -5,15 +5,11 @@ extern crate cocoa; use self::cocoa::base::{id, class, BOOL}; pub trait NSStatusItem { - unsafe fn setHighlightMode_(self, mode: BOOL); unsafe fn setMenu_(self, menu: id); unsafe fn statusBar(self) -> id; } impl NSStatusItem for id { - unsafe fn setHighlightMode_(self, mode: BOOL) { - msg_send![self, setHighlightMode: mode] - } unsafe fn statusBar(self) -> id { msg_send![self, statusBar] } -- 2.51.2 From 2e827a37b12423df4a7c149971436049a93b9031 Mon Sep 17 00:00:00 2001 From: Corey Farwell Date: Fri, 16 Jun 2017 10:19:27 -0700 Subject: [PATCH 08/13] Rely on cocoa-rs for all cocoa bindings. --- src/osx/mod.rs | 5 +---- src/osx/objc_ext.rs | 19 ------------------- 2 files changed, 1 insertion(+), 23 deletions(-) delete mode 100644 src/osx/objc_ext.rs diff --git a/src/osx/mod.rs b/src/osx/mod.rs index e16bc83..2ba0ba6 100644 --- a/src/osx/mod.rs +++ b/src/osx/mod.rs @@ -10,7 +10,7 @@ extern crate cocoa; pub use self::cocoa::base::{selector, nil, YES /* id, class, BOOL */}; pub use self::cocoa::appkit::{NSApp, NSApplication, NSWindow, NSMenu, NSMenuItem, NSRunningApplication, NSApplicationActivateIgnoringOtherApps, - NSStatusBar}; + NSStatusBar, NSStatusItem}; extern crate libc; pub use self::libc::c_void; @@ -20,9 +20,6 @@ pub use self::objc::runtime::{Class, Object, Sel}; extern crate objc_id; pub use self::objc_id::Id; -mod objc_ext; -use self::objc_ext::NSStatusItem; - extern crate objc_foundation; pub use self::cocoa::foundation::{NSAutoreleasePool, NSString}; pub use self::objc_foundation::{INSObject, NSObject}; diff --git a/src/osx/objc_ext.rs b/src/osx/objc_ext.rs deleted file mode 100644 index d9944b3..0000000 --- a/src/osx/objc_ext.rs +++ /dev/null @@ -1,19 +0,0 @@ -extern crate core_graphics; -use self::core_graphics::base::CGFloat; - -extern crate cocoa; -use self::cocoa::base::{id, class, BOOL}; - -pub trait NSStatusItem { - unsafe fn setMenu_(self, menu: id); - unsafe fn statusBar(self) -> id; -} - -impl NSStatusItem for id { - unsafe fn statusBar(self) -> id { - msg_send![self, statusBar] - } - unsafe fn setMenu_(self, menu: id) { - msg_send![self, setMenu: menu] - } -} -- 2.51.2 From e3d77529eb7059956f0eb6afa6d9a6777f29b34d Mon Sep 17 00:00:00 2001 From: Corey Farwell Date: Fri, 16 Jun 2017 18:49:25 -0700 Subject: [PATCH 09/13] Bump cocoa crate. --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 9bd85d9..ef657c5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,7 +9,7 @@ repository = "https://github.com/jmquigs/rs-barfly" [dependencies] [target.x86_64-apple-darwin.dependencies] -cocoa = "0.8" +cocoa = "0.9" objc = "0.2" core-graphics = "0.8" objc-foundation = "0.1" -- 2.51.2 From b25f459b502c3ba764196175d60d2ce79ee0e85a Mon Sep 17 00:00:00 2001 From: Corey Farwell Date: Fri, 16 Jun 2017 18:56:42 -0700 Subject: [PATCH 10/13] snake_case. --- src/lib.rs | 2 +- src/osx/mod.rs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index e61572c..260f1a0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -7,7 +7,7 @@ extern crate objc; pub trait Barfly { fn new(name: &str) -> Self; - fn add_item(&mut self, menuItem: &str, cbs: Box ()>); + fn add_item(&mut self, menu_item: &str, cbs: Box ()>); fn add_quit_item(&mut self, label: &str); fn display(&mut self); } diff --git a/src/osx/mod.rs b/src/osx/mod.rs index 2ba0ba6..d6b971f 100644 --- a/src/osx/mod.rs +++ b/src/osx/mod.rs @@ -44,7 +44,7 @@ impl Barfly for OsxBarfly { } } - fn add_item(&mut self, menuItem: &str, cbs: Box ()>) { + fn add_item(&mut self, menu_item: &str, cbs: Box ()>) { unsafe { let cb_obj = Callback::from(cbs); @@ -52,7 +52,7 @@ impl Barfly for OsxBarfly { let no_key = NSString::init_str(astring, ""); // TODO want this eventually let astring = NSString::alloc(nil); - let itemtitle = NSString::init_str(astring, menuItem); + let itemtitle = NSString::init_str(astring, menu_item); let action = sel!(call); let aitem = NSMenuItem::alloc(nil); let item = -- 2.51.2 From 7308eb42fe3edfc732653ab4760ffcff3af7cfc4 Mon Sep 17 00:00:00 2001 From: Corey Farwell Date: Fri, 16 Jun 2017 19:12:02 -0700 Subject: [PATCH 11/13] Condense `NSString` logic. --- src/osx/mod.rs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/osx/mod.rs b/src/osx/mod.rs index d6b971f..f6a5379 100644 --- a/src/osx/mod.rs +++ b/src/osx/mod.rs @@ -48,11 +48,9 @@ impl Barfly for OsxBarfly { unsafe { let cb_obj = Callback::from(cbs); - let astring = NSString::alloc(nil); - let no_key = NSString::init_str(astring, ""); // TODO want this eventually + let no_key = NSString::alloc(nil).init_str(""); // TODO want this eventually - let astring = NSString::alloc(nil); - let itemtitle = NSString::init_str(astring, menu_item); + let itemtitle = NSString::alloc(nil).init_str(menu_item); let action = sel!(call); let aitem = NSMenuItem::alloc(nil); let item = -- 2.51.2 From 79a426dddbef4f3e71d1c2b4279215fd43978511 Mon Sep 17 00:00:00 2001 From: Corey Farwell Date: Fri, 16 Jun 2017 20:02:31 -0700 Subject: [PATCH 12/13] Condense `NSMenuItem` calls. --- src/osx/mod.rs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/osx/mod.rs b/src/osx/mod.rs index f6a5379..c45f193 100644 --- a/src/osx/mod.rs +++ b/src/osx/mod.rs @@ -52,9 +52,11 @@ impl Barfly for OsxBarfly { let itemtitle = NSString::alloc(nil).init_str(menu_item); let action = sel!(call); - let aitem = NSMenuItem::alloc(nil); - let item = - NSMenuItem::initWithTitle_action_keyEquivalent_(aitem, itemtitle, action, no_key); + let item = NSMenuItem::alloc(nil).initWithTitle_action_keyEquivalent_( + itemtitle, + action, + no_key, + ); let _: () = msg_send![item, setTarget: cb_obj]; NSMenu::addItem_(self.menu, item); -- 2.51.2 From 36ff7a27f1703c0d87d54e14297555c0714f2e32 Mon Sep 17 00:00:00 2001 From: Corey Farwell Date: Sat, 17 Jun 2017 10:23:11 -0700 Subject: [PATCH 13/13] Drain the pool upon dropping. --- src/osx/mod.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/osx/mod.rs b/src/osx/mod.rs index c45f193..37722cb 100644 --- a/src/osx/mod.rs +++ b/src/osx/mod.rs @@ -32,6 +32,14 @@ pub struct OsxBarfly { pool: *mut objc::runtime::Object, } +impl Drop for OsxBarfly { + fn drop(&mut self) { + unsafe { + self.pool.drain() + } + } +} + impl Barfly for OsxBarfly { fn new(name: &str) -> Self { unsafe {