Skip to content

armgit / ente

Public Imported from GitHub
0

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

More balanced haptics

laurenspriem4437f5dd4ab1
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 53bedf395d..a07840f6ed 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
@@ -129,7 +129,7 @@ internal fun MessageList(
val haptic = rememberEnsuHaptics()
var autoScrollEnabled by remember { mutableStateOf(true) }
var isAutoScrolling by remember { mutableStateOf(false) }
- var lastHapticLength by remember { mutableStateOf(0) }
+ var didPerformStreamingStartHaptic by remember { mutableStateOf(false) }
var shouldJumpToBottomOnLoad by remember { mutableStateOf(true) }
var wasAtBottomBeforeResize by remember { mutableStateOf(true) }
var lastViewportHeight by remember { mutableStateOf(0) }
@@ -152,12 +152,14 @@ internal fun MessageList(
val streamingAnchorId = if (isGenerating) streamingParentId else null
val lastMessage = messages.lastOrNull()
- LaunchedEffect(isGenerating, lastMessage?.id) {
+ LaunchedEffect(isGenerating, streamingParentId) {
if (isGenerating) {
autoScrollEnabled = true
- lastHapticLength = 0
+ didPerformStreamingStartHaptic = false
}
+ }
+ LaunchedEffect(lastMessage?.id) {
if (lastMessage == null) {
lastUserMessageId = null
} else if (lastMessage.author == MessageAuthor.User && lastMessage.id != lastUserMessageId) {
@@ -204,11 +206,10 @@ internal fun MessageList(
}
LaunchedEffect(streamingResponse, isGenerating) {
- if (!isGenerating) return@LaunchedEffect
- val length = streamingResponse.length
- if (length > lastHapticLength) {
+ if (!isGenerating || didPerformStreamingStartHaptic) return@LaunchedEffect
+ if (hasVisibleStreamingContent(streamingResponse)) {
haptic.perform(HapticFeedbackType.TextHandleMove)
- lastHapticLength = length
+ didPerformStreamingStartHaptic = true
}
}
@@ -840,6 +841,13 @@ private fun stripHiddenMessageParts(text: String): String {
.trim()
}
+private fun hasVisibleStreamingContent(text: String): Boolean {
+ return stripHiddenMessageParts(text)
+ .replace(Regex("<think>[\\s\\S]*"), "")
+ .replace(Regex("<todo_list>[\\s\\S]*"), "")
+ .isNotBlank()
+}
+
@Composable
private fun TimestampText(timestampMillis: Long) {
Text(
diff --git a/mobile/native/darwin/Apps/Ensu/Ensu/Chat/MessageListView.swift b/mobile/native/darwin/Apps/Ensu/Ensu/Chat/MessageListView.swift
index a2ae35ca0b..762cb27f6e 100644
--- a/mobile/native/darwin/Apps/Ensu/Ensu/Chat/MessageListView.swift
+++ b/mobile/native/darwin/Apps/Ensu/Ensu/Chat/MessageListView.swift
@@ -27,6 +27,7 @@ struct MessageListView: View {
@State private var lastScrollChange = ScrollChange()
@State private var didInitialScroll = false
@State private var streamingTextStorageId = UUID().uuidString
+ @State private var didPerformStreamingStartHaptic = false
var body: some View {
let content = GeometryReader { proxy in
@@ -128,8 +129,18 @@ struct MessageListView: View {
scheduleInitialScroll(scrollProxy)
}
+ if newValue.isGenerating != previous.isGenerating {
+ didPerformStreamingStartHaptic = false
+ if newValue.isGenerating {
+ autoScrollEnabled = true
+ scrollToBottom(scrollProxy, force: true, animated: false)
+ performStreamingStartHapticIfNeeded()
+ }
+ }
+
if newValue.streamingLength != previous.streamingLength {
scrollToBottom(scrollProxy, animated: false)
+ performStreamingStartHapticIfNeeded()
}
if newValue.keyboardHeight != previous.keyboardHeight {
@@ -145,13 +156,6 @@ struct MessageListView: View {
}
}
- if newValue.isGenerating != previous.isGenerating {
- if newValue.isGenerating {
- autoScrollEnabled = true
- scrollToBottom(scrollProxy, force: true, animated: false)
- }
- }
-
if newValue.isAtBottom != previous.isAtBottom {
if newValue.isAtBottom && !isUserDragging {
autoScrollEnabled = true
@@ -316,6 +320,18 @@ struct MessageListView: View {
}
}
+ private func performStreamingStartHapticIfNeeded() {
+ #if os(iOS)
+ guard isGenerating,
+ !didPerformStreamingStartHaptic,
+ hasVisibleStreamingContent(streamingResponse) else {
+ return
+ }
+ hapticTap()
+ didPerformStreamingStartHaptic = true
+ #endif
+ }
+
private func openAttachment(_ attachment: ChatAttachment) {
guard let url = attachment.url else { return }
guard FileManager.default.fileExists(atPath: url.path) else { return }
@@ -349,6 +365,33 @@ struct MessageListView: View {
let sanitized = baseName.components(separatedBy: invalidCharacters).joined(separator: "-")
return sanitized.isEmpty ? attachment.id.uuidString : sanitized
}
+
+ private func hasVisibleStreamingContent(_ text: String) -> Bool {
+ let textWithoutUnclosedThink = removingUnclosedStreamingTag("think", from: text)
+ let visibleText = removingUnclosedStreamingTag("todo_list", from: textWithoutUnclosedThink)
+ let parsed = ParsedMessage(text: visibleText)
+ return !parsed.todoBlocks.isEmpty ||
+ !parsed.markdown.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty
+ }
+
+ private func removingUnclosedStreamingTag(_ tag: String, from text: String) -> String {
+ let openingTag = "<\(tag)>"
+ let closingTag = "</\(tag)>"
+ var result = ""
+ var cursor = text.startIndex
+
+ while let openRange = text.range(of: openingTag, range: cursor..<text.endIndex) {
+ result.append(contentsOf: text[cursor..<openRange.lowerBound])
+ guard let closeRange = text.range(of: closingTag, range: openRange.upperBound..<text.endIndex) else {
+ return result
+ }
+ result.append(contentsOf: text[openRange.lowerBound..<closeRange.upperBound])
+ cursor = closeRange.upperBound
+ }
+
+ result.append(contentsOf: text[cursor..<text.endIndex])
+ return result
+ }
}
private struct StreamingDotsView: View {