src/windows/room/space.rs

156 lines · 4145 bytes

  1use std::ops::{Deref, DerefMut};
  2use std::time::{Duration, Instant};
  3
  4use matrix_sdk::{
  5    room::Room as MatrixRoom,
  6    ruma::{OwnedRoomId, RoomId},
  7};
  8
  9use modalkit::tui::{
 10    buffer::Buffer,
 11    layout::Rect,
 12    style::{Color, Style},
 13    text::{Span, Spans, Text},
 14    widgets::StatefulWidget,
 15};
 16
 17use modalkit::{
 18    widgets::list::{List, ListState},
 19    widgets::{TermOffset, TerminalCursor, WindowOps},
 20};
 21
 22use crate::base::{IambBufferId, IambInfo, ProgramStore, RoomFocus};
 23
 24use crate::windows::RoomItem;
 25
 26const SPACE_HIERARCHY_DEBOUNCE: Duration = Duration::from_secs(5);
 27
 28pub struct SpaceState {
 29    room_id: OwnedRoomId,
 30    room: MatrixRoom,
 31    list: ListState<RoomItem, IambInfo>,
 32    last_fetch: Option<Instant>,
 33}
 34
 35impl SpaceState {
 36    pub fn new(room: MatrixRoom) -> Self {
 37        let room_id = room.room_id().to_owned();
 38        let content = IambBufferId::Room(room_id.clone(), RoomFocus::Scrollback);
 39        let list = ListState::new(content, vec![]);
 40        let last_fetch = None;
 41
 42        SpaceState { room_id, room, list, last_fetch }
 43    }
 44
 45    pub fn refresh_room(&mut self, store: &mut ProgramStore) {
 46        if let Some(room) = store.application.worker.client.get_room(self.id()) {
 47            self.room = room;
 48        }
 49    }
 50
 51    pub fn room(&self) -> &MatrixRoom {
 52        &self.room
 53    }
 54
 55    pub fn id(&self) -> &RoomId {
 56        &self.room_id
 57    }
 58
 59    pub fn dup(&self, store: &mut ProgramStore) -> Self {
 60        SpaceState {
 61            room_id: self.room_id.clone(),
 62            room: self.room.clone(),
 63            list: self.list.dup(store),
 64            last_fetch: self.last_fetch,
 65        }
 66    }
 67}
 68
 69impl TerminalCursor for SpaceState {
 70    fn get_term_cursor(&self) -> Option<TermOffset> {
 71        self.list.get_term_cursor()
 72    }
 73}
 74
 75impl Deref for SpaceState {
 76    type Target = ListState<RoomItem, IambInfo>;
 77
 78    fn deref(&self) -> &Self::Target {
 79        &self.list
 80    }
 81}
 82
 83impl DerefMut for SpaceState {
 84    fn deref_mut(&mut self) -> &mut Self::Target {
 85        &mut self.list
 86    }
 87}
 88
 89pub struct Space<'a> {
 90    focused: bool,
 91    store: &'a mut ProgramStore,
 92}
 93
 94impl<'a> Space<'a> {
 95    pub fn new(store: &'a mut ProgramStore) -> Self {
 96        Space { focused: false, store }
 97    }
 98
 99    pub fn focus(mut self, focused: bool) -> Self {
100        self.focused = focused;
101        self
102    }
103}
104
105impl<'a> StatefulWidget for Space<'a> {
106    type State = SpaceState;
107
108    fn render(self, area: Rect, buffer: &mut Buffer, state: &mut Self::State) {
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)
155    }
156}