Commit 0f870367b3
Unregistered key
Layout: unified · split
src/windows/room/space.rs +60 −27
| @@ -1,11 +1,18 @@ | |||
| 1 | use std::ops::{Deref, DerefMut}; | 1 | use std::ops::{Deref, DerefMut}; |
| 2 | use std::time::{Duration, Instant}; | ||
| 2 | 3 | ||
| 3 | use matrix_sdk::{ | 4 | use matrix_sdk::{ |
| 4 | room::Room as MatrixRoom, | 5 | room::Room as MatrixRoom, |
| 5 | ruma::{OwnedRoomId, RoomId}, | 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 | use modalkit::{ | 17 | use modalkit::{ |
| 11 | widgets::list::{List, ListState}, | 18 | widgets::list::{List, ListState}, |
| @@ -16,10 +23,13 @@ use crate::base::{IambBufferId, IambInfo, ProgramStore, RoomFocus}; | |||
| 16 | 23 | ||
| 17 | use crate::windows::RoomItem; | 24 | use crate::windows::RoomItem; |
| 18 | 25 | ||
| 26 | const SPACE_HIERARCHY_DEBOUNCE: Duration = Duration::from_secs(5); | ||
| 27 | |||
| 19 | pub struct SpaceState { | 28 | pub struct SpaceState { |
| 20 | room_id: OwnedRoomId, | 29 | room_id: OwnedRoomId, |
| 21 | room: MatrixRoom, | 30 | room: MatrixRoom, |
| 22 | list: ListState<RoomItem, IambInfo>, | 31 | list: ListState<RoomItem, IambInfo>, |
| 32 | last_fetch: Option<Instant>, | ||
| 23 | } | 33 | } |
| 24 | 34 | ||
| 25 | impl SpaceState { | 35 | impl SpaceState { |
| @@ -27,8 +37,9 @@ impl SpaceState { | |||
| 27 | let room_id = room.room_id().to_owned(); | 37 | let room_id = room.room_id().to_owned(); |
| 28 | let content = IambBufferId::Room(room_id.clone(), RoomFocus::Scrollback); | 38 | let content = IambBufferId::Room(room_id.clone(), RoomFocus::Scrollback); |
| 29 | let list = ListState::new(content, vec![]); | 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 | pub fn refresh_room(&mut self, store: &mut ProgramStore) { | 45 | pub fn refresh_room(&mut self, store: &mut ProgramStore) { |
| @@ -50,6 +61,7 @@ impl SpaceState { | |||
| 50 | room_id: self.room_id.clone(), | 61 | room_id: self.room_id.clone(), |
| 51 | room: self.room.clone(), | 62 | room: self.room.clone(), |
| 52 | list: self.list.dup(store), | 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 | type State = SpaceState; | 106 | type State = SpaceState; |
| 95 | 107 | ||
| 96 | fn render(self, area: Rect, buffer: &mut Buffer, state: &mut Self::State) { | 108 | fn render(self, area: Rect, buffer: &mut Buffer, state: &mut Self::State) { |
| 97 | let members = | 109 | let mut empty_message = None; |
| 98 | if let Ok(m) = self.store.application.worker.space_members(state.room_id.clone()) { | 110 | let need_fetch = match state.last_fetch { |
| 99 | m | 111 | Some(i) => i.elapsed() >= SPACE_HIERARCHY_DEBOUNCE, |
| 100 | } else { | 112 | None => true, |
| 101 | return; | 113 | }; |
| 102 | }; | 114 | |
| 103 | 115 | if need_fetch { | |
| 104 | let items = members | 116 | let res = self.store.application.worker.space_members(state.room_id.clone()); |
| 105 | .into_iter() | 117 | |
| 106 | .filter_map(|id| { | 118 | match res { |
| 107 | let (room, name, tags) = self.store.application.worker.get_room(id.clone()).ok()?; | 119 | Ok(members) => { |
| 108 | 120 | let items = members | |
| 109 | if id != state.room_id { | 121 | .into_iter() |
| 110 | Some(RoomItem::new(room, name, tags, self.store)) | 122 | .filter_map(|id| { |
| 111 | } else { | 123 | let (room, name, tags) = |
| 112 | None | 124 | self.store.application.worker.get_room(id.clone()).ok()?; |
| 113 | } | 125 | |
| 114 | }) | 126 | if id != state.room_id { |
| 115 | .collect(); | 127 | Some(RoomItem::new(room, name, tags, self.store)) |
| 116 | 128 | } else { | |
| 117 | state.list.set(items); | 129 | None |
| 118 | 130 | } | |
| 119 | List::new(self.store) | 131 | }) |
| 120 | .focus(self.focused) | 132 | .collect(); |
| 121 | .render(area, buffer, &mut state.list) | 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 | } |