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 {
1766 pub fn is_msgbar(&self) -> bool { 1766 pub fn is_msgbar(&self) -> bool {
1767 matches!(self, RoomFocus::MessageBar) 1767 matches!(self, RoomFocus::MessageBar)
1768 } 1768 }
1769
1770 pub fn toggle(&mut self) {
1771 *self = match self {
1772 RoomFocus::MessageBar => RoomFocus::Scrollback,
1773 RoomFocus::Scrollback => RoomFocus::MessageBar,
1774 };
1775 }
1769} 1776}
1770 1777
1771/// Identifiers used to track where a mark was placed. 1778/// Identifiers used to track where a mark was placed.
src/commands.rs +6 −6
@@ -1141,18 +1141,18 @@ mod tests {
1141 let mut cmds = setup_commands(); 1141 let mut cmds = setup_commands();
1142 let ctx = EditContext::default(); 1142 let ctx = EditContext::default();
1143 1143
1144 let cmd = format!("room notify set mute"); 1144 let cmd = "room notify set mute";
1145 let res = cmds.input_cmd(&cmd, ctx.clone()).unwrap(); 1145 let res = cmds.input_cmd(cmd, ctx.clone()).unwrap();
1146 let act = RoomAction::Set(RoomField::NotificationMode, "mute".into()); 1146 let act = RoomAction::Set(RoomField::NotificationMode, "mute".into());
1147 assert_eq!(res, vec![(act.into(), ctx.clone())]); 1147 assert_eq!(res, vec![(act.into(), ctx.clone())]);
1148 1148
1149 let cmd = format!("room notify unset"); 1149 let cmd = "room notify unset";
1150 let res = cmds.input_cmd(&cmd, ctx.clone()).unwrap(); 1150 let res = cmds.input_cmd(cmd, ctx.clone()).unwrap();
1151 let act = RoomAction::Unset(RoomField::NotificationMode); 1151 let act = RoomAction::Unset(RoomField::NotificationMode);
1152 assert_eq!(res, vec![(act.into(), ctx.clone())]); 1152 assert_eq!(res, vec![(act.into(), ctx.clone())]);
1153 1153
1154 let cmd = format!("room notify show"); 1154 let cmd = "room notify show";
1155 let res = cmds.input_cmd(&cmd, ctx.clone()).unwrap(); 1155 let res = cmds.input_cmd(cmd, ctx.clone()).unwrap();
1156 let act = RoomAction::Show(RoomField::NotificationMode); 1156 let act = RoomAction::Show(RoomField::NotificationMode);
1157 assert_eq!(res, vec![(act.into(), ctx.clone())]); 1157 assert_eq!(res, vec![(act.into(), ctx.clone())]);
1158 } 1158 }
src/windows/room/chat.rs +171 −4
@@ -638,10 +638,7 @@ impl ChatState {
638 } 638 }
639 639
640 pub fn focus_toggle(&mut self) { 640 pub fn focus_toggle(&mut self) {
641 self.focus = match self.focus { 641 self.focus.toggle();
642 RoomFocus::Scrollback => RoomFocus::MessageBar,
643 RoomFocus::MessageBar => RoomFocus::Scrollback,
644 };
645 } 642 }
646 643
647 pub fn room(&self) -> &MatrixRoom { 644 pub fn room(&self) -> &MatrixRoom {
@@ -652,6 +649,14 @@ impl ChatState {
652 &self.room_id 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 pub fn typing_notice( 660 pub fn typing_notice(
656 &self, 661 &self,
657 act: &EditorAction, 662 act: &EditorAction,
@@ -754,8 +759,15 @@ impl Editable<ProgramContext, ProgramStore, IambInfo> for ChatState {
754 ctx: &ProgramContext, 759 ctx: &ProgramContext,
755 store: &mut ProgramStore, 760 store: &mut ProgramStore,
756 ) -> EditResult<EditInfo, IambInfo> { 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 self.typing_notice(act, ctx, store); 768 self.typing_notice(act, ctx, store);
758 769
770 // And now we can finally run the editor command.
759 match delegate!(self, w => w.editor_command(act, ctx, store)) { 771 match delegate!(self, w => w.editor_command(act, ctx, store)) {
760 res @ Ok(_) => res, 772 res @ Ok(_) => res,
761 Err(EditError::WrongBuffer(IambBufferId::Room(room_id, thread, focus))) 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 None 1006 None
995} 1007}
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 {
79} 79}
80 80
81fn nth_before(pos: MessageKey, n: usize, thread: &Messages) -> MessageCursor { 81fn 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 }
83} 89}
84 90
85fn nth_key_after(pos: MessageKey, n: usize, thread: &Messages) -> MessageKey { 91fn nth_key_after(pos: MessageKey, n: usize, thread: &Messages) -> Option<MessageKey> {
86 let mut end = &pos; 92 let mut end = &pos;
87 let iter = thread.range(&pos..).enumerate(); 93 let mut iter = thread.range(&pos..).enumerate();
88 94
89 for (i, (key, _)) in iter { 95 for (i, (key, _)) in iter.by_ref() {
90 end = key; 96 end = key;
91 97
92 if i >= n { 98 if i >= n {
@@ -94,11 +100,12 @@ fn nth_key_after(pos: MessageKey, n: usize, thread: &Messages) -> MessageKey {
94 } 100 }
95 } 101 }
96 102
97 end.clone() 103 // Avoid returning the key if it's at the end.
104 iter.next().map(|_| end.clone())
98} 105}
99 106
100fn nth_after(pos: MessageKey, n: usize, thread: &Messages) -> MessageCursor { 107fn 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()
102} 109}
103 110
104fn prevmsg<'a>(key: &MessageKey, thread: &'a Messages) -> Option<&'a Message> { 111fn prevmsg<'a>(key: &MessageKey, thread: &'a Messages) -> Option<&'a Message> {
@@ -150,6 +157,10 @@ impl ScrollbackState {
150 } 157 }
151 } 158 }
152 159
160 pub fn is_latest(&self) -> bool {
161 self.cursor.timestamp.is_none()
162 }
163
153 pub fn goto_latest(&mut self) { 164 pub fn goto_latest(&mut self) {
154 self.cursor = MessageCursor::latest(); 165 self.cursor = MessageCursor::latest();
155 } 166 }
@@ -1524,8 +1535,9 @@ mod tests {
1524 scrollback.edit(&EditAction::Motion, &next(1), &ctx, &mut store).unwrap(); 1535 scrollback.edit(&EditAction::Motion, &next(1), &ctx, &mut store).unwrap();
1525 assert_eq!(scrollback.cursor, MSG5_KEY.clone().into()); 1536 assert_eq!(scrollback.cursor, MSG5_KEY.clone().into());
1526 1537
1538 // And one more becomes "latest" cursor:
1527 scrollback.edit(&EditAction::Motion, &next(1), &ctx, &mut store).unwrap(); 1539 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());
1529 } 1541 }
1530 1542
1531 #[tokio::test] 1543 #[tokio::test]