src/windows/room/mod.rs
271 lines · 7225 bytes
1use matrix_sdk::{room::Room as MatrixRoom, ruma::RoomId, DisplayName};
2
3use modalkit::tui::{
4 buffer::Buffer,
5 layout::Rect,
6 style::{Modifier as StyleModifier, Style},
7 text::{Span, Spans},
8 widgets::StatefulWidget,
9};
10
11use modalkit::{
12 editing::action::{
13 Action,
14 EditInfo,
15 EditResult,
16 Editable,
17 EditorAction,
18 Jumpable,
19 PromptAction,
20 Promptable,
21 Scrollable,
22 },
23 editing::base::{
24 Axis,
25 CloseFlags,
26 Count,
27 MoveDir1D,
28 OpenTarget,
29 PositionList,
30 ScrollStyle,
31 WordStyle,
32 },
33 input::InputContext,
34 widgets::{TermOffset, TerminalCursor, WindowOps},
35};
36
37use crate::base::{
38 IambError,
39 IambId,
40 IambInfo,
41 IambResult,
42 MessageAction,
43 ProgramAction,
44 ProgramContext,
45 ProgramStore,
46 RoomAction,
47 SendAction,
48};
49
50use self::chat::ChatState;
51use self::space::{Space, SpaceState};
52
53mod chat;
54mod scrollback;
55mod space;
56
57macro_rules! delegate {
58 ($s: expr, $id: ident => $e: expr) => {
59 match $s {
60 RoomState::Chat($id) => $e,
61 RoomState::Space($id) => $e,
62 }
63 };
64}
65
66pub enum RoomState {
67 Chat(ChatState),
68 Space(SpaceState),
69}
70
71impl From<ChatState> for RoomState {
72 fn from(chat: ChatState) -> Self {
73 RoomState::Chat(chat)
74 }
75}
76
77impl From<SpaceState> for RoomState {
78 fn from(space: SpaceState) -> Self {
79 RoomState::Space(space)
80 }
81}
82
83impl RoomState {
84 pub fn new(room: MatrixRoom, name: DisplayName, store: &mut ProgramStore) -> Self {
85 let room_id = room.room_id().to_owned();
86 let info = store.application.get_room_info(room_id);
87 info.name = name.to_string().into();
88
89 if room.is_space() {
90 SpaceState::new(room).into()
91 } else {
92 ChatState::new(room, store).into()
93 }
94 }
95
96 pub async fn message_command(
97 &mut self,
98 act: MessageAction,
99 ctx: ProgramContext,
100 store: &mut ProgramStore,
101 ) -> IambResult<EditInfo> {
102 match self {
103 RoomState::Chat(chat) => chat.message_command(act, ctx, store).await,
104 RoomState::Space(_) => Err(IambError::NoSelectedMessage.into()),
105 }
106 }
107
108 pub async fn send_command(
109 &mut self,
110 act: SendAction,
111 ctx: ProgramContext,
112 store: &mut ProgramStore,
113 ) -> IambResult<EditInfo> {
114 match self {
115 RoomState::Chat(chat) => chat.send_command(act, ctx, store).await,
116 RoomState::Space(_) => Err(IambError::NoSelectedRoom.into()),
117 }
118 }
119
120 pub async fn room_command(
121 &mut self,
122 act: RoomAction,
123 _: ProgramContext,
124 store: &mut ProgramStore,
125 ) -> IambResult<Vec<(Action<IambInfo>, ProgramContext)>> {
126 match act {
127 RoomAction::Members(mut cmd) => {
128 let width = Count::Exact(30);
129 let act =
130 cmd.default_axis(Axis::Vertical).default_relation(MoveDir1D::Next).window(
131 OpenTarget::Application(IambId::MemberList(self.id().to_owned())),
132 width.into(),
133 );
134
135 Ok(vec![(act, cmd.context.take())])
136 },
137 RoomAction::Set(field) => {
138 store.application.worker.set_room(self.id().to_owned(), field)?;
139
140 Ok(vec![])
141 },
142 }
143 }
144
145 pub fn get_title(&self, store: &mut ProgramStore) -> Spans {
146 let title = store.application.get_room_title(self.id());
147 let style = Style::default().add_modifier(StyleModifier::BOLD);
148 let mut spans = vec![Span::styled(title, style)];
149
150 match self.room().topic() {
151 Some(desc) if !desc.is_empty() => {
152 spans.push(" (".into());
153 spans.push(desc.into());
154 spans.push(")".into());
155 },
156 _ => {},
157 }
158
159 Spans(spans)
160 }
161
162 pub fn focus_toggle(&mut self) {
163 match self {
164 RoomState::Chat(chat) => chat.focus_toggle(),
165 RoomState::Space(_) => return,
166 }
167 }
168
169 pub fn room(&self) -> &MatrixRoom {
170 match self {
171 RoomState::Chat(chat) => chat.room(),
172 RoomState::Space(space) => space.room(),
173 }
174 }
175
176 pub fn id(&self) -> &RoomId {
177 match self {
178 RoomState::Chat(chat) => chat.id(),
179 RoomState::Space(space) => space.id(),
180 }
181 }
182}
183
184impl Editable<ProgramContext, ProgramStore, IambInfo> for RoomState {
185 fn editor_command(
186 &mut self,
187 act: &EditorAction,
188 ctx: &ProgramContext,
189 store: &mut ProgramStore,
190 ) -> EditResult<EditInfo, IambInfo> {
191 delegate!(self, w => w.editor_command(act, ctx, store))
192 }
193}
194
195impl Jumpable<ProgramContext, IambInfo> for RoomState {
196 fn jump(
197 &mut self,
198 list: PositionList,
199 dir: MoveDir1D,
200 count: usize,
201 ctx: &ProgramContext,
202 ) -> IambResult<usize> {
203 delegate!(self, w => w.jump(list, dir, count, ctx))
204 }
205}
206
207impl Scrollable<ProgramContext, ProgramStore, IambInfo> for RoomState {
208 fn scroll(
209 &mut self,
210 style: &ScrollStyle,
211 ctx: &ProgramContext,
212 store: &mut ProgramStore,
213 ) -> EditResult<EditInfo, IambInfo> {
214 delegate!(self, w => w.scroll(style, ctx, store))
215 }
216}
217
218impl Promptable<ProgramContext, ProgramStore, IambInfo> for RoomState {
219 fn prompt(
220 &mut self,
221 act: &PromptAction,
222 ctx: &ProgramContext,
223 store: &mut ProgramStore,
224 ) -> EditResult<Vec<(ProgramAction, ProgramContext)>, IambInfo> {
225 delegate!(self, w => w.prompt(act, ctx, store))
226 }
227}
228
229impl TerminalCursor for RoomState {
230 fn get_term_cursor(&self) -> Option<TermOffset> {
231 delegate!(self, w => w.get_term_cursor())
232 }
233}
234
235impl WindowOps<IambInfo> for RoomState {
236 fn draw(&mut self, area: Rect, buf: &mut Buffer, focused: bool, store: &mut ProgramStore) {
237 match self {
238 RoomState::Chat(chat) => chat.draw(area, buf, focused, store),
239 RoomState::Space(space) => {
240 Space::new(store).focus(focused).render(area, buf, space);
241 },
242 }
243 }
244
245 fn dup(&self, store: &mut ProgramStore) -> Self {
246 match self {
247 RoomState::Chat(chat) => RoomState::Chat(chat.dup(store)),
248 RoomState::Space(space) => RoomState::Space(space.dup(store)),
249 }
250 }
251
252 fn close(&mut self, _: CloseFlags, _: &mut ProgramStore) -> bool {
253 // XXX: what's the right closing behaviour for a room?
254 // Should write send a message?
255 true
256 }
257
258 fn get_cursor_word(&self, style: &WordStyle) -> Option<String> {
259 match self {
260 RoomState::Chat(chat) => chat.get_cursor_word(style),
261 RoomState::Space(space) => space.get_cursor_word(style),
262 }
263 }
264
265 fn get_selected_word(&self) -> Option<String> {
266 match self {
267 RoomState::Chat(chat) => chat.get_selected_word(),
268 RoomState::Space(space) => space.get_selected_word(),
269 }
270 }
271}