[fractal] Show number of rooms with notifications in window title (fixes #10).



commit 0673e4a41f7d689f9b876a2814ec310b91c3f157
Author: Nils Asmussen <nils os inf tu-dresden de>
Date:   Sun Mar 8 08:07:28 2020 +0100

    Show number of rooms with notifications in window title (fixes #10).

 fractal-gtk/src/appop/mod.rs           | 28 ++++++++++++++++++++++++----
 fractal-gtk/src/appop/notifications.rs |  2 ++
 fractal-gtk/src/widgets/roomlist.rs    | 13 +++++++++++++
 3 files changed, 39 insertions(+), 4 deletions(-)
---
diff --git a/fractal-gtk/src/appop/mod.rs b/fractal-gtk/src/appop/mod.rs
index 87c71c06..7c7e2a2d 100644
--- a/fractal-gtk/src/appop/mod.rs
+++ b/fractal-gtk/src/appop/mod.rs
@@ -14,6 +14,8 @@ use fractal_api::url::Url;
 use crate::backend;
 use crate::backend::BKCommand;
 
+use crate::i18n;
+
 use crate::types::Member;
 use crate::types::Room;
 use crate::types::RoomList;
@@ -72,6 +74,7 @@ pub struct AppOp {
     pub room_settings: Option<widgets::RoomSettings>,
     pub history: Option<widgets::RoomHistory>,
     pub roomlist: widgets::RoomList,
+    unread_rooms: usize,
     pub unsent_messages: HashMap<RoomId, (String, i32)>,
     pub typing: HashMap<RoomId, std::time::Instant>,
 
@@ -114,6 +117,7 @@ impl AppOp {
             state: AppState::Login,
             room_back_history: Rc::new(RefCell::new(vec![])),
             roomlist: widgets::RoomList::new(None, None),
+            unread_rooms: 0,
             since: None,
             unsent_messages: HashMap::new(),
             typing: HashMap::new(),
@@ -154,16 +158,32 @@ impl AppOp {
         }
     }
 
-    pub fn activate(&self) {
-        let window: gtk::Window = self
-            .ui
+    fn get_window(&self) -> gtk::Window {
+        self.ui
             .builder
             .get_object("main_window")
-            .expect("Couldn't find main_window in ui file.");
+            .expect("Couldn't find main_window in ui file.")
+    }
+
+    pub fn activate(&self) {
+        let window = self.get_window();
         window.show();
         window.present();
     }
 
+    pub fn update_title(&mut self) {
+        let unread = self.roomlist.rooms_with_notifications();
+        if self.unread_rooms != unread {
+            let window = self.get_window();
+            if unread == 0 {
+                window.set_title(&i18n::i18n("Fractal"));
+            } else {
+                window.set_title(&i18n::i18n_f("Fractal [{}]", &[&unread.to_string()]));
+            }
+            self.unread_rooms = unread;
+        }
+    }
+
     pub fn quit(&self) {
         self.cache_rooms();
         self.disconnect();
diff --git a/fractal-gtk/src/appop/notifications.rs b/fractal-gtk/src/appop/notifications.rs
index fee22e1d..d39ce3e9 100644
--- a/fractal-gtk/src/appop/notifications.rs
+++ b/fractal-gtk/src/appop/notifications.rs
@@ -5,6 +5,7 @@ impl AppOp {
     pub fn clear_room_notifications(&mut self, room_id: RoomId) {
         self.set_room_notifications(room_id.clone(), 0, 0);
         self.roomlist.set_bold(room_id, false);
+        self.update_title();
     }
 
     pub fn set_room_notifications(&mut self, room_id: RoomId, n: i32, h: i32) {
@@ -14,5 +15,6 @@ impl AppOp {
             self.roomlist
                 .set_room_notifications(room_id, r.notifications, r.highlight);
         }
+        self.update_title();
     }
 }
diff --git a/fractal-gtk/src/widgets/roomlist.rs b/fractal-gtk/src/widgets/roomlist.rs
index 0a10d8fb..83dea480 100644
--- a/fractal-gtk/src/widgets/roomlist.rs
+++ b/fractal-gtk/src/widgets/roomlist.rs
@@ -182,6 +182,13 @@ impl RoomListGroup {
         }
     }
 
+    pub fn rooms_with_notifications(&self) -> usize {
+        self.rooms
+            .iter()
+            .filter(|(_, r)| r.room.notifications > 0 || r.room.highlight > 0)
+            .count()
+    }
+
     pub fn set_room_notifications(&mut self, room_id: RoomId, n: i32, h: i32) {
         if let Some(ref mut r) = self.rooms.get_mut(&room_id) {
             r.set_notifications(n, h);
@@ -635,6 +642,12 @@ impl RoomList {
         run_in_group!(self, &room_id, set_room_avatar, room_id, av);
     }
 
+    pub fn rooms_with_notifications(&self) -> usize {
+        self.inv.get().rooms_with_notifications()
+            + self.fav.get().rooms_with_notifications()
+            + self.rooms.get().rooms_with_notifications()
+    }
+
     pub fn set_room_notifications(&mut self, room_id: RoomId, n: i32, h: i32) {
         run_in_group!(self, &room_id, set_room_notifications, room_id, n, h);
     }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]