| @@ -638,10 +638,7 @@ impl ChatState { |
| 638 | 638 | } |
| 639 | 639 | |
| 640 | 640 | pub fn focus_toggle(&mut self) { |
| 641 | | self.focus = match self.focus { |
| 642 | | RoomFocus::Scrollback => RoomFocus::MessageBar, |
| 643 | | RoomFocus::MessageBar => RoomFocus::Scrollback, |
| 644 | | }; |
| 641 | self.focus.toggle(); |
| 645 | 642 | } |
| 646 | 643 | |
| 647 | 644 | pub fn room(&self) -> &MatrixRoom { |
| @@ -652,6 +649,14 @@ impl ChatState { |
| 652 | 649 | &self.room_id |
| 653 | 650 | } |
| 654 | 651 | |
| 652 | pub fn auto_toggle_focus( |
| 653 | &mut self, |
| 654 | act: &EditorAction, |
| 655 | ctx: &ProgramContext, |
| 656 | ) -> Option<EditorAction> { |
| 657 | auto_toggle_focus(&mut self.focus, act, ctx, &self.scrollback, &mut self.tbox) |
| 658 | } |
| 659 | |
| 655 | 660 | pub fn typing_notice( |
| 656 | 661 | &self, |
| 657 | 662 | act: &EditorAction, |
| @@ -754,8 +759,15 @@ impl Editable<ProgramContext, ProgramStore, IambInfo> for ChatState { |
| 754 | 759 | ctx: &ProgramContext, |
| 755 | 760 | store: &mut ProgramStore, |
| 756 | 761 | ) -> EditResult<EditInfo, IambInfo> { |
| 762 | // Check whether we should automatically switch between the message bar |
| 763 | // or message scrollback, and use an adjusted action if we do so. |
| 764 | let adjusted = self.auto_toggle_focus(act, ctx); |
| 765 | let act = adjusted.as_ref().unwrap_or(act); |
| 766 | |
| 767 | // Send typing notice if needed. |
| 757 | 768 | self.typing_notice(act, ctx, store); |
| 758 | 769 | |
| 770 | // And now we can finally run the editor command. |
| 759 | 771 | match delegate!(self, w => w.editor_command(act, ctx, store)) { |
| 760 | 772 | res @ Ok(_) => res, |
| 761 | 773 | Err(EditError::WrongBuffer(IambBufferId::Room(room_id, thread, focus))) |
| @@ -993,3 +1005,158 @@ fn cmd(open_command: &Vec<String>) -> Option<Command> { |
| 993 | 1005 | } |
| 994 | 1006 | None |
| 995 | 1007 | } |
| 1008 | |
| 1009 | pub fn auto_toggle_focus( |
| 1010 | focus: &mut RoomFocus, |
| 1011 | act: &EditorAction, |
| 1012 | ctx: &ProgramContext, |
| 1013 | scrollback: &ScrollbackState, |
| 1014 | tbox: &mut TextBoxState<IambInfo>, |
| 1015 | ) -> Option<EditorAction> { |
| 1016 | let is_insert = ctx.get_insert_style().is_some(); |
| 1017 | |
| 1018 | match (focus, act) { |
| 1019 | (f @ RoomFocus::Scrollback, _) if is_insert => { |
| 1020 | // Insert mode commands should switch focus. |
| 1021 | f.toggle(); |
| 1022 | None |
| 1023 | }, |
| 1024 | (f @ RoomFocus::Scrollback, EditorAction::InsertText(_)) => { |
| 1025 | // Pasting or otherwise inserting text should switch. |
| 1026 | f.toggle(); |
| 1027 | None |
| 1028 | }, |
| 1029 | ( |
| 1030 | f @ RoomFocus::Scrollback, |
| 1031 | EditorAction::Edit( |
| 1032 | op, |
| 1033 | EditTarget::Motion(mov @ MoveType::Line(MoveDir1D::Next), count), |
| 1034 | ), |
| 1035 | ) if ctx.resolve(op).is_motion() => { |
| 1036 | let count = ctx.resolve(count); |
| 1037 | |
| 1038 | if count > 0 && scrollback.is_latest() { |
| 1039 | // Trying to move down a line when already at the end of room history should |
| 1040 | // switch. |
| 1041 | f.toggle(); |
| 1042 | |
| 1043 | // And decrement the count for the action. |
| 1044 | let count = count.saturating_sub(1).into(); |
| 1045 | let target = EditTarget::Motion(mov.clone(), count); |
| 1046 | let dec = EditorAction::Edit(op.clone(), target); |
| 1047 | |
| 1048 | Some(dec) |
| 1049 | } else { |
| 1050 | None |
| 1051 | } |
| 1052 | }, |
| 1053 | ( |
| 1054 | f @ RoomFocus::MessageBar, |
| 1055 | EditorAction::Edit( |
| 1056 | op, |
| 1057 | EditTarget::Motion(mov @ MoveType::Line(MoveDir1D::Previous), count), |
| 1058 | ), |
| 1059 | ) if !is_insert && ctx.resolve(op).is_motion() => { |
| 1060 | let count = ctx.resolve(count); |
| 1061 | |
| 1062 | if count > 0 && tbox.get_cursor().y == 0 { |
| 1063 | // Trying to move up a line when already at the top of the msgbar should |
| 1064 | // switch as long as we're not in Insert mode. |
| 1065 | f.toggle(); |
| 1066 | |
| 1067 | // And decrement the count for the action. |
| 1068 | let count = count.saturating_sub(1).into(); |
| 1069 | let target = EditTarget::Motion(mov.clone(), count); |
| 1070 | let dec = EditorAction::Edit(op.clone(), target); |
| 1071 | |
| 1072 | Some(dec) |
| 1073 | } else { |
| 1074 | None |
| 1075 | } |
| 1076 | }, |
| 1077 | (RoomFocus::Scrollback, _) | (RoomFocus::MessageBar, _) => { |
| 1078 | // Do not switch. |
| 1079 | None |
| 1080 | }, |
| 1081 | } |
| 1082 | } |
| 1083 | |
| 1084 | #[cfg(test)] |
| 1085 | mod tests { |
| 1086 | use super::*; |
| 1087 | |
| 1088 | use modalkit::actions::{EditAction, InsertTextAction}; |
| 1089 | |
| 1090 | use crate::tests::{mock_store, TEST_ROOM1_ID}; |
| 1091 | |
| 1092 | macro_rules! move_line { |
| 1093 | ($dir: expr, $count: expr) => { |
| 1094 | EditorAction::Edit( |
| 1095 | EditAction::Motion.into(), |
| 1096 | EditTarget::Motion(MoveType::Line($dir), $count.into()), |
| 1097 | ) |
| 1098 | }; |
| 1099 | } |
| 1100 | |
| 1101 | #[tokio::test] |
| 1102 | async fn test_auto_focus() { |
| 1103 | let mut store = mock_store().await; |
| 1104 | let ctx = ProgramContext::default(); |
| 1105 | |
| 1106 | let room_id = TEST_ROOM1_ID.clone(); |
| 1107 | let scrollback = ScrollbackState::new(room_id.clone(), None); |
| 1108 | |
| 1109 | let id = IambBufferId::Room(room_id, None, RoomFocus::MessageBar); |
| 1110 | let ebuf = store.load_buffer(id); |
| 1111 | let mut tbox = TextBoxState::new(ebuf); |
| 1112 | |
| 1113 | // Start out focused on the scrollback. |
| 1114 | let mut focused = RoomFocus::Scrollback; |
| 1115 | |
| 1116 | // Inserting text toggles: |
| 1117 | let act = EditorAction::InsertText(InsertTextAction::Type( |
| 1118 | Char::from('a').into(), |
| 1119 | MoveDir1D::Next, |
| 1120 | 1.into(), |
| 1121 | )); |
| 1122 | let res = auto_toggle_focus(&mut focused, &act, &ctx, &scrollback, &mut tbox); |
| 1123 | assert_eq!(focused, RoomFocus::MessageBar); |
| 1124 | assert!(res.is_none()); |
| 1125 | |
| 1126 | // Going down in message bar doesn't toggle: |
| 1127 | let act = move_line!(MoveDir1D::Next, 1); |
| 1128 | let res = auto_toggle_focus(&mut focused, &act, &ctx, &scrollback, &mut tbox); |
| 1129 | assert_eq!(focused, RoomFocus::MessageBar); |
| 1130 | assert!(res.is_none()); |
| 1131 | |
| 1132 | // But going up will: |
| 1133 | let act = move_line!(MoveDir1D::Previous, 1); |
| 1134 | let res = auto_toggle_focus(&mut focused, &act, &ctx, &scrollback, &mut tbox); |
| 1135 | assert_eq!(focused, RoomFocus::Scrollback); |
| 1136 | assert_eq!(res, Some(move_line!(MoveDir1D::Previous, 0))); |
| 1137 | |
| 1138 | // Going up in scrollback doesn't toggle: |
| 1139 | let act = move_line!(MoveDir1D::Previous, 1); |
| 1140 | let res = auto_toggle_focus(&mut focused, &act, &ctx, &scrollback, &mut tbox); |
| 1141 | assert_eq!(focused, RoomFocus::Scrollback); |
| 1142 | assert_eq!(res, None); |
| 1143 | |
| 1144 | // And then go back down: |
| 1145 | let act = move_line!(MoveDir1D::Next, 1); |
| 1146 | let res = auto_toggle_focus(&mut focused, &act, &ctx, &scrollback, &mut tbox); |
| 1147 | assert_eq!(focused, RoomFocus::MessageBar); |
| 1148 | assert_eq!(res, Some(move_line!(MoveDir1D::Next, 0))); |
| 1149 | |
| 1150 | // Go up 2 will go up 1 in scrollback: |
| 1151 | let act = move_line!(MoveDir1D::Previous, 2); |
| 1152 | let res = auto_toggle_focus(&mut focused, &act, &ctx, &scrollback, &mut tbox); |
| 1153 | assert_eq!(focused, RoomFocus::Scrollback); |
| 1154 | assert_eq!(res, Some(move_line!(MoveDir1D::Previous, 1))); |
| 1155 | |
| 1156 | // Go down 3 will go down 2 in messagebar: |
| 1157 | let act = move_line!(MoveDir1D::Next, 3); |
| 1158 | let res = auto_toggle_focus(&mut focused, &act, &ctx, &scrollback, &mut tbox); |
| 1159 | assert_eq!(focused, RoomFocus::MessageBar); |
| 1160 | assert_eq!(res, Some(move_line!(MoveDir1D::Next, 2))); |
| 1161 | } |
| 1162 | } |