src/windows/room/mod.rs
215 lines · 5612 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 Action,
10 EditInfo,
11 EditResult,
12 Editable,
13 EditorAction,
14 Jumpable,
15 PromptAction,
16 Promptable,
17 Scrollable,
18 },
19 editing::base::{
20 Axis,
21 CloseFlags,
22 Count,
23 MoveDir1D,
24 OpenTarget,
25 PositionList,
26 ScrollStyle,
27 WordStyle,
28 },
29 input::InputContext,
30 widgets::{TermOffset, TerminalCursor, WindowOps},
31};
32
33use crate::base::{
34 IambId,
35 IambInfo,
36 IambResult,
37 ProgramAction,
38 ProgramContext,
39 ProgramStore,
40 RoomAction,
41};
42
43use self::chat::ChatState;
44use self::space::{Space, SpaceState};
45
46mod chat;
47mod scrollback;
48mod space;
49
50macro_rules! delegate {
51 ($s: expr, $id: ident => $e: expr) => {
52 match $s {
53 RoomState::Chat($id) => $e,
54 RoomState::Space($id) => $e,
55 }
56 };
57}
58
59pub enum RoomState {
60 Chat(ChatState),
61 Space(SpaceState),
62}
63
64impl From<ChatState> for RoomState {
65 fn from(chat: ChatState) -> Self {
66 RoomState::Chat(chat)
67 }
68}
69
70impl From<SpaceState> for RoomState {
71 fn from(space: SpaceState) -> Self {
72 RoomState::Space(space)
73 }
74}
75
76impl RoomState {
77 pub fn new(room: MatrixRoom, name: DisplayName, store: &mut ProgramStore) -> Self {
78 let room_id = room.room_id().to_owned();
79 let info = store.application.get_room_info(room_id);
80 info.name = name.to_string().into();
81
82 if room.is_space() {
83 SpaceState::new(room).into()
84 } else {
85 ChatState::new(room, store).into()
86 }
87 }
88
89 pub fn room_command(
90 &mut self,
91 act: RoomAction,
92 _: ProgramContext,
93 _: &mut ProgramStore,
94 ) -> IambResult<Vec<(Action<IambInfo>, ProgramContext)>> {
95 match act {
96 RoomAction::Members(mut cmd) => {
97 let width = Count::Exact(30);
98 let act =
99 cmd.default_axis(Axis::Vertical).default_relation(MoveDir1D::Next).window(
100 OpenTarget::Application(IambId::MemberList(self.id().to_owned())),
101 width.into(),
102 );
103
104 Ok(vec![(act, cmd.context.take())])
105 },
106 }
107 }
108
109 pub fn get_title(&self, store: &mut ProgramStore) -> String {
110 store.application.get_room_title(self.id())
111 }
112
113 pub fn focus_toggle(&mut self) {
114 match self {
115 RoomState::Chat(chat) => chat.focus_toggle(),
116 RoomState::Space(_) => return,
117 }
118 }
119
120 pub fn id(&self) -> &RoomId {
121 match self {
122 RoomState::Chat(chat) => chat.id(),
123 RoomState::Space(space) => space.id(),
124 }
125 }
126}
127
128impl Editable<ProgramContext, ProgramStore, IambInfo> for RoomState {
129 fn editor_command(
130 &mut self,
131 act: &EditorAction,
132 ctx: &ProgramContext,
133 store: &mut ProgramStore,
134 ) -> EditResult<EditInfo, IambInfo> {
135 delegate!(self, w => w.editor_command(act, ctx, store))
136 }
137}
138
139impl Jumpable<ProgramContext, IambInfo> for RoomState {
140 fn jump(
141 &mut self,
142 list: PositionList,
143 dir: MoveDir1D,
144 count: usize,
145 ctx: &ProgramContext,
146 ) -> IambResult<usize> {
147 delegate!(self, w => w.jump(list, dir, count, ctx))
148 }
149}
150
151impl Scrollable<ProgramContext, ProgramStore, IambInfo> for RoomState {
152 fn scroll(
153 &mut self,
154 style: &ScrollStyle,
155 ctx: &ProgramContext,
156 store: &mut ProgramStore,
157 ) -> EditResult<EditInfo, IambInfo> {
158 delegate!(self, w => w.scroll(style, ctx, store))
159 }
160}
161
162impl Promptable<ProgramContext, ProgramStore, IambInfo> for RoomState {
163 fn prompt(
164 &mut self,
165 act: &PromptAction,
166 ctx: &ProgramContext,
167 store: &mut ProgramStore,
168 ) -> EditResult<Vec<(ProgramAction, ProgramContext)>, IambInfo> {
169 delegate!(self, w => w.prompt(act, ctx, store))
170 }
171}
172
173impl TerminalCursor for RoomState {
174 fn get_term_cursor(&self) -> Option<TermOffset> {
175 delegate!(self, w => w.get_term_cursor())
176 }
177}
178
179impl WindowOps<IambInfo> for RoomState {
180 fn draw(&mut self, area: Rect, buf: &mut Buffer, focused: bool, store: &mut ProgramStore) {
181 match self {
182 RoomState::Chat(chat) => chat.draw(area, buf, focused, store),
183 RoomState::Space(space) => {
184 Space::new(store).focus(focused).render(area, buf, space);
185 },
186 }
187 }
188
189 fn dup(&self, store: &mut ProgramStore) -> Self {
190 match self {
191 RoomState::Chat(chat) => RoomState::Chat(chat.dup(store)),
192 RoomState::Space(space) => RoomState::Space(space.dup(store)),
193 }
194 }
195
196 fn close(&mut self, _: CloseFlags, _: &mut ProgramStore) -> bool {
197 // XXX: what's the right closing behaviour for a room?
198 // Should write send a message?
199 true
200 }
201
202 fn get_cursor_word(&self, style: &WordStyle) -> Option<String> {
203 match self {
204 RoomState::Chat(chat) => chat.get_cursor_word(style),
205 RoomState::Space(space) => space.get_cursor_word(style),
206 }
207 }
208
209 fn get_selected_word(&self) -> Option<String> {
210 match self {
211 RoomState::Chat(chat) => chat.get_selected_word(),
212 RoomState::Space(space) => space.get_selected_word(),
213 }
214 }
215}