Skip to content

Commit e57cb27

Browse files
committed
Fix chat panel jumping to top, again
1 parent d8b1bf8 commit e57cb27

2 files changed

Lines changed: 34 additions & 7 deletions

File tree

Core/Sources/ChatGPTChatTab/ChatPanel.swift

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,12 @@ struct ChatPanelMessages: View {
4545
@State var isScrollToBottomButtonDisplayed = true
4646
@State var isPinnedToBottom = true
4747
@Namespace var bottomID
48+
@Namespace var topID
4849
@Namespace var scrollSpace
4950
@State var scrollOffset: Double = 0
5051
@State var listHeight: Double = 0
52+
@State var didAppearOnce = false
53+
@State var isBottomHidden = true
5154
@Environment(\.isEnabled) var isEnabled
5255

5356
var body: some View {
@@ -56,6 +59,7 @@ struct ChatPanelMessages: View {
5659
List {
5760
Group {
5861
Spacer(minLength: 12)
62+
.id(topID)
5963

6064
Instruction(chat: chat)
6165

@@ -71,10 +75,19 @@ struct ChatPanelMessages: View {
7175
Spacer(minLength: 12)
7276
.id(bottomID)
7377
.onAppear {
74-
proxy.scrollTo(bottomID, anchor: .bottom)
78+
isBottomHidden = false
79+
if !didAppearOnce {
80+
proxy.scrollTo(bottomID, anchor: .bottom)
81+
}
82+
}
83+
.onDisappear {
84+
isBottomHidden = true
7585
}
7686
.task {
77-
proxy.scrollTo(bottomID, anchor: .bottom)
87+
if !didAppearOnce {
88+
proxy.scrollTo(bottomID, anchor: .bottom)
89+
didAppearOnce = true
90+
}
7891
}
7992
.background(GeometryReader { geo in
8093
let offset = geo.frame(in: .named(scrollSpace)).minY
@@ -129,7 +142,11 @@ struct ChatPanelMessages: View {
129142
scrollToBottomButton(proxy: proxy)
130143
}
131144
.background {
132-
PinToBottomHandler(chat: chat, pinnedToBottom: $isPinnedToBottom) {
145+
PinToBottomHandler(
146+
chat: chat,
147+
isBottomHidden: isBottomHidden,
148+
pinnedToBottom: $isPinnedToBottom
149+
) {
133150
proxy.scrollTo(bottomID, anchor: .bottom)
134151
}
135152
}
@@ -199,6 +216,7 @@ struct ChatPanelMessages: View {
199216

200217
struct PinToBottomHandler: View {
201218
let chat: StoreOf<Chat>
219+
let isBottomHidden: Bool
202220
@Binding var pinnedToBottom: Bool
203221
let scrollToBottom: () -> Void
204222

@@ -222,7 +240,9 @@ struct ChatPanelMessages: View {
222240
Task {
223241
pinnedToBottom = true
224242
await Task.yield()
225-
scrollToBottom()
243+
withAnimation(.easeInOut(duration: 0.1)) {
244+
scrollToBottom()
245+
}
226246
}
227247
}
228248
}
@@ -233,10 +253,18 @@ struct ChatPanelMessages: View {
233253
}
234254
Task {
235255
await Task.yield()
236-
scrollToBottom()
256+
withAnimation(.easeInOut(duration: 0.1)) {
257+
scrollToBottom()
258+
}
237259
}
238260
}
239261
}
262+
.onChange(of: isBottomHidden) { value in
263+
// This is important to prevent it from jumping to the top!
264+
if value, pinnedToBottom {
265+
scrollToBottom()
266+
}
267+
}
240268
}
241269
}
242270
}

Tool/Sources/ChatContextCollectors/ActiveDocumentChatContextCollector/ActiveDocumentChatContextCollector.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,7 @@ public final class ActiveDocumentChatContextCollector: ChatContextCollector {
5555

5656
var functionPrompt = """
5757
ONLY call it when one of the following conditions are satisfied:
58-
- the user explicitly ask you about specific line of code, \
59-
but the line was NOT in the focused range \(contextLineRange).
58+
- the user explicitly ask you about specific line of code, that is NOT in the focused range \(contextLineRange).
6059
"""
6160

6261
if let annotations = context.focusedContext?.otherLineAnnotations,

0 commit comments

Comments
 (0)