Browse Source

Allow setting background color

Thomas Dy 7 years ago
parent
commit
4d3ebf99d3
4 changed files with 30 additions and 3 deletions
  1. 6 0
      Cargo.lock
  2. 1 0
      Cargo.toml
  3. 18 1
      src/main.rs
  4. 5 2
      src/tray.rs

+ 6 - 0
Cargo.lock

@@ -4,6 +4,7 @@ version = "0.1.0"
 dependencies = [
  "chan 0.1.18 (registry+https://github.com/rust-lang/crates.io-index)",
  "chan-signal 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)",
+ "css-color-parser 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
  "getopts 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)",
  "xcb 0.7.5 (registry+https://github.com/rust-lang/crates.io-index)",
 ]
@@ -40,6 +41,11 @@ dependencies = [
  "libc 0.2.15 (registry+https://github.com/rust-lang/crates.io-index)",
 ]
 
+[[package]]
+name = "css-color-parser"
+version = "0.1.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+
 [[package]]
 name = "getopts"
 version = "0.2.14"

+ 1 - 0
Cargo.toml

@@ -7,6 +7,7 @@ authors = ["Thomas Dy <thatsmydoing@gmail.com>"]
 chan = "0.1.18"
 chan-signal = "0.1.6"
 getopts = "0.2"
+css-color-parser = "*"
 
 [dependencies.xcb]
 version = "0.7.5"

+ 18 - 1
src/main.rs

@@ -1,12 +1,15 @@
 #[macro_use]
 extern crate chan;
 extern crate chan_signal;
+extern crate css_color_parser;
 extern crate getopts;
 extern crate xcb;
 
 mod atom;
 mod tray;
 
+use css_color_parser::Color;
+
 use std::env;
 use std::process;
 use std::thread;
@@ -28,6 +31,7 @@ fn real_main() -> i32 {
     let mut opts = getopts::Options::new();
     opts.optopt("i", "icon-size", "size of the tray icons, default 20", "<size>");
     opts.optopt("p", "position", "position of the tray, one of: top-left, top-right, bottom-left, bottom-right", "<pos>");
+    opts.optopt("b", "background", "background color of the tray", "<color>");
     opts.optflag("h", "help", "print this help menu");
     let matches = match opts.parse(&args[1..]) {
         Ok(m) => m,
@@ -61,12 +65,25 @@ fn real_main() -> i32 {
         },
         None => 20
     };
+    let black = Color { r: 0, g: 0, b: 0, a: 1.0 };
+    let bg = matches.opt_str("b");
+    let bg = match bg {
+        Some(color) => match color.parse::<Color>() {
+            Ok(color) => color,
+            Err(e) => {
+                println!("Invalid color specified, {}.", e.to_string());
+                return EXIT_WRONG_ARGS
+            }
+        },
+        None => black
+    };
+    let bg = ((bg.a * 255.0) as u32) << 24 | (bg.r as u32) << 16 | (bg.g as u32) << 8 | (bg.b as u32);
 
     if let Ok((conn, preferred)) = xcb::Connection::connect(None) {
         let conn = Arc::new(conn);
         let atoms = atom::Atoms::new(&conn);
 
-        let mut tray = tray::Tray::new(&conn, &atoms, preferred as usize, size, pos);
+        let mut tray = tray::Tray::new(&conn, &atoms, preferred as usize, size, pos, bg);
 
         if !tray.is_selection_available() {
             println!("Another system tray is already running");

+ 5 - 2
src/tray.rs

@@ -30,6 +30,7 @@ pub struct Tray<'a> {
     screen: usize,
     icon_size: u16,
     position: Position,
+    bg: u32,
     window: xcb::Window,
     children: Vec<xcb::Window>,
     timestamp: xcb::Timestamp,
@@ -42,7 +43,8 @@ impl<'a> Tray<'a> {
         atoms: &'b atom::Atoms,
         screen: usize,
         icon_size: u16,
-        position: Position
+        position: Position,
+        bg: u32
     ) -> Tray<'b> {
         Tray::<'b> {
             conn: conn,
@@ -50,6 +52,7 @@ impl<'a> Tray<'a> {
             screen: screen,
             icon_size: icon_size,
             position: position,
+            bg: bg,
             window: conn.generate_id(),
             children: vec![],
             timestamp: 0,
@@ -72,7 +75,7 @@ impl<'a> Tray<'a> {
             xcb::WINDOW_CLASS_INPUT_OUTPUT as u16,
             screen.root_visual(),
             &[
-                (xcb::CW_BACK_PIXEL, screen.black_pixel()),
+                (xcb::CW_BACK_PIXEL, self.bg),
                 (xcb::CW_EVENT_MASK, xcb::EVENT_MASK_PROPERTY_CHANGE)
             ]
         );