Browse Source

Make text and bg colors configurable

Thomas Dy 7 năm trước cách đây
mục cha
commit
6176c718e5
4 tập tin đã thay đổi với 65 bổ sung9 xóa
  1. 32 0
      src/ui/color.rs
  2. 8 6
      src/ui/context.rs
  3. 24 2
      src/ui/window.rs
  4. 1 1
      src/widgets/wm.rs

+ 32 - 0
src/ui/color.rs

@@ -19,3 +19,35 @@ 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);
+
+fn double(num: u16) -> u16 {
+    num << 8 | num
+}
+
+pub fn from_str(input: &str) -> Option<Color> {
+    if input.len() != 7 {
+        return None
+    }
+    let (head, body) = input.split_at(1);
+    if head != "#" {
+        return None
+    }
+    let valid = "0123456789ABCDEF";
+    if !body.chars().all(|c| valid.contains(c)) {
+        return None
+    }
+    let mut num = u32::from_str_radix(body, 16).unwrap();
+    let blue = (num % 256) as u16;
+    num >>= 8;
+    let green = (num % 256) as u16;
+    num >>= 8;
+    let red = (num % 256) as u16;
+    Some(Color::new(double(red), double(green), double(blue), 0xFFFF))
+}
+
+pub fn as_u32(input: Color) -> u32 {
+    (input.alpha() as u32 % 256) << 24 |
+        (input.red() as u32 % 256) << 16  |
+        (input.green() as u32 % 256) << 16  |
+        (input.blue() as u32 % 256)
+}

+ 8 - 6
src/ui/context.rs

@@ -2,7 +2,6 @@ use std::rc::Rc;
 
 use ui::x11;
 use ui::font;
-use ui::color;
 
 use xcb;
 
@@ -14,7 +13,8 @@ pub struct Context {
     picture: xcb::render::Picture,
     pen: xcb::render::Picture,
     fonts: Rc<font::FontLoader>,
-    bg_color: xcb::render::Color
+    bg_color: xcb::render::Color,
+    pub default_bg_color: xcb::render::Color
 }
 
 impl Context {
@@ -23,13 +23,15 @@ impl Context {
         height: u16,
         window: xcb::Window,
         picture: xcb::render::Picture,
+        pen_color: xcb::render::Color,
+        bg_color: xcb::render::Color,
         fonts: Rc<font::FontLoader>
     ) -> Self {
         let pen = conn.generate_id();
         xcb::render::create_solid_fill(
             &conn,
             pen,
-            color::WHITE
+            pen_color
         );
 
         Context {
@@ -38,8 +40,9 @@ impl Context {
             window: window,
             picture: picture,
             pen: pen,
-            fonts: fonts,
-            bg_color: color::BLACK
+            bg_color: bg_color,
+            default_bg_color: bg_color,
+            fonts: fonts
         }
     }
 
@@ -62,7 +65,6 @@ impl Context {
         self.bg_color = color;
     }
 
-
     pub fn draw_text(&self, name: &str, x: u16) {
         if !name.is_empty() {
             let text = self.fonts.create_renderable_text(name);

+ 24 - 2
src/ui/window.rs

@@ -1,8 +1,10 @@
 use std::rc::Rc;
 
 use xcb;
+use xcb::render::Color;
 
 use config::Config;
+use ui::color;
 use ui::context::Context;
 use ui::ext;
 use ui::ext::ConnectionExt;
@@ -14,6 +16,8 @@ pub struct Window {
     pub window: xcb::Window,
     pub height: u16,
     pub width: u16,
+    pub text_color: Color,
+    pub bg_color: Color,
     fonts: Rc<font::FontLoader>,
     picture: xcb::render::Picture
 }
@@ -25,6 +29,14 @@ impl Window {
         let picture = conn.generate_id();
 
         let height = cfg.lookup("bar.height").and_then(|v| v.as_integer()).expect("bar.height is not set") as u16;
+        let text_color = cfg.lookup("bar.text_color")
+            .and_then(|v| v.as_str())
+            .and_then(|s| color::from_str(s))
+            .unwrap_or(color::WHITE);
+        let bg_color = cfg.lookup("bar.bg_color")
+            .and_then(|v| v.as_str())
+            .and_then(|s| color::from_str(s))
+            .unwrap_or(color::BLACK);
         let width = conn.default_screen().width;
         let font_loader = Rc::new(font::FontLoader::from_config(conn.clone(), cfg));
 
@@ -32,6 +44,8 @@ impl Window {
             conn: conn,
             height: height,
             width: width,
+            text_color: text_color,
+            bg_color: bg_color,
             window: window,
             fonts: font_loader,
             picture: picture
@@ -55,7 +69,7 @@ impl Window {
             xcb::WINDOW_CLASS_INPUT_OUTPUT as u16,
             xcb::COPY_FROM_PARENT,
             &[
-                (xcb::CW_BACK_PIXEL, 0xFF000000),
+                (xcb::CW_BACK_PIXEL, color::as_u32(self.bg_color)),
                 (xcb::CW_EVENT_MASK, xcb::EVENT_MASK_EXPOSURE | xcb::EVENT_MASK_PROPERTY_CHANGE | xcb::EVENT_MASK_STRUCTURE_NOTIFY | xcb::EVENT_MASK_BUTTON_PRESS)
             ]
         );
@@ -74,7 +88,15 @@ impl Window {
     }
 
     pub fn make_context(&self) -> Context {
-        Context::new(self.conn.clone(), self.height, self.window, self.picture, self.fonts.clone())
+        Context::new(
+            self.conn.clone(),
+            self.height,
+            self.window,
+            self.picture,
+            self.text_color,
+            self.bg_color,
+            self.fonts.clone()
+        )
     }
 
     pub fn set_property<T>(&self, name: xcb::Atom, type_: xcb::Atom, format: u8, data: &[T]) {

+ 1 - 1
src/widgets/wm.rs

@@ -96,7 +96,7 @@ impl Widget for Bspwm {
                 color::RED
             }
             else {
-                color::BLACK
+                self.context.default_bg_color
             };
             self.context.set_bg_color(color);
             self.context.draw_bg(x + desktop.position, desktop.width);