src/windows/room/space.rs
160 lines · 4323 bytes
1//! Window for Matrix spaces
2use std::ops::{Deref, DerefMut};
3use std::time::{Duration, Instant};
4
5use matrix_sdk::{
6 room::Room as MatrixRoom,
7 ruma::{OwnedRoomId, RoomId},
8};
9
10use modalkit::tui::{
11 buffer::Buffer,
12 layout::Rect,
13 style::{Color, Style},
14 text::{Span, Spans, Text},
15 widgets::StatefulWidget,
16};
17
18use modalkit::{
19 widgets::list::{List, ListState},
20 widgets::{TermOffset, TerminalCursor, WindowOps},
21};
22
23use crate::base::{IambBufferId, IambInfo, ProgramStore, RoomFocus};
24
25use crate::windows::RoomItem;
26
27const SPACE_HIERARCHY_DEBOUNCE: Duration = Duration::from_secs(5);
28
29/// State needed for rendering [Space].
30pub struct SpaceState {
31 room_id: OwnedRoomId,
32 room: MatrixRoom,
33 list: ListState<RoomItem, IambInfo>,
34 last_fetch: Option<Instant>,
35}
36
37impl SpaceState {
38 pub fn new(room: MatrixRoom) -> Self {
39 let room_id = room.room_id().to_owned();
40 let content = IambBufferId::Room(room_id.clone(), RoomFocus::Scrollback);
41 let list = ListState::new(content, vec![]);
42 let last_fetch = None;
43
44 SpaceState { room_id, room, list, last_fetch }
45 }
46
47 pub fn refresh_room(&mut self, store: &mut ProgramStore) {
48 if let Some(room) = store.application.worker.client.get_room(self.id()) {
49 self.room = room;
50 }
51 }
52
53 pub fn room(&self) -> &MatrixRoom {
54 &self.room
55 }
56
57 pub fn id(&self) -> &RoomId {
58 &self.room_id
59 }
60
61 pub fn dup(&self, store: &mut ProgramStore) -> Self {
62 SpaceState {
63 room_id: self.room_id.clone(),
64 room: self.room.clone(),
65 list: self.list.dup(store),
66 last_fetch: self.last_fetch,
67 }
68 }
69}
70
71impl TerminalCursor for SpaceState {
72 fn get_term_cursor(&self) -> Option<TermOffset> {
73 self.list.get_term_cursor()
74 }
75}
76
77impl Deref for SpaceState {
78 type Target = ListState<RoomItem, IambInfo>;
79
80 fn deref(&self) -> &Self::Target {
81 &self.list
82 }
83}
84
85impl DerefMut for SpaceState {
86 fn deref_mut(&mut self) -> &mut Self::Target {
87 &mut self.list
88 }
89}
90
91/// [StatefulWidget] for Matrix spaces.
92pub struct Space<'a> {
93 focused: bool,
94 store: &'a mut ProgramStore,
95}
96
97impl<'a> Space<'a> {
98 pub fn new(store: &'a mut ProgramStore) -> Self {
99 Space { focused: false, store }
100 }
101
102 pub fn focus(mut self, focused: bool) -> Self {
103 self.focused = focused;
104 self
105 }
106}
107
108impl<'a> StatefulWidget for Space<'a> {
109 type State = SpaceState;
110
111 fn render(self, area: Rect, buffer: &mut Buffer, state: &mut Self::State) {
112 let mut empty_message = None;
113 let need_fetch = match state.last_fetch {
114 Some(i) => i.elapsed() >= SPACE_HIERARCHY_DEBOUNCE,
115 None => true,
116 };
117
118 if need_fetch {
119 let res = self.store.application.worker.space_members(state.room_id.clone());
120
121 match res {
122 Ok(members) => {
123 let items = members
124 .into_iter()
125 .filter_map(|id| {
126 let (room, _, tags) =
127 self.store.application.worker.get_room(id.clone()).ok()?;
128 let room_info = std::sync::Arc::new((room, tags));
129
130 if id != state.room_id {
131 Some(RoomItem::new(room_info, self.store))
132 } else {
133 None
134 }
135 })
136 .collect();
137
138 state.list.set(items);
139 state.last_fetch = Some(Instant::now());
140 },
141 Err(e) => {
142 let lines = vec![
143 Spans::from("Unable to fetch space room hierarchy:"),
144 Span::styled(e.to_string(), Style::default().fg(Color::Red)).into(),
145 ];
146
147 empty_message = Text { lines }.into();
148 },
149 }
150 }
151
152 let mut list = List::new(self.store).focus(self.focused);
153
154 if let Some(text) = empty_message {
155 list = list.empty_message(text);
156 }
157
158 list.render(area, buffer, &mut state.list)
159 }
160}