From 34b93d5cce74a8a2dcf018c1e4114584f08a6dd3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Garc=C3=ADa=20Moreno?= Date: Sun, 24 Dec 2017 15:54:35 +0100 Subject: [PATCH] Maintain room selection on refresh See #34 --- fractal-gtk/res/app.css | 3 ++- fractal-gtk/res/main_window.glade | 3 +++ fractal-gtk/src/app.rs | 14 +++-------- fractal-gtk/src/widgets/roomlist.rs | 38 +++++++++++++++++++++++++++++ fractal-gtk/src/widgets/roomrow.rs | 14 ++++++++++- 5 files changed, 60 insertions(+), 12 deletions(-) diff --git a/fractal-gtk/res/app.css b/fractal-gtk/res/app.css index 2adfbc29..aee70484 100644 --- a/fractal-gtk/res/app.css +++ b/fractal-gtk/res/app.css @@ -26,7 +26,8 @@ color: @theme_selected_bg_color; } -.room-list list { +.room-list list, +.rooms-sidebar { background-color: #f4f4f3; } diff --git a/fractal-gtk/res/main_window.glade b/fractal-gtk/res/main_window.glade index 7c0b828d..1f129c2b 100644 --- a/fractal-gtk/res/main_window.glade +++ b/fractal-gtk/res/main_window.glade @@ -317,6 +317,9 @@ + diff --git a/fractal-gtk/src/app.rs b/fractal-gtk/src/app.rs index db0ab039..b8566e40 100644 --- a/fractal-gtk/src/app.rs +++ b/fractal-gtk/src/app.rs @@ -471,27 +471,21 @@ impl AppOp { .get_object("room_container") .expect("Couldn't find room_container in ui file."); - let mut array: Vec = vec![]; + let selected_room = self.roomlist.get_selected(); self.rooms.clear(); for ch in container.get_children().iter() { container.remove(ch); } - for r in rooms { + for r in rooms.iter() { self.rooms.insert(r.id.clone(), r.clone()); - array.push(r); } - // TODO: sort by last message - array.sort_by(|x, y| x.name.clone().unwrap_or_default().to_lowercase().cmp(&y.name.clone().unwrap_or_default().to_lowercase())); - self.roomlist = widgets::RoomList::new(Some(self.server_url.clone())); + self.roomlist.add_rooms(rooms.iter().cloned().collect()); container.add(&self.roomlist.widget()); - - for v in array { - self.roomlist.add_room(v.clone()); - } + self.roomlist.set_selected(selected_room); let bk = self.backend.clone(); self.roomlist.connect(move |room| { diff --git a/fractal-gtk/src/widgets/roomlist.rs b/fractal-gtk/src/widgets/roomlist.rs index f2f871fb..d681a333 100644 --- a/fractal-gtk/src/widgets/roomlist.rs +++ b/fractal-gtk/src/widgets/roomlist.rs @@ -102,6 +102,7 @@ impl RoomList { b.pack_start(&self.list, true, true, 0); b.show_all(); + self.render_notifies(); b } @@ -113,4 +114,41 @@ impl RoomList { cb(rs[idx as usize].clone()); }); } + + pub fn get_selected(&self) -> Option { + match self.list.get_selected_row() { + Some(row) => Some(self.roomvec[row.get_index() as usize].id.clone()), + None => None, + } + } + + pub fn set_selected(&self, room: Option) { + self.list.unselect_all(); + + if room.is_none() { + return; + } + + let room = room.unwrap(); + + if let Some(idx) = self.roomvec.iter().position(|x| { x.id == room}) { + if let Some(ref row) = self.list.get_row_at_index(idx as i32) { + self.list.select_row(row); + } + } + } + + pub fn add_rooms(&mut self, mut array: Vec) { + array.sort_by_key(|x| x.name.clone().unwrap_or_default().to_lowercase()); + + for r in array { + self.add_room(r); + } + } + + fn render_notifies(&self) { + for (_k, r) in self.rooms.iter() { + r.render_notifies(); + } + } } diff --git a/fractal-gtk/src/widgets/roomrow.rs b/fractal-gtk/src/widgets/roomrow.rs index 3a1ad341..8fa266af 100644 --- a/fractal-gtk/src/widgets/roomrow.rs +++ b/fractal-gtk/src/widgets/roomrow.rs @@ -43,10 +43,15 @@ impl RoomRow { text.set_alignment(0.0, 0.0); text.set_ellipsize(pango::EllipsizeMode::End); - let notifications = gtk::Label::new(&format!("{}", room.notifications)[..]); + let n = room.notifications; + let notifications = gtk::Label::new(&format!("{}", n)[..]); if let Some(style) = notifications.get_style_context() { style.add_class("notify-badge"); } + match n { + 0 => notifications.hide(), + _ => notifications.show(), + } icon.default(String::from("avatar-default-symbolic"), Some(ICON_SIZE)); if avatar.starts_with("mxc") || avatar.is_empty() { @@ -72,6 +77,13 @@ impl RoomRow { } } + pub fn render_notifies(&self) { + match self.room.notifications { + 0 => self.notifications.hide(), + _ => self.notifications.show(), + } + } + pub fn set_name(&mut self, name: String) { self.room.name = Some(name.clone()); self.text.set_text(&name);