Commit 9ed9400b67

9ed9400b67ffee3191c342b817781cbb679e0d8d

parent: 84eaadc09a

Unregistered key

Ulyssa <git@ulyssa.dev> · 2025-05-31 16:29 UTC
committer: <noreply@github.com>

Support automatically toggling room focus (#337)

Layout: unified · split

src/base.rs +7
@@ -1766,6 +1766,13 @@ impl RoomFocus {
17661766 pub fn is_msgbar(&self) -> bool {
17671767 matches!(self, RoomFocus::MessageBar)
17681768 }
1769
1770 pub fn toggle(&mut self) {
1771 *self = match self {
1772 RoomFocus::MessageBar => RoomFocus::Scrollback,
1773 RoomFocus::Scrollback => RoomFocus::MessageBar,
1774 };
1775 }
17691776}
17701777
17711778/// Identifiers used to track where a mark was placed.
src/commands.rs +6 −6
@@ -1141,18 +1141,18 @@ mod tests {
11411141 let mut cmds = setup_commands();
11421142 let ctx = EditContext::default();
11431143
1144 let cmd = format!("room notify set mute");
1145 let res = cmds.input_cmd(&cmd, ctx.clone()).unwrap();
1144 let cmd = "room notify set mute";
1145 let res = cmds.input_cmd(cmd, ctx.clone()).unwrap();
11461146 let act = RoomAction::Set(RoomField::NotificationMode, "mute".into());
11471147 assert_eq!(res, vec![(act.into(), ctx.clone())]);
11481148
1149 let cmd = format!("room notify unset");
1150 let res = cmds.input_cmd(&cmd, ctx.clone()).unwrap();
1149 let cmd = "room notify unset";
1150 let res = cmds.input_cmd(cmd, ctx.clone()).unwrap();
11511151 let act = RoomAction::Unset(RoomField::NotificationMode);
11521152 assert_eq!(res, vec![(act.into(), ctx.clone())]);
11531153
1154 let cmd = format!("room notify show");
1155 let res = cmds.input_cmd(&cmd, ctx.clone()).unwrap();
1154 let cmd = "room notify show";
1155 let res = cmds.input_cmd(cmd, ctx.clone()).unwrap();
11561156 let act = RoomAction::Show(RoomField::NotificationMode);
11571157 assert_eq!(res, vec![(act.into(), ctx.clone())]);
11581158 }
src/windows/room/chat.rs +171 −4
@@ -638,10 +638,7 @@ impl ChatState {
638638 }
639639
640640 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();
645642 }
646643
647644 pub fn room(&self) -> &MatrixRoom {
@@ -652,6 +649,14 @@ impl ChatState {
652649 &self.room_id
653650 }
654651
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
655660 pub fn typing_notice(
656661 &self,
657662 act: &EditorAction,
@@ -754,8 +759,15 @@ impl Editable<ProgramContext, ProgramStore, IambInfo> for ChatState {
754759 ctx: &ProgramContext,
755760 store: &mut ProgramStore,
756761 ) -> 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.
757768 self.typing_notice(act, ctx, store);
758769
770 // And now we can finally run the editor command.
759771 match delegate!(self, w => w.editor_command(act, ctx, store)) {
760772 res @ Ok(_) => res,
761773 Err(EditError::WrongBuffer(IambBufferId::Room(room_id, thread, focus)))
@@ -993,3 +1005,158 @@ fn cmd(open_command: &Vec<String>) -> Option<Command> {
9931005 }
9941006 None
9951007}
1008
1009pub 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)]
1085mod 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}
src/windows/room/scrollback.rs +19 −7
@@ -79,14 +79,20 @@ fn nth_key_before(pos: MessageKey, n: usize, thread: &Messages) -> MessageKey {
7979}
8080
8181fn nth_before(pos: MessageKey, n: usize, thread: &Messages) -> MessageCursor {
82 nth_key_before(pos, n, thread).into()
82 let key = nth_key_before(pos, n, thread);
83
84 if matches!(thread.last_key_value(), Some((last, _)) if &key == last) {
85 MessageCursor::latest()
86 } else {
87 MessageCursor::from(key)
88 }
8389}
8490
85fn nth_key_after(pos: MessageKey, n: usize, thread: &Messages) -> MessageKey {
91fn nth_key_after(pos: MessageKey, n: usize, thread: &Messages) -> Option<MessageKey> {
8692 let mut end = &pos;
87 let iter = thread.range(&pos..).enumerate();
93 let mut iter = thread.range(&pos..).enumerate();
8894
89 for (i, (key, _)) in iter {
95 for (i, (key, _)) in iter.by_ref() {
9096 end = key;
9197
9298 if i >= n {
@@ -94,11 +100,12 @@ fn nth_key_after(pos: MessageKey, n: usize, thread: &Messages) -> MessageKey {
94100 }
95101 }
96102
97 end.clone()
103 // Avoid returning the key if it's at the end.
104 iter.next().map(|_| end.clone())
98105}
99106
100107fn nth_after(pos: MessageKey, n: usize, thread: &Messages) -> MessageCursor {
101 nth_key_after(pos, n, thread).into()
108 nth_key_after(pos, n, thread).map(MessageCursor::from).unwrap_or_default()
102109}
103110
104111fn prevmsg<'a>(key: &MessageKey, thread: &'a Messages) -> Option<&'a Message> {
@@ -150,6 +157,10 @@ impl ScrollbackState {
150157 }
151158 }
152159
160 pub fn is_latest(&self) -> bool {
161 self.cursor.timestamp.is_none()
162 }
163
153164 pub fn goto_latest(&mut self) {
154165 self.cursor = MessageCursor::latest();
155166 }
@@ -1524,8 +1535,9 @@ mod tests {
15241535 scrollback.edit(&EditAction::Motion, &next(1), &ctx, &mut store).unwrap();
15251536 assert_eq!(scrollback.cursor, MSG5_KEY.clone().into());
15261537
1538 // And one more becomes "latest" cursor:
15271539 scrollback.edit(&EditAction::Motion, &next(1), &ctx, &mut store).unwrap();
1528 assert_eq!(scrollback.cursor, MSG1_KEY.clone().into());
1540 assert_eq!(scrollback.cursor, MessageCursor::latest());
15291541 }
15301542
15311543 #[tokio::test]