| @@ -596,8 +596,19 @@ impl ChatState { |
| 596 | &mut self, |
596 | &mut self, |
| 597 | act: &EditorAction, |
597 | act: &EditorAction, |
| 598 | ctx: &ProgramContext, |
598 | ctx: &ProgramContext, |
| |
599 | info: &RoomInfo, |
| |
600 | settings: &ApplicationSettings, |
| 599 | ) -> Option<EditorAction> { |
601 | ) -> Option<EditorAction> { |
| 600 | auto_toggle_focus(&mut self.focus, act, ctx, &self.scrollback, &mut self.tbox) |
602 | auto_toggle_focus( |
| |
603 | &mut self.focus, |
| |
604 | act, |
| |
605 | ctx, |
| |
606 | &self.scrollback, |
| |
607 | &mut self.tbox, |
| |
608 | &mut self.reply_to, |
| |
609 | info, |
| |
610 | settings, |
| |
611 | ) |
| 601 | } |
612 | } |
| 602 | |
613 | |
| 603 | pub fn typing_notice( |
614 | pub fn typing_notice( |
| @@ -1119,7 +1130,8 @@ impl Editable<ProgramContext, ProgramStore, IambInfo> for ChatState { |
| 1119 | ) -> EditResult<EditInfo, IambInfo> { |
1130 | ) -> EditResult<EditInfo, IambInfo> { |
| 1120 | // Check whether we should automatically switch between the message bar |
1131 | // Check whether we should automatically switch between the message bar |
| 1121 | // or message scrollback, and use an adjusted action if we do so. |
1132 | // or message scrollback, and use an adjusted action if we do so. |
| 1122 | let adjusted = self.auto_toggle_focus(act, ctx); |
1133 | let info = store.application.rooms.get_or_default(self.room_id.clone()); |
| |
1134 | let adjusted = self.auto_toggle_focus(act, ctx, info, &store.application.settings); |
| 1123 | let act = adjusted.as_ref().unwrap_or(act); |
1135 | let act = adjusted.as_ref().unwrap_or(act); |
| 1124 | |
1136 | |
| 1125 | // Send typing notice if needed. |
1137 | // Send typing notice if needed. |
| @@ -1478,23 +1490,42 @@ fn cmd(open_command: &Vec<String>) -> Option<Command> { |
| 1478 | None |
1490 | None |
| 1479 | } |
1491 | } |
| 1480 | |
1492 | |
| 1481 | pub fn auto_toggle_focus( |
1493 | #[expect( |
| |
1494 | clippy::too_many_arguments, |
| |
1495 | reason = "this is only a seperate function to make testing easier" |
| |
1496 | )] |
| |
1497 | fn auto_toggle_focus( |
| 1482 | focus: &mut RoomFocus, |
1498 | focus: &mut RoomFocus, |
| 1483 | act: &EditorAction, |
1499 | act: &EditorAction, |
| 1484 | ctx: &ProgramContext, |
1500 | ctx: &ProgramContext, |
| 1485 | scrollback: &ScrollbackState, |
1501 | scrollback: &ScrollbackState, |
| 1486 | tbox: &mut TextBoxState<IambInfo>, |
1502 | tbox: &mut TextBoxState<IambInfo>, |
| |
1503 | reply_to: &mut Option<MessageKey>, |
| |
1504 | info: &RoomInfo, |
| |
1505 | settings: &ApplicationSettings, |
| 1487 | ) -> Option<EditorAction> { |
1506 | ) -> Option<EditorAction> { |
| 1488 | let is_insert = ctx.get_insert_style().is_some(); |
1507 | let is_insert = ctx.get_insert_style().is_some(); |
| 1489 | |
1508 | |
| 1490 | match (focus, act) { |
1509 | match (focus, act) { |
| 1491 | (f @ RoomFocus::Scrollback, _) if is_insert => { |
1510 | (f @ RoomFocus::Scrollback, _) if is_insert => { |
| 1492 | // Insert mode commands should switch focus. |
1511 | // Insert mode commands should switch focus. |
| |
1512 | if settings.tunables.reply_on_insert && |
| |
1513 | reply_to.is_none() && |
| |
1514 | !tbox.get_text().is_empty() |
| |
1515 | { |
| |
1516 | *reply_to = scrollback.get_key(info); |
| |
1517 | } |
| 1493 | f.toggle(); |
1518 | f.toggle(); |
| 1494 | None |
1519 | None |
| 1495 | }, |
1520 | }, |
| 1496 | (f @ RoomFocus::Scrollback, EditorAction::InsertText(_)) => { |
1521 | (f @ RoomFocus::Scrollback, EditorAction::InsertText(_)) => { |
| 1497 | // Pasting or otherwise inserting text should switch. |
1522 | // Pasting or otherwise inserting text should switch. |
| |
1523 | if settings.tunables.reply_on_insert && |
| |
1524 | reply_to.is_none() && |
| |
1525 | !tbox.get_text().is_empty() |
| |
1526 | { |
| |
1527 | *reply_to = scrollback.get_key(info); |
| |
1528 | } |
| 1498 | f.toggle(); |
1529 | f.toggle(); |
| 1499 | None |
1530 | None |
| 1500 | }, |
1531 | }, |
| @@ -1670,6 +1701,9 @@ mod tests { |
| 1670 | let id = IambBufferId::Room(room_id, RoomView::Main, RoomFocus::MessageBar); |
1701 | let id = IambBufferId::Room(room_id, RoomView::Main, RoomFocus::MessageBar); |
| 1671 | let ebuf = store.load_buffer(id); |
1702 | let ebuf = store.load_buffer(id); |
| 1672 | let mut tbox = TextBoxState::new(ebuf); |
1703 | let mut tbox = TextBoxState::new(ebuf); |
| |
1704 | let mut reply_to = None; |
| |
1705 | let info = RoomInfo::default(); |
| |
1706 | let settings = &store.application.settings; |
| 1673 | |
1707 | |
| 1674 | // Start out focused on the scrollback. |
1708 | // Start out focused on the scrollback. |
| 1675 | let mut focused = RoomFocus::Scrollback; |
1709 | let mut focused = RoomFocus::Scrollback; |
| @@ -1680,43 +1714,106 @@ mod tests { |
| 1680 | MoveDir1D::Next, |
1714 | MoveDir1D::Next, |
| 1681 | 1.into(), |
1715 | 1.into(), |
| 1682 | )); |
1716 | )); |
| 1683 | let res = auto_toggle_focus(&mut focused, &act, &ctx, &scrollback, &mut tbox); |
1717 | let res = auto_toggle_focus( |
| |
1718 | &mut focused, |
| |
1719 | &act, |
| |
1720 | &ctx, |
| |
1721 | &scrollback, |
| |
1722 | &mut tbox, |
| |
1723 | &mut reply_to, |
| |
1724 | &info, |
| |
1725 | settings, |
| |
1726 | ); |
| 1684 | assert_eq!(focused, RoomFocus::MessageBar); |
1727 | assert_eq!(focused, RoomFocus::MessageBar); |
| 1685 | assert!(res.is_none()); |
1728 | assert!(res.is_none()); |
| 1686 | |
1729 | |
| 1687 | // Going down in message bar doesn't toggle: |
1730 | // Going down in message bar doesn't toggle: |
| 1688 | let act = move_line!(MoveDir1D::Next, 1); |
1731 | let act = move_line!(MoveDir1D::Next, 1); |
| 1689 | let res = auto_toggle_focus(&mut focused, &act, &ctx, &scrollback, &mut tbox); |
1732 | let res = auto_toggle_focus( |
| |
1733 | &mut focused, |
| |
1734 | &act, |
| |
1735 | &ctx, |
| |
1736 | &scrollback, |
| |
1737 | &mut tbox, |
| |
1738 | &mut reply_to, |
| |
1739 | &info, |
| |
1740 | settings, |
| |
1741 | ); |
| 1690 | assert_eq!(focused, RoomFocus::MessageBar); |
1742 | assert_eq!(focused, RoomFocus::MessageBar); |
| 1691 | assert!(res.is_none()); |
1743 | assert!(res.is_none()); |
| 1692 | |
1744 | |
| 1693 | // But going up will: |
1745 | // But going up will: |
| 1694 | let act = move_line!(MoveDir1D::Previous, 1); |
1746 | let act = move_line!(MoveDir1D::Previous, 1); |
| 1695 | let res = auto_toggle_focus(&mut focused, &act, &ctx, &scrollback, &mut tbox); |
1747 | let res = auto_toggle_focus( |
| |
1748 | &mut focused, |
| |
1749 | &act, |
| |
1750 | &ctx, |
| |
1751 | &scrollback, |
| |
1752 | &mut tbox, |
| |
1753 | &mut reply_to, |
| |
1754 | &info, |
| |
1755 | settings, |
| |
1756 | ); |
| 1696 | assert_eq!(focused, RoomFocus::Scrollback); |
1757 | assert_eq!(focused, RoomFocus::Scrollback); |
| 1697 | assert_eq!(res, Some(move_line!(MoveDir1D::Previous, 0))); |
1758 | assert_eq!(res, Some(move_line!(MoveDir1D::Previous, 0))); |
| 1698 | |
1759 | |
| 1699 | // Going up in scrollback doesn't toggle: |
1760 | // Going up in scrollback doesn't toggle: |
| 1700 | let act = move_line!(MoveDir1D::Previous, 1); |
1761 | let act = move_line!(MoveDir1D::Previous, 1); |
| 1701 | let res = auto_toggle_focus(&mut focused, &act, &ctx, &scrollback, &mut tbox); |
1762 | let res = auto_toggle_focus( |
| |
1763 | &mut focused, |
| |
1764 | &act, |
| |
1765 | &ctx, |
| |
1766 | &scrollback, |
| |
1767 | &mut tbox, |
| |
1768 | &mut reply_to, |
| |
1769 | &info, |
| |
1770 | settings, |
| |
1771 | ); |
| 1702 | assert_eq!(focused, RoomFocus::Scrollback); |
1772 | assert_eq!(focused, RoomFocus::Scrollback); |
| 1703 | assert_eq!(res, None); |
1773 | assert_eq!(res, None); |
| 1704 | |
1774 | |
| 1705 | // And then go back down: |
1775 | // And then go back down: |
| 1706 | let act = move_line!(MoveDir1D::Next, 1); |
1776 | let act = move_line!(MoveDir1D::Next, 1); |
| 1707 | let res = auto_toggle_focus(&mut focused, &act, &ctx, &scrollback, &mut tbox); |
1777 | let res = auto_toggle_focus( |
| |
1778 | &mut focused, |
| |
1779 | &act, |
| |
1780 | &ctx, |
| |
1781 | &scrollback, |
| |
1782 | &mut tbox, |
| |
1783 | &mut reply_to, |
| |
1784 | &info, |
| |
1785 | settings, |
| |
1786 | ); |
| 1708 | assert_eq!(focused, RoomFocus::MessageBar); |
1787 | assert_eq!(focused, RoomFocus::MessageBar); |
| 1709 | assert_eq!(res, Some(move_line!(MoveDir1D::Next, 0))); |
1788 | assert_eq!(res, Some(move_line!(MoveDir1D::Next, 0))); |
| 1710 | |
1789 | |
| 1711 | // Go up 2 will go up 1 in scrollback: |
1790 | // Go up 2 will go up 1 in scrollback: |
| 1712 | let act = move_line!(MoveDir1D::Previous, 2); |
1791 | let act = move_line!(MoveDir1D::Previous, 2); |
| 1713 | let res = auto_toggle_focus(&mut focused, &act, &ctx, &scrollback, &mut tbox); |
1792 | let res = auto_toggle_focus( |
| |
1793 | &mut focused, |
| |
1794 | &act, |
| |
1795 | &ctx, |
| |
1796 | &scrollback, |
| |
1797 | &mut tbox, |
| |
1798 | &mut reply_to, |
| |
1799 | &info, |
| |
1800 | settings, |
| |
1801 | ); |
| 1714 | assert_eq!(focused, RoomFocus::Scrollback); |
1802 | assert_eq!(focused, RoomFocus::Scrollback); |
| 1715 | assert_eq!(res, Some(move_line!(MoveDir1D::Previous, 1))); |
1803 | assert_eq!(res, Some(move_line!(MoveDir1D::Previous, 1))); |
| 1716 | |
1804 | |
| 1717 | // Go down 3 will go down 2 in messagebar: |
1805 | // Go down 3 will go down 2 in messagebar: |
| 1718 | let act = move_line!(MoveDir1D::Next, 3); |
1806 | let act = move_line!(MoveDir1D::Next, 3); |
| 1719 | let res = auto_toggle_focus(&mut focused, &act, &ctx, &scrollback, &mut tbox); |
1807 | let res = auto_toggle_focus( |
| |
1808 | &mut focused, |
| |
1809 | &act, |
| |
1810 | &ctx, |
| |
1811 | &scrollback, |
| |
1812 | &mut tbox, |
| |
1813 | &mut reply_to, |
| |
1814 | &info, |
| |
1815 | settings, |
| |
1816 | ); |
| 1720 | assert_eq!(focused, RoomFocus::MessageBar); |
1817 | assert_eq!(focused, RoomFocus::MessageBar); |
| 1721 | assert_eq!(res, Some(move_line!(MoveDir1D::Next, 2))); |
1818 | assert_eq!(res, Some(move_line!(MoveDir1D::Next, 2))); |
| 1722 | } |
1819 | } |