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