瀏覽代碼

Move checking if selection available to tray

Thomas Dy 7 年之前
父節點
當前提交
e3da2e7c2e
共有 2 個文件被更改,包括 11 次插入6 次删除
  1. 5 6
      src/main.rs
  2. 6 0
      src/tray.rs

+ 5 - 6
src/main.rs

@@ -17,12 +17,6 @@ fn main() {
         let conn = Arc::new(conn);
         let atoms = atom::Atoms::new(&conn);
 
-        let owner = xcb::get_selection_owner(&conn, atoms.get(atom::_NET_SYSTEM_TRAY_S0)).get_reply().unwrap().owner();
-        if owner != xcb::NONE {
-            println!("Another system tray is already running");
-            return
-        }
-
         let dir = "top-right";
         let dir = match dir {
             "top-right" => tray::TOP_RIGHT,
@@ -33,6 +27,11 @@ fn main() {
 
         let mut tray = tray::Tray::new(&conn, &atoms, preferred as usize, 20, dir);
 
+        if !tray.is_selection_available() {
+            println!("Another system tray is already running");
+            return
+        }
+
         let (tx, rx) = chan::sync::<event::Event>(0);
         {
             let conn = conn.clone();

+ 6 - 0
src/tray.rs

@@ -80,6 +80,12 @@ impl<'a> Tray<'a> {
         self.conn.flush();
     }
 
+    pub fn is_selection_available(&self) -> bool {
+        let selection = self.atoms.get(atom::_NET_SYSTEM_TRAY_S0);
+        let owner = xcb::get_selection_owner(self.conn, selection).get_reply().unwrap().owner();
+        owner == xcb::NONE
+    }
+
     pub fn take_selection(&mut self, timestamp: xcb::Timestamp) -> bool {
         let selection = self.atoms.get(atom::_NET_SYSTEM_TRAY_S0);
         xcb::set_selection_owner(self.conn, self.window, selection, timestamp);