|
| 1 | +use std::time::{Duration, Instant}; |
| 2 | + |
| 3 | +use modalkit::{ |
| 4 | + actions::{Editable, EditorAction, Jumpable, PromptAction, Promptable, Scrollable}, |
| 5 | + editing::completion::CompletionList, |
| 6 | + errors::EditResult, |
| 7 | + prelude::*, |
| 8 | +}; |
| 9 | +use modalkit_ratatui::{ |
| 10 | + list::{List, ListState}, |
| 11 | + TermOffset, |
| 12 | + TerminalCursor, |
| 13 | + WindowOps, |
| 14 | +}; |
| 15 | +use ratatui::{ |
| 16 | + buffer::Buffer, |
| 17 | + layout::Rect, |
| 18 | + style::{Color, Style}, |
| 19 | + text::{Line, Span, Text}, |
| 20 | + widgets::StatefulWidget, |
| 21 | +}; |
| 22 | + |
| 23 | +use crate::base::{ |
| 24 | + IambBufferId, |
| 25 | + IambInfo, |
| 26 | + IambResult, |
| 27 | + ProgramAction, |
| 28 | + ProgramContext, |
| 29 | + ProgramStore, |
| 30 | +}; |
| 31 | + |
| 32 | +use crate::windows::RoomItem; |
| 33 | + |
| 34 | +use super::room_fields_cmp; |
| 35 | + |
| 36 | +const SPACE_HIERARCHY_DEBOUNCE: Duration = Duration::from_secs(15); |
| 37 | + |
| 38 | +/// [StatefulWidget] for Matrix space tree. |
| 39 | +pub struct SpaceTree<'a> { |
| 40 | + focused: bool, |
| 41 | + store: &'a mut ProgramStore, |
| 42 | +} |
| 43 | + |
| 44 | +impl<'a> SpaceTree<'a> { |
| 45 | + pub fn new(store: &'a mut ProgramStore) -> Self { |
| 46 | + SpaceTree { focused: false, store } |
| 47 | + } |
| 48 | + |
| 49 | + pub fn focus(mut self, focused: bool) -> Self { |
| 50 | + self.focused = focused; |
| 51 | + self |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +impl StatefulWidget for SpaceTree<'_> { |
| 56 | + type State = SpaceTreeState; |
| 57 | + |
| 58 | + fn render(self, area: Rect, buffer: &mut Buffer, state: &mut Self::State) { |
| 59 | + let mut empty_message = None; |
| 60 | + let need_fetch = match state.last_fetch { |
| 61 | + Some(i) => i.elapsed() >= SPACE_HIERARCHY_DEBOUNCE, |
| 62 | + None => true, |
| 63 | + }; |
| 64 | + |
| 65 | + if need_fetch { |
| 66 | + let mut children = vec![]; |
| 67 | + let res = self.store.application.sync_info.spaces.iter().try_for_each(|room| { |
| 68 | + let id = room.0.room_id(); |
| 69 | + let res = self.store.application.worker.space_members(id.to_owned()); |
| 70 | + |
| 71 | + res.map(|members| children.extend(members.into_iter().filter(|child| child != id))) |
| 72 | + }); |
| 73 | + |
| 74 | + if let Err(e) = res { |
| 75 | + let lines = vec![ |
| 76 | + Line::from("Unable to fetch space room hierarchy:"), |
| 77 | + Span::styled(e.to_string(), Style::default().fg(Color::Red)).into(), |
| 78 | + ]; |
| 79 | + |
| 80 | + empty_message = Text::from(lines).into(); |
| 81 | + } else { |
| 82 | + let mut items = self |
| 83 | + .store |
| 84 | + .application |
| 85 | + .sync_info |
| 86 | + .spaces |
| 87 | + .clone() |
| 88 | + .into_iter() |
| 89 | + .filter(|space| !children.contains(&space.0.room_id().to_owned())) |
| 90 | + .map(|room| RoomItem::new(room, self.store)) |
| 91 | + .collect::<Vec<_>>(); |
| 92 | + let fields = &self.store.application.settings.tunables.sort.spaces; |
| 93 | + let collator = &mut self.store.application.collator; |
| 94 | + items.sort_by(|a, b| room_fields_cmp(a, b, fields, collator)); |
| 95 | + |
| 96 | + state.list.set(items); |
| 97 | + state.last_fetch = Some(Instant::now()); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + let mut list = List::new(self.store).focus(self.focused); |
| 102 | + |
| 103 | + if let Some(text) = empty_message { |
| 104 | + list = list.empty_message(text); |
| 105 | + } else { |
| 106 | + list = list.empty_message(Text::from("You haven't joined any spaces yet")); |
| 107 | + } |
| 108 | + |
| 109 | + list.render(area, buffer, &mut state.list) |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +/// State for the list of toplevel spaces |
| 114 | +pub struct SpaceTreeState { |
| 115 | + list: ListState<RoomItem, IambInfo>, |
| 116 | + last_fetch: Option<Instant>, |
| 117 | +} |
| 118 | + |
| 119 | +impl SpaceTreeState { |
| 120 | + pub fn new() -> Self { |
| 121 | + let content = IambBufferId::SpaceTree; |
| 122 | + let list = ListState::new(content, vec![]); |
| 123 | + |
| 124 | + SpaceTreeState { list, last_fetch: None } |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +impl Editable<ProgramContext, ProgramStore, IambInfo> for SpaceTreeState { |
| 129 | + fn editor_command( |
| 130 | + &mut self, |
| 131 | + act: &EditorAction, |
| 132 | + ctx: &ProgramContext, |
| 133 | + store: &mut ProgramStore, |
| 134 | + ) -> EditResult<EditInfo, IambInfo> { |
| 135 | + self.list.editor_command(act, ctx, store) |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +impl Jumpable<ProgramContext, IambInfo> for SpaceTreeState { |
| 140 | + fn jump( |
| 141 | + &mut self, |
| 142 | + list: PositionList, |
| 143 | + dir: MoveDir1D, |
| 144 | + count: usize, |
| 145 | + ctx: &ProgramContext, |
| 146 | + ) -> IambResult<usize> { |
| 147 | + self.list.jump(list, dir, count, ctx) |
| 148 | + } |
| 149 | +} |
| 150 | + |
| 151 | +impl Scrollable<ProgramContext, ProgramStore, IambInfo> for SpaceTreeState { |
| 152 | + fn scroll( |
| 153 | + &mut self, |
| 154 | + style: &ScrollStyle, |
| 155 | + ctx: &ProgramContext, |
| 156 | + store: &mut ProgramStore, |
| 157 | + ) -> EditResult<EditInfo, IambInfo> { |
| 158 | + self.list.scroll(style, ctx, store) |
| 159 | + } |
| 160 | +} |
| 161 | + |
| 162 | +impl Promptable<ProgramContext, ProgramStore, IambInfo> for SpaceTreeState { |
| 163 | + fn prompt( |
| 164 | + &mut self, |
| 165 | + act: &PromptAction, |
| 166 | + ctx: &ProgramContext, |
| 167 | + store: &mut ProgramStore, |
| 168 | + ) -> EditResult<Vec<(ProgramAction, ProgramContext)>, IambInfo> { |
| 169 | + self.list.prompt(act, ctx, store) |
| 170 | + } |
| 171 | +} |
| 172 | + |
| 173 | +impl TerminalCursor for SpaceTreeState { |
| 174 | + fn get_term_cursor(&self) -> Option<TermOffset> { |
| 175 | + self.list.get_term_cursor() |
| 176 | + } |
| 177 | +} |
| 178 | + |
| 179 | +impl WindowOps<IambInfo> for SpaceTreeState { |
| 180 | + fn draw(&mut self, area: Rect, buf: &mut Buffer, focused: bool, store: &mut ProgramStore) { |
| 181 | + SpaceTree::new(store).focus(focused).render(area, buf, self); |
| 182 | + } |
| 183 | + |
| 184 | + fn dup(&self, store: &mut ProgramStore) -> Self { |
| 185 | + SpaceTreeState { |
| 186 | + list: self.list.dup(store), |
| 187 | + last_fetch: self.last_fetch, |
| 188 | + } |
| 189 | + } |
| 190 | + |
| 191 | + fn close(&mut self, flags: CloseFlags, store: &mut ProgramStore) -> bool { |
| 192 | + self.list.close(flags, store) |
| 193 | + } |
| 194 | + |
| 195 | + fn get_completions(&self) -> Option<CompletionList> { |
| 196 | + self.list.get_completions() |
| 197 | + } |
| 198 | + |
| 199 | + fn get_cursor_word(&self, style: &WordStyle) -> Option<String> { |
| 200 | + self.list.get_cursor_word(style) |
| 201 | + } |
| 202 | + |
| 203 | + fn get_selected_word(&self) -> Option<String> { |
| 204 | + self.list.get_selected_word() |
| 205 | + } |
| 206 | + |
| 207 | + fn write( |
| 208 | + &mut self, |
| 209 | + path: Option<&str>, |
| 210 | + flags: WriteFlags, |
| 211 | + store: &mut ProgramStore, |
| 212 | + ) -> IambResult<EditInfo> { |
| 213 | + self.list.write(path, flags, store) |
| 214 | + } |
| 215 | +} |
0 commit comments