Pārlūkot izejas kodu

Make widget creation a bit more consistent

Thomas Dy 7 gadi atpakaļ
vecāks
revīzija
e1623168a9
8 mainītis faili ar 81 papildinājumiem un 67 dzēšanām
  1. 19 17
      src/main.rs
  2. 13 6
      src/widgets/mod.rs
  3. 7 6
      src/widgets/music.rs
  4. 6 7
      src/widgets/sensors.rs
  5. 4 3
      src/widgets/spacer.rs
  6. 12 11
      src/widgets/title.rs
  7. 11 10
      src/widgets/tray.rs
  8. 9 7
      src/widgets/wm.rs

+ 19 - 17
src/main.rs

@@ -51,23 +51,25 @@ fn main() {
 
         let mut panel = ui::panel::Panel::new(conn, &cfg);
 
-        let bspwm = widgets::bspwm::Bspwm::new(tx.clone(), panel.make_draw_context());
-        panel.add_left_widget(Box::new(bspwm));
-
-        let spacer = widgets::spacer::create(panel.make_draw_context(), 4, 0x6666, 0x6666, 0x6666, 0xFFFF);
-        panel.add_left_widget(Box::new(spacer));
-
-        let title = widgets::title::Title::new(panel.conn.clone(), panel.make_draw_context());
-        panel.add_left_widget(Box::new(title));
-
-        let tray = widgets::tray::Tray::new(tx.clone(), panel.conn.clone(), panel.window);
-        panel.add_right_widget(Box::new(tray));
-
-        let sensors = widgets::sensors::Sensors::new(panel.make_draw_context(), &cfg);
-        panel.add_right_widget(Box::new(sensors));
-
-        let music = widgets::music::mpd(tx.clone(), panel.make_draw_context());
-        panel.add_right_widget(Box::new(music));
+        let left_widgets = vec![
+            widgets::bspwm(tx.clone(), panel.make_draw_context()),
+            widgets::spacer(panel.make_draw_context(), 4, 0x6666, 0x6666, 0x6666, 0xFFFF),
+            widgets::title(panel.conn.clone(), panel.make_draw_context())
+        ];
+
+        let mut right_widgets = vec![
+            widgets::mpd(tx.clone(), panel.make_draw_context()),
+            widgets::sensors(panel.make_draw_context(), &cfg),
+            widgets::tray(tx.clone(), panel.conn.clone(), panel.window)
+        ];
+        right_widgets.reverse();
+
+        for widget in left_widgets {
+            panel.add_left_widget(widget);
+        }
+        for widget in right_widgets {
+            panel.add_right_widget(widget);
+        }
 
         panel.create();
 

+ 13 - 6
src/widgets/mod.rs

@@ -1,14 +1,21 @@
-pub mod title;
-pub mod tray;
-pub mod sensors;
-pub mod bspwm;
-pub mod spacer;
-pub mod music;
+mod title;
+mod tray;
+mod sensors;
+mod wm;
+mod spacer;
+mod music;
 
 use xcb;
 use mpd;
 use std::sync::mpsc;
 
+pub use self::title::title;
+pub use self::tray::tray;
+pub use self::sensors::sensors;
+pub use self::spacer::spacer;
+pub use self::music::mpd;
+pub use self::wm::bspwm;
+
 pub type MessageSender = mpsc::Sender<Message>;
 
 pub enum Message {

+ 7 - 6
src/widgets/music.rs

@@ -11,7 +11,7 @@ use widgets::{Message, MessageSender, Widget};
 const MARGIN: u16 = 7;
 const WIDTH: u16 = 250;
 
-pub struct Music {
+pub struct Mpd {
     context: DrawContext,
     tx: MessageSender,
     state: State,
@@ -19,17 +19,18 @@ pub struct Music {
     last_pos: u16
 }
 
-pub fn mpd(tx: MessageSender, context: DrawContext) -> Music {
-    Music {
+pub fn mpd(tx: MessageSender, context: DrawContext) -> Box<Widget> {
+    let widget = Mpd {
         context: context,
         tx: tx,
         state: State::Stop,
         song: None,
         last_pos: 0
-    }
+    };
+    Box::new(widget)
 }
 
-impl Music {
+impl Mpd {
     fn icon(&self) -> &str {
         match self.state {
             State::Play => "",
@@ -63,7 +64,7 @@ impl Music {
 
 }
 
-impl Widget for Music {
+impl Widget for Mpd {
     fn init(&mut self) {
         let tx = self.tx.clone();
         thread::spawn(move || monitor_thread(tx));

+ 6 - 7
src/widgets/sensors.rs

@@ -11,13 +11,12 @@ pub struct Sensors {
     sensors: Vec<Box<Sensor>>
 }
 
-impl Sensors {
-    pub fn new(context: DrawContext, config: &Config) -> Sensors {
-        Sensors {
-            context: context,
-            sensors: sensors::sensor_list(config)
-        }
-    }
+pub fn sensors(context: DrawContext, config: &Config) -> Box<Widget> {
+    let widget = Sensors {
+        context: context,
+        sensors: sensors::sensor_list(config)
+    };
+    Box::new(widget)
 }
 
 impl Widget for Sensors {

+ 4 - 3
src/widgets/spacer.rs

@@ -6,12 +6,13 @@ pub struct Spacer {
     width: u16
 }
 
-pub fn create(mut context: DrawContext, width: u16, red: u16, green: u16, blue: u16, alpha: u16) -> Spacer {
+pub fn spacer(mut context: DrawContext, width: u16, red: u16, green: u16, blue: u16, alpha: u16) -> Box<Widget> {
     context.set_bg_color(red, green, blue, alpha);
-    Spacer {
+    let widget = Spacer {
         context: context,
         width: width
-    }
+    };
+    Box::new(widget)
 }
 
 impl Widget for Spacer {

+ 12 - 11
src/widgets/title.rs

@@ -16,18 +16,19 @@ pub struct Title {
     last_win: xcb::Window
 }
 
-impl Title {
-    pub fn new(conn: Arc<x11::Connection>, context: DrawContext) -> Title {
-        Title {
-            conn: conn,
-            context: context,
-            title: "".to_string(),
-            last_pos: 0,
-            last_width: 0,
-            last_win: 0
-        }
-    }
+pub fn title(conn: Arc<x11::Connection>, context: DrawContext) -> Box<Widget> {
+    let widget = Title {
+        conn: conn,
+        context: context,
+        title: "".to_string(),
+        last_pos: 0,
+        last_width: 0,
+        last_win: 0
+    };
+    Box::new(widget)
+}
 
+impl Title {
     pub fn redraw(&mut self) {
         self.context.draw_bg(self.last_pos, self.last_width);
         let x = self.last_pos;

+ 11 - 10
src/widgets/tray.rs

@@ -15,17 +15,18 @@ pub struct Tray {
     timestamp: xcb::Timestamp,
 }
 
-impl Tray {
-    pub fn new(tx: MessageSender, conn: Arc<x11::Connection>, window: xcb::Window) -> Tray {
-        Tray {
-            conn: conn,
-            tx: tx,
-            window: window,
-            children: vec![],
-            timestamp: 0
-        }
-    }
+pub fn tray(tx: MessageSender, conn: Arc<x11::Connection>, window: xcb::Window) -> Box<Widget> {
+    let widget = Tray {
+        conn: conn,
+        tx: tx,
+        window: window,
+        children: vec![],
+        timestamp: 0
+    };
+    Box::new(widget)
+}
 
+impl Tray {
     pub fn take_selection(&mut self, timestamp: xcb::Timestamp) -> bool {
         let selection = self.conn.atom(x11::_NET_SYSTEM_TRAY_S0);
         xcb::set_selection_owner(&self.conn, self.window, selection, timestamp);

+ 9 - 7
src/widgets/bspwm.rs → src/widgets/wm.rs

@@ -28,14 +28,16 @@ pub struct Bspwm {
     desktops: Vec<Desktop>
 }
 
+pub fn bspwm(tx: MessageSender, context: DrawContext) -> Box<Widget> {
+    let widget = Bspwm {
+        context: context,
+        tx: tx,
+        desktops: vec![]
+    };
+    Box::new(widget)
+}
+
 impl Bspwm {
-    pub fn new(tx: MessageSender, context: DrawContext) -> Bspwm {
-        Bspwm {
-            context: context,
-            tx: tx,
-            desktops: vec![]
-        }
-    }
 
     fn parse_bspwm(&mut self, line: &str) {
         let (kind, line) = line.split_at(1);