소스 검색

Make widget constructor consistent

Thomas Dy 7 년 전
부모
커밋
986c08487a
11개의 변경된 파일124개의 추가작업 그리고 78개의 파일을 삭제
  1. 18 11
      src/main.rs
  2. 17 8
      src/ui/context.rs
  3. 1 1
      src/ui/mod.rs
  4. 6 7
      src/ui/panel.rs
  5. 30 4
      src/widgets/mod.rs
  6. 12 10
      src/widgets/music.rs
  7. 6 7
      src/widgets/sensors.rs
  8. 9 7
      src/widgets/spacer.rs
  9. 10 9
      src/widgets/title.rs
  10. 9 8
      src/widgets/tray.rs
  11. 6 6
      src/widgets/wm.rs

+ 18 - 11
src/main.rs

@@ -18,7 +18,6 @@ use std::sync::mpsc;
 use std::thread;
 use std::time;
 
-use ui::color;
 use ui::x11;
 use widgets::Message;
 
@@ -51,24 +50,32 @@ fn main() {
         }
 
         let mut panel = ui::panel::Panel::new(conn, &cfg);
+        let params = widgets::WidgetParams {
+            id: "".to_string(),
+            context: panel.make_context(),
+            tx: tx.clone(),
+            config: cfg.clone()
+        };
 
-        let left_widgets = vec![
-            widgets::bspwm(tx.clone(), panel.make_draw_context()),
-            widgets::spacer(panel.make_draw_context(), 4, color::GREY),
-            widgets::title(panel.conn.clone(), panel.make_draw_context())
+        let left_widgets: Vec<widgets::WidgetConstructor> = vec![
+            widgets::bspwm,
+            widgets::spacer,
+            widgets::title
         ];
 
-        let mut right_widgets = vec![
-            widgets::mpd(tx.clone(), panel.make_draw_context()),
-            widgets::sensors(panel.make_draw_context(), &cfg),
-            widgets::tray(panel.conn.clone(), panel.window)
+        let mut right_widgets: Vec<widgets::WidgetConstructor> = vec![
+            widgets::mpd,
+            widgets::sensors,
+            widgets::tray
         ];
         right_widgets.reverse();
 
-        for widget in left_widgets {
+        for cons in left_widgets {
+            let widget = cons(params.clone());
             panel.add_left_widget(widget);
         }
-        for widget in right_widgets {
+        for cons in right_widgets {
+            let widget = cons(params.clone());
             panel.add_right_widget(widget);
         }
 

+ 17 - 8
src/ui/draw_context.rs → src/ui/context.rs

@@ -1,21 +1,29 @@
-use xcb;
+use std::rc::Rc;
+
 use ui;
+use ui::x11;
 use ui::font;
 use ui::color;
 
-use std::rc::Rc;
-use std::sync::Arc;
+use xcb;
 
-pub struct DrawContext {
-    conn: Arc<xcb::Connection>,
+#[derive(Clone)]
+pub struct Context {
+    pub conn: Rc<x11::Connection>,
+    pub window: xcb::Window,
     picture: xcb::render::Picture,
     pen: xcb::render::Picture,
     fonts: Rc<font::FontLoader>,
     bg_color: xcb::render::Color
 }
 
-impl DrawContext {
-    pub fn new(conn: Arc<xcb::Connection>, picture: xcb::render::Picture, fonts: Rc<font::FontLoader>) -> DrawContext {
+impl Context {
+    pub fn new(
+        conn: Rc<x11::Connection>,
+        window: xcb::Window,
+        picture: xcb::render::Picture,
+        fonts: Rc<font::FontLoader>
+    ) -> Self {
         let pen = conn.generate_id();
         xcb::render::create_solid_fill(
             &conn,
@@ -23,8 +31,9 @@ impl DrawContext {
             color::WHITE
         );
 
-        DrawContext {
+        Context {
             conn: conn,
+            window: window,
             picture: picture,
             pen: pen,
             fonts: fonts,

+ 1 - 1
src/ui/mod.rs

@@ -3,7 +3,7 @@ pub mod font;
 pub mod panel;
 pub mod ext;
 pub mod util;
-pub mod draw_context;
+pub mod context;
 pub mod x11;
 
 pub const SIZE: u16 = 20;

+ 6 - 7
src/ui/panel.rs

@@ -2,7 +2,7 @@ use config::Config;
 use xcb;
 use ui;
 use ui::color;
-use ui::draw_context::DrawContext;
+use ui::context::Context;
 use ui::ext;
 use ui::ext::ConnectionExt;
 use ui::font;
@@ -10,7 +10,6 @@ use ui::x11;
 use widgets::{Message, Update, Widget};
 
 use std::rc::Rc;
-use std::sync::Arc;
 use std::iter;
 use std::slice;
 
@@ -73,7 +72,7 @@ impl WidgetState {
 }
 
 pub struct Panel {
-    pub conn: Arc<x11::Connection>,
+    pub conn: Rc<x11::Connection>,
     pub window: xcb::Window,
     pub width: u16,
     left_widgets: Vec<WidgetState>,
@@ -85,7 +84,7 @@ pub struct Panel {
 
 impl Panel {
     pub fn new(conn: x11::Connection, cfg: &Config) -> Panel {
-        let conn = Arc::new(conn);
+        let conn = Rc::new(conn);
         let window = conn.generate_id();
         let picture = conn.generate_id();
 
@@ -143,8 +142,8 @@ impl Panel {
         self.conn.flush();
     }
 
-    pub fn make_draw_context(&self) -> DrawContext {
-        DrawContext::new(self.conn.clone_connection(), self.picture, self.fonts.clone())
+    pub fn make_context(&self) -> Context {
+        Context::new(self.conn.clone(), self.window, self.picture, self.fonts.clone())
     }
 
     fn widgets_iter(&mut self) -> iter::Chain<slice::IterMut<WidgetState>, slice::IterMut<WidgetState>>{
@@ -282,7 +281,7 @@ impl Panel {
     }
 
     pub fn rerender(&mut self) {
-        let context = self.make_draw_context();
+        let context = self.make_context();
         for state in self.widgets_iter() {
             if state.should_render() {
                 context.draw_bg(state.position, state.width);

+ 30 - 4
src/widgets/mod.rs

@@ -1,3 +1,13 @@
+use std::rc::Rc;
+use std::sync::mpsc;
+
+use xcb;
+use mpd;
+
+use config::Config;
+use ui::context::Context;
+use ui::x11;
+
 mod title;
 mod tray;
 mod sensors;
@@ -5,10 +15,6 @@ 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;
@@ -46,3 +52,23 @@ pub trait Widget {
     }
     fn finish(&mut self) {}
 }
+
+#[derive(Clone)]
+pub struct WidgetParams {
+    pub id: String,
+    pub context: Context,
+    pub tx: MessageSender,
+    pub config: Config
+}
+
+impl WidgetParams {
+    fn conn(&self) -> Rc<x11::Connection> {
+        self.context.conn.clone()
+    }
+
+    fn window(&self) -> xcb::Window {
+        self.context.window
+    }
+}
+
+pub type WidgetConstructor = fn(WidgetParams) -> Box<Widget>;

+ 12 - 10
src/widgets/music.rs

@@ -1,28 +1,30 @@
+use std::path::Path;
+use std::thread;
+
 use mpd;
-use mpd::song::Song;
-use mpd::status::State;
 use mpd::idle;
 use mpd::idle::Idle;
-use std::path::Path;
-use std::thread;
+use mpd::song::Song;
+use mpd::status::State;
+
 use ui::color;
-use ui::draw_context::DrawContext;
-use widgets::{Message, MessageSender, Update, Widget};
+use ui::context::Context;
+use widgets::{Message, MessageSender, Update, Widget, WidgetParams};
 
 const MARGIN: u16 = 7;
 const WIDTH: u16 = 250;
 
 pub struct Mpd {
-    context: DrawContext,
+    context: Context,
     tx: MessageSender,
     state: State,
     song: Option<Song>
 }
 
-pub fn mpd(tx: MessageSender, context: DrawContext) -> Box<Widget> {
+pub fn mpd(params: WidgetParams) -> Box<Widget> {
     let widget = Mpd {
-        context: context,
-        tx: tx,
+        context: params.context,
+        tx: params.tx,
         state: State::Stop,
         song: None
     };

+ 6 - 7
src/widgets/sensors.rs

@@ -1,21 +1,20 @@
-use config::Config;
 use super::super::sensors;
 use super::super::sensors::Sensor;
 use ui::color;
-use ui::draw_context::DrawContext;
-use widgets::{Message, Update, Widget};
+use ui::context::Context;
+use widgets::{Message, Update, Widget, WidgetParams};
 
 const MARGIN: u16 = 7;
 
 pub struct Sensors {
-    context: DrawContext,
+    context: Context,
     sensors: Vec<Box<Sensor>>
 }
 
-pub fn sensors(context: DrawContext, config: &Config) -> Box<Widget> {
+pub fn sensors(params: WidgetParams) -> Box<Widget> {
     let widget = Sensors {
-        context: context,
-        sensors: sensors::sensor_list(config)
+        context: params.context,
+        sensors: sensors::sensor_list(&params.config)
     };
     Box::new(widget)
 }

+ 9 - 7
src/widgets/spacer.rs

@@ -1,17 +1,19 @@
-use ui::draw_context::DrawContext;
-use widgets::Widget;
-use xcb::render::Color;
+use ui::context::Context;
+use ui::color;
+use widgets::{Widget, WidgetParams};
 
 pub struct Spacer {
-    context: DrawContext,
+    context: Context,
     width: u16
 }
 
-pub fn spacer(mut context: DrawContext, width: u16, color: Color) -> Box<Widget> {
-    context.set_bg_color(color);
+pub fn spacer(params: WidgetParams) -> Box<Widget> {
+    // TODO parse config
+    let mut context = params.context;
+    context.set_bg_color(color::GREY);
     let widget = Spacer {
         context: context,
-        width: width
+        width: 4
     };
     Box::new(widget)
 }

+ 10 - 9
src/widgets/title.rs

@@ -1,23 +1,24 @@
-use ui::draw_context::DrawContext;
-use ui::x11;
-use widgets::{Message, Update, Widget};
+use std::rc::Rc;
+
 use xcb;
 
-use std::sync::Arc;
+use ui::context::Context;
+use ui::x11;
+use widgets::{Message, Update, Widget, WidgetParams};
 
 const MARGIN: u16 = 7;
 
 pub struct Title {
-    conn: Arc<x11::Connection>,
-    context: DrawContext,
+    conn: Rc<x11::Connection>,
+    context: Context,
     title: String,
     last_win: xcb::Window
 }
 
-pub fn title(conn: Arc<x11::Connection>, context: DrawContext) -> Box<Widget> {
+pub fn title(params: WidgetParams) -> Box<Widget> {
     let widget = Title {
-        conn: conn,
-        context: context,
+        conn: params.conn(),
+        context: params.context,
         title: "".to_string(),
         last_win: 0
     };

+ 9 - 8
src/widgets/tray.rs

@@ -1,23 +1,24 @@
-use ui;
-use ui::x11;
-use widgets::{Message, Update, Widget};
+use std::rc::Rc;
+
 use xcb;
 
-use std::sync::Arc;
+use ui;
+use ui::x11;
+use widgets::{Message, Update, Widget, WidgetParams};
 
 const CLIENT_MESSAGE: u8 = xcb::CLIENT_MESSAGE | 0x80; // 0x80 flag for client messages
 
 pub struct Tray {
-    conn: Arc<x11::Connection>,
+    conn: Rc<x11::Connection>,
     window: xcb::Window,
     children: Vec<xcb::Window>,
     timestamp: xcb::Timestamp,
 }
 
-pub fn tray(conn: Arc<x11::Connection>, window: xcb::Window) -> Box<Widget> {
+pub fn tray(params: WidgetParams) -> Box<Widget> {
     let widget = Tray {
-        conn: conn,
-        window: window,
+        conn: params.conn(),
+        window: params.window(),
         children: vec![],
         timestamp: 0
     };

+ 6 - 6
src/widgets/wm.rs

@@ -3,9 +3,9 @@ use std::io::BufReader;
 use std::process::{Command, Stdio};
 use std::thread;
 
-use ui::draw_context::DrawContext;
+use ui::context::Context;
 use ui::color;
-use widgets::{Message, MessageSender, Update, Widget};
+use widgets::{Message, MessageSender, Update, Widget, WidgetParams};
 
 const MARGIN: u16 = 7;
 
@@ -24,15 +24,15 @@ impl Desktop {
 }
 
 pub struct Bspwm {
-    context: DrawContext,
+    context: Context,
     tx: MessageSender,
     desktops: Vec<Desktop>
 }
 
-pub fn bspwm(tx: MessageSender, context: DrawContext) -> Box<Widget> {
+pub fn bspwm(params: WidgetParams) -> Box<Widget> {
     let widget = Bspwm {
-        context: context,
-        tx: tx,
+        context: params.context,
+        tx: params.tx,
         desktops: vec![]
     };
     Box::new(widget)