src/windows/room/mod.rs

246 lines · 6465 bytes

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