Jelajahi Sumber

Force icon sizes

Thomas Dy 7 tahun lalu
induk
melakukan
a2bc5ba00a
3 mengubah file dengan 25 tambahan dan 7 penghapusan
  1. 6 1
      src/event.rs
  2. 4 1
      src/main.rs
  3. 15 5
      src/tray.rs

+ 6 - 1
src/event.rs

@@ -3,7 +3,8 @@ use xcb;
 
 pub enum Event {
     ChildRequest(xcb::Window),
-    ChildDestroyed(xcb::Window)
+    ChildDestroyed(xcb::Window),
+    ChildConfigured(xcb::Window)
 }
 
 const CLIENT_MESSAGE: u8 = xcb::CLIENT_MESSAGE | 0x80;
@@ -23,6 +24,10 @@ pub fn event_loop(conn: &xcb::Connection, tx: chan::Sender<Event>) {
                     let event: &xcb::DestroyNotifyEvent = xcb::cast_event(&event);
                     tx.send(Event::ChildDestroyed(event.window()));
                 },
+                xcb::CONFIGURE_NOTIFY => {
+                    let event: &xcb::ConfigureNotifyEvent = xcb::cast_event(&event);
+                    tx.send(Event::ChildConfigured(event.window()));
+                },
                 _ => {}
             },
             None => { break; }

+ 4 - 1
src/main.rs

@@ -23,7 +23,7 @@ fn main() {
             return
         }
 
-        let mut tray = tray::Tray::new(&conn, &atoms, preferred as usize);
+        let mut tray = tray::Tray::new(&conn, &atoms, preferred as usize, 20);
         tray.create();
         if !tray.take_selection() {
             println!("Could not take ownership of tray selection. Maybe another tray is also running?");
@@ -47,6 +47,9 @@ fn main() {
                     },
                     ChildDestroyed(window) => {
                         tray.forget(window);
+                    },
+                    ChildConfigured(window) => {
+                        tray.force_size(window);
                     }
                 },
                 signal.recv() => {

+ 15 - 5
src/tray.rs

@@ -5,16 +5,18 @@ pub struct Tray<'a> {
     conn: &'a xcb::Connection,
     atoms: &'a atom::Atoms<'a>,
     screen: usize,
+    icon_size: u16,
     window: xcb::Window,
     children: Vec<xcb::Window>
 }
 
 impl<'a> Tray<'a> {
-    pub fn new<'b>(conn: &'b xcb::Connection, atoms: &'b atom::Atoms, screen: usize) -> Tray<'b> {
+    pub fn new<'b>(conn: &'b xcb::Connection, atoms: &'b atom::Atoms, screen: usize, icon_size: u16) -> Tray<'b> {
         Tray::<'b> {
             conn: conn,
             atoms: atoms,
             screen: screen,
+            icon_size: icon_size,
             window: conn.generate_id(),
             children: vec![]
         }
@@ -30,7 +32,7 @@ impl<'a> Tray<'a> {
             self.window,
             screen.root(),
             0, 0,
-            20, 20,
+            self.icon_size, self.icon_size,
             0,
             xcb::WINDOW_CLASS_INPUT_OUTPUT as u16,
             screen.root_visual(),
@@ -49,7 +51,7 @@ impl<'a> Tray<'a> {
     }
 
     pub fn adopt(&mut self, window: xcb::Window) {
-        let offset = (self.children.len() * 20) as i16;
+        let offset = (self.children.len() as u16 * self.icon_size) as i16;
         xcb::change_window_attributes(self.conn, window, &[
             (xcb::CW_EVENT_MASK, xcb::EVENT_MASK_STRUCTURE_NOTIFY)
         ]);
@@ -65,11 +67,19 @@ impl<'a> Tray<'a> {
         self.resize();
     }
 
+    pub fn force_size(&self, window: xcb::Window) {
+        xcb::configure_window(self.conn, window, &[
+            (xcb::CONFIG_WINDOW_WIDTH as u16, self.icon_size as u32),
+            (xcb::CONFIG_WINDOW_HEIGHT as u16, self.icon_size as u32)
+        ]);
+        self.conn.flush();
+    }
+
     pub fn resize(&self) {
-        let len = self.children.len();
+        let len = self.children.len() as u16;
         if len > 0 {
             xcb::configure_window(self.conn, self.window, &[
-                (xcb::CONFIG_WINDOW_WIDTH as u16, (len * 20) as u32)
+                (xcb::CONFIG_WINDOW_WIDTH as u16, (len * self.icon_size) as u32)
             ]);
             xcb::map_window(self.conn, self.window);
         }