Commit 0f870367b3
Unregistered key
Layout: unified · split
src/windows/room/space.rs +60 −27
| @@ -1,11 +1,18 @@ | ||
| 1 | 1 | use std::ops::{Deref, DerefMut}; |
| 2 | use std::time::{Duration, Instant}; | |
| 2 | 3 | |
| 3 | 4 | use matrix_sdk::{ |
| 4 | 5 | room::Room as MatrixRoom, |
| 5 | 6 | ruma::{OwnedRoomId, RoomId}, |
| 6 | 7 | }; |
| 7 | 8 | |
| 8 | use modalkit::tui::{buffer::Buffer, layout::Rect, widgets::StatefulWidget}; | |
| 9 | use modalkit::tui::{ | |
| 10 | buffer::Buffer, | |
| 11 | layout::Rect, | |
| 12 | style::{Color, Style}, | |
| 13 | text::{Span, Spans, Text}, | |
| 14 | widgets::StatefulWidget, | |
| 15 | }; | |
| 9 | 16 | |
| 10 | 17 | use modalkit::{ |
| 11 | 18 | widgets::list::{List, ListState}, |
| @@ -16,10 +23,13 @@ use crate::base::{IambBufferId, IambInfo, ProgramStore, RoomFocus}; | ||
| 16 | 23 | |
| 17 | 24 | use crate::windows::RoomItem; |
| 18 | 25 | |
| 26 | const SPACE_HIERARCHY_DEBOUNCE: Duration = Duration::from_secs(5); | |
| 27 | ||
| 19 | 28 | pub struct SpaceState { |
| 20 | 29 | room_id: OwnedRoomId, |
| 21 | 30 | room: MatrixRoom, |
| 22 | 31 | list: ListState<RoomItem, IambInfo>, |
| 32 | last_fetch: Option<Instant>, | |
| 23 | 33 | } |
| 24 | 34 | |
| 25 | 35 | impl SpaceState { |
| @@ -27,8 +37,9 @@ impl SpaceState { | ||
| 27 | 37 | let room_id = room.room_id().to_owned(); |
| 28 | 38 | let content = IambBufferId::Room(room_id.clone(), RoomFocus::Scrollback); |
| 29 | 39 | let list = ListState::new(content, vec![]); |
| 40 | let last_fetch = None; | |
| 30 | 41 | |
| 31 | SpaceState { room_id, room, list } | |
| 42 | SpaceState { room_id, room, list, last_fetch } | |
| 32 | 43 | } |
| 33 | 44 | |
| 34 | 45 | pub fn refresh_room(&mut self, store: &mut ProgramStore) { |
| @@ -50,6 +61,7 @@ impl SpaceState { | ||
| 50 | 61 | room_id: self.room_id.clone(), |
| 51 | 62 | room: self.room.clone(), |
| 52 | 63 | list: self.list.dup(store), |
| 64 | last_fetch: self.last_fetch, | |
| 53 | 65 | } |
| 54 | 66 | } |
| 55 | 67 | } |
| @@ -94,30 +106,51 @@ impl<'a> StatefulWidget for Space<'a> { | ||
| 94 | 106 | type State = SpaceState; |
| 95 | 107 | |
| 96 | 108 | fn render(self, area: Rect, buffer: &mut Buffer, state: &mut Self::State) { |
| 97 | let members = | |
| 98 | if let Ok(m) = self.store.application.worker.space_members(state.room_id.clone()) { | |
| 99 | m | |
| 100 | } else { | |
| 101 | return; | |
| 102 | }; | |
| 103 | ||
| 104 | let items = members | |
| 105 | .into_iter() | |
| 106 | .filter_map(|id| { | |
| 107 | let (room, name, tags) = self.store.application.worker.get_room(id.clone()).ok()?; | |
| 108 | ||
| 109 | if id != state.room_id { | |
| 110 | Some(RoomItem::new(room, name, tags, self.store)) | |
| 111 | } else { | |
| 112 | None | |
| 113 | } | |
| 114 | }) | |
| 115 | .collect(); | |
| 116 | ||
| 117 | state.list.set(items); | |
| 118 | ||
| 119 | List::new(self.store) | |
| 120 | .focus(self.focused) | |
| 121 | .render(area, buffer, &mut state.list) | |
| 109 | let mut empty_message = None; | |
| 110 | let need_fetch = match state.last_fetch { | |
| 111 | Some(i) => i.elapsed() >= SPACE_HIERARCHY_DEBOUNCE, | |
| 112 | None => true, | |
| 113 | }; | |
| 114 | ||
| 115 | if need_fetch { | |
| 116 | let res = self.store.application.worker.space_members(state.room_id.clone()); | |
| 117 | ||
| 118 | match res { | |
| 119 | Ok(members) => { | |
| 120 | let items = members | |
| 121 | .into_iter() | |
| 122 | .filter_map(|id| { | |
| 123 | let (room, name, tags) = | |
| 124 | self.store.application.worker.get_room(id.clone()).ok()?; | |
| 125 | ||
| 126 | if id != state.room_id { | |
| 127 | Some(RoomItem::new(room, name, tags, self.store)) | |
| 128 | } else { | |
| 129 | None | |
| 130 | } | |
| 131 | }) | |
| 132 | .collect(); | |
| 133 | ||
| 134 | state.list.set(items); | |
| 135 | state.last_fetch = Some(Instant::now()); | |
| 136 | }, | |
| 137 | Err(e) => { | |
| 138 | let lines = vec![ | |
| 139 | Spans::from("Unable to fetch space room hierarchy:"), | |
| 140 | Span::styled(e.to_string(), Style::default().fg(Color::Red)).into(), | |
| 141 | ]; | |
| 142 | ||
| 143 | empty_message = Text { lines }.into(); | |
| 144 | }, | |
| 145 | } | |
| 146 | } | |
| 147 | ||
| 148 | let mut list = List::new(self.store).focus(self.focused); | |
| 149 | ||
| 150 | if let Some(text) = empty_message { | |
| 151 | list = list.empty_message(text); | |
| 152 | } | |
| 153 | ||
| 154 | list.render(area, buffer, &mut state.list) | |
| 122 | 155 | } |
| 123 | 156 | } |