src/windows/mod.rs
866 lines · 25912 bytes
1use std::cmp::{Ord, Ordering, PartialOrd};
2use std::collections::hash_map::Entry;
3
4use matrix_sdk::{
5 encryption::verification::{format_emojis, SasVerification},
6 room::{Room as MatrixRoom, RoomMember},
7 ruma::{events::room::member::MembershipState, OwnedRoomId, RoomId},
8 DisplayName,
9};
10
11use modalkit::tui::{
12 buffer::Buffer,
13 layout::{Alignment, Rect},
14 style::{Modifier as StyleModifier, Style},
15 text::{Span, Spans, Text},
16 widgets::{Block, Borders, StatefulWidget, Widget},
17};
18
19use modalkit::{
20 editing::{
21 action::{
22 Action,
23 EditError,
24 EditInfo,
25 EditResult,
26 Editable,
27 EditorAction,
28 Jumpable,
29 PromptAction,
30 Promptable,
31 Scrollable,
32 UIError,
33 WindowAction,
34 },
35 base::{
36 CloseFlags,
37 MoveDir1D,
38 OpenTarget,
39 PositionList,
40 ScrollStyle,
41 ViewportContext,
42 WordStyle,
43 },
44 },
45 widgets::{
46 list::{List, ListCursor, ListItem, ListState},
47 TermOffset,
48 TerminalCursor,
49 Window,
50 WindowOps,
51 },
52};
53
54use crate::{
55 base::{
56 ChatStore,
57 IambBufferId,
58 IambId,
59 IambInfo,
60 IambResult,
61 ProgramAction,
62 ProgramContext,
63 ProgramStore,
64 RoomAction,
65 },
66 message::user_style,
67};
68
69use self::{room::RoomState, welcome::WelcomeState};
70
71pub mod room;
72pub mod welcome;
73
74#[inline]
75fn selected_style(selected: bool) -> Style {
76 if selected {
77 Style::default().add_modifier(StyleModifier::REVERSED)
78 } else {
79 Style::default()
80 }
81}
82
83#[inline]
84fn selected_span(s: &str, selected: bool) -> Span {
85 Span::styled(s, selected_style(selected))
86}
87
88#[inline]
89fn selected_text(s: &str, selected: bool) -> Text {
90 Text::from(selected_span(s, selected))
91}
92
93#[inline]
94fn room_prompt(
95 room_id: &RoomId,
96 act: &PromptAction,
97 ctx: &ProgramContext,
98) -> EditResult<Vec<(ProgramAction, ProgramContext)>, IambInfo> {
99 match act {
100 PromptAction::Submit => {
101 let room = IambId::Room(room_id.to_owned());
102 let open = WindowAction::Switch(OpenTarget::Application(room));
103 let acts = vec![(open.into(), ctx.clone())];
104
105 Ok(acts)
106 },
107 PromptAction::Abort(_) => {
108 let msg = "Cannot abort entry inside a list";
109 let err = EditError::Failure(msg.into());
110
111 Err(err)
112 },
113 PromptAction::Recall(_, _) => {
114 let msg = "Cannot recall history inside a list";
115 let err = EditError::Failure(msg.into());
116
117 Err(err)
118 },
119 _ => Err(EditError::Unimplemented("unknown prompt action".to_string())),
120 }
121}
122
123macro_rules! delegate {
124 ($s: expr, $id: ident => $e: expr) => {
125 match $s {
126 IambWindow::Room($id) => $e,
127 IambWindow::DirectList($id) => $e,
128 IambWindow::MemberList($id, _) => $e,
129 IambWindow::RoomList($id) => $e,
130 IambWindow::SpaceList($id) => $e,
131 IambWindow::VerifyList($id) => $e,
132 IambWindow::Welcome($id) => $e,
133 }
134 };
135}
136
137pub enum IambWindow {
138 DirectList(DirectListState),
139 MemberList(MemberListState, OwnedRoomId),
140 Room(RoomState),
141 VerifyList(VerifyListState),
142 RoomList(RoomListState),
143 SpaceList(SpaceListState),
144 Welcome(WelcomeState),
145}
146
147impl IambWindow {
148 pub fn focus_toggle(&mut self) {
149 if let IambWindow::Room(w) = self {
150 w.focus_toggle()
151 } else {
152 return;
153 }
154 }
155
156 pub fn room_command(
157 &mut self,
158 act: RoomAction,
159 ctx: ProgramContext,
160 store: &mut ProgramStore,
161 ) -> IambResult<Vec<(Action<IambInfo>, ProgramContext)>> {
162 if let IambWindow::Room(w) = self {
163 w.room_command(act, ctx, store)
164 } else {
165 let msg = "No room currently focused!";
166 let err = UIError::Failure(msg.into());
167
168 return Err(err);
169 }
170 }
171
172 pub fn get_title(&self, store: &mut ProgramStore) -> String {
173 match self {
174 IambWindow::DirectList(_) => "Direct Messages".to_string(),
175 IambWindow::RoomList(_) => "Rooms".to_string(),
176 IambWindow::SpaceList(_) => "Spaces".to_string(),
177 IambWindow::VerifyList(_) => "Verifications".to_string(),
178 IambWindow::Welcome(_) => "Welcome to iamb".to_string(),
179
180 IambWindow::Room(w) => w.get_title(store),
181 IambWindow::MemberList(_, room_id) => {
182 let title = store.application.get_room_title(room_id.as_ref());
183 format!("Room Members: {}", title)
184 },
185 }
186 }
187}
188
189pub type DirectListState = ListState<DirectItem, IambInfo>;
190pub type MemberListState = ListState<MemberItem, IambInfo>;
191pub type RoomListState = ListState<RoomItem, IambInfo>;
192pub type SpaceListState = ListState<SpaceItem, IambInfo>;
193pub type VerifyListState = ListState<VerifyItem, IambInfo>;
194
195impl From<RoomState> for IambWindow {
196 fn from(room: RoomState) -> Self {
197 IambWindow::Room(room)
198 }
199}
200
201impl From<VerifyListState> for IambWindow {
202 fn from(list: VerifyListState) -> Self {
203 IambWindow::VerifyList(list)
204 }
205}
206
207impl From<DirectListState> for IambWindow {
208 fn from(list: DirectListState) -> Self {
209 IambWindow::DirectList(list)
210 }
211}
212
213impl From<RoomListState> for IambWindow {
214 fn from(list: RoomListState) -> Self {
215 IambWindow::RoomList(list)
216 }
217}
218
219impl From<SpaceListState> for IambWindow {
220 fn from(list: SpaceListState) -> Self {
221 IambWindow::SpaceList(list)
222 }
223}
224
225impl From<WelcomeState> for IambWindow {
226 fn from(win: WelcomeState) -> Self {
227 IambWindow::Welcome(win)
228 }
229}
230
231impl Editable<ProgramContext, ProgramStore, IambInfo> for IambWindow {
232 fn editor_command(
233 &mut self,
234 act: &EditorAction,
235 ctx: &ProgramContext,
236 store: &mut ProgramStore,
237 ) -> EditResult<EditInfo, IambInfo> {
238 delegate!(self, w => w.editor_command(act, ctx, store))
239 }
240}
241
242impl Jumpable<ProgramContext, IambInfo> for IambWindow {
243 fn jump(
244 &mut self,
245 list: PositionList,
246 dir: MoveDir1D,
247 count: usize,
248 ctx: &ProgramContext,
249 ) -> IambResult<usize> {
250 delegate!(self, w => w.jump(list, dir, count, ctx))
251 }
252}
253
254impl Scrollable<ProgramContext, ProgramStore, IambInfo> for IambWindow {
255 fn scroll(
256 &mut self,
257 style: &ScrollStyle,
258 ctx: &ProgramContext,
259 store: &mut ProgramStore,
260 ) -> EditResult<EditInfo, IambInfo> {
261 delegate!(self, w => w.scroll(style, ctx, store))
262 }
263}
264
265impl Promptable<ProgramContext, ProgramStore, IambInfo> for IambWindow {
266 fn prompt(
267 &mut self,
268 act: &PromptAction,
269 ctx: &ProgramContext,
270 store: &mut ProgramStore,
271 ) -> EditResult<Vec<(ProgramAction, ProgramContext)>, IambInfo> {
272 delegate!(self, w => w.prompt(act, ctx, store))
273 }
274}
275
276impl TerminalCursor for IambWindow {
277 fn get_term_cursor(&self) -> Option<TermOffset> {
278 delegate!(self, w => w.get_term_cursor())
279 }
280}
281
282impl WindowOps<IambInfo> for IambWindow {
283 fn draw(&mut self, area: Rect, buf: &mut Buffer, focused: bool, store: &mut ProgramStore) {
284 let title = self.get_title(store);
285 let block = Block::default().title(title.as_str()).borders(Borders::ALL);
286 let inner = block.inner(area);
287 block.render(area, buf);
288
289 match self {
290 IambWindow::Room(state) => state.draw(inner, buf, focused, store),
291 IambWindow::DirectList(state) => {
292 let dms = store.application.worker.direct_messages();
293 let items = dms.into_iter().map(|(id, name)| DirectItem::new(id, name, store));
294 state.set(items.collect());
295
296 List::new(store)
297 .empty_message("No direct messages yet!")
298 .empty_alignment(Alignment::Center)
299 .focus(focused)
300 .render(inner, buf, state);
301 },
302 IambWindow::MemberList(state, room_id) => {
303 if let Ok(mems) = store.application.worker.members(room_id.clone()) {
304 let items = mems.into_iter().map(MemberItem::new);
305 state.set(items.collect());
306 }
307
308 List::new(store)
309 .empty_message("No users here yet!")
310 .empty_alignment(Alignment::Center)
311 .focus(focused)
312 .render(inner, buf, state);
313 },
314 IambWindow::RoomList(state) => {
315 let joined = store.application.worker.joined_rooms();
316 let items = joined.into_iter().map(|(id, name)| RoomItem::new(id, name, store));
317 state.set(items.collect());
318
319 List::new(store)
320 .empty_message("You haven't joined any rooms yet")
321 .empty_alignment(Alignment::Center)
322 .focus(focused)
323 .render(inner, buf, state);
324 },
325 IambWindow::SpaceList(state) => {
326 let spaces = store.application.worker.spaces();
327 let items =
328 spaces.into_iter().map(|(room, name)| SpaceItem::new(room, name, store));
329 state.set(items.collect());
330 state.draw(inner, buf, focused, store);
331
332 List::new(store)
333 .empty_message("You haven't joined any spaces yet")
334 .empty_alignment(Alignment::Center)
335 .focus(focused)
336 .render(inner, buf, state);
337 },
338 IambWindow::VerifyList(state) => {
339 let verifications = &store.application.verifications;
340 let mut items = verifications.iter().map(VerifyItem::from).collect::<Vec<_>>();
341
342 // Sort the active verifications towards the top.
343 items.sort();
344
345 state.set(items);
346
347 List::new(store)
348 .empty_message("No in-progress verifications")
349 .empty_alignment(Alignment::Center)
350 .focus(focused)
351 .render(inner, buf, state);
352 },
353 IambWindow::Welcome(state) => state.draw(inner, buf, focused, store),
354 }
355 }
356
357 fn dup(&self, store: &mut ProgramStore) -> Self {
358 match self {
359 IambWindow::Room(w) => w.dup(store).into(),
360 IambWindow::DirectList(w) => w.dup(store).into(),
361 IambWindow::MemberList(w, room_id) => {
362 IambWindow::MemberList(w.dup(store), room_id.clone())
363 },
364 IambWindow::RoomList(w) => w.dup(store).into(),
365 IambWindow::SpaceList(w) => w.dup(store).into(),
366 IambWindow::VerifyList(w) => w.dup(store).into(),
367 IambWindow::Welcome(w) => w.dup(store).into(),
368 }
369 }
370
371 fn close(&mut self, flags: CloseFlags, store: &mut ProgramStore) -> bool {
372 delegate!(self, w => w.close(flags, store))
373 }
374
375 fn get_cursor_word(&self, style: &WordStyle) -> Option<String> {
376 delegate!(self, w => w.get_cursor_word(style))
377 }
378
379 fn get_selected_word(&self) -> Option<String> {
380 delegate!(self, w => w.get_selected_word())
381 }
382}
383
384impl Window<IambInfo> for IambWindow {
385 fn id(&self) -> IambId {
386 match self {
387 IambWindow::Room(room) => IambId::Room(room.id().to_owned()),
388 IambWindow::DirectList(_) => IambId::DirectList,
389 IambWindow::MemberList(_, room_id) => IambId::MemberList(room_id.clone()),
390 IambWindow::RoomList(_) => IambId::RoomList,
391 IambWindow::SpaceList(_) => IambId::SpaceList,
392 IambWindow::VerifyList(_) => IambId::VerifyList,
393 IambWindow::Welcome(_) => IambId::Welcome,
394 }
395 }
396
397 fn open(id: IambId, store: &mut ProgramStore) -> IambResult<Self> {
398 match id {
399 IambId::Room(room_id) => {
400 let (room, name) = store.application.worker.get_room(room_id)?;
401 let room = RoomState::new(room, name, store);
402
403 return Ok(room.into());
404 },
405 IambId::DirectList => {
406 let list = DirectListState::new(IambBufferId::DirectList, vec![]);
407
408 return Ok(list.into());
409 },
410 IambId::MemberList(room_id) => {
411 let id = IambBufferId::MemberList(room_id.clone());
412 let list = MemberListState::new(id, vec![]);
413 let win = IambWindow::MemberList(list, room_id);
414
415 return Ok(win);
416 },
417 IambId::RoomList => {
418 let list = RoomListState::new(IambBufferId::RoomList, vec![]);
419
420 return Ok(list.into());
421 },
422 IambId::SpaceList => {
423 let list = SpaceListState::new(IambBufferId::SpaceList, vec![]);
424
425 return Ok(list.into());
426 },
427 IambId::VerifyList => {
428 let list = VerifyListState::new(IambBufferId::VerifyList, vec![]);
429
430 return Ok(list.into());
431 },
432 IambId::Welcome => {
433 let win = WelcomeState::new(store);
434
435 return Ok(win.into());
436 },
437 }
438 }
439
440 fn find(name: String, store: &mut ProgramStore) -> IambResult<Self> {
441 let ChatStore { names, worker, .. } = &mut store.application;
442
443 match names.entry(name) {
444 Entry::Vacant(v) => {
445 let room_id = worker.join_room(v.key().to_string())?;
446 v.insert(room_id.clone());
447
448 let (room, name) = store.application.worker.get_room(room_id)?;
449 let room = RoomState::new(room, name, store);
450
451 Ok(room.into())
452 },
453 Entry::Occupied(o) => {
454 let id = IambId::Room(o.get().clone());
455
456 IambWindow::open(id, store)
457 },
458 }
459 }
460
461 fn posn(index: usize, _: &mut ProgramStore) -> IambResult<Self> {
462 let msg = format!("Cannot find indexed buffer (index = {})", index);
463 let err = UIError::Unimplemented(msg);
464
465 Err(err)
466 }
467}
468
469#[derive(Clone)]
470pub struct RoomItem {
471 room: MatrixRoom,
472 name: String,
473}
474
475impl RoomItem {
476 fn new(room: MatrixRoom, name: DisplayName, store: &mut ProgramStore) -> Self {
477 let name = name.to_string();
478
479 store.application.set_room_name(room.room_id(), name.as_str());
480
481 RoomItem { room, name }
482 }
483}
484
485impl ToString for RoomItem {
486 fn to_string(&self) -> String {
487 return self.name.clone();
488 }
489}
490
491impl ListItem<IambInfo> for RoomItem {
492 fn show(&self, selected: bool, _: &ViewportContext<ListCursor>, _: &mut ProgramStore) -> Text {
493 selected_text(self.name.as_str(), selected)
494 }
495
496 fn get_word(&self) -> Option<String> {
497 self.room.room_id().to_string().into()
498 }
499}
500
501impl Promptable<ProgramContext, ProgramStore, IambInfo> for RoomItem {
502 fn prompt(
503 &mut self,
504 act: &PromptAction,
505 ctx: &ProgramContext,
506 _: &mut ProgramStore,
507 ) -> EditResult<Vec<(ProgramAction, ProgramContext)>, IambInfo> {
508 room_prompt(self.room.room_id(), act, ctx)
509 }
510}
511
512#[derive(Clone)]
513pub struct DirectItem {
514 room: MatrixRoom,
515 name: String,
516}
517
518impl DirectItem {
519 fn new(room: MatrixRoom, name: DisplayName, store: &mut ProgramStore) -> Self {
520 let name = name.to_string();
521
522 store.application.set_room_name(room.room_id(), name.as_str());
523
524 DirectItem { room, name }
525 }
526}
527
528impl ToString for DirectItem {
529 fn to_string(&self) -> String {
530 return self.name.clone();
531 }
532}
533
534impl ListItem<IambInfo> for DirectItem {
535 fn show(&self, selected: bool, _: &ViewportContext<ListCursor>, _: &mut ProgramStore) -> Text {
536 selected_text(self.name.as_str(), selected)
537 }
538
539 fn get_word(&self) -> Option<String> {
540 self.room.room_id().to_string().into()
541 }
542}
543
544impl Promptable<ProgramContext, ProgramStore, IambInfo> for DirectItem {
545 fn prompt(
546 &mut self,
547 act: &PromptAction,
548 ctx: &ProgramContext,
549 _: &mut ProgramStore,
550 ) -> EditResult<Vec<(ProgramAction, ProgramContext)>, IambInfo> {
551 room_prompt(self.room.room_id(), act, ctx)
552 }
553}
554
555#[derive(Clone)]
556pub struct SpaceItem {
557 room: MatrixRoom,
558 name: String,
559}
560
561impl SpaceItem {
562 fn new(room: MatrixRoom, name: DisplayName, store: &mut ProgramStore) -> Self {
563 let name = name.to_string();
564
565 store.application.set_room_name(room.room_id(), name.as_str());
566
567 SpaceItem { room, name }
568 }
569}
570
571impl ToString for SpaceItem {
572 fn to_string(&self) -> String {
573 return self.room.room_id().to_string();
574 }
575}
576
577impl ListItem<IambInfo> for SpaceItem {
578 fn show(&self, selected: bool, _: &ViewportContext<ListCursor>, _: &mut ProgramStore) -> Text {
579 selected_text(self.name.as_str(), selected)
580 }
581
582 fn get_word(&self) -> Option<String> {
583 self.room.room_id().to_string().into()
584 }
585}
586
587impl Promptable<ProgramContext, ProgramStore, IambInfo> for SpaceItem {
588 fn prompt(
589 &mut self,
590 act: &PromptAction,
591 ctx: &ProgramContext,
592 _: &mut ProgramStore,
593 ) -> EditResult<Vec<(ProgramAction, ProgramContext)>, IambInfo> {
594 room_prompt(self.room.room_id(), act, ctx)
595 }
596}
597
598#[derive(Clone)]
599pub struct VerifyItem {
600 user_dev: String,
601 sasv1: SasVerification,
602}
603
604impl VerifyItem {
605 fn new(user_dev: String, sasv1: SasVerification) -> Self {
606 VerifyItem { user_dev, sasv1 }
607 }
608
609 fn show_item(&self) -> String {
610 let state = if self.sasv1.is_done() {
611 "done"
612 } else if self.sasv1.is_cancelled() {
613 "cancelled"
614 } else if self.sasv1.emoji().is_some() {
615 "accepted"
616 } else {
617 "not accepted"
618 };
619
620 if self.sasv1.is_self_verification() {
621 let device = self.sasv1.other_device();
622
623 if let Some(display_name) = device.display_name() {
624 format!("Device verification with {} ({})", display_name, state)
625 } else {
626 format!("Device verification with device {} ({})", device.device_id(), state)
627 }
628 } else {
629 format!("User Verification with {} ({})", self.sasv1.other_user_id(), state)
630 }
631 }
632}
633
634impl PartialEq for VerifyItem {
635 fn eq(&self, other: &Self) -> bool {
636 self.user_dev == other.user_dev
637 }
638}
639
640impl Eq for VerifyItem {}
641
642impl Ord for VerifyItem {
643 fn cmp(&self, other: &Self) -> Ordering {
644 fn state_val(sas: &SasVerification) -> usize {
645 if sas.is_done() {
646 return 3;
647 } else if sas.is_cancelled() {
648 return 2;
649 } else {
650 return 1;
651 }
652 }
653
654 fn device_val(sas: &SasVerification) -> usize {
655 if sas.is_self_verification() {
656 return 1;
657 } else {
658 return 2;
659 }
660 }
661
662 let state1 = state_val(&self.sasv1);
663 let state2 = state_val(&other.sasv1);
664
665 let dev1 = device_val(&self.sasv1);
666 let dev2 = device_val(&other.sasv1);
667
668 let scmp = state1.cmp(&state2);
669 let dcmp = dev1.cmp(&dev2);
670
671 scmp.then(dcmp).then_with(|| {
672 let did1 = self.sasv1.other_device().device_id();
673 let did2 = other.sasv1.other_device().device_id();
674
675 did1.cmp(did2)
676 })
677 }
678}
679
680impl PartialOrd for VerifyItem {
681 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
682 self.cmp(other).into()
683 }
684}
685
686impl From<(&String, &SasVerification)> for VerifyItem {
687 fn from((user_dev, sasv1): (&String, &SasVerification)) -> Self {
688 VerifyItem::new(user_dev.clone(), sasv1.clone())
689 }
690}
691
692impl ToString for VerifyItem {
693 fn to_string(&self) -> String {
694 if self.sasv1.is_done() {
695 String::new()
696 } else if self.sasv1.is_cancelled() {
697 format!(":verify request {}", self.sasv1.other_user_id())
698 } else if self.sasv1.emoji().is_some() {
699 format!(":verify confirm {}", self.user_dev)
700 } else {
701 format!(":verify accept {}", self.user_dev)
702 }
703 }
704}
705
706impl ListItem<IambInfo> for VerifyItem {
707 fn show(&self, selected: bool, _: &ViewportContext<ListCursor>, _: &mut ProgramStore) -> Text {
708 let mut lines = vec![];
709
710 let bold = Style::default().add_modifier(StyleModifier::BOLD);
711 let item = Span::styled(self.show_item(), selected_style(selected));
712 lines.push(Spans::from(item));
713
714 if self.sasv1.is_done() {
715 // Print nothing.
716 } else if self.sasv1.is_cancelled() {
717 if let Some(info) = self.sasv1.cancel_info() {
718 lines.push(Spans::from(format!(" Cancelled: {}", info.reason())));
719 lines.push(Spans::from(""));
720 }
721
722 lines.push(Spans::from(" You can start a new verification request with:"));
723 } else if let Some(emoji) = self.sasv1.emoji() {
724 lines.push(Spans::from(
725 " Both devices should see the following Emoji sequence:".to_string(),
726 ));
727 lines.push(Spans::from(""));
728
729 for line in format_emojis(emoji).lines() {
730 lines.push(Spans::from(format!(" {}", line)));
731 }
732
733 lines.push(Spans::from(""));
734 lines.push(Spans::from(" If they don't match, run:"));
735 lines.push(Spans::from(""));
736 lines.push(Spans::from(Span::styled(
737 format!(":verify mismatch {}", self.user_dev),
738 bold,
739 )));
740 lines.push(Spans::from(""));
741 lines.push(Spans::from(" If everything looks right, you can confirm with:"));
742 } else {
743 lines.push(Spans::from(" To accept this request, run:"));
744 }
745
746 let cmd = self.to_string();
747
748 if !cmd.is_empty() {
749 lines.push(Spans::from(""));
750 lines.push(Spans(vec![Span::from(" "), Span::styled(cmd, bold)]));
751 lines.push(Spans::from(""));
752 lines.push(Spans(vec![
753 Span::from("You can copy the above command with "),
754 Span::styled("yy", bold),
755 Span::from(" and then execute it with "),
756 Span::styled("@\"", bold),
757 ]));
758 }
759
760 Text { lines }
761 }
762
763 fn get_word(&self) -> Option<String> {
764 None
765 }
766}
767
768impl Promptable<ProgramContext, ProgramStore, IambInfo> for VerifyItem {
769 fn prompt(
770 &mut self,
771 act: &PromptAction,
772 _: &ProgramContext,
773 _: &mut ProgramStore,
774 ) -> EditResult<Vec<(ProgramAction, ProgramContext)>, IambInfo> {
775 match act {
776 PromptAction::Submit => Ok(vec![]),
777 PromptAction::Abort(_) => {
778 let msg = "Cannot abort entry inside a list";
779 let err = EditError::Failure(msg.into());
780
781 Err(err)
782 },
783 PromptAction::Recall(_, _) => {
784 let msg = "Cannot recall history inside a list";
785 let err = EditError::Failure(msg.into());
786
787 Err(err)
788 },
789 _ => Err(EditError::Unimplemented("unknown prompt action".to_string())),
790 }
791 }
792}
793
794#[derive(Clone)]
795pub struct MemberItem {
796 member: RoomMember,
797}
798
799impl MemberItem {
800 fn new(member: RoomMember) -> Self {
801 Self { member }
802 }
803}
804
805impl ToString for MemberItem {
806 fn to_string(&self) -> String {
807 self.member.user_id().to_string()
808 }
809}
810
811impl ListItem<IambInfo> for MemberItem {
812 fn show(&self, selected: bool, _: &ViewportContext<ListCursor>, _: &mut ProgramStore) -> Text {
813 let mut style = user_style(self.member.user_id().as_str());
814
815 if selected {
816 style = style.add_modifier(StyleModifier::REVERSED);
817 }
818
819 let user = Span::styled(self.to_string(), style);
820
821 let state = match self.member.membership() {
822 MembershipState::Ban => Span::raw(" (banned)").into(),
823 MembershipState::Invite => Span::raw(" (invited)").into(),
824 MembershipState::Knock => Span::raw(" (wants to join)").into(),
825 MembershipState::Leave => Span::raw(" (left)").into(),
826 MembershipState::Join => None,
827 _ => None,
828 };
829
830 if let Some(state) = state {
831 Spans(vec![user, state]).into()
832 } else {
833 user.into()
834 }
835 }
836
837 fn get_word(&self) -> Option<String> {
838 self.member.user_id().to_string().into()
839 }
840}
841
842impl Promptable<ProgramContext, ProgramStore, IambInfo> for MemberItem {
843 fn prompt(
844 &mut self,
845 act: &PromptAction,
846 _: &ProgramContext,
847 _: &mut ProgramStore,
848 ) -> EditResult<Vec<(ProgramAction, ProgramContext)>, IambInfo> {
849 match act {
850 PromptAction::Submit => Ok(vec![]),
851 PromptAction::Abort(_) => {
852 let msg = "Cannot abort entry inside a list";
853 let err = EditError::Failure(msg.into());
854
855 Err(err)
856 },
857 PromptAction::Recall(_, _) => {
858 let msg = "Cannot recall history inside a list";
859 let err = EditError::Failure(msg.into());
860
861 Err(err)
862 },
863 _ => Err(EditError::Unimplemented("unknown prompt action".to_string())),
864 }
865 }
866}