The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 23, 01:45 UTC
fix(mobile): Android review diff scrolls to the end of long lines (#19431)
Co-authored-by: PR Batch Tester <agent@local.test> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: Julius Marminge <julius0216@outlook.com>
shivam committed
Oct 20, 2026 at 00:57 UTC
caa8c601acdb3646360031a2504fcacb680bace5
5 files changed
+127
-15
apps/mobile/modules/t3-review-diff/android/src/main/java/expo/modules/t3reviewdiff/ReviewDiffCanvasDrawing.kt
+63
@@ -10,6 +10,7 @@ import android.graphics.RectF
10
import android.graphics.Shader
11
import android.graphics.Typeface
12
import android.text.TextPaint
13
+import kotlin.math.ceil
14
import kotlin.math.max
15
import kotlin.math.min
16
@@ -211,6 +212,68 @@ internal class ReviewDiffCanvasDrawing(context: Context) {
212
return codeLayouts.layout(rows, tokens, textPaint, style, theme, availableWidth.toInt())
213
}
214
215
+ private data class WidthEntry(val row: DiffRow, val tokens: List<DiffToken>?, val width: Float)
216
+ private var widthEntries = emptyMap<String, WidthEntry>()
217
+ private var widthRows: List<DiffRow>? = null
218
+ private var widthTokens: Map<String, List<DiffToken>>? = null
219
+ private var widthStyle: DiffStyle? = null
220
+ private var measuredContentWidth = 0
221
+
222
+ fun contentWidth(
223
+ rows: List<DiffRow>,
224
+ tokens: Map<String, List<DiffToken>>,
225
+ style: DiffStyle
226
+ ): Int {
227
+ if (style.wordWrap) {
228
+ widthEntries = emptyMap()
229
+ widthRows = null
230
+ widthTokens = null
231
+ widthStyle = null
232
+ return 0
233
+ }
234
+ // Comment heights and viewport resizing do not change unwrapped text widths.
235
+ if (widthRows !== rows || widthTokens !== tokens || widthStyle != style) {
236
+ if (widthStyle != style) widthEntries = emptyMap()
237
+ val next = HashMap<String, WidthEntry>()
238
+ var widest = 0f
239
+ for (row in rows) {
240
+ if (row.kind != "line" && row.kind != "hunk") continue
241
+ val rowTokens = if (row.kind == "line") tokens[row.id] else null
242
+ val cached = widthEntries[row.id]
243
+ val entry = if (cached?.row == row && cached.tokens == rowTokens) {
244
+ cached
245
+ } else {
246
+ WidthEntry(row, rowTokens, measureRowWidth(row, rowTokens, style))
247
+ }
248
+ next[row.id] = entry
249
+ widest = max(widest, entry.width)
250
+ }
251
+ widthEntries = next
252
+ widthRows = rows
253
+ widthTokens = tokens
254
+ widthStyle = style
255
+ measuredContentWidth = ceil(
256
+ style.changeBarWidthPx + style.gutterWidthPx + style.codePaddingPx * 2f + widest,
257
+ ).toInt()
258
+ }
259
+ return measuredContentWidth
260
+ }
261
+
262
+ private fun measureRowWidth(row: DiffRow, tokens: List<DiffToken>?, style: DiffStyle): Float {
263
+ if (row.kind == "hunk") {
264
+ configureMonospacePaint(theme.hunkText, style.hunkFontSizePx, style.hunkFontWeight)
265
+ return textPaint.measureText(row.text.ifEmpty { row.content })
266
+ }
267
+ val runs = if (tokens.isNullOrEmpty()) listOf(DiffToken(row.content, null, 0)) else tokens
268
+ var width = 0f
269
+ // Match drawCode's run boundaries and fonts, including fallback glyphs and tabs.
270
+ for (run in runs) {
271
+ configureCodePaint(run.color ?: theme.text, run.fontStyle, style)
272
+ width += textPaint.measureText(run.content)
273
+ }
274
+ return width
275
+ }
276
+
277
fun lineNumberColor(change: String): Int = when (change) {
278
"add" -> theme.addText
279
"delete" -> theme.deleteText
apps/mobile/modules/t3-review-diff/android/src/main/java/expo/modules/t3reviewdiff/T3ReviewDiffModule.kt
+2
-3
@@ -38,9 +38,8 @@ class T3ReviewDiffModule : Module() {
38
Prop("rowHeight") { view: T3ReviewDiffView, rowHeight: Double ->
39
view.setRowHeight(rowHeight.toFloat())
40
}
41
- Prop("contentWidth") { view: T3ReviewDiffView, contentWidth: Double ->
42
- view.setContentWidth(contentWidth.toFloat())
43
- }
41
+ // Accept the shared prop while deriving Android's scroll range from the drawn text.
42
+ Prop("contentWidth") { _: T3ReviewDiffView, _: Double -> }
43
Prop("contentInsetBottom") { view: T3ReviewDiffView, value: Double ->
44
view.setContentInsetBottom(value.toFloat())
45
}
apps/mobile/modules/t3-review-diff/android/src/main/java/expo/modules/t3reviewdiff/T3ReviewDiffView.kt
+9
-12
@@ -139,10 +139,6 @@ class T3ReviewDiffView(context: Context, appContext: AppContext) : ExpoView(cont
139
canvasView.style = canvasView.style.copy(rowHeightPx = dp(value))
140
}
141
142
- fun setContentWidth(value: Float) {
143
- canvasView.contentWidthPx = max(width, dp(value).toInt())
144
- }
145
-
142
fun setContentInsetBottom(value: Float) {
143
canvasView.setPadding(0, 0, 0, max(0, dp(value).toInt()))
144
canvasView.setVerticalOffset(canvasView.verticalOffset())
@@ -718,6 +714,8 @@ private class DiffCanvasView(context: Context) : View(context) {
714
private var codeWrap = CodeWrapLayout.NONE
715
private var verticalOffset = 0
716
private var horizontalOffset = 0
717
+
718
+ private var contentWidthPx = 0
719
private val headerPathOffsetsByFileId = mutableMapOf<String, Int>()
720
private var lastVisibleRange: Pair<Int, Int>? = null
721
var rows: List<DiffRow> = emptyList()
@@ -731,7 +729,12 @@ private class DiffCanvasView(context: Context) : View(context) {
729
var tokensByRowId: Map<String, List<DiffToken>> = emptyMap()
730
set(value) {
731
field = value
734
- if (style.wordWrap) rebuildOffsets()
732
+ if (style.wordWrap) {
733
+ rebuildOffsets()
734
+ } else {
735
+ contentWidthPx = drawing.contentWidth(rows, value, style)
736
+ setHorizontalOffset(horizontalOffset)
737
+ }
738
invalidate()
739
}
740
var viewedFileIds: Set<String> = emptySet()
@@ -766,13 +769,6 @@ private class DiffCanvasView(context: Context) : View(context) {
769
field = value
770
rebuildOffsets()
771
}
769
- var contentWidthPx: Int = (1200 * density).toInt()
770
- set(value) {
771
- field = max(value, suggestedMinimumWidth)
772
- setHorizontalOffset(horizontalOffset)
773
- clampHeaderPathOffsets()
774
- invalidate()
775
- }
772
var onRowTap: ((DiffRow, String, RowTapTarget) -> Unit)? = null
773
var onVisibleRowsChanged: ((Int, Int) -> Unit)? = null
774
var onContentHeightChanged: ((Int) -> Unit)? = null
@@ -894,6 +890,7 @@ private class DiffCanvasView(context: Context) : View(context) {
890
}
891
892
private fun layoutRows() {
893
+ contentWidthPx = drawing.contentWidth(rows, tokensByRowId, style)
894
codeWrap = drawing.codeWrapLayout(rows, tokensByRowId, style, width)
895
rowOffsets = IntArray(rows.size + 1)
896
rows.forEachIndexed { index, row ->
apps/mobile/modules/t3-review-diff/android/src/test/java/expo/modules/t3reviewdiff/ReviewDiffCodeLayoutTest.kt
+52
@@ -14,6 +14,7 @@ import org.junit.Assert.assertTrue
14
import org.junit.Test
15
import org.junit.runner.RunWith
16
import org.robolectric.RobolectricTestRunner
17
+import org.robolectric.RuntimeEnvironment
18
import org.robolectric.annotation.Config
19
import org.robolectric.annotation.GraphicsMode
20
@@ -141,6 +142,57 @@ class ReviewDiffCodeLayoutTest {
142
assertEquals(21, text.getSpanEnd(span))
143
}
144
145
+ @Test
146
+ fun cachedContentWidthMatchesFreshMeasurementAfterUpdatesAndRemoval() {
147
+ val context = RuntimeEnvironment.getApplication()
148
+ val drawing = ReviewDiffCanvasDrawing(context)
149
+ val style = DiffStyle.defaults(1f)
150
+ fun assertFresh(
151
+ rows: List<DiffRow>,
152
+ tokens: Map<String, List<DiffToken>> = emptyMap(),
153
+ currentStyle: DiffStyle = style
154
+ ) {
155
+ assertEquals(
156
+ ReviewDiffCanvasDrawing(context).contentWidth(rows, tokens, currentStyle),
157
+ drawing.contentWidth(rows, tokens, currentStyle),
158
+ )
159
+ }
160
+
161
+ val line = row("مرحبا\t漢字 e\u0301 ".repeat(20))
162
+ val rows = listOf(line)
163
+ val tokens = mapOf(
164
+ line.id to listOf(
165
+ DiffToken("مرحبا", Color.RED, 2),
166
+ DiffToken(line.content.drop(5), Color.BLUE, 1),
167
+ ),
168
+ )
169
+ assertFresh(rows)
170
+ assertFresh(rows, tokens)
171
+ assertFresh(rows, tokens)
172
+ assertFresh(
173
+ rows,
174
+ tokens,
175
+ style.copy(
176
+ codeFontSizePx = 24f,
177
+ codeFontWeight = "bold",
178
+ gutterWidthPx = 90f,
179
+ codePaddingPx = 16f,
180
+ changeBarWidthPx = 5f,
181
+ ),
182
+ )
183
+
184
+ val hunk = line.copy(kind = "hunk", id = "hunk", text = "@@ context @@ ".repeat(100))
185
+ val hunkRows = listOf(line, hunk)
186
+ assertFresh(hunkRows, tokens)
187
+ assertFresh(hunkRows, tokens, style.copy(hunkFontSizePx = 22f, hunkFontWeight = "bold"))
188
+ val shortHunk = hunk.copy(text = "@@ short @@")
189
+ assertFresh(listOf(line, shortHunk), tokens)
190
+ assertFresh(listOf(shortHunk), tokens)
191
+ assertFresh(rows, tokens, style.copy(wordWrap = true))
192
+ assertFresh(rows, tokens)
193
+ assertFresh(rows)
194
+ }
195
+
196
private fun row(content: String) = DiffRow(
197
kind = "line", id = "line", fileId = "file", filePath = "test.ts", previousPath = null,
198
changeType = "modified", additions = 1, deletions = 0, text = "", content = content,
apps/mobile/src/features/diffs/nativeReviewDiffSurface.ts
+1
@@ -122,6 +122,7 @@ export interface NativeReviewDiffViewProps extends ViewProps {
122
readonly themeJson: string;
123
readonly styleJson?: string;
124
readonly rowHeight: number;
125
+ /** Android ignores this and measures the widest row. */
126
readonly contentWidth: number;
127
readonly initialRowIndex?: number;
128
readonly scrollEnabled?: boolean;