src/windows/room/space.rs

123 lines · 2951 bytes

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