diff --git a/examples/simple.rs b/examples/simple.rs index b99a9e0..3f705a2 100644 --- a/examples/simple.rs +++ b/examples/simple.rs @@ -5,7 +5,7 @@ use std::sync::{Arc, RwLock}; use std::mem; extern crate barfly; -use barfly::{Barfly, add_fly_item}; +use barfly::Barfly; fn main() { let mut fly = Barfly::new("Barfly"); @@ -15,8 +15,7 @@ fn main() { let hm = Arc::new(RwLock::new(hm)); let phm = hm.clone(); - add_fly_item(&fly, - "Prefs", + fly.add_item("Prefs", Box::new(move || { let mut hm = phm.write().unwrap(); let size = hm.len(); @@ -27,8 +26,7 @@ fn main() { })); let fhm = hm.clone(); - add_fly_item(&fly, - "Summon Herb", + fly.add_item("Summon Herb", Box::new(move || { let mut hm = fhm.write().unwrap(); let size = hm.len(); diff --git a/src/lib.rs b/src/lib.rs index 8da2d5b..c351cdf 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -43,6 +43,27 @@ impl Barfly { } } + pub fn add_item(&mut self, menuItem: &str, cbs: Box ()>) { + unsafe { + let cb_obj = Callback::from(cbs); + + let astring = NSString::alloc(nil); + let no_key = NSString::init_str(astring, ""); // TODO want this eventually + + let astring = NSString::alloc(nil); + 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]; + + NSMenu::addItem_(self.menu, item); + } + } + // TODO: allow user callback pub fn add_quit_item(&mut self, label: &str) { unsafe { @@ -157,31 +178,11 @@ impl INSObject for Callback { } } -pub fn add_fly_item(fly: &Barfly, menuItem: &str, cbs: Box ()>) { - unsafe { - let cb_obj = Callback::from(cbs); - - let astring = NSString::alloc(nil); - let no_key = NSString::init_str(astring, ""); // TODO want this eventually - - let astring = NSString::alloc(nil); - 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]; - - NSMenu::addItem_(fly.menu, item); - } -} #[test] fn it_works() { - let bf = Barfly::new("Test"); - add_fly_item(&bf, "Test", Box::new(|| {})); + let mut bf = Barfly::new("Test"); + bf.add_item("Test", Box::new(|| {})); }