summary history branches tags files
commit:2a24bf1711b031ac3c8ed2a576501bd4caff4c8f
author:Trevor Bentley
committer:Trevor Bentley
date:Sat Apr 22 19:36:53 2017 +0200
parents:bc91fd5deff7791c8a6e3a02970d02a32dfe1cc8
Add dummy status bar implementation for non-mac platforms
diff --git a/src/lib.rs b/src/lib.rs
line changes: +40/-0
index 84262ee..9e2da61
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -35,3 +35,43 @@ pub mod spotify_api {
     pub const REPEAT: &'static str = "https://api.spotify.com/v1/me/player/repeat";
     pub const PLAYER: &'static str = "https://api.spotify.com/v1/me/player";
 }
+
+#[cfg(target_os = "macos")]
+pub type Object = osx::Object;
+#[cfg(not(target_os = "macos"))]
+pub type Object = u64;
+
+pub type MenuItem = *mut Object;
+pub trait TStatusBar {
+    type S: TStatusBar;
+    fn new(tx: Sender<String>) -> Self::S;
+    fn clear_items(&mut self);
+    fn add_separator(&mut self);
+    fn add_label(&mut self, label: &str);
+    fn add_item(&mut self, item: &str, callback: NSCallback, selected: bool) -> *mut Object;
+    fn add_quit(&mut self, label: &str);
+    fn update_item(&mut self, item: *mut Object, label: &str);
+    fn sel_item(&mut self, sender: u64);
+    fn unsel_item(&mut self, sender: u64);
+    fn set_tooltip(&self, text: &str);
+    fn run(&mut self, block: bool);
+}
+
+use std::sync::mpsc::Sender;
+pub type NSCallback = Box<Fn(u64, &Sender<String>)>;
+
+pub struct DummyStatusBar {}
+impl TStatusBar for DummyStatusBar {
+    type S = DummyStatusBar;
+    fn new(_: Sender<String>) -> Self::S { DummyStatusBar {} }
+    fn clear_items(&mut self) {}
+    fn add_separator(&mut self) {}
+    fn add_label(&mut self, _: &str) {}
+    fn add_item(&mut self, _: &str, _: NSCallback, _: bool) -> *mut Object { 0 as *mut Object }
+    fn add_quit(&mut self, _: &str) {}
+    fn update_item(&mut self, _: *mut Object, _: &str) {}
+    fn sel_item(&mut self, _: u64) {}
+    fn unsel_item(&mut self, _: u64) {}
+    fn set_tooltip(&self, _: &str) {}
+    fn run(&mut self, _: bool) {}
+}

diff --git a/src/main.rs b/src/main.rs
line changes: +12/-10
index 05274d3..9be6908
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,5 +1,8 @@
 extern crate connectr;
 use connectr::SpotifyResponse;
+use connectr::TStatusBar;
+use connectr::MenuItem;
+use connectr::NSCallback;
 
 #[macro_use]
 extern crate log;
@@ -35,10 +38,6 @@ struct MenuCallbackCommand {
 
 #[cfg(target_os = "macos")]
 use connectr::osx;
-#[cfg(target_os = "macos")]
-use connectr::osx::TStatusBar;
-#[cfg(target_os = "macos")]
-use connectr::osx::MenuItem;
 
 struct MenuItems {
     device: Vec<(MenuItem, String)>,
@@ -86,7 +85,7 @@ fn fill_menu<T: TStatusBar>(app: &mut ConnectrApp, spotify: &mut connectr::Spoti
     status.add_separator();
     {
         let play_str = play_action_label(player_state.is_playing);
-        let cb: osx::NSCallback = Box::new(move |sender, tx| {
+        let cb: NSCallback = Box::new(move |sender, tx| {
             let is_playing = &player_state.is_playing;
             let cmd = MenuCallbackCommand {
                 action: CallbackAction::PlayPause,
@@ -97,7 +96,7 @@ fn fill_menu<T: TStatusBar>(app: &mut ConnectrApp, spotify: &mut connectr::Spoti
         });
         app.menu.play = status.add_item(&play_str, cb, false);
 
-        let cb: osx::NSCallback = Box::new(move |sender, tx| {
+        let cb: NSCallback = Box::new(move |sender, tx| {
             let cmd = MenuCallbackCommand {
                 action: CallbackAction::SkipNext,
                 sender: sender,
@@ -107,7 +106,7 @@ fn fill_menu<T: TStatusBar>(app: &mut ConnectrApp, spotify: &mut connectr::Spoti
         });
         app.menu.next = status.add_item("Next", cb, false);
 
-        let cb: osx::NSCallback = Box::new(move |sender, tx| {
+        let cb: NSCallback = Box::new(move |sender, tx| {
             let cmd = MenuCallbackCommand {
                 action: CallbackAction::SkipPrev,
                 sender: sender,
@@ -126,7 +125,7 @@ fn fill_menu<T: TStatusBar>(app: &mut ConnectrApp, spotify: &mut connectr::Spoti
         for preset in presets {
             let ref name = preset.0;
             let uri = preset.1.clone();
-            let cb: osx::NSCallback = Box::new(move |sender, tx| {
+            let cb: NSCallback = Box::new(move |sender, tx| {
                 let cmd = MenuCallbackCommand {
                     action: CallbackAction::Preset,
                     sender: sender,
@@ -147,7 +146,7 @@ fn fill_menu<T: TStatusBar>(app: &mut ConnectrApp, spotify: &mut connectr::Spoti
     for dev in device_list {
         println!("{}", dev);
         let id = dev.id.clone();
-        let cb: osx::NSCallback = Box::new(move |sender, tx| {
+        let cb: NSCallback = Box::new(move |sender, tx| {
             let cmd = MenuCallbackCommand {
                 action: CallbackAction::SelectDevice,
                 sender: sender,
@@ -175,7 +174,7 @@ fn fill_menu<T: TStatusBar>(app: &mut ConnectrApp, spotify: &mut connectr::Spoti
         let mut i = 0;
         while i <= 100 {
             let vol_str = format!("{}%", i);
-            let cb: osx::NSCallback = Box::new(move |sender, tx| {
+            let cb: NSCallback = Box::new(move |sender, tx| {
                 let cmd = MenuCallbackCommand {
                     action: CallbackAction::Volume,
                     sender: sender,
@@ -253,8 +252,11 @@ fn main() {
     spotify.connect();
     info!("Created Spotify connection.");
     spotify.set_target_device(None);
+    #[cfg(target_os = "macos")]
     let mut status = osx::OSXStatusBar::new(tx);
     info!("Created status bar.");
+    #[cfg(not(target_os = "macos"))]
+    let mut status = connectr::DummyStatusBar::new(tx);
 
     loop {
         let now = time::now_utc().to_timespec().sec as i64;

diff --git a/src/osx/mod.rs b/src/osx/mod.rs
line changes: +4/-17
index 88ac846..cfa30f5
--- a/src/osx/mod.rs
+++ b/src/osx/mod.rs
@@ -5,9 +5,10 @@ extern crate objc_foundation;
 extern crate cocoa;
 extern crate libc;
 
-pub use self::rustnsobject::NSCallback;
+pub use ::TStatusBar;
+pub use ::NSCallback;
 
-use objc::runtime::{Class, Object};
+use objc::runtime::Class;
 
 use self::cocoa::base::{nil, YES};
 use self::cocoa::appkit::NSStatusBar;
@@ -34,7 +35,7 @@ use std::time::Duration;
 extern crate objc_id;
 use self::objc_id::Id;
 
-pub type MenuItem = *mut Object;
+pub type Object = objc::runtime::Object;
 
 pub struct OSXStatusBar {
     object: NSObj,
@@ -42,20 +43,6 @@ pub struct OSXStatusBar {
     status_bar_item: *mut objc::runtime::Object,
     menu_bar: *mut objc::runtime::Object,
 }
-pub trait TStatusBar {
-    type S: TStatusBar;
-    fn new(tx: Sender<String>) -> Self::S;
-    fn clear_items(&mut self);
-    fn add_separator(&mut self);
-    fn add_label(&mut self, label: &str);
-    fn add_item(&mut self, item: &str, callback: NSCallback, selected: bool) -> *mut Object;
-    fn add_quit(&mut self, label: &str);
-    fn update_item(&mut self, item: *mut Object, label: &str);
-    fn sel_item(&mut self, sender: u64);
-    fn unsel_item(&mut self, sender: u64);
-    fn set_tooltip(&self, text: &str);
-    fn run(&mut self, block: bool);
-}
 
 impl TStatusBar for OSXStatusBar {
     type S = OSXStatusBar;

diff --git a/src/osx/rustnsobject.rs b/src/osx/rustnsobject.rs
line changes: +2/-1
index 3418fe6..a574936
--- a/src/osx/rustnsobject.rs
+++ b/src/osx/rustnsobject.rs
@@ -6,6 +6,8 @@ extern crate objc;
 extern crate objc_foundation;
 extern crate objc_id;
 
+pub use ::NSCallback;
+
 use std::sync::{Once, ONCE_INIT};
 
 use objc::Message;
@@ -89,7 +91,6 @@ impl NSObjTrait for NSObj {
 }
 
 pub type NSObjCallback<T> = Box<Fn(&mut T, u64)>;
-pub type NSCallback = Box<Fn(u64, &Sender<String>)>;
 
 impl NSObjCallbackTrait for NSObj {
     fn set_value(&mut self, key: u64, val: NSCallback) {