| @@ -1,3 +1,6 @@ |
| 1 | //! # Common types and utilities |
| 2 | //! |
| 3 | //! The types defined here get used throughout iamb. |
| 1 | 4 | use std::borrow::Cow; |
| 2 | 5 | use std::collections::{HashMap, HashSet}; |
| 3 | 6 | use std::convert::TryFrom; |
| @@ -84,6 +87,7 @@ use crate::{ |
| 84 | 87 | ApplicationSettings, |
| 85 | 88 | }; |
| 86 | 89 | |
| 90 | /// The set of characters used in different Matrix IDs. |
| 87 | 91 | pub const MATRIX_ID_WORD: WordStyle = WordStyle::CharSet(is_mxid_char); |
| 88 | 92 | |
| 89 | 93 | /// Find the boundaries for a Matrix username, room alias, or room ID. |
| @@ -100,17 +104,27 @@ fn is_mxid_char(c: char) -> bool { |
| 100 | 104 | |
| 101 | 105 | const ROOM_FETCH_DEBOUNCE: Duration = Duration::from_secs(2); |
| 102 | 106 | |
| 107 | /// Empty type used solely to implement [ApplicationInfo]. |
| 103 | 108 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 104 | 109 | pub enum IambInfo {} |
| 105 | 110 | |
| 111 | /// An action taken against an ongoing verification request. |
| 106 | 112 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 107 | 113 | pub enum VerifyAction { |
| 114 | /// Accept a verification request. |
| 108 | 115 | Accept, |
| 116 | |
| 117 | /// Cancel an in-progress verification. |
| 109 | 118 | Cancel, |
| 119 | |
| 120 | /// Confirm an in-progress verification. |
| 110 | 121 | Confirm, |
| 122 | |
| 123 | /// Reject an in-progress verification due to mismatched Emoji. |
| 111 | 124 | Mismatch, |
| 112 | 125 | } |
| 113 | 126 | |
| 127 | /// An action taken against the currently selected message. |
| 114 | 128 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 115 | 129 | pub enum MessageAction { |
| 116 | 130 | /// Cance the current reply or edit. |
| @@ -145,6 +159,7 @@ pub enum MessageAction { |
| 145 | 159 | Unreact(Option<String>), |
| 146 | 160 | } |
| 147 | 161 | |
| 162 | /// The type of room being created. |
| 148 | 163 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 149 | 164 | pub enum CreateRoomType { |
| 150 | 165 | /// A direct message room. |
| @@ -158,7 +173,9 @@ pub enum CreateRoomType { |
| 158 | 173 | } |
| 159 | 174 | |
| 160 | 175 | bitflags::bitflags! { |
| 176 | /// Available options for newly created rooms. |
| 161 | 177 | pub struct CreateRoomFlags: u32 { |
| 178 | /// No flags specified. |
| 162 | 179 | const NONE = 0b00000000; |
| 163 | 180 | |
| 164 | 181 | /// Make the room public. |
| @@ -170,7 +187,9 @@ bitflags::bitflags! { |
| 170 | 187 | } |
| 171 | 188 | |
| 172 | 189 | bitflags::bitflags! { |
| 190 | /// Available options when downloading files. |
| 173 | 191 | pub struct DownloadFlags: u32 { |
| 192 | /// No flags specified. |
| 174 | 193 | const NONE = 0b00000000; |
| 175 | 194 | |
| 176 | 195 | /// Overwrite file if it already exists. |
| @@ -181,45 +200,91 @@ bitflags::bitflags! { |
| 181 | 200 | } |
| 182 | 201 | } |
| 183 | 202 | |
| 203 | /// A room property. |
| 184 | 204 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 185 | 205 | pub enum RoomField { |
| 206 | /// The room name. |
| 186 | 207 | Name, |
| 208 | |
| 209 | /// A room tag. |
| 187 | 210 | Tag(TagName), |
| 211 | |
| 212 | /// The room topic. |
| 188 | 213 | Topic, |
| 189 | 214 | } |
| 190 | 215 | |
| 216 | /// An action that operates on a focused room. |
| 191 | 217 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 192 | 218 | pub enum RoomAction { |
| 219 | /// Accept an invitation to join this room. |
| 193 | 220 | InviteAccept, |
| 221 | |
| 222 | /// Reject an invitation to join this room. |
| 194 | 223 | InviteReject, |
| 224 | |
| 225 | /// Invite a user to this room. |
| 195 | 226 | InviteSend(OwnedUserId), |
| 227 | |
| 228 | /// Leave this room. |
| 196 | 229 | Leave(bool), |
| 230 | |
| 231 | /// Open the members window. |
| 197 | 232 | Members(Box<CommandContext<ProgramContext>>), |
| 233 | |
| 234 | /// Set a room property. |
| 198 | 235 | Set(RoomField, String), |
| 236 | |
| 237 | /// Unset a room property. |
| 199 | 238 | Unset(RoomField), |
| 200 | 239 | } |
| 201 | 240 | |
| 241 | /// An action that sends a message to a room. |
| 202 | 242 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 203 | 243 | pub enum SendAction { |
| 244 | /// Send the text in the message bar. |
| 204 | 245 | Submit, |
| 246 | |
| 247 | /// Send text provided from an external editor. |
| 205 | 248 | SubmitFromEditor, |
| 249 | |
| 250 | /// Upload a file. |
| 206 | 251 | Upload(String), |
| 252 | |
| 253 | /// Upload the image data. |
| 207 | 254 | UploadImage(usize, usize, Cow<'static, [u8]>), |
| 208 | 255 | } |
| 209 | 256 | |
| 257 | /// An action performed against the user's homeserver. |
| 210 | 258 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 211 | 259 | pub enum HomeserverAction { |
| 260 | /// Create a new room with an optional localpart. |
| 212 | 261 | CreateRoom(Option<String>, CreateRoomType, CreateRoomFlags), |
| 213 | 262 | } |
| 214 | 263 | |
| 264 | /// An action that the main program loop should. |
| 265 | /// |
| 266 | /// See [the commands module][super::commands] for where these are usually created. |
| 215 | 267 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 216 | 268 | pub enum IambAction { |
| 269 | /// Perform an action against the homeserver. |
| 217 | 270 | Homeserver(HomeserverAction), |
| 271 | |
| 272 | /// Perform an action on the currently selected message. |
| 218 | 273 | Message(MessageAction), |
| 274 | |
| 275 | /// Perform an action on the currently focused room. |
| 219 | 276 | Room(RoomAction), |
| 277 | |
| 278 | /// Send a message to the currently focused room. |
| 220 | 279 | Send(SendAction), |
| 280 | |
| 281 | /// Perform an action for an in-progress verification. |
| 221 | 282 | Verify(VerifyAction, String), |
| 283 | |
| 284 | /// Request a new verification with the specified user. |
| 222 | 285 | VerifyRequest(String), |
| 286 | |
| 287 | /// Toggle the focus within the focused room. |
| 223 | 288 | ToggleScrollbackFocus, |
| 224 | 289 | } |
| 225 | 290 | |
| @@ -316,14 +381,21 @@ impl From<IambAction> for ProgramAction { |
| 316 | 381 | } |
| 317 | 382 | } |
| 318 | 383 | |
| 384 | /// Alias for program actions. |
| 319 | 385 | pub type ProgramAction = Action<IambInfo>; |
| 386 | /// Alias for program context. |
| 320 | 387 | pub type ProgramContext = VimContext<IambInfo>; |
| 388 | /// Alias for program keybindings. |
| 321 | 389 | pub type Keybindings = VimMachine<TerminalKey, IambInfo>; |
| 390 | /// Alias for a program command. |
| 322 | 391 | pub type ProgramCommand = VimCommand<ProgramContext, IambInfo>; |
| 392 | /// Alias for mapped program commands. |
| 323 | 393 | pub type ProgramCommands = VimCommandMachine<ProgramContext, IambInfo>; |
| 394 | /// Alias for program store. |
| 324 | 395 | pub type ProgramStore = Store<IambInfo>; |
| 396 | /// Alias for shared program store. |
| 325 | 397 | pub type AsyncProgramStore = Arc<AsyncMutex<ProgramStore>>; |
| 326 | | |
| 398 | /// Alias for an action result. |
| 327 | 399 | pub type IambResult<T> = UIResult<T, IambInfo>; |
| 328 | 400 | |
| 329 | 401 | /// Reaction events for some message. |
| @@ -332,61 +404,81 @@ pub type IambResult<T> = UIResult<T, IambInfo>; |
| 332 | 404 | /// it's reacting to. |
| 333 | 405 | pub type MessageReactions = HashMap<OwnedEventId, (String, OwnedUserId)>; |
| 334 | 406 | |
| 407 | /// Map of read receipts for different events. |
| 335 | 408 | pub type Receipts = HashMap<OwnedEventId, Vec<OwnedUserId>>; |
| 336 | 409 | |
| 410 | /// Errors encountered during application use. |
| 337 | 411 | #[derive(thiserror::Error, Debug)] |
| 338 | 412 | pub enum IambError { |
| 413 | /// An invalid user identifier was specified. |
| 339 | 414 | #[error("Invalid user identifier: {0}")] |
| 340 | 415 | InvalidUserId(String), |
| 341 | 416 | |
| 417 | /// An invalid verification identifier was specified. |
| 342 | 418 | #[error("Invalid verification user/device pair: {0}")] |
| 343 | 419 | InvalidVerificationId(String), |
| 344 | 420 | |
| 421 | /// A failure related to the cryptographic store. |
| 345 | 422 | #[error("Cryptographic storage error: {0}")] |
| 346 | 423 | CryptoStore(#[from] matrix_sdk::encryption::CryptoStoreError), |
| 347 | 424 | |
| 425 | /// An HTTP error. |
| 348 | 426 | #[error("HTTP client error: {0}")] |
| 349 | 427 | Http(#[from] matrix_sdk::HttpError), |
| 350 | 428 | |
| 429 | /// A failure from the Matrix client. |
| 351 | 430 | #[error("Matrix client error: {0}")] |
| 352 | 431 | Matrix(#[from] matrix_sdk::Error), |
| 353 | 432 | |
| 433 | /// A failure in the sled storage. |
| 354 | 434 | #[error("Matrix client storage error: {0}")] |
| 355 | 435 | Store(#[from] matrix_sdk::StoreError), |
| 356 | 436 | |
| 437 | /// A failure during serialization or deserialization. |
| 357 | 438 | #[error("Serialization/deserialization error: {0}")] |
| 358 | 439 | Serde(#[from] serde_json::Error), |
| 359 | 440 | |
| 441 | /// A failure due to not having a configured download directory. |
| 360 | 442 | #[error("No download directory configured")] |
| 361 | 443 | NoDownloadDir, |
| 362 | 444 | |
| 445 | /// A failure due to not having a message with an attachment selected. |
| 363 | 446 | #[error("Selected message does not have any attachments")] |
| 364 | 447 | NoAttachment, |
| 365 | 448 | |
| 449 | /// A failure due to not having a message selected. |
| 366 | 450 | #[error("No message currently selected")] |
| 367 | 451 | NoSelectedMessage, |
| 368 | 452 | |
| 453 | /// A failure due to not having a room or space selected. |
| 369 | 454 | #[error("Current window is not a room or space")] |
| 370 | 455 | NoSelectedRoomOrSpace, |
| 371 | 456 | |
| 457 | /// A failure due to not having a room selected. |
| 372 | 458 | #[error("Current window is not a room")] |
| 373 | 459 | NoSelectedRoom, |
| 374 | 460 | |
| 461 | /// A failure due to not having an outstanding room invitation. |
| 375 | 462 | #[error("You do not have a current invitation to this room")] |
| 376 | 463 | NotInvited, |
| 377 | 464 | |
| 465 | /// A failure due to not being a joined room member. |
| 378 | 466 | #[error("You need to join the room before you can do that")] |
| 379 | 467 | NotJoined, |
| 380 | 468 | |
| 469 | /// An unknown room was specified. |
| 381 | 470 | #[error("Unknown room identifier: {0}")] |
| 382 | 471 | UnknownRoom(OwnedRoomId), |
| 383 | 472 | |
| 473 | /// A failure occurred during verification. |
| 384 | 474 | #[error("Verification request error: {0}")] |
| 385 | 475 | VerificationRequestError(#[from] matrix_sdk::encryption::identities::RequestVerificationError), |
| 386 | 476 | |
| 477 | /// A failure related to images. |
| 387 | 478 | #[error("Image error: {0}")] |
| 388 | 479 | Image(#[from] image::ImageError), |
| 389 | 480 | |
| 481 | /// A failure to access the system's clipboard. |
| 390 | 482 | #[error("Could not use system clipboard data")] |
| 391 | 483 | Clipboard, |
| 392 | 484 | } |
| @@ -399,16 +491,26 @@ impl From<IambError> for UIError<IambInfo> { |
| 399 | 491 | |
| 400 | 492 | impl ApplicationError for IambError {} |
| 401 | 493 | |
| 494 | /// Status for tracking how much room scrollback we've fetched. |
| 402 | 495 | #[derive(Default)] |
| 403 | 496 | pub enum RoomFetchStatus { |
| 497 | /// Room history has been completely fetched. |
| 404 | 498 | Done, |
| 499 | |
| 500 | /// More room history can be fetched. |
| 405 | 501 | HaveMore(String), |
| 502 | |
| 503 | /// We have not yet started fetching history for this room. |
| 406 | 504 | #[default] |
| 407 | 505 | NotStarted, |
| 408 | 506 | } |
| 409 | 507 | |
| 508 | /// Indicates where an [EventId] lives in the [ChatStore]. |
| 410 | 509 | pub enum EventLocation { |
| 510 | /// The [EventId] belongs to a message. |
| 411 | 511 | Message(MessageKey), |
| 512 | |
| 513 | /// The [EventId] belongs to a reaction to the given event. |
| 412 | 514 | Reaction(OwnedEventId), |
| 413 | 515 | } |
| 414 | 516 | |
| @@ -422,6 +524,7 @@ impl EventLocation { |
| 422 | 524 | } |
| 423 | 525 | } |
| 424 | 526 | |
| 527 | /// Information about room's the user's joined. |
| 425 | 528 | #[derive(Default)] |
| 426 | 529 | pub struct RoomInfo { |
| 427 | 530 | /// The display name for this room. |
| @@ -462,6 +565,7 @@ pub struct RoomInfo { |
| 462 | 565 | } |
| 463 | 566 | |
| 464 | 567 | impl RoomInfo { |
| 568 | /// Get the reactions and their counts for a message. |
| 465 | 569 | pub fn get_reactions(&self, event_id: &EventId) -> Vec<(&str, usize)> { |
| 466 | 570 | if let Some(reacts) = self.reactions.get(event_id) { |
| 467 | 571 | let mut counts = HashMap::new(); |
| @@ -480,14 +584,17 @@ impl RoomInfo { |
| 480 | 584 | } |
| 481 | 585 | } |
| 482 | 586 | |
| 587 | /// Map an event identifier to its [MessageKey]. |
| 483 | 588 | pub fn get_message_key(&self, event_id: &EventId) -> Option<&MessageKey> { |
| 484 | 589 | self.keys.get(event_id)?.to_message_key() |
| 485 | 590 | } |
| 486 | 591 | |
| 592 | /// Get an event for an identifier. |
| 487 | 593 | pub fn get_event(&self, event_id: &EventId) -> Option<&Message> { |
| 488 | 594 | self.messages.get(self.get_message_key(event_id)?) |
| 489 | 595 | } |
| 490 | 596 | |
| 597 | /// Insert a reaction to a message. |
| 491 | 598 | pub fn insert_reaction(&mut self, react: ReactionEvent) { |
| 492 | 599 | match react { |
| 493 | 600 | MessageLikeEvent::Original(react) => { |
| @@ -509,6 +616,7 @@ impl RoomInfo { |
| 509 | 616 | } |
| 510 | 617 | } |
| 511 | 618 | |
| 619 | /// Insert an edit. |
| 512 | 620 | pub fn insert_edit(&mut self, msg: Replacement) { |
| 513 | 621 | let event_id = msg.event_id; |
| 514 | 622 | let new_content = msg.new_content; |
| @@ -551,6 +659,7 @@ impl RoomInfo { |
| 551 | 659 | self.messages.insert(key, msg.into()); |
| 552 | 660 | } |
| 553 | 661 | |
| 662 | /// Insert a new message. |
| 554 | 663 | pub fn insert_message(&mut self, msg: RoomMessageEvent) { |
| 555 | 664 | let event_id = msg.event_id().to_owned(); |
| 556 | 665 | let key = (msg.origin_server_ts().into(), event_id.clone()); |
| @@ -563,6 +672,7 @@ impl RoomInfo { |
| 563 | 672 | let _ = self.messages.remove(&key); |
| 564 | 673 | } |
| 565 | 674 | |
| 675 | /// Insert a new message event. |
| 566 | 676 | pub fn insert(&mut self, msg: RoomMessageEvent) { |
| 567 | 677 | match msg { |
| 568 | 678 | RoomMessageEvent::Original(OriginalRoomMessageEvent { |
| @@ -574,6 +684,7 @@ impl RoomInfo { |
| 574 | 684 | } |
| 575 | 685 | } |
| 576 | 686 | |
| 687 | /// Indicates whether we've recently fetched scrollback for this room. |
| 577 | 688 | pub fn recently_fetched(&self) -> bool { |
| 578 | 689 | self.fetch_last.map_or(false, |i| i.elapsed() < ROOM_FETCH_DEBOUNCE) |
| 579 | 690 | } |
| @@ -617,10 +728,12 @@ impl RoomInfo { |
| 617 | 728 | } |
| 618 | 729 | } |
| 619 | 730 | |
| 731 | /// Update typing information for this room. |
| 620 | 732 | pub fn set_typing(&mut self, user_ids: Vec<OwnedUserId>) { |
| 621 | 733 | self.users_typing = (Instant::now(), user_ids).into(); |
| 622 | 734 | } |
| 623 | 735 | |
| 736 | /// Create a [Rect] that displays what users are typing. |
| 624 | 737 | pub fn render_typing( |
| 625 | 738 | &mut self, |
| 626 | 739 | area: Rect, |
| @@ -646,6 +759,7 @@ impl RoomInfo { |
| 646 | 759 | } |
| 647 | 760 | } |
| 648 | 761 | |
| 762 | /// Generate a [CompletionMap] for Emoji shortcodes. |
| 649 | 763 | fn emoji_map() -> CompletionMap<String, &'static Emoji> { |
| 650 | 764 | let mut emojis = CompletionMap::default(); |
| 651 | 765 | |
| @@ -658,27 +772,54 @@ fn emoji_map() -> CompletionMap<String, &'static Emoji> { |
| 658 | 772 | return emojis; |
| 659 | 773 | } |
| 660 | 774 | |
| 775 | /// Information gathered during server syncs about joined rooms. |
| 661 | 776 | #[derive(Default)] |
| 662 | 777 | pub struct SyncInfo { |
| 778 | /// Spaces that the user is a member of. |
| 663 | 779 | pub spaces: Vec<MatrixRoom>, |
| 780 | |
| 781 | /// Rooms that the user is a member of. |
| 664 | 782 | pub rooms: Vec<Arc<(MatrixRoom, Option<Tags>)>>, |
| 783 | |
| 784 | /// DMs that the user is a member of. |
| 665 | 785 | pub dms: Vec<Arc<(MatrixRoom, Option<Tags>)>>, |
| 666 | 786 | } |
| 667 | 787 | |
| 788 | /// The main application state. |
| 668 | 789 | pub struct ChatStore { |
| 790 | /// `:`-commands |
| 669 | 791 | pub cmds: ProgramCommands, |
| 792 | |
| 793 | /// Handle for communicating w/ the worker thread. |
| 670 | 794 | pub worker: Requester, |
| 795 | |
| 796 | /// Map of joined rooms. |
| 671 | 797 | pub rooms: CompletionMap<OwnedRoomId, RoomInfo>, |
| 798 | |
| 799 | /// Map of room names. |
| 672 | 800 | pub names: CompletionMap<String, OwnedRoomId>, |
| 801 | |
| 802 | /// Presence information for other users. |
| 673 | 803 | pub presences: CompletionMap<OwnedUserId, PresenceState>, |
| 804 | |
| 805 | /// In-progress and completed verifications. |
| 674 | 806 | pub verifications: HashMap<String, SasVerification>, |
| 807 | |
| 808 | /// Settings for the current profile loaded from config file. |
| 675 | 809 | pub settings: ApplicationSettings, |
| 810 | |
| 811 | /// Set of rooms that need more messages loaded in their scrollback. |
| 676 | 812 | pub need_load: HashSet<OwnedRoomId>, |
| 813 | |
| 814 | /// [CompletionMap] of Emoji shortcodes. |
| 677 | 815 | pub emojis: CompletionMap<String, &'static Emoji>, |
| 816 | |
| 817 | /// Information gathered by the background thread. |
| 678 | 818 | pub sync_info: SyncInfo, |
| 679 | 819 | } |
| 680 | 820 | |
| 681 | 821 | impl ChatStore { |
| 822 | /// Create a new [ChatStore]. |
| 682 | 823 | pub fn new(worker: Requester, settings: ApplicationSettings) -> Self { |
| 683 | 824 | ChatStore { |
| 684 | 825 | worker, |
| @@ -696,10 +837,12 @@ impl ChatStore { |
| 696 | 837 | } |
| 697 | 838 | } |
| 698 | 839 | |
| 840 | /// Get a joined room. |
| 699 | 841 | pub fn get_joined_room(&self, room_id: &RoomId) -> Option<Joined> { |
| 700 | 842 | self.worker.client.get_joined_room(room_id) |
| 701 | 843 | } |
| 702 | 844 | |
| 845 | /// Get the title for a room. |
| 703 | 846 | pub fn get_room_title(&self, room_id: &RoomId) -> String { |
| 704 | 847 | self.rooms |
| 705 | 848 | .get(room_id) |
| @@ -708,6 +851,7 @@ impl ChatStore { |
| 708 | 851 | .unwrap_or_else(|| "Untitled Matrix Room".to_string()) |
| 709 | 852 | } |
| 710 | 853 | |
| 854 | /// Update the receipts for multiple rooms. |
| 711 | 855 | pub async fn set_receipts( |
| 712 | 856 | &mut self, |
| 713 | 857 | receipts: Vec<(OwnedRoomId, Receipts)>, |
| @@ -727,18 +871,22 @@ impl ChatStore { |
| 727 | 871 | return updates; |
| 728 | 872 | } |
| 729 | 873 | |
| 874 | /// Mark a room for loading more scrollback. |
| 730 | 875 | pub fn mark_for_load(&mut self, room_id: OwnedRoomId) { |
| 731 | 876 | self.need_load.insert(room_id); |
| 732 | 877 | } |
| 733 | 878 | |
| 879 | /// Get the [RoomInfo] for a given room identifier. |
| 734 | 880 | pub fn get_room_info(&mut self, room_id: OwnedRoomId) -> &mut RoomInfo { |
| 735 | 881 | self.rooms.get_or_default(room_id) |
| 736 | 882 | } |
| 737 | 883 | |
| 884 | /// Set the name for a room. |
| 738 | 885 | pub fn set_room_name(&mut self, room_id: &RoomId, name: &str) { |
| 739 | 886 | self.rooms.get_or_default(room_id.to_owned()).name = name.to_string().into(); |
| 740 | 887 | } |
| 741 | 888 | |
| 889 | /// Insert a new E2EE verification. |
| 742 | 890 | pub fn insert_sas(&mut self, sas: SasVerification) { |
| 743 | 891 | let key = format!("{}/{}", sas.other_user_id(), sas.other_device().device_id()); |
| 744 | 892 | |
| @@ -748,6 +896,7 @@ impl ChatStore { |
| 748 | 896 | |
| 749 | 897 | impl ApplicationStore for ChatStore {} |
| 750 | 898 | |
| 899 | /// Identified used to track window content. |
| 751 | 900 | #[derive(Clone, Debug, Eq, Hash, PartialEq)] |
| 752 | 901 | pub enum IambId { |
| 753 | 902 | /// A Matrix room. |
| @@ -810,6 +959,7 @@ impl<'de> Deserialize<'de> for IambId { |
| 810 | 959 | } |
| 811 | 960 | } |
| 812 | 961 | |
| 962 | /// [serde] visitor for deserializing [IambId]. |
| 813 | 963 | struct IambIdVisitor; |
| 814 | 964 | |
| 815 | 965 | impl<'de> Visitor<'de> for IambIdVisitor { |
| @@ -903,35 +1053,61 @@ impl<'de> Visitor<'de> for IambIdVisitor { |
| 903 | 1053 | } |
| 904 | 1054 | } |
| 905 | 1055 | |
| 1056 | /// Which part of the room window's UI is focused. |
| 906 | 1057 | #[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)] |
| 907 | 1058 | pub enum RoomFocus { |
| 1059 | /// The scrollback for a room window is focused. |
| 908 | 1060 | Scrollback, |
| 1061 | |
| 1062 | /// The message bar for a room window is focused. |
| 909 | 1063 | MessageBar, |
| 910 | 1064 | } |
| 911 | 1065 | |
| 912 | 1066 | impl RoomFocus { |
| 1067 | /// Whether this is [RoomFocus::Scrollback]. |
| 913 | 1068 | pub fn is_scrollback(&self) -> bool { |
| 914 | 1069 | matches!(self, RoomFocus::Scrollback) |
| 915 | 1070 | } |
| 916 | 1071 | |
| 1072 | /// Whether this is [RoomFocus::MessageBar]. |
| 917 | 1073 | pub fn is_msgbar(&self) -> bool { |
| 918 | 1074 | matches!(self, RoomFocus::MessageBar) |
| 919 | 1075 | } |
| 920 | 1076 | } |
| 921 | 1077 | |
| 1078 | /// Identifiers used to track where a mark was placed. |
| 1079 | /// |
| 1080 | /// While this is the "buffer identifier" for the mark, |
| 1081 | /// not all of these are necessarily actual buffers. |
| 922 | 1082 | #[derive(Clone, Debug, Eq, Hash, PartialEq)] |
| 923 | 1083 | pub enum IambBufferId { |
| 1084 | /// The command bar buffer. |
| 924 | 1085 | Command(CommandType), |
| 1086 | |
| 1087 | /// The message buffer or a specific message in a room. |
| 925 | 1088 | Room(OwnedRoomId, RoomFocus), |
| 1089 | |
| 1090 | /// The `:dms` window. |
| 926 | 1091 | DirectList, |
| 1092 | |
| 1093 | /// The `:members` window for a room. |
| 927 | 1094 | MemberList(OwnedRoomId), |
| 1095 | |
| 1096 | /// The `:rooms` window. |
| 928 | 1097 | RoomList, |
| 1098 | |
| 1099 | /// The `:spaces` window. |
| 929 | 1100 | SpaceList, |
| 1101 | |
| 1102 | /// The `:verify` window. |
| 930 | 1103 | VerifyList, |
| 1104 | |
| 1105 | /// The buffer for the `:rooms` window. |
| 931 | 1106 | Welcome, |
| 932 | 1107 | } |
| 933 | 1108 | |
| 934 | 1109 | impl IambBufferId { |
| 1110 | /// Get the identifier for the window that contains this buffer. |
| 935 | 1111 | pub fn to_window(&self) -> Option<IambId> { |
| 936 | 1112 | match self { |
| 937 | 1113 | IambBufferId::Command(_) => None, |
| @@ -982,6 +1158,7 @@ impl ApplicationInfo for IambInfo { |
| 982 | 1158 | } |
| 983 | 1159 | } |
| 984 | 1160 | |
| 1161 | /// Tab completion for user IDs. |
| 985 | 1162 | fn complete_users(text: &EditRope, cursor: &mut Cursor, store: &ProgramStore) -> Vec<String> { |
| 986 | 1163 | let id = text |
| 987 | 1164 | .get_prefix_word_mut(cursor, &MATRIX_ID_WORD) |
| @@ -997,6 +1174,7 @@ fn complete_users(text: &EditRope, cursor: &mut Cursor, store: &ProgramStore) -> |
| 997 | 1174 | .collect() |
| 998 | 1175 | } |
| 999 | 1176 | |
| 1177 | /// Tab completion within the message bar. |
| 1000 | 1178 | fn complete_msgbar(text: &EditRope, cursor: &mut Cursor, store: &ProgramStore) -> Vec<String> { |
| 1001 | 1179 | let id = text |
| 1002 | 1180 | .get_prefix_word_mut(cursor, &MATRIX_ID_WORD) |
| @@ -1044,6 +1222,7 @@ fn complete_msgbar(text: &EditRope, cursor: &mut Cursor, store: &ProgramStore) - |
| 1044 | 1222 | } |
| 1045 | 1223 | } |
| 1046 | 1224 | |
| 1225 | /// Tab completion for Matrix identifiers (usernames, room aliases, etc.) |
| 1047 | 1226 | fn complete_matrix_names( |
| 1048 | 1227 | text: &EditRope, |
| 1049 | 1228 | cursor: &mut Cursor, |
| @@ -1073,6 +1252,7 @@ fn complete_matrix_names( |
| 1073 | 1252 | .collect() |
| 1074 | 1253 | } |
| 1075 | 1254 | |
| 1255 | /// Tab completion for Emoji shortcode names. |
| 1076 | 1256 | fn complete_emoji(text: &EditRope, cursor: &mut Cursor, store: &ProgramStore) -> Vec<String> { |
| 1077 | 1257 | let sc = text.get_prefix_word_mut(cursor, &WordStyle::Little); |
| 1078 | 1258 | let sc = sc.unwrap_or_else(EditRope::empty); |
| @@ -1081,6 +1261,7 @@ fn complete_emoji(text: &EditRope, cursor: &mut Cursor, store: &ProgramStore) -> |
| 1081 | 1261 | store.application.emojis.complete(sc.as_ref()) |
| 1082 | 1262 | } |
| 1083 | 1263 | |
| 1264 | /// Tab completion for command names. |
| 1084 | 1265 | fn complete_cmdname( |
| 1085 | 1266 | desc: CommandDescription, |
| 1086 | 1267 | text: &EditRope, |
| @@ -1092,6 +1273,7 @@ fn complete_cmdname( |
| 1092 | 1273 | store.application.cmds.complete_name(desc.command.as_str()) |
| 1093 | 1274 | } |
| 1094 | 1275 | |
| 1276 | /// Tab completion for command arguments. |
| 1095 | 1277 | fn complete_cmdarg( |
| 1096 | 1278 | desc: CommandDescription, |
| 1097 | 1279 | text: &EditRope, |
| @@ -1120,6 +1302,7 @@ fn complete_cmdarg( |
| 1120 | 1302 | } |
| 1121 | 1303 | } |
| 1122 | 1304 | |
| 1305 | /// Tab completion for commands. |
| 1123 | 1306 | fn complete_cmd( |
| 1124 | 1307 | cmd: &str, |
| 1125 | 1308 | text: &EditRope, |
| @@ -1141,6 +1324,7 @@ fn complete_cmd( |
| 1141 | 1324 | } |
| 1142 | 1325 | } |
| 1143 | 1326 | |
| 1327 | /// Tab completion for the command bar. |
| 1144 | 1328 | fn complete_cmdbar(text: &EditRope, cursor: &mut Cursor, store: &ProgramStore) -> Vec<String> { |
| 1145 | 1329 | let eo = text.cursor_to_offset(cursor); |
| 1146 | 1330 | let slice = text.slice(0.into(), eo, false); |