Skip to content

Commit ceab986

Browse files
authored
Merge branch 'main' into fix/skip-upstream-workflows-in-forks
2 parents 1eccfe2 + caa8c60 commit ceab986

5 files changed

Lines changed: 127 additions & 15 deletions

File tree

‎apps/mobile/modules/t3-review-diff/android/src/main/java/expo/modules/t3reviewdiff/ReviewDiffCanvasDrawing.kt‎

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import android.graphics.RectF
1010
import android.graphics.Shader
1111
import android.graphics.Typeface
1212
import android.text.TextPaint
13+
import kotlin.math.ceil
1314
import kotlin.math.max
1415
import kotlin.math.min
1516

@@ -211,6 +212,68 @@ internal class ReviewDiffCanvasDrawing(context: Context) {
211212
return codeLayouts.layout(rows, tokens, textPaint, style, theme, availableWidth.toInt())
212213
}
213214

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+
214277
fun lineNumberColor(change: String): Int = when (change) {
215278
"add" -> theme.addText
216279
"delete" -> theme.deleteText

‎apps/mobile/modules/t3-review-diff/android/src/main/java/expo/modules/t3reviewdiff/T3ReviewDiffModule.kt‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,8 @@ class T3ReviewDiffModule : Module() {
3838
Prop("rowHeight") { view: T3ReviewDiffView, rowHeight: Double ->
3939
view.setRowHeight(rowHeight.toFloat())
4040
}
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 -> }
4443
Prop("contentInsetBottom") { view: T3ReviewDiffView, value: Double ->
4544
view.setContentInsetBottom(value.toFloat())
4645
}

‎apps/mobile/modules/t3-review-diff/android/src/main/java/expo/modules/t3reviewdiff/T3ReviewDiffView.kt‎

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,6 @@ class T3ReviewDiffView(context: Context, appContext: AppContext) : ExpoView(cont
139139
canvasView.style = canvasView.style.copy(rowHeightPx = dp(value))
140140
}
141141

142-
fun setContentWidth(value: Float) {
143-
canvasView.contentWidthPx = max(width, dp(value).toInt())
144-
}
145-
146142
fun setContentInsetBottom(value: Float) {
147143
canvasView.setPadding(0, 0, 0, max(0, dp(value).toInt()))
148144
canvasView.setVerticalOffset(canvasView.verticalOffset())
@@ -718,6 +714,8 @@ private class DiffCanvasView(context: Context) : View(context) {
718714
private var codeWrap = CodeWrapLayout.NONE
719715
private var verticalOffset = 0
720716
private var horizontalOffset = 0
717+
718+
private var contentWidthPx = 0
721719
private val headerPathOffsetsByFileId = mutableMapOf<String, Int>()
722720
private var lastVisibleRange: Pair<Int, Int>? = null
723721
var rows: List<DiffRow> = emptyList()
@@ -731,7 +729,12 @@ private class DiffCanvasView(context: Context) : View(context) {
731729
var tokensByRowId: Map<String, List<DiffToken>> = emptyMap()
732730
set(value) {
733731
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+
}
735738
invalidate()
736739
}
737740
var viewedFileIds: Set<String> = emptySet()
@@ -766,13 +769,6 @@ private class DiffCanvasView(context: Context) : View(context) {
766769
field = value
767770
rebuildOffsets()
768771
}
769-
var contentWidthPx: Int = (1200 * density).toInt()
770-
set(value) {
771-
field = max(value, suggestedMinimumWidth)
772-
setHorizontalOffset(horizontalOffset)
773-
clampHeaderPathOffsets()
774-
invalidate()
775-
}
776772
var onRowTap: ((DiffRow, String, RowTapTarget) -> Unit)? = null
777773
var onVisibleRowsChanged: ((Int, Int) -> Unit)? = null
778774
var onContentHeightChanged: ((Int) -> Unit)? = null
@@ -894,6 +890,7 @@ private class DiffCanvasView(context: Context) : View(context) {
894890
}
895891

896892
private fun layoutRows() {
893+
contentWidthPx = drawing.contentWidth(rows, tokensByRowId, style)
897894
codeWrap = drawing.codeWrapLayout(rows, tokensByRowId, style, width)
898895
rowOffsets = IntArray(rows.size + 1)
899896
rows.forEachIndexed { index, row ->

‎apps/mobile/modules/t3-review-diff/android/src/test/java/expo/modules/t3reviewdiff/ReviewDiffCodeLayoutTest.kt‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import org.junit.Assert.assertTrue
1414
import org.junit.Test
1515
import org.junit.runner.RunWith
1616
import org.robolectric.RobolectricTestRunner
17+
import org.robolectric.RuntimeEnvironment
1718
import org.robolectric.annotation.Config
1819
import org.robolectric.annotation.GraphicsMode
1920

@@ -141,6 +142,57 @@ class ReviewDiffCodeLayoutTest {
141142
assertEquals(21, text.getSpanEnd(span))
142143
}
143144

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+
144196
private fun row(content: String) = DiffRow(
145197
kind = "line", id = "line", fileId = "file", filePath = "test.ts", previousPath = null,
146198
changeType = "modified", additions = 1, deletions = 0, text = "", content = content,

‎apps/mobile/src/features/diffs/nativeReviewDiffSurface.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ export interface NativeReviewDiffViewProps extends ViewProps {
122122
readonly themeJson: string;
123123
readonly styleJson?: string;
124124
readonly rowHeight: number;
125+
/** Android ignores this and measures the widest row. */
125126
readonly contentWidth: number;
126127
readonly initialRowIndex?: number;
127128
readonly scrollEnabled?: boolean;

0 commit comments

Comments
 (0)