ソースを参照

Replace chan and chan_signal with standard mpsc

Thomas Dy 7 年 前
コミット
e2f1c35a57
6 ファイル変更34 行追加30 行削除
  1. 1 2
      Cargo.toml
  2. 2 5
      src/main.rs
  3. 18 17
      src/ui/mod.rs
  4. 3 1
      src/ui/panel.rs
  5. 9 5
      src/ui/tray.rs
  6. 1 0
      src/ui/widget.rs

+ 1 - 2
Cargo.toml

@@ -7,8 +7,7 @@ authors = ["Thomas Dy <thatsmydoing@gmail.com>"]
 time = "0.1"
 toml = "0.1"
 mpd = "0.0.12"
-chan = "0.1"
-chan-signal = "0.1"
+simple-signal = "1.1.0"
 freetype-rs = "0.11"
 silverknife-fontconfig-sys = "0.1"
 

+ 2 - 5
src/main.rs

@@ -1,7 +1,5 @@
 extern crate xcb;
-#[macro_use]
-extern crate chan;
-extern crate chan_signal;
+extern crate simple_signal;
 
 extern crate freetype;
 extern crate fontconfig_sys;
@@ -27,7 +25,6 @@ use std::process;
 //use std::thread;
 
 fn main() {
-    let signal = chan_signal::notify(&[chan_signal::Signal::INT, chan_signal::Signal::TERM]);
     let config_path = env::args().nth(1).unwrap_or("./panel.toml".to_string());
     let cfg = config::load(&config_path);
 //
@@ -55,7 +52,7 @@ fn main() {
 //        ));
 //    }
 
-    process::exit(ui::ui_main(signal, &cfg));
+    process::exit(ui::ui_main(&cfg));
 }
 
 //fn make_thread(tx: &Channel, cfg: &Arc<Config>, func: fn(&Channel, &Config) -> ()) {

+ 18 - 17
src/ui/mod.rs

@@ -8,18 +8,25 @@ mod util;
 mod widget;
 mod x11;
 
-use chan;
-use chan_signal;
+use simple_signal;
+use simple_signal::Signal;
 use config::Config;
 
+use std::sync::mpsc;
 use std::thread;
 use std::time;
 
 pub const SIZE: u16 = 20;
 
-pub fn ui_main(signal: chan::Receiver<chan_signal::Signal>, cfg: &Config) -> i32 {
+pub fn ui_main(cfg: &Config) -> i32 {
     if let Some(conn) = x11::Connection::new() {
-        let (tx, rx) = chan::sync(10);
+        let (tx, rx) = mpsc::channel();
+        {
+            let tx = tx.clone();
+            simple_signal::set_handler(&[Signal::Int, Signal::Term], move |_signals| {
+                tx.send(widget::WidgetMessage::Quit).expect("Failed to send quit");
+            });
+        }
         {
             let tx = tx.clone();
             let conn = conn.clone_connection();
@@ -28,7 +35,7 @@ pub fn ui_main(signal: chan::Receiver<chan_signal::Signal>, cfg: &Config) -> i32
                     match conn.wait_for_event() {
                         Some(event) => {
                             let message = widget::WidgetMessage::XcbEvent(event);
-                            tx.send(message);
+                            tx.send(message).expect("Failed to send xcb event");
                         },
                         None => { break; }
                     }
@@ -53,24 +60,18 @@ pub fn ui_main(signal: chan::Receiver<chan_signal::Signal>, cfg: &Config) -> i32
             let tx = tx.clone();
             thread::spawn(move || {
                 loop {
-                    tx.send(widget::WidgetMessage::Update);
+                    tx.send(widget::WidgetMessage::Update).expect("Failed to send update");
                     thread::sleep(time::Duration::from_secs(1));
                 }
             });
         }
 
         loop {
-            chan_select!(
-                rx.recv() -> event => {
-                    if panel.handle_event(event.unwrap()) {
-                        println!("Exiting");
-                        break;
-                    }
-                },
-                signal.recv() => {
-                    panel.finish();
-                }
-            );
+            let event = rx.recv();
+            if panel.handle_event(event.unwrap()) {
+                println!("Exiting");
+                break;
+            }
         }
     }
     0

+ 3 - 1
src/ui/panel.rs

@@ -126,7 +126,9 @@ impl Panel {
             widget::WidgetMessage::Update =>
                 self.relayout(),
             widget::WidgetMessage::Relayout =>
-                self.relayout()
+                self.relayout(),
+            widget::WidgetMessage::Quit =>
+                self.finish()
         }
         should_exit
     }

+ 9 - 5
src/ui/tray.rs

@@ -1,15 +1,15 @@
-use chan;
 use ui;
 use ui::widget;
 use ui::x11;
 use xcb;
 
 use std::sync::Arc;
+use std::sync::mpsc;
 
 const CLIENT_MESSAGE: u8 = xcb::CLIENT_MESSAGE | 0x80; // 0x80 flag for client messages
 
 pub struct Tray {
-    tx: chan::Sender<widget::WidgetMessage>,
+    tx: mpsc::Sender<widget::WidgetMessage>,
     conn: Arc<x11::Connection>,
     window: xcb::Window,
     children: Vec<xcb::Window>,
@@ -17,7 +17,7 @@ pub struct Tray {
 }
 
 impl Tray {
-    pub fn new(tx: chan::Sender<widget::WidgetMessage>, conn: Arc<x11::Connection>, window: xcb::Window) -> Tray {
+    pub fn new(tx: mpsc::Sender<widget::WidgetMessage>, conn: Arc<x11::Connection>, window: xcb::Window) -> Tray {
         Tray {
             conn: conn,
             tx: tx,
@@ -58,12 +58,16 @@ impl Tray {
         self.force_size(window, None);
         conn.flush();
         self.children.push(window);
-        self.tx.send(widget::WidgetMessage::Relayout);
+        self.relayout();
     }
 
     pub fn forget(&mut self, window: xcb::Window) {
         self.children.retain(|child| *child != window);
-        self.tx.send(widget::WidgetMessage::Relayout);
+        self.relayout();
+    }
+
+    fn relayout(&self) {
+        self.tx.send(widget::WidgetMessage::Relayout).expect("Failed to send relayout");
     }
 
     pub fn force_size(&self, window: xcb::Window, dimensions: Option<(u16, u16)>) {

+ 1 - 0
src/ui/widget.rs

@@ -7,6 +7,7 @@ use std::sync::Arc;
 pub enum WidgetMessage {
     Relayout,
     Update,
+    Quit,
     XcbEvent(xcb::GenericEvent)
 }