src/windows/room/mod.rs
182 lines · 4940 bytes
1use matrix_sdk::room::Room as MatrixRoom;
2use matrix_sdk::ruma::RoomId;
3use matrix_sdk::DisplayName;
4
5use modalkit::tui::{buffer::Buffer, layout::Rect, widgets::StatefulWidget};
6
7use modalkit::{
8 editing::action::{
9 EditInfo,
10 EditResult,
11 Editable,
12 EditorAction,
13 Jumpable,
14 PromptAction,
15 Promptable,
16 Scrollable,
17 },
18 editing::base::{CloseFlags, MoveDir1D, PositionList, ScrollStyle, WordStyle},
19 widgets::{TermOffset, TerminalCursor, WindowOps},
20};
21
22use crate::base::{IambInfo, IambResult, ProgramAction, ProgramContext, ProgramStore};
23
24use self::chat::ChatState;
25use self::space::{Space, SpaceState};
26
27mod chat;
28mod scrollback;
29mod space;
30
31macro_rules! delegate {
32 ($s: expr, $id: ident => $e: expr) => {
33 match $s {
34 RoomState::Chat($id) => $e,
35 RoomState::Space($id) => $e,
36 }
37 };
38}
39
40pub enum RoomState {
41 Chat(ChatState),
42 Space(SpaceState),
43}
44
45impl From<ChatState> for RoomState {
46 fn from(chat: ChatState) -> Self {
47 RoomState::Chat(chat)
48 }
49}
50
51impl From<SpaceState> for RoomState {
52 fn from(space: SpaceState) -> Self {
53 RoomState::Space(space)
54 }
55}
56
57impl RoomState {
58 pub fn new(room: MatrixRoom, name: DisplayName, store: &mut ProgramStore) -> Self {
59 let room_id = room.room_id().to_owned();
60 let info = store.application.get_room_info(room_id);
61 info.name = name.to_string().into();
62
63 if room.is_space() {
64 SpaceState::new(room).into()
65 } else {
66 ChatState::new(room, store).into()
67 }
68 }
69
70 pub fn get_title(&self, store: &mut ProgramStore) -> String {
71 store
72 .application
73 .rooms
74 .get(self.id())
75 .and_then(|i| i.name.as_ref())
76 .map(String::from)
77 .unwrap_or_else(|| "Untitled Matrix Room".to_string())
78 }
79
80 pub fn focus_toggle(&mut self) {
81 match self {
82 RoomState::Chat(chat) => chat.focus_toggle(),
83 RoomState::Space(_) => return,
84 }
85 }
86
87 pub fn id(&self) -> &RoomId {
88 match self {
89 RoomState::Chat(chat) => chat.id(),
90 RoomState::Space(space) => space.id(),
91 }
92 }
93}
94
95impl Editable<ProgramContext, ProgramStore, IambInfo> for RoomState {
96 fn editor_command(
97 &mut self,
98 act: &EditorAction,
99 ctx: &ProgramContext,
100 store: &mut ProgramStore,
101 ) -> EditResult<EditInfo, IambInfo> {
102 delegate!(self, w => w.editor_command(act, ctx, store))
103 }
104}
105
106impl Jumpable<ProgramContext, IambInfo> for RoomState {
107 fn jump(
108 &mut self,
109 list: PositionList,
110 dir: MoveDir1D,
111 count: usize,
112 ctx: &ProgramContext,
113 ) -> IambResult<usize> {
114 delegate!(self, w => w.jump(list, dir, count, ctx))
115 }
116}
117
118impl Scrollable<ProgramContext, ProgramStore, IambInfo> for RoomState {
119 fn scroll(
120 &mut self,
121 style: &ScrollStyle,
122 ctx: &ProgramContext,
123 store: &mut ProgramStore,
124 ) -> EditResult<EditInfo, IambInfo> {
125 delegate!(self, w => w.scroll(style, ctx, store))
126 }
127}
128
129impl Promptable<ProgramContext, ProgramStore, IambInfo> for RoomState {
130 fn prompt(
131 &mut self,
132 act: &PromptAction,
133 ctx: &ProgramContext,
134 store: &mut ProgramStore,
135 ) -> EditResult<Vec<(ProgramAction, ProgramContext)>, IambInfo> {
136 delegate!(self, w => w.prompt(act, ctx, store))
137 }
138}
139
140impl TerminalCursor for RoomState {
141 fn get_term_cursor(&self) -> Option<TermOffset> {
142 delegate!(self, w => w.get_term_cursor())
143 }
144}
145
146impl WindowOps<IambInfo> for RoomState {
147 fn draw(&mut self, area: Rect, buf: &mut Buffer, focused: bool, store: &mut ProgramStore) {
148 match self {
149 RoomState::Chat(chat) => chat.draw(area, buf, focused, store),
150 RoomState::Space(space) => {
151 Space::new(store).focus(focused).render(area, buf, space);
152 },
153 }
154 }
155
156 fn dup(&self, store: &mut ProgramStore) -> Self {
157 match self {
158 RoomState::Chat(chat) => RoomState::Chat(chat.dup(store)),
159 RoomState::Space(space) => RoomState::Space(space.dup(store)),
160 }
161 }
162
163 fn close(&mut self, _: CloseFlags, _: &mut ProgramStore) -> bool {
164 // XXX: what's the right closing behaviour for a room?
165 // Should write send a message?
166 true
167 }
168
169 fn get_cursor_word(&self, style: &WordStyle) -> Option<String> {
170 match self {
171 RoomState::Chat(chat) => chat.get_cursor_word(style),
172 RoomState::Space(space) => space.get_cursor_word(style),
173 }
174 }
175
176 fn get_selected_word(&self) -> Option<String> {
177 match self {
178 RoomState::Chat(chat) => chat.get_selected_word(),
179 RoomState::Space(space) => space.get_selected_word(),
180 }
181 }
182}