Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

restore auto-scroll behavior

Prateek Sunal8611e831b8c0
diff --git a/mobile/native/android/apps/ensu/app-ui/src/main/java/io/ente/ensu/chat/ChatMessages.kt b/mobile/native/android/apps/ensu/app-ui/src/main/java/io/ente/ensu/chat/ChatMessages.kt
index d7f2f2a8d0..7bdd85b51d 100644
--- a/mobile/native/android/apps/ensu/app-ui/src/main/java/io/ente/ensu/chat/ChatMessages.kt
+++ b/mobile/native/android/apps/ensu/app-ui/src/main/java/io/ente/ensu/chat/ChatMessages.kt
@@ -126,8 +126,10 @@ internal fun MessageList(
val listState = rememberLazyListState(
initialFirstVisibleItemIndex = if (messages.isNotEmpty()) messages.size else 0
)
+ val haptic = rememberEnsuHaptics()
var autoScrollEnabled by remember { mutableStateOf(true) }
var isAutoScrolling by remember { mutableStateOf(false) }
+ var lastHapticLength by remember { mutableStateOf(0) }
var shouldJumpToBottomOnLoad by remember { mutableStateOf(true) }
var wasAtBottomBeforeResize by remember { mutableStateOf(true) }
var lastViewportHeight by remember { mutableStateOf(0) }
@@ -142,6 +144,11 @@ internal fun MessageList(
val lastMessage = messages.lastOrNull()
LaunchedEffect(isGenerating, lastMessage?.id) {
+ if (isGenerating) {
+ autoScrollEnabled = true
+ lastHapticLength = 0
+ }
+
if (lastMessage == null) {
lastUserMessageId = null
} else if (lastMessage.author == MessageAuthor.User && lastMessage.id != lastUserMessageId) {
@@ -167,15 +174,32 @@ internal fun MessageList(
}
}
+ LaunchedEffect(isAtBottom, isGenerating) {
+ if (isGenerating && isAtBottom) {
+ autoScrollEnabled = true
+ }
+ }
+
+ LaunchedEffect(streamingResponse, isGenerating) {
+ if (!isGenerating) return@LaunchedEffect
+ val length = streamingResponse.length
+ if (length > lastHapticLength) {
+ haptic.perform(HapticFeedbackType.TextHandleMove)
+ lastHapticLength = length
+ }
+ }
- LaunchedEffect(messages.size, streamingParentId, isGenerating, autoScrollEnabled) {
- val shouldScrollForNewUserMessage = messages.lastOrNull()?.author == MessageAuthor.User
- if (!autoScrollEnabled || isGenerating || !shouldScrollForNewUserMessage) return@LaunchedEffect
+ LaunchedEffect(messages.size, streamingResponse, streamingParentId, isGenerating, autoScrollEnabled) {
+ if (!autoScrollEnabled) return@LaunchedEffect
val targetIndex = messages.size
if (targetIndex >= 0) {
isAutoScrolling = true
try {
- listState.animateScrollToItem(targetIndex)
+ if (isGenerating) {
+ listState.animateScrollToItem(targetIndex)
+ } else {
+ listState.scrollToItem(targetIndex)
+ }
} finally {
isAutoScrolling = false
}
diff --git a/mobile/native/darwin/Apps/ensu/ensu/Chat/MessageListView.swift b/mobile/native/darwin/Apps/ensu/ensu/Chat/MessageListView.swift
index 56ed61005f..9c54998ece 100644
--- a/mobile/native/darwin/Apps/ensu/ensu/Chat/MessageListView.swift
+++ b/mobile/native/darwin/Apps/ensu/ensu/Chat/MessageListView.swift
@@ -69,14 +69,8 @@ struct MessageListView: View {
.onPreferenceChange(ContentHeightKey.self) { newHeight in
let delta = newHeight - lastContentHeight
lastContentHeight = newHeight
- let lastMessageIsUser = messages.last?.role == .user
- if delta > 1,
- autoScrollEnabled,
- !isUserDragging,
- !isGenerating,
- !suppressAutoScrollAfterGeneration,
- lastMessageIsUser {
- scrollToBottom(scrollProxy, force: true, animated: true)
+ if delta > 1, autoScrollEnabled, !isUserDragging {
+ scrollToBottom(scrollProxy, force: true, animated: false)
}
}
.onChange(of: currentScrollChange) { newValue in
@@ -155,14 +149,12 @@ struct MessageListView: View {
private func handleScrollChange(_ newValue: ScrollChange, scrollProxy: ScrollViewProxy) {
let previous = lastScrollChange
lastScrollChange = newValue
- let didGenerationJustFinish = previous.isGenerating && !newValue.isGenerating
if newValue.messagesCount != previous.messagesCount {
if !didInitialScroll {
scheduleInitialScroll(scrollProxy)
- } else if messages.last?.role == .user {
- autoScrollEnabled = true
- scrollToBottom(scrollProxy, force: true, animated: true)
+ } else {
+ scrollToBottom(scrollProxy, animated: newValue.isGenerating)
}
}
@@ -172,11 +164,15 @@ struct MessageListView: View {
scheduleInitialScroll(scrollProxy)
}
+ if newValue.streamingLength != previous.streamingLength {
+ scrollToBottom(scrollProxy, animated: false)
+ }
+
if newValue.keyboardHeight != previous.keyboardHeight {
if newValue.keyboardHeight > 0 {
wasAtBottomBeforeKeyboard = isAtBottom
}
- if wasAtBottomBeforeKeyboard && !didGenerationJustFinish && !suppressAutoScrollAfterGeneration {
+ if wasAtBottomBeforeKeyboard {
autoScrollEnabled = true
scrollToBottom(scrollProxy, force: true, animated: false)
}
@@ -186,11 +182,14 @@ struct MessageListView: View {
}
if newValue.inputBarHeight != previous.inputBarHeight {
- if autoScrollEnabled &&
- !isUserDragging &&
- !isGenerating &&
- !didGenerationJustFinish &&
- !suppressAutoScrollAfterGeneration {
+ if autoScrollEnabled && !isUserDragging {
+ scrollToBottom(scrollProxy, force: true, animated: false)
+ }
+ }
+
+ if newValue.isGenerating != previous.isGenerating {
+ if newValue.isGenerating {
+ autoScrollEnabled = true
scrollToBottom(scrollProxy, force: true, animated: false)
}
}
@@ -198,6 +197,7 @@ struct MessageListView: View {
if newValue.isAtBottom != previous.isAtBottom {
if newValue.isAtBottom && !isUserDragging {
autoScrollEnabled = true
+ scrollToBottom(scrollProxy, force: true, animated: false)
}
}
}
@@ -337,8 +337,15 @@ struct MessageListView: View {
}
}
- private func scrollToBottom(_ : ScrollViewProxy, force _: Bool = false, animated _: Bool = true) {
- // Intentionally no-op: auto-scroll is disabled for Swift chat.
+ private func scrollToBottom(_ proxy: ScrollViewProxy, force: Bool = false, animated: Bool = true) {
+ guard force || (autoScrollEnabled && isAtBottom) else { return }
+ if animated {
+ withAnimation(.easeOut(duration: 0.2)) {
+ proxy.scrollTo("bottom", anchor: .bottom)
+ }
+ } else {
+ proxy.scrollTo("bottom", anchor: .bottom)
+ }
}
private func openAttachment(_ attachment: ChatAttachment) {