src/windows/room/not_joined.rs
100 lines · 2513 bytes
1use crate::prelude::*;
2
3#[derive(Clone)]
4pub struct NotJoinedState {
5 pub(super) room: String,
6 err: Option<String>,
7}
8
9impl NotJoinedState {
10 /// Create a window for a room the user hasn't yet joined.
11 pub fn new(room: String) -> Self {
12 Self { room, err: None }
13 }
14
15 /// Create a window for a room whose `:join` command failed.
16 pub fn failed(room: String, err: &UIError<IambInfo>) -> Self {
17 Self { room, err: Some(err.to_string()) }
18 }
19}
20
21impl Editable<ProgramContext, ProgramStore, IambInfo> for NotJoinedState {
22 fn editor_command(
23 &mut self,
24 _: &EditorAction,
25 _: &ProgramContext,
26 _: &mut ProgramStore,
27 ) -> EditResult<EditInfo, IambInfo> {
28 Ok(None)
29 }
30}
31
32impl Jumpable<ProgramContext, IambInfo> for NotJoinedState {
33 fn jump(
34 &mut self,
35 _: PositionList,
36 _: MoveDir1D,
37 count: usize,
38 _: &ProgramContext,
39 ) -> IambResult<usize> {
40 Ok(count)
41 }
42}
43
44impl Scrollable<ProgramContext, ProgramStore, IambInfo> for NotJoinedState {
45 fn scroll(
46 &mut self,
47 _: &ScrollStyle,
48 _: &ProgramContext,
49 _: &mut ProgramStore,
50 ) -> EditResult<EditInfo, IambInfo> {
51 Ok(None)
52 }
53}
54
55impl Promptable<ProgramContext, ProgramStore, IambInfo> for NotJoinedState {
56 fn prompt(
57 &mut self,
58 _: &PromptAction,
59 _: &ProgramContext,
60 _: &mut ProgramStore,
61 ) -> EditResult<Vec<(ProgramAction, ProgramContext)>, IambInfo> {
62 Ok(vec![])
63 }
64}
65
66impl TerminalCursor for NotJoinedState {
67 fn get_term_cursor(&self) -> Option<TermOffset> {
68 None
69 }
70
71 fn hide_term_cursor(&self) -> bool {
72 true
73 }
74}
75
76/// [StatefulWidget] for Matrix rooms that haven't been joined.
77pub struct NotJoined;
78
79impl StatefulWidget for NotJoined {
80 type State = NotJoinedState;
81
82 fn render(self, area: Rect, buffer: &mut Buffer, state: &mut Self::State) {
83 let mut lines = vec![];
84
85 lines.push(Line::from(vec![
86 Span::raw("Use "),
87 Span::styled(format!("`:join {}`", state.room), StyleModifier::BOLD),
88 Span::raw(" to join this room"),
89 ]));
90
91 if let Some(err) = &state.err {
92 let style = Style::default().fg(Color::Red);
93 lines.push(Line::from(Span::styled(err.as_str(), style)));
94 }
95
96 Paragraph::new(Text::from(lines))
97 .alignment(Alignment::Center)
98 .render(area, buffer);
99 }
100}