Commit d8713141f2
Unregistered key
Layout: unified · split
src/windows/mod.rs +98 −52
| @@ -124,6 +124,57 @@ fn room_cmp(a: &MatrixRoom, b: &MatrixRoom) -> Ordering { | ||
| 124 | 124 | ord.then_with(|| a.room_id().cmp(b.room_id())) |
| 125 | 125 | } |
| 126 | 126 | |
| 127 | fn tag_cmp(a: &Option<Tags>, b: &Option<Tags>) -> Ordering { | |
| 128 | let (fava, lowa) = a | |
| 129 | .as_ref() | |
| 130 | .map(|tags| { | |
| 131 | (tags.contains_key(&TagName::Favorite), tags.contains_key(&TagName::LowPriority)) | |
| 132 | }) | |
| 133 | .unwrap_or((false, false)); | |
| 134 | ||
| 135 | let (favb, lowb) = b | |
| 136 | .as_ref() | |
| 137 | .map(|tags| { | |
| 138 | (tags.contains_key(&TagName::Favorite), tags.contains_key(&TagName::LowPriority)) | |
| 139 | }) | |
| 140 | .unwrap_or((false, false)); | |
| 141 | ||
| 142 | // If a has Favorite and b doesn't, it should sort earlier in room list. | |
| 143 | let cmpf = favb.cmp(&fava); | |
| 144 | ||
| 145 | // If a has LowPriority and b doesn't, it should sort later in room list. | |
| 146 | let cmpl = lowa.cmp(&lowb); | |
| 147 | ||
| 148 | cmpl.then(cmpf) | |
| 149 | } | |
| 150 | ||
| 151 | fn append_tags<'a>(tags: &'a Tags, spans: &mut Vec<Span<'a>>, style: Style) { | |
| 152 | if tags.is_empty() { | |
| 153 | return; | |
| 154 | } | |
| 155 | ||
| 156 | spans.push(Span::styled(" (", style)); | |
| 157 | ||
| 158 | for (i, tag) in tags.keys().enumerate() { | |
| 159 | if i > 0 { | |
| 160 | spans.push(Span::styled(", ", style)); | |
| 161 | } | |
| 162 | ||
| 163 | match tag { | |
| 164 | TagName::Favorite => spans.push(Span::styled("Favorite", style)), | |
| 165 | TagName::LowPriority => spans.push(Span::styled("Low Priority", style)), | |
| 166 | TagName::ServerNotice => spans.push(Span::styled("Server Notice", style)), | |
| 167 | TagName::User(tag) => { | |
| 168 | spans.push(Span::styled("User Tag: ", style)); | |
| 169 | spans.push(Span::styled(tag.as_ref(), style)); | |
| 170 | }, | |
| 171 | tag => spans.push(Span::styled(format!("{:?}", tag), style)), | |
| 172 | } | |
| 173 | } | |
| 174 | ||
| 175 | spans.push(Span::styled(")", style)); | |
| 176 | } | |
| 177 | ||
| 127 | 178 | #[inline] |
| 128 | 179 | fn room_prompt( |
| 129 | 180 | room_id: &RoomId, |
| @@ -326,8 +377,13 @@ impl WindowOps<IambInfo> for IambWindow { | ||
| 326 | 377 | IambWindow::Room(state) => state.draw(area, buf, focused, store), |
| 327 | 378 | IambWindow::DirectList(state) => { |
| 328 | 379 | let dms = store.application.worker.direct_messages(); |
| 329 | let items = dms.into_iter().map(|(id, name)| DirectItem::new(id, name, store)); | |
| 330 | state.set(items.collect()); | |
| 380 | let mut items = dms | |
| 381 | .into_iter() | |
| 382 | .map(|(id, name, tags)| DirectItem::new(id, name, tags, store)) | |
| 383 | .collect::<Vec<_>>(); | |
| 384 | items.sort(); | |
| 385 | ||
| 386 | state.set(items); | |
| 331 | 387 | |
| 332 | 388 | List::new(store) |
| 333 | 389 | .empty_message("No direct messages yet!") |
| @@ -583,29 +639,7 @@ impl Eq for RoomItem {} | ||
| 583 | 639 | |
| 584 | 640 | impl Ord for RoomItem { |
| 585 | 641 | fn cmp(&self, other: &Self) -> Ordering { |
| 586 | let (fava, lowa) = self | |
| 587 | .tags | |
| 588 | .as_ref() | |
| 589 | .map(|tags| { | |
| 590 | (tags.contains_key(&TagName::Favorite), tags.contains_key(&TagName::LowPriority)) | |
| 591 | }) | |
| 592 | .unwrap_or((false, false)); | |
| 593 | ||
| 594 | let (favb, lowb) = other | |
| 595 | .tags | |
| 596 | .as_ref() | |
| 597 | .map(|tags| { | |
| 598 | (tags.contains_key(&TagName::Favorite), tags.contains_key(&TagName::LowPriority)) | |
| 599 | }) | |
| 600 | .unwrap_or((false, false)); | |
| 601 | ||
| 602 | // If self has Favorite and other doesn't, it should sort earlier in room list. | |
| 603 | let cmpf = favb.cmp(&fava); | |
| 604 | ||
| 605 | // If self has LowPriority and other doesn't, it should sort later in room list. | |
| 606 | let cmpl = lowa.cmp(&lowb); | |
| 607 | ||
| 608 | cmpl.then(cmpf).then_with(|| room_cmp(&self.room, &other.room)) | |
| 642 | tag_cmp(&self.tags, &other.tags).then_with(|| room_cmp(&self.room, &other.room)) | |
| 609 | 643 | } |
| 610 | 644 | } |
| 611 | 645 | |
| @@ -627,30 +661,7 @@ impl ListItem<IambInfo> for RoomItem { | ||
| 627 | 661 | let style = selected_style(selected); |
| 628 | 662 | let mut spans = vec![Span::styled(self.name.as_str(), style)]; |
| 629 | 663 | |
| 630 | if tags.is_empty() { | |
| 631 | return Text::from(Spans(spans)); | |
| 632 | } | |
| 633 | ||
| 634 | spans.push(Span::styled(" (", style)); | |
| 635 | ||
| 636 | for (i, tag) in tags.keys().enumerate() { | |
| 637 | if i > 0 { | |
| 638 | spans.push(Span::styled(", ", style)); | |
| 639 | } | |
| 640 | ||
| 641 | match tag { | |
| 642 | TagName::Favorite => spans.push(Span::styled("Favorite", style)), | |
| 643 | TagName::LowPriority => spans.push(Span::styled("Low Priority", style)), | |
| 644 | TagName::ServerNotice => spans.push(Span::styled("Server Notice", style)), | |
| 645 | TagName::User(tag) => { | |
| 646 | spans.push(Span::styled("User Tag: ", style)); | |
| 647 | spans.push(Span::styled(tag.as_ref(), style)); | |
| 648 | }, | |
| 649 | tag => spans.push(Span::styled(format!("{:?}", tag), style)), | |
| 650 | } | |
| 651 | } | |
| 652 | ||
| 653 | spans.push(Span::styled(")", style)); | |
| 664 | append_tags(tags, &mut spans, style); | |
| 654 | 665 | |
| 655 | 666 | Text::from(Spans(spans)) |
| 656 | 667 | } else { |
| @@ -677,16 +688,22 @@ impl Promptable<ProgramContext, ProgramStore, IambInfo> for RoomItem { | ||
| 677 | 688 | #[derive(Clone)] |
| 678 | 689 | pub struct DirectItem { |
| 679 | 690 | room: MatrixRoom, |
| 691 | tags: Option<Tags>, | |
| 680 | 692 | name: String, |
| 681 | 693 | } |
| 682 | 694 | |
| 683 | 695 | impl DirectItem { |
| 684 | fn new(room: MatrixRoom, name: DisplayName, store: &mut ProgramStore) -> Self { | |
| 696 | fn new( | |
| 697 | room: MatrixRoom, | |
| 698 | name: DisplayName, | |
| 699 | tags: Option<Tags>, | |
| 700 | store: &mut ProgramStore, | |
| 701 | ) -> Self { | |
| 685 | 702 | let name = name.to_string(); |
| 686 | 703 | |
| 687 | 704 | store.application.set_room_name(room.room_id(), name.as_str()); |
| 688 | 705 | |
| 689 | DirectItem { room, name } | |
| 706 | DirectItem { room, tags, name } | |
| 690 | 707 | } |
| 691 | 708 | } |
| 692 | 709 | |
| @@ -698,7 +715,16 @@ impl ToString for DirectItem { | ||
| 698 | 715 | |
| 699 | 716 | impl ListItem<IambInfo> for DirectItem { |
| 700 | 717 | fn show(&self, selected: bool, _: &ViewportContext<ListCursor>, _: &mut ProgramStore) -> Text { |
| 701 | selected_text(self.name.as_str(), selected) | |
| 718 | if let Some(tags) = &self.tags { | |
| 719 | let style = selected_style(selected); | |
| 720 | let mut spans = vec![Span::styled(self.name.as_str(), style)]; | |
| 721 | ||
| 722 | append_tags(tags, &mut spans, style); | |
| 723 | ||
| 724 | Text::from(Spans(spans)) | |
| 725 | } else { | |
| 726 | selected_text(self.name.as_str(), selected) | |
| 727 | } | |
| 702 | 728 | } |
| 703 | 729 | |
| 704 | 730 | fn get_word(&self) -> Option<String> { |
| @@ -706,6 +732,26 @@ impl ListItem<IambInfo> for DirectItem { | ||
| 706 | 732 | } |
| 707 | 733 | } |
| 708 | 734 | |
| 735 | impl PartialEq for DirectItem { | |
| 736 | fn eq(&self, other: &Self) -> bool { | |
| 737 | self.room.room_id() == other.room.room_id() | |
| 738 | } | |
| 739 | } | |
| 740 | ||
| 741 | impl Eq for DirectItem {} | |
| 742 | ||
| 743 | impl Ord for DirectItem { | |
| 744 | fn cmp(&self, other: &Self) -> Ordering { | |
| 745 | tag_cmp(&self.tags, &other.tags).then_with(|| room_cmp(&self.room, &other.room)) | |
| 746 | } | |
| 747 | } | |
| 748 | ||
| 749 | impl PartialOrd for DirectItem { | |
| 750 | fn partial_cmp(&self, other: &Self) -> Option<Ordering> { | |
| 751 | self.cmp(other).into() | |
| 752 | } | |
| 753 | } | |
| 754 | ||
| 709 | 755 | impl Promptable<ProgramContext, ProgramStore, IambInfo> for DirectItem { |
| 710 | 756 | fn prompt( |
| 711 | 757 | &mut self, |
src/worker.rs +17 −24
| @@ -99,14 +99,16 @@ fn oneshot<T>() -> (ClientReply<T>, ClientResponse<T>) { | ||
| 99 | 99 | return (reply, response); |
| 100 | 100 | } |
| 101 | 101 | |
| 102 | pub type FetchedRoom = (MatrixRoom, DisplayName, Option<Tags>); | |
| 103 | ||
| 102 | 104 | pub enum WorkerTask { |
| 103 | ActiveRooms(ClientReply<Vec<(MatrixRoom, DisplayName, Option<Tags>)>>), | |
| 104 | DirectMessages(ClientReply<Vec<(MatrixRoom, DisplayName)>>), | |
| 105 | ActiveRooms(ClientReply<Vec<FetchedRoom>>), | |
| 106 | DirectMessages(ClientReply<Vec<FetchedRoom>>), | |
| 105 | 107 | Init(AsyncProgramStore, ClientReply<()>), |
| 106 | 108 | LoadOlder(OwnedRoomId, Option<String>, u32, ClientReply<MessageFetchResult>), |
| 107 | 109 | Login(LoginStyle, ClientReply<IambResult<EditInfo>>), |
| 108 | 110 | GetInviter(Invited, ClientReply<IambResult<Option<RoomMember>>>), |
| 109 | GetRoom(OwnedRoomId, ClientReply<IambResult<(MatrixRoom, DisplayName, Option<Tags>)>>), | |
| 111 | GetRoom(OwnedRoomId, ClientReply<IambResult<FetchedRoom>>), | |
| 110 | 112 | JoinRoom(String, ClientReply<IambResult<OwnedRoomId>>), |
| 111 | 113 | Members(OwnedRoomId, ClientReply<IambResult<Vec<RoomMember>>>), |
| 112 | 114 | SpaceMembers(OwnedRoomId, ClientReply<IambResult<Vec<OwnedRoomId>>>), |
| @@ -235,7 +237,7 @@ impl Requester { | ||
| 235 | 237 | return response.recv(); |
| 236 | 238 | } |
| 237 | 239 | |
| 238 | pub fn direct_messages(&self) -> Vec<(MatrixRoom, DisplayName)> { | |
| 240 | pub fn direct_messages(&self) -> Vec<FetchedRoom> { | |
| 239 | 241 | let (reply, response) = oneshot(); |
| 240 | 242 | |
| 241 | 243 | self.tx.send(WorkerTask::DirectMessages(reply)).unwrap(); |
| @@ -251,10 +253,7 @@ impl Requester { | ||
| 251 | 253 | return response.recv(); |
| 252 | 254 | } |
| 253 | 255 | |
| 254 | pub fn get_room( | |
| 255 | &self, | |
| 256 | room_id: OwnedRoomId, | |
| 257 | ) -> IambResult<(MatrixRoom, DisplayName, Option<Tags>)> { | |
| 256 | pub fn get_room(&self, room_id: OwnedRoomId) -> IambResult<FetchedRoom> { | |
| 258 | 257 | let (reply, response) = oneshot(); |
| 259 | 258 | |
| 260 | 259 | self.tx.send(WorkerTask::GetRoom(room_id, reply)).unwrap(); |
| @@ -270,7 +269,7 @@ impl Requester { | ||
| 270 | 269 | return response.recv(); |
| 271 | 270 | } |
| 272 | 271 | |
| 273 | pub fn active_rooms(&self) -> Vec<(MatrixRoom, DisplayName, Option<Tags>)> { | |
| 272 | pub fn active_rooms(&self) -> Vec<FetchedRoom> { | |
| 274 | 273 | let (reply, response) = oneshot(); |
| 275 | 274 | |
| 276 | 275 | self.tx.send(WorkerTask::ActiveRooms(reply)).unwrap(); |
| @@ -704,14 +703,9 @@ impl ClientWorker { | ||
| 704 | 703 | Ok(Some(InfoMessage::from("Successfully logged in!"))) |
| 705 | 704 | } |
| 706 | 705 | |
| 707 | async fn direct_message( | |
| 708 | &mut self, | |
| 709 | user: OwnedUserId, | |
| 710 | ) -> IambResult<(MatrixRoom, DisplayName, Option<Tags>)> { | |
| 711 | for (room, name) in self.direct_messages().await { | |
| 706 | async fn direct_message(&mut self, user: OwnedUserId) -> IambResult<FetchedRoom> { | |
| 707 | for (room, name, tags) in self.direct_messages().await { | |
| 712 | 708 | if room.get_member(user.as_ref()).await.map_err(IambError::from)?.is_some() { |
| 713 | let tags = room.tags().await.map_err(IambError::from)?; | |
| 714 | ||
| 715 | 709 | return Ok((room, name, tags)); |
| 716 | 710 | } |
| 717 | 711 | } |
| @@ -746,10 +740,7 @@ impl ClientWorker { | ||
| 746 | 740 | Ok(details.inviter) |
| 747 | 741 | } |
| 748 | 742 | |
| 749 | async fn get_room( | |
| 750 | &mut self, | |
| 751 | room_id: OwnedRoomId, | |
| 752 | ) -> IambResult<(MatrixRoom, DisplayName, Option<Tags>)> { | |
| 743 | async fn get_room(&mut self, room_id: OwnedRoomId) -> IambResult<FetchedRoom> { | |
| 753 | 744 | if let Some(room) = self.client.get_room(&room_id) { |
| 754 | 745 | let name = room.display_name().await.map_err(IambError::from)?; |
| 755 | 746 | let tags = room.tags().await.map_err(IambError::from)?; |
| @@ -783,7 +774,7 @@ impl ClientWorker { | ||
| 783 | 774 | } |
| 784 | 775 | } |
| 785 | 776 | |
| 786 | async fn direct_messages(&self) -> Vec<(MatrixRoom, DisplayName)> { | |
| 777 | async fn direct_messages(&self) -> Vec<FetchedRoom> { | |
| 787 | 778 | let mut rooms = vec![]; |
| 788 | 779 | |
| 789 | 780 | for room in self.client.invited_rooms().into_iter() { |
| @@ -792,8 +783,9 @@ impl ClientWorker { | ||
| 792 | 783 | } |
| 793 | 784 | |
| 794 | 785 | let name = room.display_name().await.unwrap_or(DisplayName::Empty); |
| 786 | let tags = room.tags().await.unwrap_or_default(); | |
| 795 | 787 | |
| 796 | rooms.push((room.into(), name)); | |
| 788 | rooms.push((room.into(), name, tags)); | |
| 797 | 789 | } |
| 798 | 790 | |
| 799 | 791 | for room in self.client.joined_rooms().into_iter() { |
| @@ -802,14 +794,15 @@ impl ClientWorker { | ||
| 802 | 794 | } |
| 803 | 795 | |
| 804 | 796 | let name = room.display_name().await.unwrap_or(DisplayName::Empty); |
| 797 | let tags = room.tags().await.unwrap_or_default(); | |
| 805 | 798 | |
| 806 | rooms.push((room.into(), name)); | |
| 799 | rooms.push((room.into(), name, tags)); | |
| 807 | 800 | } |
| 808 | 801 | |
| 809 | 802 | return rooms; |
| 810 | 803 | } |
| 811 | 804 | |
| 812 | async fn active_rooms(&self) -> Vec<(MatrixRoom, DisplayName, Option<Tags>)> { | |
| 805 | async fn active_rooms(&self) -> Vec<FetchedRoom> { | |
| 813 | 806 | let mut rooms = vec![]; |
| 814 | 807 | |
| 815 | 808 | for room in self.client.invited_rooms().into_iter() { |