| @@ -79,9 +79,11 @@ use crate::base::{ |
| 79 | SortFieldRoom, |
79 | SortFieldRoom, |
| 80 | SortFieldUser, |
80 | SortFieldUser, |
| 81 | SortOrder, |
81 | SortOrder, |
| |
82 | UnreadInfo, |
| 82 | }; |
83 | }; |
| 83 | |
84 | |
| 84 | use self::{room::RoomState, welcome::WelcomeState}; |
85 | use self::{room::RoomState, welcome::WelcomeState}; |
| |
86 | use crate::message::MessageTimeStamp; |
| 85 | |
87 | |
| 86 | pub mod room; |
88 | pub mod room; |
| 87 | pub mod welcome; |
89 | pub mod welcome; |
| @@ -124,11 +126,31 @@ fn selected_text(s: &str, selected: bool) -> Text { |
| 124 | Text::from(selected_span(s, selected)) |
126 | Text::from(selected_span(s, selected)) |
| 125 | } |
127 | } |
| 126 | |
128 | |
| |
129 | fn name_and_labels(name: &str, unread: bool, style: Style) -> (Span<'_>, Vec<Vec<Span<'_>>>) { |
| |
130 | let name_style = if unread { |
| |
131 | style.add_modifier(StyleModifier::BOLD) |
| |
132 | } else { |
| |
133 | style |
| |
134 | }; |
| |
135 | |
| |
136 | let name = Span::styled(name, name_style); |
| |
137 | let labels = if unread { |
| |
138 | vec![vec![Span::styled("Unread", style)]] |
| |
139 | } else { |
| |
140 | vec![] |
| |
141 | }; |
| |
142 | |
| |
143 | (name, labels) |
| |
144 | } |
| |
145 | |
| 127 | /// Sort `Some` to be less than `None` so that list items with values come before those without. |
146 | /// Sort `Some` to be less than `None` so that list items with values come before those without. |
| 128 | #[inline] |
147 | #[inline] |
| 129 | fn some_cmp<T: Ord>(a: Option<T>, b: Option<T>) -> Ordering { |
148 | fn some_cmp<T, F>(a: Option<T>, b: Option<T>, f: F) -> Ordering |
| |
149 | where |
| |
150 | F: Fn(&T, &T) -> Ordering, |
| |
151 | { |
| 130 | match (a, b) { |
152 | match (a, b) { |
| 131 | (Some(a), Some(b)) => a.cmp(&b), |
153 | (Some(a), Some(b)) => f(&a, &b), |
| 132 | (None, None) => Ordering::Equal, |
154 | (None, None) => Ordering::Equal, |
| 133 | (None, Some(_)) => Ordering::Greater, |
155 | (None, Some(_)) => Ordering::Greater, |
| 134 | (Some(_), None) => Ordering::Less, |
156 | (Some(_), None) => Ordering::Less, |
| @@ -167,8 +189,16 @@ fn room_cmp<T: RoomLikeItem>(a: &T, b: &T, field: &SortFieldRoom) -> Ordering { |
| 167 | lowa.cmp(&lowb) |
189 | lowa.cmp(&lowb) |
| 168 | }, |
190 | }, |
| 169 | SortFieldRoom::Name => a.name().cmp(b.name()), |
191 | SortFieldRoom::Name => a.name().cmp(b.name()), |
| 170 | SortFieldRoom::Alias => some_cmp(a.alias(), b.alias()), |
192 | SortFieldRoom::Alias => some_cmp(a.alias(), b.alias(), Ord::cmp), |
| 171 | SortFieldRoom::RoomId => a.room_id().cmp(b.room_id()), |
193 | SortFieldRoom::RoomId => a.room_id().cmp(b.room_id()), |
| |
194 | SortFieldRoom::Unread => { |
| |
195 | // Sort true (unread) before false (read) |
| |
196 | b.is_unread().cmp(&a.is_unread()) |
| |
197 | }, |
| |
198 | SortFieldRoom::Recent => { |
| |
199 | // sort larger timestamps towards the top. |
| |
200 | some_cmp(a.recent_ts(), b.recent_ts(), |a, b| b.cmp(a)) |
| |
201 | }, |
| 172 | } |
202 | } |
| 173 | } |
203 | } |
| 174 | |
204 | |
| @@ -243,6 +273,8 @@ fn append_tags<'a>(tags: Vec<Vec<Span<'a>>>, spans: &mut Vec<Span<'a>>, style: S |
| 243 | trait RoomLikeItem { |
273 | trait RoomLikeItem { |
| 244 | fn room_id(&self) -> &RoomId; |
274 | fn room_id(&self) -> &RoomId; |
| 245 | fn has_tag(&self, tag: TagName) -> bool; |
275 | fn has_tag(&self, tag: TagName) -> bool; |
| |
276 | fn is_unread(&self) -> bool; |
| |
277 | fn recent_ts(&self) -> Option<&MessageTimeStamp>; |
| 246 | fn alias(&self) -> Option<&RoomAliasId>; |
278 | fn alias(&self) -> Option<&RoomAliasId>; |
| 247 | fn name(&self) -> &str; |
279 | fn name(&self) -> &str; |
| 248 | } |
280 | } |
| @@ -780,6 +812,7 @@ pub struct GenericChatItem { |
| 780 | room_info: MatrixRoomInfo, |
812 | room_info: MatrixRoomInfo, |
| 781 | name: String, |
813 | name: String, |
| 782 | alias: Option<OwnedRoomAliasId>, |
814 | alias: Option<OwnedRoomAliasId>, |
| |
815 | unread: UnreadInfo, |
| 783 | is_dm: bool, |
816 | is_dm: bool, |
| 784 | } |
817 | } |
| 785 | |
818 | |
| @@ -788,17 +821,17 @@ impl GenericChatItem { |
| 788 | let room = &room_info.deref().0; |
821 | let room = &room_info.deref().0; |
| 789 | let room_id = room.room_id(); |
822 | let room_id = room.room_id(); |
| 790 | |
823 | |
| 791 | let info = store.application.get_room_info(room_id.to_owned()); |
824 | let info = store.application.rooms.get_or_default(room_id.to_owned()); |
| 792 | |
| |
| 793 | let name = info.name.clone().unwrap_or_default(); |
825 | let name = info.name.clone().unwrap_or_default(); |
| 794 | let alias = room.canonical_alias(); |
826 | let alias = room.canonical_alias(); |
| |
827 | let unread = info.unreads(&store.application.settings); |
| 795 | info.tags = room_info.deref().1.clone(); |
828 | info.tags = room_info.deref().1.clone(); |
| 796 | |
829 | |
| 797 | if let Some(alias) = &alias { |
830 | if let Some(alias) = &alias { |
| 798 | store.application.names.insert(alias.to_string(), room_id.to_owned()); |
831 | store.application.names.insert(alias.to_string(), room_id.to_owned()); |
| 799 | } |
832 | } |
| 800 | |
833 | |
| 801 | GenericChatItem { room_info, name, alias, is_dm } |
834 | GenericChatItem { room_info, name, alias, is_dm, unread } |
| 802 | } |
835 | } |
| 803 | |
836 | |
| 804 | #[inline] |
837 | #[inline] |
| @@ -832,6 +865,14 @@ impl RoomLikeItem for GenericChatItem { |
| 832 | false |
865 | false |
| 833 | } |
866 | } |
| 834 | } |
867 | } |
| |
868 | |
| |
869 | fn recent_ts(&self) -> Option<&MessageTimeStamp> { |
| |
870 | self.unread.latest() |
| |
871 | } |
| |
872 | |
| |
873 | fn is_unread(&self) -> bool { |
| |
874 | self.unread.is_unread() |
| |
875 | } |
| 835 | } |
876 | } |
| 836 | |
877 | |
| 837 | impl ToString for GenericChatItem { |
878 | impl ToString for GenericChatItem { |
| @@ -842,13 +883,16 @@ impl ToString for GenericChatItem { |
| 842 | |
883 | |
| 843 | impl ListItem<IambInfo> for GenericChatItem { |
884 | impl ListItem<IambInfo> for GenericChatItem { |
| 844 | fn show(&self, selected: bool, _: &ViewportContext<ListCursor>, _: &mut ProgramStore) -> Text { |
885 | fn show(&self, selected: bool, _: &ViewportContext<ListCursor>, _: &mut ProgramStore) -> Text { |
| |
886 | let unread = self.unread.is_unread(); |
| 845 | let style = selected_style(selected); |
887 | let style = selected_style(selected); |
| 846 | let mut spans = vec![Span::styled(self.name.as_str(), style)]; |
888 | let (name, mut labels) = name_and_labels(&self.name, unread, style); |
| 847 | let mut labels = if self.is_dm { |
889 | let mut spans = vec![name]; |
| 848 | vec![vec![Span::styled("DM", style)]] |
890 | |
| |
891 | labels.push(if self.is_dm { |
| |
892 | vec![Span::styled("DM", style)] |
| 849 | } else { |
893 | } else { |
| 850 | vec![vec![Span::styled("Room", style)]] |
894 | vec![Span::styled("Room", style)] |
| 851 | }; |
895 | }); |
| 852 | |
896 | |
| 853 | if let Some(tags) = &self.tags() { |
897 | if let Some(tags) = &self.tags() { |
| 854 | labels.extend(tags.keys().map(|t| tag_to_span(t, style))); |
898 | labels.extend(tags.keys().map(|t| tag_to_span(t, style))); |
| @@ -879,6 +923,7 @@ pub struct RoomItem { |
| 879 | room_info: MatrixRoomInfo, |
923 | room_info: MatrixRoomInfo, |
| 880 | name: String, |
924 | name: String, |
| 881 | alias: Option<OwnedRoomAliasId>, |
925 | alias: Option<OwnedRoomAliasId>, |
| |
926 | unread: UnreadInfo, |
| 882 | } |
927 | } |
| 883 | |
928 | |
| 884 | impl RoomItem { |
929 | impl RoomItem { |
| @@ -886,16 +931,17 @@ impl RoomItem { |
| 886 | let room = &room_info.deref().0; |
931 | let room = &room_info.deref().0; |
| 887 | let room_id = room.room_id(); |
932 | let room_id = room.room_id(); |
| 888 | |
933 | |
| 889 | let info = store.application.get_room_info(room_id.to_owned()); |
934 | let info = store.application.rooms.get_or_default(room_id.to_owned()); |
| 890 | let name = info.name.clone().unwrap_or_default(); |
935 | let name = info.name.clone().unwrap_or_default(); |
| 891 | let alias = room.canonical_alias(); |
936 | let alias = room.canonical_alias(); |
| |
937 | let unread = info.unreads(&store.application.settings); |
| 892 | info.tags = room_info.deref().1.clone(); |
938 | info.tags = room_info.deref().1.clone(); |
| 893 | |
939 | |
| 894 | if let Some(alias) = &alias { |
940 | if let Some(alias) = &alias { |
| 895 | store.application.names.insert(alias.to_string(), room_id.to_owned()); |
941 | store.application.names.insert(alias.to_string(), room_id.to_owned()); |
| 896 | } |
942 | } |
| 897 | |
943 | |
| 898 | RoomItem { room_info, name, alias } |
944 | RoomItem { room_info, name, alias, unread } |
| 899 | } |
945 | } |
| 900 | |
946 | |
| 901 | #[inline] |
947 | #[inline] |
| @@ -929,6 +975,14 @@ impl RoomLikeItem for RoomItem { |
| 929 | false |
975 | false |
| 930 | } |
976 | } |
| 931 | } |
977 | } |
| |
978 | |
| |
979 | fn recent_ts(&self) -> Option<&MessageTimeStamp> { |
| |
980 | self.unread.latest() |
| |
981 | } |
| |
982 | |
| |
983 | fn is_unread(&self) -> bool { |
| |
984 | self.unread.is_unread() |
| |
985 | } |
| 932 | } |
986 | } |
| 933 | |
987 | |
| 934 | impl ToString for RoomItem { |
988 | impl ToString for RoomItem { |
| @@ -939,17 +993,18 @@ impl ToString for RoomItem { |
| 939 | |
993 | |
| 940 | impl ListItem<IambInfo> for RoomItem { |
994 | impl ListItem<IambInfo> for RoomItem { |
| 941 | fn show(&self, selected: bool, _: &ViewportContext<ListCursor>, _: &mut ProgramStore) -> Text { |
995 | fn show(&self, selected: bool, _: &ViewportContext<ListCursor>, _: &mut ProgramStore) -> Text { |
| |
996 | let unread = self.unread.is_unread(); |
| |
997 | let style = selected_style(selected); |
| |
998 | let (name, mut labels) = name_and_labels(&self.name, unread, style); |
| |
999 | let mut spans = vec![name]; |
| |
1000 | |
| 942 | if let Some(tags) = &self.tags() { |
1001 | if let Some(tags) = &self.tags() { |
| 943 | let style = selected_style(selected); |
1002 | labels.extend(tags.keys().map(|t| tag_to_span(t, style))); |
| 944 | let mut spans = vec![Span::styled(self.name.as_str(), style)]; |
1003 | } |
| 945 | let tags = tags.keys().map(|t| tag_to_span(t, style)).collect(); |
| |
| 946 | |
1004 | |
| 947 | append_tags(tags, &mut spans, style); |
1005 | append_tags(labels, &mut spans, style); |
| 948 | |
1006 | |
| 949 | Text::from(Line::from(spans)) |
1007 | Text::from(Line::from(spans)) |
| 950 | } else { |
| |
| 951 | selected_text(self.name.as_str(), selected) |
| |
| 952 | } |
| |
| 953 | } |
1008 | } |
| 954 | |
1009 | |
| 955 | fn get_word(&self) -> Option<String> { |
1010 | fn get_word(&self) -> Option<String> { |
| @@ -973,15 +1028,20 @@ pub struct DirectItem { |
| 973 | room_info: MatrixRoomInfo, |
1028 | room_info: MatrixRoomInfo, |
| 974 | name: String, |
1029 | name: String, |
| 975 | alias: Option<OwnedRoomAliasId>, |
1030 | alias: Option<OwnedRoomAliasId>, |
| |
1031 | unread: UnreadInfo, |
| 976 | } |
1032 | } |
| 977 | |
1033 | |
| 978 | impl DirectItem { |
1034 | impl DirectItem { |
| 979 | fn new(room_info: MatrixRoomInfo, store: &mut ProgramStore) -> Self { |
1035 | fn new(room_info: MatrixRoomInfo, store: &mut ProgramStore) -> Self { |
| 980 | let room_id = room_info.0.room_id().to_owned(); |
1036 | let room_id = room_info.0.room_id().to_owned(); |
| 981 | let name = store.application.get_room_info(room_id).name.clone().unwrap_or_default(); |
| |
| 982 | let alias = room_info.0.canonical_alias(); |
1037 | let alias = room_info.0.canonical_alias(); |
| 983 | |
1038 | |
| 984 | DirectItem { room_info, name, alias } |
1039 | let info = store.application.rooms.get_or_default(room_id); |
| |
1040 | let name = info.name.clone().unwrap_or_default(); |
| |
1041 | let unread = info.unreads(&store.application.settings); |
| |
1042 | info.tags = room_info.deref().1.clone(); |
| |
1043 | |
| |
1044 | DirectItem { room_info, name, alias, unread } |
| 985 | } |
1045 | } |
| 986 | |
1046 | |
| 987 | #[inline] |
1047 | #[inline] |
| @@ -1015,6 +1075,14 @@ impl RoomLikeItem for DirectItem { |
| 1015 | fn room_id(&self) -> &RoomId { |
1075 | fn room_id(&self) -> &RoomId { |
| 1016 | self.room().room_id() |
1076 | self.room().room_id() |
| 1017 | } |
1077 | } |
| |
1078 | |
| |
1079 | fn recent_ts(&self) -> Option<&MessageTimeStamp> { |
| |
1080 | self.unread.latest() |
| |
1081 | } |
| |
1082 | |
| |
1083 | fn is_unread(&self) -> bool { |
| |
1084 | self.unread.is_unread() |
| |
1085 | } |
| 1018 | } |
1086 | } |
| 1019 | |
1087 | |
| 1020 | impl ToString for DirectItem { |
1088 | impl ToString for DirectItem { |
| @@ -1025,17 +1093,18 @@ impl ToString for DirectItem { |
| 1025 | |
1093 | |
| 1026 | impl ListItem<IambInfo> for DirectItem { |
1094 | impl ListItem<IambInfo> for DirectItem { |
| 1027 | fn show(&self, selected: bool, _: &ViewportContext<ListCursor>, _: &mut ProgramStore) -> Text { |
1095 | fn show(&self, selected: bool, _: &ViewportContext<ListCursor>, _: &mut ProgramStore) -> Text { |
| |
1096 | let unread = self.unread.is_unread(); |
| |
1097 | let style = selected_style(selected); |
| |
1098 | let (name, mut labels) = name_and_labels(&self.name, unread, style); |
| |
1099 | let mut spans = vec![name]; |
| |
1100 | |
| 1028 | if let Some(tags) = &self.tags() { |
1101 | if let Some(tags) = &self.tags() { |
| 1029 | let style = selected_style(selected); |
1102 | labels.extend(tags.keys().map(|t| tag_to_span(t, style))); |
| 1030 | let mut spans = vec![Span::styled(self.name.as_str(), style)]; |
1103 | } |
| 1031 | let tags = tags.keys().map(|t| tag_to_span(t, style)).collect(); |
| |
| 1032 | |
1104 | |
| 1033 | append_tags(tags, &mut spans, style); |
1105 | append_tags(labels, &mut spans, style); |
| 1034 | |
1106 | |
| 1035 | Text::from(Line::from(spans)) |
1107 | Text::from(Line::from(spans)) |
| 1036 | } else { |
| |
| 1037 | selected_text(self.name.as_str(), selected) |
| |
| 1038 | } |
| |
| 1039 | } |
1108 | } |
| 1040 | |
1109 | |
| 1041 | fn get_word(&self) -> Option<String> { |
1110 | fn get_word(&self) -> Option<String> { |
| @@ -1103,6 +1172,16 @@ impl RoomLikeItem for SpaceItem { |
| 1103 | // exposes them, so we'll just always return false here for now. |
1172 | // exposes them, so we'll just always return false here for now. |
| 1104 | false |
1173 | false |
| 1105 | } |
1174 | } |
| |
1175 | |
| |
1176 | fn recent_ts(&self) -> Option<&MessageTimeStamp> { |
| |
1177 | // XXX: this needs to determine the room with most recent message and return its timestamp. |
| |
1178 | None |
| |
1179 | } |
| |
1180 | |
| |
1181 | fn is_unread(&self) -> bool { |
| |
1182 | // XXX: this needs to check whether the space contains rooms with unread messages |
| |
1183 | false |
| |
1184 | } |
| 1106 | } |
1185 | } |
| 1107 | |
1186 | |
| 1108 | impl ToString for SpaceItem { |
1187 | impl ToString for SpaceItem { |
| @@ -1433,6 +1512,7 @@ mod tests { |
| 1433 | tags: Vec<TagName>, |
1512 | tags: Vec<TagName>, |
| 1434 | alias: Option<OwnedRoomAliasId>, |
1513 | alias: Option<OwnedRoomAliasId>, |
| 1435 | name: &'static str, |
1514 | name: &'static str, |
| |
1515 | unread: UnreadInfo, |
| 1436 | } |
1516 | } |
| 1437 | |
1517 | |
| 1438 | impl RoomLikeItem for &TestRoomItem { |
1518 | impl RoomLikeItem for &TestRoomItem { |
| @@ -1451,6 +1531,14 @@ mod tests { |
| 1451 | fn name(&self) -> &str { |
1531 | fn name(&self) -> &str { |
| 1452 | self.name |
1532 | self.name |
| 1453 | } |
1533 | } |
| |
1534 | |
| |
1535 | fn recent_ts(&self) -> Option<&MessageTimeStamp> { |
| |
1536 | self.unread.latest() |
| |
1537 | } |
| |
1538 | |
| |
1539 | fn is_unread(&self) -> bool { |
| |
1540 | self.unread.is_unread() |
| |
1541 | } |
| 1454 | } |
1542 | } |
| 1455 | |
1543 | |
| 1456 | #[test] |
1544 | #[test] |
| @@ -1462,6 +1550,7 @@ mod tests { |
| 1462 | tags: vec![TagName::Favorite], |
1550 | tags: vec![TagName::Favorite], |
| 1463 | alias: Some(room_alias_id!("#room1:example.com").to_owned()), |
1551 | alias: Some(room_alias_id!("#room1:example.com").to_owned()), |
| 1464 | name: "Z", |
1552 | name: "Z", |
| |
1553 | unread: UnreadInfo::default(), |
| 1465 | }; |
1554 | }; |
| 1466 | |
1555 | |
| 1467 | let room2 = TestRoomItem { |
1556 | let room2 = TestRoomItem { |
| @@ -1469,6 +1558,7 @@ mod tests { |
| 1469 | tags: vec![], |
1558 | tags: vec![], |
| 1470 | alias: Some(room_alias_id!("#a:example.com").to_owned()), |
1559 | alias: Some(room_alias_id!("#a:example.com").to_owned()), |
| 1471 | name: "Unnamed Room", |
1560 | name: "Unnamed Room", |
| |
1561 | unread: UnreadInfo::default(), |
| 1472 | }; |
1562 | }; |
| 1473 | |
1563 | |
| 1474 | let room3 = TestRoomItem { |
1564 | let room3 = TestRoomItem { |
| @@ -1476,6 +1566,7 @@ mod tests { |
| 1476 | tags: vec![], |
1566 | tags: vec![], |
| 1477 | alias: None, |
1567 | alias: None, |
| 1478 | name: "Cool Room", |
1568 | name: "Cool Room", |
| |
1569 | unread: UnreadInfo::default(), |
| 1479 | }; |
1570 | }; |
| 1480 | |
1571 | |
| 1481 | // Sort by Name ascending. |
1572 | // Sort by Name ascending. |
| @@ -1510,4 +1601,51 @@ mod tests { |
| 1510 | rooms.sort_by(|a, b| room_fields_cmp(a, b, fields)); |
1601 | rooms.sort_by(|a, b| room_fields_cmp(a, b, fields)); |
| 1511 | assert_eq!(rooms, vec![&room2, &room3, &room1]); |
1602 | assert_eq!(rooms, vec![&room2, &room3, &room1]); |
| 1512 | } |
1603 | } |
| |
1604 | |
| |
1605 | #[test] |
| |
1606 | fn test_sort_room_recents() { |
| |
1607 | let server = server_name!("example.com"); |
| |
1608 | |
| |
1609 | let room1 = TestRoomItem { |
| |
1610 | room_id: RoomId::new(server).to_owned(), |
| |
1611 | tags: vec![], |
| |
1612 | alias: None, |
| |
1613 | name: "Room 1", |
| |
1614 | unread: UnreadInfo { unread: false, latest: None }, |
| |
1615 | }; |
| |
1616 | |
| |
1617 | let room2 = TestRoomItem { |
| |
1618 | room_id: RoomId::new(server).to_owned(), |
| |
1619 | tags: vec![], |
| |
1620 | alias: None, |
| |
1621 | name: "Room 2", |
| |
1622 | unread: UnreadInfo { |
| |
1623 | unread: false, |
| |
1624 | latest: Some(MessageTimeStamp::OriginServer(40u32.into())), |
| |
1625 | }, |
| |
1626 | }; |
| |
1627 | |
| |
1628 | let room3 = TestRoomItem { |
| |
1629 | room_id: RoomId::new(server).to_owned(), |
| |
1630 | tags: vec![], |
| |
1631 | alias: None, |
| |
1632 | name: "Room 3", |
| |
1633 | unread: UnreadInfo { |
| |
1634 | unread: false, |
| |
1635 | latest: Some(MessageTimeStamp::OriginServer(20u32.into())), |
| |
1636 | }, |
| |
1637 | }; |
| |
1638 | |
| |
1639 | // Sort by Recent ascending. |
| |
1640 | let mut rooms = vec![&room1, &room2, &room3]; |
| |
1641 | let fields = &[SortColumn(SortFieldRoom::Recent, SortOrder::Ascending)]; |
| |
1642 | rooms.sort_by(|a, b| room_fields_cmp(a, b, fields)); |
| |
1643 | assert_eq!(rooms, vec![&room2, &room3, &room1]); |
| |
1644 | |
| |
1645 | // Sort by Recent descending. |
| |
1646 | let mut rooms = vec![&room1, &room2, &room3]; |
| |
1647 | let fields = &[SortColumn(SortFieldRoom::Recent, SortOrder::Descending)]; |
| |
1648 | rooms.sort_by(|a, b| room_fields_cmp(a, b, fields)); |
| |
1649 | assert_eq!(rooms, vec![&room1, &room3, &room2]); |
| |
1650 | } |
| 1513 | } |
1651 | } |