summary history branches tags files
commit:fe28ffa4ab1c8427baef6fc821272620a8185677
author:Trevor Bentley
committer:Trevor Bentley
date:Tue Apr 25 15:52:58 2017 +0200
parents:7eecc09b36bcb54892430e005a83366f94646a53
Forgotten commit: actually allow selecting items in Windows systray :)
diff --git a/src/win/mod.rs b/src/win/mod.rs
line changes: +20/-4
index 223a28a..db28041
--- a/src/win/mod.rs
+++ b/src/win/mod.rs
@@ -3,6 +3,9 @@ extern crate systray;
 pub use ::TStatusBar;
 pub use ::NSCallback;
 
+use self::systray::api::api::MenuEnableFlag;
+
+use std::collections::BTreeMap;
 use std::sync::mpsc::Sender;
 
 //pub type Object = objc::runtime::Object;
@@ -12,6 +15,8 @@ use std::cell::Cell;
 pub struct WindowsStatusBar {
     app: systray::Application,
     idx: Cell<u32>,
+    tx: Sender<String>,
+    items: BTreeMap<u64, u32>,
 }
 
 impl TStatusBar for WindowsStatusBar {
@@ -20,6 +25,8 @@ impl TStatusBar for WindowsStatusBar {
         let mut bar = WindowsStatusBar {
             app: systray::Application::new().unwrap(),
             idx: Cell::new(0),
+            tx: tx,
+            items: BTreeMap::<u64, u32>::new(),
         };
         {
             let ref mut win = &mut bar.app.window;
@@ -34,6 +41,7 @@ impl TStatusBar for WindowsStatusBar {
     fn clear_items(&mut self) {
         let ref mut win = &mut self.app.window;
         win.clear_menu();
+        self.items.clear();
     }
     fn set_tooltip(&mut self, text: &str) {
         let ref mut win = &mut self.app.window;
@@ -41,7 +49,8 @@ impl TStatusBar for WindowsStatusBar {
     }
     fn add_label(&mut self, label: &str) {
         let ref mut win = &mut self.app.window;
-        win.add_menu_item(&label.to_string(), false, |window| {});
+        let idx = win.add_menu_item(&label.to_string(), false, |window| {});
+        win.enable_menu_item(idx.unwrap(), MenuEnableFlag::Disabled);
     }
     fn add_quit(&mut self, label: &str) {
         let ref mut win = &mut self.app.window;
@@ -55,17 +64,24 @@ impl TStatusBar for WindowsStatusBar {
         let ref mut win = &mut self.app.window;
         let idx = self.idx.get();
         self.idx.set(idx+1);
-        win.add_menu_item(&item.to_string(), selected, move |window| {println!("rand: {}", idx);}).unwrap() as *mut Object
+        let tx = self.tx.clone();
+        let item = win.add_menu_item(&item.to_string(), selected, move |window| {
+            callback(idx as u64, &tx);
+        }).unwrap();
+        self.items.insert(idx as u64, item);
+        idx as *mut Object
     }
     fn update_item(&mut self, item: *mut Object, label: &str) {
     }
     fn sel_item(&mut self, sender: u64) {
         let ref mut win = &mut self.app.window;
-        win.select_menu_item(sender as u32);
+        let obj = self.items.get(&sender).unwrap();
+        win.select_menu_item(*obj);
     }
     fn unsel_item(&mut self, sender: u64) {
         let ref mut win = &mut self.app.window;
-        win.unselect_menu_item(sender as u32);
+        let obj = self.items.get(&sender).unwrap();
+        win.unselect_menu_item(*obj);
     }
     fn run(&mut self, block: bool) {
         let ref mut win = &mut self.app.window;