@@ -39,6 +39,8 @@ class T3ReviewDiffView(context: Context, appContext: AppContext) : ExpoView(cont
3939 private var collapsedCommentIds: Set <String > = emptySet()
4040 private var initialRowIndex = 0
4141 private var pendingInitialScroll = false
42+ private var initialFileId: String? = null
43+ private var pendingScrollFileId: String? = null
4244 private var lastVisibleFileId: String? = null
4345 private var tokensResetKey = " "
4446 private var contentResetKey = " "
@@ -66,12 +68,14 @@ class T3ReviewDiffView(context: Context, appContext: AppContext) : ExpoView(cont
6668 )
6769 }
6870 canvasView.onRowTap = { row, gesture, target -> handleRowTap(row, gesture, target) }
71+ canvasView.onViewportSizeChanged = { applyPendingInitialScroll() }
6972 canvasView.onVisibleRowsChanged = { first, last ->
7073 onDebug(
7174 mapOf (
7275 " message" to " visible-range" ,
7376 " firstRowIndex" to first,
7477 " lastRowIndex" to last,
78+ " totalRows" to rows.size,
7579 ),
7680 )
7781 emitVisibleFile(first)
@@ -92,10 +96,18 @@ class T3ReviewDiffView(context: Context, appContext: AppContext) : ExpoView(cont
9296 fun setContentResetKey (value : String ) {
9397 if (contentResetKey == value) return
9498 contentResetKey = value
99+ rowsDecodeGeneration + = 1
95100 tokensDecodeGeneration + = 1
101+ canvasView.setViewportEventsPaused(true )
102+ verticalScroller.forceFinished(true )
103+ horizontalScroller.forceFinished(true )
104+ rows = emptyList()
105+ visibleRows = emptyList()
106+ canvasView.rows = emptyList()
96107 canvasView.tokensByRowId = emptyMap()
97108 lastVisibleFileId = null
98109 pendingInitialScroll = true
110+ pendingScrollFileId = initialFileId
99111 canvasView.setVerticalOffset(0 )
100112 canvasView.resetHorizontalOffsets()
101113 applyPendingInitialScroll()
@@ -146,13 +158,24 @@ class T3ReviewDiffView(context: Context, appContext: AppContext) : ExpoView(cont
146158 }
147159
148160 fun setInitialRowIndex (value : Double ) {
149- initialRowIndex = value.toInt().coerceAtLeast(0 )
150- pendingInitialScroll = true
161+ initialRowIndex = value.toInt()
162+ pendingInitialScroll = value.isFinite() && value >= 0
163+ canvasView.setViewportEventsPaused(true )
164+ applyPendingInitialScroll()
165+ }
166+
167+ fun setInitialFileId (value : String ) {
168+ val nextFileId = value.takeIf { it.isNotEmpty() }
169+ if (nextFileId == initialFileId) return
170+ initialFileId = nextFileId
171+ pendingScrollFileId = nextFileId
172+ canvasView.setViewportEventsPaused(true )
151173 applyPendingInitialScroll()
152174 }
153175
154176 fun setRowsJson (value : String ) {
155177 rowsDecodeGeneration + = 1
178+ canvasView.setViewportEventsPaused(true )
156179 val generation = rowsDecodeGeneration
157180 val prepareLayout = canvasView.prepareRows()
158181 payloadDecodeExecutor.execute {
@@ -180,7 +203,8 @@ class T3ReviewDiffView(context: Context, appContext: AppContext) : ExpoView(cont
180203 }
181204 }
182205
183- fun setTokensPatchJson (value : String ) {
206+ fun setTokensPatchJson (value : String , completion : (Boolean ) -> Unit ) {
207+ val contentKey = contentResetKey
184208 payloadDecodeExecutor.execute {
185209 try {
186210 val payload = JSONObject (value)
@@ -189,12 +213,19 @@ class T3ReviewDiffView(context: Context, appContext: AppContext) : ExpoView(cont
189213 payload.optJSONObject(" tokensByRowId" ) ? : JSONObject (),
190214 )
191215 post {
192- if (resetKey.isNotEmpty() && resetKey != tokensResetKey) return @post
216+ if (contentKey != contentResetKey ||
217+ (resetKey.isNotEmpty() && resetKey != tokensResetKey)
218+ ) {
219+ completion(false )
220+ return @post
221+ }
193222 if (decodedTokens.isNotEmpty()) {
194223 canvasView.tokensByRowId = canvasView.tokensByRowId + decodedTokens
195224 }
225+ completion(true )
196226 }
197227 } catch (_: Exception ) {
228+ post { completion(false ) }
198229 }
199230 }
200231 }
@@ -354,6 +385,7 @@ class T3ReviewDiffView(context: Context, appContext: AppContext) : ExpoView(cont
354385 }
355386
356387 private fun rebuildVisibleRows () {
388+ canvasView.setViewportEventsPaused(true )
357389 val filtered = ArrayList <DiffRow >(rows.size)
358390 var currentFileCollapsed = false
359391 rows.forEach { row ->
@@ -411,10 +443,20 @@ class T3ReviewDiffView(context: Context, appContext: AppContext) : ExpoView(cont
411443 }
412444
413445 private fun applyPendingInitialScroll () {
414- if (! pendingInitialScroll || visibleRows.isEmpty()) return
415- pendingInitialScroll = false
416- val index = initialRowIndex.coerceIn(0 , visibleRows.lastIndex)
417- post { canvasView.setVerticalOffset(canvasView.rowTop(index)) }
446+ if (visibleRows.isEmpty() || canvasView.width <= 0 || canvasView.height <= 0 ) return
447+ val fileId = pendingScrollFileId
448+ if (fileId != null ) {
449+ val index = visibleRows.indexOfFirst { it.kind == " file" && it.resolvedFileId == fileId }
450+ if (index < 0 ) return
451+ pendingScrollFileId = null
452+ pendingInitialScroll = false
453+ canvasView.setVerticalOffset(canvasView.rowTop(index))
454+ } else if (pendingInitialScroll) {
455+ pendingInitialScroll = false
456+ val index = initialRowIndex.coerceIn(0 , visibleRows.lastIndex)
457+ canvasView.setVerticalOffset(canvasView.rowTop(index))
458+ }
459+ canvasView.setViewportEventsPaused(false )
418460 }
419461
420462 private fun scrollToY (y : Int , animated : Boolean ) {
@@ -718,6 +760,7 @@ private class DiffCanvasView(context: Context) : View(context) {
718760 private var contentWidthPx = 0
719761 private val headerPathOffsetsByFileId = mutableMapOf<String , Int >()
720762 private var lastVisibleRange: Pair <Int , Int >? = null
763+ private var viewportEventsPaused = true
721764 var rows: List <DiffRow > = emptyList()
722765 set(value) {
723766 field = value
@@ -772,6 +815,7 @@ private class DiffCanvasView(context: Context) : View(context) {
772815 var onRowTap: ((DiffRow , String , RowTapTarget ) -> Unit )? = null
773816 var onVisibleRowsChanged: ((Int , Int ) -> Unit )? = null
774817 var onContentHeightChanged: ((Int ) -> Unit )? = null
818+ var onViewportSizeChanged: (() -> Unit )? = null
775819
776820 fun prepareRows () = drawing.prepareRows(tokensByRowId, style, width)
777821 fun useCodeLayouts (layouts : CodeLayoutCache ) {
@@ -789,6 +833,7 @@ private class DiffCanvasView(context: Context) : View(context) {
789833 super .onSizeChanged(width, height, oldWidth, oldHeight)
790834 // Wrapped rows take their height from the width, so a new width is a new layout.
791835 if (style.wordWrap && width != oldWidth) layoutRows()
836+ onViewportSizeChanged?.invoke()
792837 setVerticalOffset(verticalOffset)
793838 setHorizontalOffset(horizontalOffset)
794839 clampHeaderPathOffsets()
@@ -914,21 +959,7 @@ private class DiffCanvasView(context: Context) : View(context) {
914959 else -> style.rowHeightPx.toInt()
915960 }.coerceAtLeast(1 )
916961
917- @Suppress(" ReturnCount" )
918- private fun rowIndexAt (y : Int ): Int {
919- if (rows.isEmpty()) return 0
920- var low = 0
921- var high = rows.lastIndex
922- while (low <= high) {
923- val middle = (low + high) ushr 1
924- when {
925- y < rowOffsets[middle] -> high = middle - 1
926- y >= rowOffsets[middle + 1 ] -> low = middle + 1
927- else -> return middle
928- }
929- }
930- return low.coerceIn(0 , rows.lastIndex)
931- }
962+ private fun rowIndexAt (y : Int ): Int = rowIndexAtOffset(y, rowOffsets, rows.size)
932963
933964 private fun rowHitAt (y : Float ): RowHit ? {
934965 stickyFileHeader(firstVisibleRow())?.let { sticky ->
@@ -949,14 +980,21 @@ private class DiffCanvasView(context: Context) : View(context) {
949980
950981 private fun firstVisibleRow (): Int = rowIndexAt(verticalOffset)
951982
983+ fun setViewportEventsPaused (paused : Boolean ) {
984+ viewportEventsPaused = paused
985+ if (paused) lastVisibleRange = null else emitVisibleRange()
986+ }
987+
952988 private fun emitVisibleRange () {
953- if (rows.isEmpty()) return
989+ if (viewportEventsPaused || rows.isEmpty()) return
990+ if (width <= 0 || height <= 0 ) return
954991 val first = rowIndexAt(verticalOffset)
955992 val last = rowIndexAt(verticalOffset + max(1 , height))
956993 val range = first to last
957- if (range == lastVisibleRange) return
958- lastVisibleRange = range
959- onVisibleRowsChanged?.invoke(first, last)
994+ if (range != lastVisibleRange) {
995+ lastVisibleRange = range
996+ onVisibleRowsChanged?.invoke(first, last)
997+ }
960998 }
961999
9621000 private fun drawRow (canvas : Canvas , row : DiffRow , top : Int , bottom : Int ) {
@@ -1464,3 +1502,19 @@ private fun JSONObject.floatDp(key: String, fallbackPx: Float, density: Float):
14641502
14651503private fun JSONObject.floatSp (key : String , fallbackPx : Float , density : Float ): Float =
14661504 if (has(key)) optDouble(key).toFloat() * density else fallbackPx
1505+
1506+ @Suppress(" ReturnCount" )
1507+ private fun rowIndexAtOffset (y : Int , rowOffsets : IntArray , rowCount : Int ): Int {
1508+ if (rowCount == 0 ) return 0
1509+ var low = 0
1510+ var high = rowCount - 1
1511+ while (low <= high) {
1512+ val middle = (low + high) ushr 1
1513+ when {
1514+ y < rowOffsets[middle] -> high = middle - 1
1515+ y >= rowOffsets[middle + 1 ] -> low = middle + 1
1516+ else -> return middle
1517+ }
1518+ }
1519+ return low.coerceIn(0 , rowCount - 1 )
1520+ }
0 commit comments