| @@ -215,28 +215,34 @@ fn user_fields_cmp( |
| 215 | 215 | user_cmp(a, b, &SortFieldUser::UserId) |
| 216 | 216 | } |
| 217 | 217 | |
| 218 | | fn append_tags<'a>(tags: &'a Tags, spans: &mut Vec<Span<'a>>, style: Style) { |
| 218 | fn tag_to_span(tag: &TagName, style: Style) -> Vec<Span<'_>> { |
| 219 | match tag { |
| 220 | TagName::Favorite => vec![Span::styled("Favorite", style)], |
| 221 | TagName::LowPriority => vec![Span::styled("Low Priority", style)], |
| 222 | TagName::ServerNotice => vec![Span::styled("Server Notice", style)], |
| 223 | TagName::User(tag) => { |
| 224 | vec![ |
| 225 | Span::styled("User Tag: ", style), |
| 226 | Span::styled(tag.as_ref(), style), |
| 227 | ] |
| 228 | }, |
| 229 | tag => vec![Span::styled(format!("{tag:?}"), style)], |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | fn append_tags<'a>(tags: Vec<Vec<Span<'a>>>, spans: &mut Vec<Span<'a>>, style: Style) { |
| 219 | 234 | if tags.is_empty() { |
| 220 | 235 | return; |
| 221 | 236 | } |
| 222 | 237 | |
| 223 | 238 | spans.push(Span::styled(" (", style)); |
| 224 | 239 | |
| 225 | | for (i, tag) in tags.keys().enumerate() { |
| 240 | for (i, tag) in tags.into_iter().enumerate() { |
| 226 | 241 | if i > 0 { |
| 227 | 242 | spans.push(Span::styled(", ", style)); |
| 228 | 243 | } |
| 229 | 244 | |
| 230 | | match tag { |
| 231 | | TagName::Favorite => spans.push(Span::styled("Favorite", style)), |
| 232 | | TagName::LowPriority => spans.push(Span::styled("Low Priority", style)), |
| 233 | | TagName::ServerNotice => spans.push(Span::styled("Server Notice", style)), |
| 234 | | TagName::User(tag) => { |
| 235 | | spans.push(Span::styled("User Tag: ", style)); |
| 236 | | spans.push(Span::styled(tag.as_ref(), style)); |
| 237 | | }, |
| 238 | | tag => spans.push(Span::styled(format!("{tag:?}"), style)), |
| 239 | | } |
| 245 | spans.extend(tag); |
| 240 | 246 | } |
| 241 | 247 | |
| 242 | 248 | spans.push(Span::styled(")", style)); |
| @@ -289,6 +295,7 @@ macro_rules! delegate { |
| 289 | 295 | IambWindow::SpaceList($id) => $e, |
| 290 | 296 | IambWindow::VerifyList($id) => $e, |
| 291 | 297 | IambWindow::Welcome($id) => $e, |
| 298 | IambWindow::ChatList($id) => $e, |
| 292 | 299 | } |
| 293 | 300 | }; |
| 294 | 301 | } |
| @@ -301,6 +308,7 @@ pub enum IambWindow { |
| 301 | 308 | RoomList(RoomListState), |
| 302 | 309 | SpaceList(SpaceListState), |
| 303 | 310 | Welcome(WelcomeState), |
| 311 | ChatList(ChatListState), |
| 304 | 312 | } |
| 305 | 313 | |
| 306 | 314 | impl IambWindow { |
| @@ -355,9 +363,16 @@ impl IambWindow { |
| 355 | 363 | pub type DirectListState = ListState<DirectItem, IambInfo>; |
| 356 | 364 | pub type MemberListState = ListState<MemberItem, IambInfo>; |
| 357 | 365 | pub type RoomListState = ListState<RoomItem, IambInfo>; |
| 366 | pub type ChatListState = ListState<GenericChatItem, IambInfo>; |
| 358 | 367 | pub type SpaceListState = ListState<SpaceItem, IambInfo>; |
| 359 | 368 | pub type VerifyListState = ListState<VerifyItem, IambInfo>; |
| 360 | 369 | |
| 370 | impl From<ChatListState> for IambWindow { |
| 371 | fn from(list: ChatListState) -> Self { |
| 372 | IambWindow::ChatList(list) |
| 373 | } |
| 374 | } |
| 375 | |
| 361 | 376 | impl From<RoomState> for IambWindow { |
| 362 | 377 | fn from(room: RoomState) -> Self { |
| 363 | 378 | IambWindow::Room(room) |
| @@ -514,6 +529,37 @@ impl WindowOps<IambInfo> for IambWindow { |
| 514 | 529 | .focus(focused) |
| 515 | 530 | .render(area, buf, state); |
| 516 | 531 | }, |
| 532 | IambWindow::ChatList(state) => { |
| 533 | let mut items = store |
| 534 | .application |
| 535 | .sync_info |
| 536 | .rooms |
| 537 | .clone() |
| 538 | .into_iter() |
| 539 | .map(|room_info| GenericChatItem::new(room_info, store, false)) |
| 540 | .collect::<Vec<_>>(); |
| 541 | |
| 542 | let dms = store |
| 543 | .application |
| 544 | .sync_info |
| 545 | .dms |
| 546 | .clone() |
| 547 | .into_iter() |
| 548 | .map(|room_info| GenericChatItem::new(room_info, store, true)); |
| 549 | |
| 550 | items.extend(dms); |
| 551 | |
| 552 | let fields = &store.application.settings.tunables.sort.chats; |
| 553 | items.sort_by(|a, b| room_fields_cmp(a, b, fields)); |
| 554 | |
| 555 | state.set(items); |
| 556 | |
| 557 | List::new(store) |
| 558 | .empty_message("You do not have rooms or dms yet") |
| 559 | .empty_alignment(Alignment::Center) |
| 560 | .focus(focused) |
| 561 | .render(area, buf, state); |
| 562 | }, |
| 517 | 563 | IambWindow::SpaceList(state) => { |
| 518 | 564 | let mut items = store |
| 519 | 565 | .application |
| @@ -564,6 +610,7 @@ impl WindowOps<IambInfo> for IambWindow { |
| 564 | 610 | IambWindow::SpaceList(w) => w.dup(store).into(), |
| 565 | 611 | IambWindow::VerifyList(w) => w.dup(store).into(), |
| 566 | 612 | IambWindow::Welcome(w) => w.dup(store).into(), |
| 613 | IambWindow::ChatList(w) => w.dup(store).into(), |
| 567 | 614 | } |
| 568 | 615 | } |
| 569 | 616 | |
| @@ -603,6 +650,7 @@ impl Window<IambInfo> for IambWindow { |
| 603 | 650 | IambWindow::SpaceList(_) => IambId::SpaceList, |
| 604 | 651 | IambWindow::VerifyList(_) => IambId::VerifyList, |
| 605 | 652 | IambWindow::Welcome(_) => IambId::Welcome, |
| 653 | IambWindow::ChatList(_) => IambId::ChatList, |
| 606 | 654 | } |
| 607 | 655 | } |
| 608 | 656 | |
| @@ -613,6 +661,7 @@ impl Window<IambInfo> for IambWindow { |
| 613 | 661 | IambWindow::SpaceList(_) => bold_spans("Spaces"), |
| 614 | 662 | IambWindow::VerifyList(_) => bold_spans("Verifications"), |
| 615 | 663 | IambWindow::Welcome(_) => bold_spans("Welcome to iamb"), |
| 664 | IambWindow::ChatList(_) => bold_spans("DMs & Rooms"), |
| 616 | 665 | |
| 617 | 666 | IambWindow::Room(w) => { |
| 618 | 667 | let title = store.application.get_room_title(w.id()); |
| @@ -639,6 +688,7 @@ impl Window<IambInfo> for IambWindow { |
| 639 | 688 | IambWindow::SpaceList(_) => bold_spans("Spaces"), |
| 640 | 689 | IambWindow::VerifyList(_) => bold_spans("Verifications"), |
| 641 | 690 | IambWindow::Welcome(_) => bold_spans("Welcome to iamb"), |
| 691 | IambWindow::ChatList(_) => bold_spans("DMs & Rooms"), |
| 642 | 692 | |
| 643 | 693 | IambWindow::Room(w) => w.get_title(store), |
| 644 | 694 | IambWindow::MemberList(state, room_id, _) => { |
| @@ -695,6 +745,11 @@ impl Window<IambInfo> for IambWindow { |
| 695 | 745 | |
| 696 | 746 | return Ok(win.into()); |
| 697 | 747 | }, |
| 748 | IambId::ChatList => { |
| 749 | let list = ChatListState::new(IambBufferId::ChatList, vec![]); |
| 750 | |
| 751 | Ok(list.into()) |
| 752 | }, |
| 698 | 753 | } |
| 699 | 754 | } |
| 700 | 755 | |
| @@ -729,6 +784,105 @@ impl Window<IambInfo> for IambWindow { |
| 729 | 784 | } |
| 730 | 785 | } |
| 731 | 786 | |
| 787 | #[derive(Clone)] |
| 788 | pub struct GenericChatItem { |
| 789 | room_info: MatrixRoomInfo, |
| 790 | name: String, |
| 791 | alias: Option<OwnedRoomAliasId>, |
| 792 | is_dm: bool, |
| 793 | } |
| 794 | |
| 795 | impl GenericChatItem { |
| 796 | fn new(room_info: MatrixRoomInfo, store: &mut ProgramStore, is_dm: bool) -> Self { |
| 797 | let room = &room_info.deref().0; |
| 798 | let room_id = room.room_id(); |
| 799 | |
| 800 | let info = store.application.get_room_info(room_id.to_owned()); |
| 801 | |
| 802 | let name = info.name.clone().unwrap_or_default(); |
| 803 | let alias = room.canonical_alias(); |
| 804 | info.tags = room_info.deref().1.clone(); |
| 805 | |
| 806 | if let Some(alias) = &alias { |
| 807 | store.application.names.insert(alias.to_string(), room_id.to_owned()); |
| 808 | } |
| 809 | |
| 810 | GenericChatItem { room_info, name, alias, is_dm } |
| 811 | } |
| 812 | |
| 813 | #[inline] |
| 814 | fn room(&self) -> &MatrixRoom { |
| 815 | &self.room_info.deref().0 |
| 816 | } |
| 817 | |
| 818 | #[inline] |
| 819 | fn tags(&self) -> &Option<Tags> { |
| 820 | &self.room_info.deref().1 |
| 821 | } |
| 822 | } |
| 823 | |
| 824 | impl RoomLikeItem for GenericChatItem { |
| 825 | fn name(&self) -> &str { |
| 826 | self.name.as_str() |
| 827 | } |
| 828 | |
| 829 | fn alias(&self) -> Option<&RoomAliasId> { |
| 830 | self.alias.as_deref() |
| 831 | } |
| 832 | |
| 833 | fn room_id(&self) -> &RoomId { |
| 834 | self.room().room_id() |
| 835 | } |
| 836 | |
| 837 | fn has_tag(&self, tag: TagName) -> bool { |
| 838 | if let Some(tags) = &self.room_info.deref().1 { |
| 839 | tags.contains_key(&tag) |
| 840 | } else { |
| 841 | false |
| 842 | } |
| 843 | } |
| 844 | } |
| 845 | |
| 846 | impl ToString for GenericChatItem { |
| 847 | fn to_string(&self) -> String { |
| 848 | return self.name.clone(); |
| 849 | } |
| 850 | } |
| 851 | |
| 852 | impl ListItem<IambInfo> for GenericChatItem { |
| 853 | fn show(&self, selected: bool, _: &ViewportContext<ListCursor>, _: &mut ProgramStore) -> Text { |
| 854 | let style = selected_style(selected); |
| 855 | let mut spans = vec![Span::styled(self.name.as_str(), style)]; |
| 856 | let mut labels = if self.is_dm { |
| 857 | vec![vec![Span::styled("DM", style)]] |
| 858 | } else { |
| 859 | vec![vec![Span::styled("Room", style)]] |
| 860 | }; |
| 861 | |
| 862 | if let Some(tags) = &self.tags() { |
| 863 | labels.extend(tags.keys().map(|t| tag_to_span(t, style))); |
| 864 | } |
| 865 | |
| 866 | append_tags(labels, &mut spans, style); |
| 867 | Text::from(Line::from(spans)) |
| 868 | } |
| 869 | |
| 870 | fn get_word(&self) -> Option<String> { |
| 871 | self.room_id().to_string().into() |
| 872 | } |
| 873 | } |
| 874 | |
| 875 | impl Promptable<ProgramContext, ProgramStore, IambInfo> for GenericChatItem { |
| 876 | fn prompt( |
| 877 | &mut self, |
| 878 | act: &PromptAction, |
| 879 | ctx: &ProgramContext, |
| 880 | _: &mut ProgramStore, |
| 881 | ) -> EditResult<Vec<(ProgramAction, ProgramContext)>, IambInfo> { |
| 882 | room_prompt(self.room_id(), act, ctx) |
| 883 | } |
| 884 | } |
| 885 | |
| 732 | 886 | #[derive(Clone)] |
| 733 | 887 | pub struct RoomItem { |
| 734 | 888 | room_info: MatrixRoomInfo, |
| @@ -797,6 +951,7 @@ impl ListItem<IambInfo> for RoomItem { |
| 797 | 951 | if let Some(tags) = &self.tags() { |
| 798 | 952 | let style = selected_style(selected); |
| 799 | 953 | let mut spans = vec![Span::styled(self.name.as_str(), style)]; |
| 954 | let tags = tags.keys().map(|t| tag_to_span(t, style)).collect(); |
| 800 | 955 | |
| 801 | 956 | append_tags(tags, &mut spans, style); |
| 802 | 957 | |
| @@ -882,6 +1037,7 @@ impl ListItem<IambInfo> for DirectItem { |
| 882 | 1037 | if let Some(tags) = &self.tags() { |
| 883 | 1038 | let style = selected_style(selected); |
| 884 | 1039 | let mut spans = vec![Span::styled(self.name.as_str(), style)]; |
| 1040 | let tags = tags.keys().map(|t| tag_to_span(t, style)).collect(); |
| 885 | 1041 | |
| 886 | 1042 | append_tags(tags, &mut spans, style); |
| 887 | 1043 | |
| @@ -1069,7 +1225,7 @@ impl Ord for VerifyItem { |
| 1069 | 1225 | |
| 1070 | 1226 | impl PartialOrd for VerifyItem { |
| 1071 | 1227 | fn partial_cmp(&self, other: &Self) -> Option<Ordering> { |
| 1072 | | self.cmp(other).into() |
| 1228 | Some(self.cmp(other)) |
| 1073 | 1229 | } |
| 1074 | 1230 | } |
| 1075 | 1231 | |