on main help sign in
fix: do not scroll to bottom if user has scrolled up (#2049)
b51c2e0Andrey Nering
internal/ui/list/list.go+3−1
@@ -77,6 +77,8 @@ func (l *List) Gap() int {
77
77
78// AtBottom returns whether the list is showing the last item at the bottom.
78// AtBottom returns whether the list is showing the last item at the bottom.
79func (l *List) AtBottom() bool {
79func (l *List) AtBottom() bool {
80+ const margin = 2
81+
80 if len(l.items) == 0 {
82 if len(l.items) == 0 {
81 return true
83 return true
82 }
84 }
@@ -92,7 +94,7 @@ func (l *List) AtBottom() bool {
92 totalHeight += itemHeight
94 totalHeight += itemHeight
93 }
95 }
94
96
95− return totalHeight-l.offsetLine <= l.height
97+ return totalHeight-l.offsetLine-margin <= l.height
96}
98}
97
99
98// SetReverse shows the list in reverse order.
100// SetReverse shows the list in reverse order.
internal/ui/model/ui.go+16−4
@@ -830,11 +830,14 @@ func (m *UI) loadNestedToolCalls(items []chat.MessageItem) {
830// if the message is a tool result it will update the corresponding tool call message
830// if the message is a tool result it will update the corresponding tool call message
831func (m *UI) appendSessionMessage(msg message.Message) tea.Cmd {
831func (m *UI) appendSessionMessage(msg message.Message) tea.Cmd {
832 var cmds []tea.Cmd
832 var cmds []tea.Cmd
833+ atBottom := m.chat.list.AtBottom()
834+
833 existing := m.chat.MessageItem(msg.ID)
835 existing := m.chat.MessageItem(msg.ID)
834 if existing != nil {
836 if existing != nil {
835 // message already exists, skip
837 // message already exists, skip
836 return nil
838 return nil
837 }
839 }
840+
838 switch msg.Role {
841 switch msg.Role {
839 case message.User:
842 case message.User:
840 m.lastUserMessageTime = msg.CreatedAt
843 m.lastUserMessageTime = msg.CreatedAt
@@ -860,14 +863,18 @@ func (m *UI) appendSessionMessage(msg message.Message) tea.Cmd {
860 }
863 }
861 }
864 }
862 m.chat.AppendMessages(items...)
865 m.chat.AppendMessages(items...)
866+ if atBottom {
863− if cmd := m.chat.ScrollToBottomAndAnimate(); cmd != nil {
867+ if cmd := m.chat.ScrollToBottomAndAnimate(); cmd != nil {
864− cmds = append(cmds, cmd)
868+ cmds = append(cmds, cmd)
869+ }
865 }
870 }
866 if msg.FinishPart() != nil && msg.FinishPart().Reason == message.FinishReasonEndTurn {
871 if msg.FinishPart() != nil && msg.FinishPart().Reason == message.FinishReasonEndTurn {
867 infoItem := chat.NewAssistantInfoItem(m.com.Styles, &msg, time.Unix(m.lastUserMessageTime, 0))
872 infoItem := chat.NewAssistantInfoItem(m.com.Styles, &msg, time.Unix(m.lastUserMessageTime, 0))
868 m.chat.AppendMessages(infoItem)
873 m.chat.AppendMessages(infoItem)
874+ if atBottom {
869− if cmd := m.chat.ScrollToBottomAndAnimate(); cmd != nil {
875+ if cmd := m.chat.ScrollToBottomAndAnimate(); cmd != nil {
870− cmds = append(cmds, cmd)
876+ cmds = append(cmds, cmd)
877+ }
871 }
878 }
872 }
879 }
873 case message.Tool:
880 case message.Tool:
@@ -879,6 +886,11 @@ func (m *UI) appendSessionMessage(msg message.Message) tea.Cmd {
879 }
886 }
880 if toolMsgItem, ok := toolItem.(chat.ToolMessageItem); ok {
887 if toolMsgItem, ok := toolItem.(chat.ToolMessageItem); ok {
881 toolMsgItem.SetResult(&tr)
888 toolMsgItem.SetResult(&tr)
889+ if atBottom {
890+ if cmd := m.chat.ScrollToBottomAndAnimate(); cmd != nil {
891+ cmds = append(cmds, cmd)
892+ }
893+ }
882 }
894 }
883 }
895 }
884 }
896 }