Browse Source

Centralize color definitions

Thomas Dy 7 years ago
parent
commit
a27fb3cd3b
9 changed files with 48 additions and 20 deletions
  1. 2 1
      src/main.rs
  2. 21 0
      src/ui/color.rs
  3. 5 5
      src/ui/draw_context.rs
  4. 1 0
      src/ui/mod.rs
  5. 2 2
      src/ui/panel.rs
  6. 4 3
      src/widgets/music.rs
  7. 3 2
      src/widgets/sensors.rs
  8. 3 2
      src/widgets/spacer.rs
  9. 7 5
      src/widgets/wm.rs

+ 2 - 1
src/main.rs

@@ -18,6 +18,7 @@ use std::sync::mpsc;
 use std::thread;
 use std::time;
 
+use ui::color;
 use ui::x11;
 use widgets::Message;
 
@@ -53,7 +54,7 @@ fn main() {
 
         let left_widgets = vec![
             widgets::bspwm(tx.clone(), panel.make_draw_context()),
-            widgets::spacer(panel.make_draw_context(), 4, 0x6666, 0x6666, 0x6666, 0xFFFF),
+            widgets::spacer(panel.make_draw_context(), 4, color::GREY),
             widgets::title(panel.conn.clone(), panel.make_draw_context())
         ];
 

+ 21 - 0
src/ui/color.rs

@@ -0,0 +1,21 @@
+use xcb::render::Color;
+use xcb::ffi::render::xcb_render_color_t;
+
+macro_rules! color_const {
+    ($name:ident, $r:expr, $g:expr, $b:expr, $a:expr) => {
+        pub const $name: Color = Color {
+            base: xcb_render_color_t {
+                red: $r,
+                green: $g,
+                blue: $b,
+                alpha: $a
+            }
+        };
+    }
+}
+
+color_const!(WHITE, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF);
+color_const!(BLACK, 0x0000, 0x0000, 0x0000, 0xFFFF);
+color_const!(GREY, 0x6666, 0x6666, 0x6666, 0xFFFF);
+color_const!(LIGHT_GREEN, 0x6666, 0xCCCC, 0x6666, 0xFFFF);
+color_const!(RED, 0xCCCC, 0x0000, 0x3333, 0xFFFF);

+ 5 - 5
src/ui/draw_context.rs

@@ -1,5 +1,6 @@
 use xcb;
 use ui::font;
+use ui::color;
 
 use std::rc::Rc;
 use std::sync::Arc;
@@ -15,11 +16,10 @@ pub struct DrawContext {
 impl DrawContext {
     pub fn new(conn: Arc<xcb::Connection>, picture: xcb::render::Picture, fonts: Rc<font::FontLoader>) -> DrawContext {
         let pen = conn.generate_id();
-        let color = xcb::render::Color::new(0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF);
         xcb::render::create_solid_fill(
             &conn,
             pen,
-            color
+            color::WHITE
         );
 
         DrawContext {
@@ -27,7 +27,7 @@ impl DrawContext {
             picture: picture,
             pen: pen,
             fonts: fonts,
-            bg_color: xcb::render::Color::new(0, 0, 0, 0xFFFF)
+            bg_color: color::BLACK
         }
     }
 
@@ -46,8 +46,8 @@ impl DrawContext {
         text.width
     }
 
-    pub fn set_bg_color(&mut self, red: u16, blue: u16, green: u16, alpha: u16) {
-        self.bg_color = xcb::render::Color::new(red, blue, green, alpha);
+    pub fn set_bg_color(&mut self, color: xcb::render::Color) {
+        self.bg_color = color;
     }
 
 

+ 1 - 0
src/ui/mod.rs

@@ -1,3 +1,4 @@
+pub mod color;
 pub mod font;
 pub mod panel;
 pub mod ext;

+ 2 - 2
src/ui/panel.rs

@@ -1,5 +1,6 @@
 use config::Config;
 use xcb;
+use ui::color;
 use ui::draw_context::DrawContext;
 use ui::ext;
 use ui::ext::ConnectionExt;
@@ -174,12 +175,11 @@ impl Panel {
     }
 
     pub fn relayout(&mut self) {
-        let color = xcb::render::Color::new(0, 0, 0, 0xFFFF);
         xcb::render::fill_rectangles(
             &self.conn,
             xcb::render::PICT_OP_SRC as u8,
             self.picture,
-            color,
+            color::BLACK,
             &[xcb::Rectangle::new(0, 0, self.width, 20)]
         );
 

+ 4 - 3
src/widgets/music.rs

@@ -5,6 +5,7 @@ use mpd::idle;
 use mpd::idle::Idle;
 use std::path::Path;
 use std::thread;
+use ui::color;
 use ui::draw_context::DrawContext;
 use widgets::{Message, MessageSender, Widget};
 
@@ -56,7 +57,7 @@ impl Mpd {
 
     fn redraw(&mut self) {
         let x = self.last_pos;
-        self.context.set_bg_color(0x0, 0x0, 0x0, 0xFFFF);
+        self.context.set_bg_color(color::BLACK);
         self.context.draw_bg(x, WIDTH);
         self.render(x, WIDTH);
         self.context.flush();
@@ -74,13 +75,13 @@ impl Widget for Mpd {
         self.last_pos = x;
         let icon_width = self.context.measure_text(self.icon());
 
-        self.context.set_bg_color(0x6666, 0x6666, 0x6666, 0xFFFF);
+        self.context.set_bg_color(color::GREY);
         self.context.draw_bg(x, icon_width + MARGIN * 2);
         self.context.draw_text(self.icon(), x + MARGIN);
 
         let text = self.get_text();
         let text_width = WIDTH - MARGIN * 4 - icon_width;
-        self.context.set_bg_color(0x0, 0x0, 0x0, 0xFFFF);
+        self.context.set_bg_color(color::BLACK);
         self.context.draw_bg(x + icon_width + MARGIN * 3, text_width);
         self.context.draw_text_with_clipping(&text, x + icon_width + MARGIN * 3, text_width);
     }

+ 3 - 2
src/widgets/sensors.rs

@@ -1,6 +1,7 @@
 use config::Config;
 use super::super::sensors;
 use super::super::sensors::Sensor;
+use ui::color;
 use ui::draw_context::DrawContext;
 use widgets::{Message, Widget};
 
@@ -26,12 +27,12 @@ impl Widget for Sensors {
             let icon_width = self.context.measure_text(&sensor.icon());
             let status_width = self.context.measure_text(&sensor.status());
 
-            self.context.set_bg_color(0x6666, 0x6666, 0x6666, 0xFFFF);
+            self.context.set_bg_color(color::GREY);
             self.context.draw_bg(offset, icon_width + MARGIN * 2);
             self.context.draw_text(&sensor.icon(), offset + MARGIN);
             offset += icon_width + MARGIN * 2;
 
-            self.context.set_bg_color(0x0, 0x0, 0x0, 0xFFFF);
+            self.context.set_bg_color(color::BLACK);
             self.context.draw_text(&sensor.status(), offset + MARGIN);
             offset += status_width + MARGIN * 2;
         }

+ 3 - 2
src/widgets/spacer.rs

@@ -1,13 +1,14 @@
 use ui::draw_context::DrawContext;
 use widgets::Widget;
+use xcb::render::Color;
 
 pub struct Spacer {
     context: DrawContext,
     width: u16
 }
 
-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);
+pub fn spacer(mut context: DrawContext, width: u16, color: Color) -> Box<Widget> {
+    context.set_bg_color(color);
     let widget = Spacer {
         context: context,
         width: width

+ 7 - 5
src/widgets/wm.rs

@@ -4,6 +4,7 @@ use std::process::{Command, Stdio};
 use std::thread;
 
 use ui::draw_context::DrawContext;
+use ui::color;
 use widgets::{Message, MessageSender, Widget};
 
 const MARGIN: u16 = 7;
@@ -88,15 +89,16 @@ impl Widget for Bspwm {
 
     fn render(&mut self, x: u16, _w: u16) {
         for desktop in self.desktops.iter() {
-            if desktop.selected {
-                self.context.set_bg_color(0x6666, 0xCCCC, 0x6666, 0xFFFF);
+            let color = if desktop.selected {
+                color::LIGHT_GREEN
             }
             else if desktop.urgent {
-                self.context.set_bg_color(0xCCCC, 0x0, 0x3333, 0xFFFF);
+                color::RED
             }
             else {
-                self.context.set_bg_color(0x0, 0x0, 0x0, 0xFFFF);
-            }
+                color::BLACK
+            };
+            self.context.set_bg_color(color);
             self.context.draw_bg(x + desktop.position, desktop.width);
             self.context.draw_text(&desktop.name, x + desktop.position + MARGIN);
         }