diff --git a/.maestro/enrichedInput/screenshots/android/checkbox_toggle.png b/.maestro/enrichedInput/screenshots/android/checkbox_toggle.png index 2ebc3fd4b..1b24829b9 100644 Binary files a/.maestro/enrichedInput/screenshots/android/checkbox_toggle.png and b/.maestro/enrichedInput/screenshots/android/checkbox_toggle.png differ diff --git a/.maestro/enrichedInput/screenshots/android/codeblock_no_link_detection.png b/.maestro/enrichedInput/screenshots/android/codeblock_no_link_detection.png index 78e27577d..22acdaf60 100644 Binary files a/.maestro/enrichedInput/screenshots/android/codeblock_no_link_detection.png and b/.maestro/enrichedInput/screenshots/android/codeblock_no_link_detection.png differ diff --git a/.maestro/enrichedInput/screenshots/android/codeblock_style_blocking.png b/.maestro/enrichedInput/screenshots/android/codeblock_style_blocking.png index 44ae70724..ad4efbb28 100644 Binary files a/.maestro/enrichedInput/screenshots/android/codeblock_style_blocking.png and b/.maestro/enrichedInput/screenshots/android/codeblock_style_blocking.png differ diff --git a/.maestro/enrichedInput/screenshots/android/conflicting_paragraph_merge.png b/.maestro/enrichedInput/screenshots/android/conflicting_paragraph_merge.png index 074a56c79..1c76fd6ec 100644 Binary files a/.maestro/enrichedInput/screenshots/android/conflicting_paragraph_merge.png and b/.maestro/enrichedInput/screenshots/android/conflicting_paragraph_merge.png differ diff --git a/.maestro/enrichedInput/screenshots/android/font_scaling.png b/.maestro/enrichedInput/screenshots/android/font_scaling.png index 6b955aa4e..3cedbb578 100644 Binary files a/.maestro/enrichedInput/screenshots/android/font_scaling.png and b/.maestro/enrichedInput/screenshots/android/font_scaling.png differ diff --git a/.maestro/enrichedInput/screenshots/android/font_scaling_placeholder.png b/.maestro/enrichedInput/screenshots/android/font_scaling_placeholder.png index 1288cc4b3..ff270540b 100644 Binary files a/.maestro/enrichedInput/screenshots/android/font_scaling_placeholder.png and b/.maestro/enrichedInput/screenshots/android/font_scaling_placeholder.png differ diff --git a/.maestro/enrichedInput/screenshots/android/initial_placeholder.png b/.maestro/enrichedInput/screenshots/android/initial_placeholder.png index 63fdb011d..98afbc256 100644 Binary files a/.maestro/enrichedInput/screenshots/android/initial_placeholder.png and b/.maestro/enrichedInput/screenshots/android/initial_placeholder.png differ diff --git a/.maestro/enrichedInput/screenshots/android/inline_code_paste_into_codeblock.png b/.maestro/enrichedInput/screenshots/android/inline_code_paste_into_codeblock.png index 995be24f8..9daf9bc8d 100644 Binary files a/.maestro/enrichedInput/screenshots/android/inline_code_paste_into_codeblock.png and b/.maestro/enrichedInput/screenshots/android/inline_code_paste_into_codeblock.png differ diff --git a/.maestro/enrichedInput/screenshots/android/inline_styles_removal.png b/.maestro/enrichedInput/screenshots/android/inline_styles_removal.png index 731ac2b2a..27c6d2760 100644 Binary files a/.maestro/enrichedInput/screenshots/android/inline_styles_removal.png and b/.maestro/enrichedInput/screenshots/android/inline_styles_removal.png differ diff --git a/.maestro/enrichedInput/screenshots/android/mention_popup_closing_on_cursor_travel_1.png b/.maestro/enrichedInput/screenshots/android/mention_popup_closing_on_cursor_travel_1.png index 8f6adef9b..b19e1dd84 100644 Binary files a/.maestro/enrichedInput/screenshots/android/mention_popup_closing_on_cursor_travel_1.png and b/.maestro/enrichedInput/screenshots/android/mention_popup_closing_on_cursor_travel_1.png differ diff --git a/.maestro/enrichedInput/screenshots/android/mention_popup_closing_on_cursor_travel_2.png b/.maestro/enrichedInput/screenshots/android/mention_popup_closing_on_cursor_travel_2.png index 6bbb64a1e..975073656 100644 Binary files a/.maestro/enrichedInput/screenshots/android/mention_popup_closing_on_cursor_travel_2.png and b/.maestro/enrichedInput/screenshots/android/mention_popup_closing_on_cursor_travel_2.png differ diff --git a/.maestro/enrichedInput/screenshots/android/mention_popup_closing_on_cursor_travel_3.png b/.maestro/enrichedInput/screenshots/android/mention_popup_closing_on_cursor_travel_3.png index eb601c3e2..8af1ec6f9 100644 Binary files a/.maestro/enrichedInput/screenshots/android/mention_popup_closing_on_cursor_travel_3.png and b/.maestro/enrichedInput/screenshots/android/mention_popup_closing_on_cursor_travel_3.png differ diff --git a/.maestro/enrichedInput/screenshots/android/paragraph_styles_blocks.png b/.maestro/enrichedInput/screenshots/android/paragraph_styles_blocks.png index 4d075df95..925764ac8 100644 Binary files a/.maestro/enrichedInput/screenshots/android/paragraph_styles_blocks.png and b/.maestro/enrichedInput/screenshots/android/paragraph_styles_blocks.png differ diff --git a/.maestro/enrichedInput/screenshots/android/paragraph_styles_lists.png b/.maestro/enrichedInput/screenshots/android/paragraph_styles_lists.png index 4a01cdb95..7bb6e38db 100644 Binary files a/.maestro/enrichedInput/screenshots/android/paragraph_styles_lists.png and b/.maestro/enrichedInput/screenshots/android/paragraph_styles_lists.png differ diff --git a/.maestro/enrichedInput/screenshots/android/paragraph_styles_no_crash.png b/.maestro/enrichedInput/screenshots/android/paragraph_styles_no_crash.png index ffeb45b81..d6ed66145 100644 Binary files a/.maestro/enrichedInput/screenshots/android/paragraph_styles_no_crash.png and b/.maestro/enrichedInput/screenshots/android/paragraph_styles_no_crash.png differ diff --git a/android/src/main/java/com/swmansion/enriched/textinput/EnrichedTextInputView.kt b/android/src/main/java/com/swmansion/enriched/textinput/EnrichedTextInputView.kt index f13c034b8..30f317376 100644 --- a/android/src/main/java/com/swmansion/enriched/textinput/EnrichedTextInputView.kt +++ b/android/src/main/java/com/swmansion/enriched/textinput/EnrichedTextInputView.kt @@ -99,6 +99,9 @@ class EnrichedTextInputView : val customStyles: CustomStyles? = CustomStyles(this) var isDuringTransaction: Boolean = false var isRemovingMany: Boolean = false + + var isWaitingForHeightUpdate: Boolean = false + private var didCapScrollWhileWaiting = false var scrollEnabled: Boolean = true var allowFontScaling: Boolean = EnrichedConstants.ALLOW_FONT_SCALING_DEFAULT set(value) { @@ -187,7 +190,25 @@ class EnrichedTextInputView : // Android's internal cursor tracker gets confused by ALIGN_CENTER + LeadingMarginSpan // and attempts to scroll the text horizontally. // We lock the horizontal scroll to 0 to prevent the view from shifting. - super.scrollTo(0, y) + // Height updates are async, so new text is laid out before the view grows. Cap the scroll at its + // post-resize value, otherwise EditText jumps to the new line and back. + val targetY = if (isWaitingForHeightUpdate) y.coerceAtMost(getMaxScrollAfterHeightUpdate()) else y + if (targetY < y) didCapScrollWhileWaiting = true + super.scrollTo(0, targetY) + } + + fun onHeightUpdateMounted() { + isWaitingForHeightUpdate = false + if (!didCapScrollWhileWaiting) return + didCapScrollWhileWaiting = false + // The predicted height can be wrong (e.g. when it comes from flex) + post { forceScrollToSelection() } + } + + private fun getMaxScrollAfterHeightUpdate(): Int { + val maxTextHeight = MeasurementStore.getMaxTextHeight(id) ?: return Int.MAX_VALUE + val textHeight = layout?.height ?: return Int.MAX_VALUE + return (textHeight - maxTextHeight).toInt().coerceAtLeast(0) } override fun onCreateInputConnection(outAttrs: EditorInfo): InputConnection? { @@ -1127,6 +1148,9 @@ class EnrichedTextInputView : } } + fun usesLocalePreferredLineHeight(): Boolean = + Build.VERSION.SDK_INT >= Build.VERSION_CODES.VANILLA_ICE_CREAM && isLocalePreferredLineHeightForMinimumUsed + private fun forceScrollToSelection() { val textLayout = layout ?: return val cursorOffset = selectionStart diff --git a/android/src/main/java/com/swmansion/enriched/textinput/EnrichedTextInputViewLayoutManager.kt b/android/src/main/java/com/swmansion/enriched/textinput/EnrichedTextInputViewLayoutManager.kt index 0d20e7632..27b45708c 100644 --- a/android/src/main/java/com/swmansion/enriched/textinput/EnrichedTextInputViewLayoutManager.kt +++ b/android/src/main/java/com/swmansion/enriched/textinput/EnrichedTextInputViewLayoutManager.kt @@ -13,12 +13,13 @@ class EnrichedTextInputViewLayoutManager( val text = view.text val paint = view.paint - val needUpdate = MeasurementStore.store(view.id, text, paint) + val needUpdate = MeasurementStore.store(view.id, text, paint, view.usesLocalePreferredLineHeight()) if (!needUpdate) return forceHeightRecalculationCounter++ val state = Arguments.createMap() state.putInt("forceHeightRecalculationCounter", forceHeightRecalculationCounter) + view.isWaitingForHeightUpdate = true stateWrapper.updateState(state) } diff --git a/android/src/main/java/com/swmansion/enriched/textinput/EnrichedTextInputViewManager.kt b/android/src/main/java/com/swmansion/enriched/textinput/EnrichedTextInputViewManager.kt index 26f20b552..a0a9aa617 100644 --- a/android/src/main/java/com/swmansion/enriched/textinput/EnrichedTextInputViewManager.kt +++ b/android/src/main/java/com/swmansion/enriched/textinput/EnrichedTextInputViewManager.kt @@ -58,6 +58,7 @@ class EnrichedTextInputViewManager : stateWrapper: StateWrapper?, ): Any? { view.stateWrapper = stateWrapper + view.onHeightUpdateMounted() return super.updateState(view, props, stateWrapper) } diff --git a/android/src/main/java/com/swmansion/enriched/textinput/MeasurementStore.kt b/android/src/main/java/com/swmansion/enriched/textinput/MeasurementStore.kt index 7ca655a45..d0ddcd5cd 100644 --- a/android/src/main/java/com/swmansion/enriched/textinput/MeasurementStore.kt +++ b/android/src/main/java/com/swmansion/enriched/textinput/MeasurementStore.kt @@ -1,6 +1,7 @@ package com.swmansion.enriched.textinput import android.content.Context +import android.graphics.Paint import android.graphics.Typeface import android.graphics.text.LineBreaker import android.os.Build @@ -28,6 +29,7 @@ object MeasurementStore { data class PaintParams( val typeface: Typeface, val fontSize: Float, + val useLocalePreferredLineHeight: Boolean, ) data class MeasurementParams( @@ -36,6 +38,8 @@ object MeasurementStore { val cachedSize: Long, val spannable: CharSequence?, val paintParams: PaintParams, + // In px, as given by Yoga's measure + val maxTextHeight: Float? = null, ) private val data = ConcurrentHashMap() @@ -44,15 +48,17 @@ object MeasurementStore { id: Int, spannable: Spannable?, paint: TextPaint, + useLocalePreferredLineHeight: Boolean, ): Boolean { val cachedWidth = data[id]?.cachedWidth ?: 0f val cachedSize = data[id]?.cachedSize ?: 0L val initialized = data[id]?.initialized ?: true + val maxTextHeight = data[id]?.maxTextHeight - val size = measure(cachedWidth, spannable, paint) - val paintParams = PaintParams(paint.typeface, paint.textSize) + val size = measure(cachedWidth, spannable, paint, useLocalePreferredLineHeight) + val paintParams = PaintParams(paint.typeface, paint.textSize, useLocalePreferredLineHeight) - data[id] = MeasurementParams(initialized, cachedWidth, size, spannable, paintParams) + data[id] = MeasurementParams(initialized, cachedWidth, size, spannable, paintParams, maxTextHeight) return cachedSize != size } @@ -60,6 +66,8 @@ object MeasurementStore { data.remove(id) } + fun getMaxTextHeight(id: Int): Float? = data[id]?.maxTextHeight + private fun measure( maxWidth: Float, spannable: CharSequence?, @@ -71,13 +79,14 @@ object MeasurementStore { textSize = paintParams.fontSize } - return measure(maxWidth, spannable, paint) + return measure(maxWidth, spannable, paint, paintParams.useLocalePreferredLineHeight) } private fun measure( maxWidth: Float, spannable: CharSequence?, paint: TextPaint, + useLocalePreferredLineHeight: Boolean, ): Long { val text = spannable ?: "" val textLength = text.length @@ -95,6 +104,13 @@ object MeasurementStore { builder.setUseLineSpacingFromFallbacks(true) } + // EditText (targetSdk 35+) keeps empty lines as tall as lines with text, otherwise they're measured shorter than drawn + if (useLocalePreferredLineHeight && Build.VERSION.SDK_INT >= Build.VERSION_CODES.VANILLA_ICE_CREAM) { + val minimumFontMetrics = Paint.FontMetrics() + paint.getFontMetricsForLocale(minimumFontMetrics) + builder.setMinimumFontMetrics(minimumFontMetrics) + } + val staticLayout = builder.build() val heightInSP = PixelUtil.toDIPFromPixel(staticLayout.height.toFloat()) val widthInSP = PixelUtil.toDIPFromPixel(maxWidth) @@ -175,7 +191,7 @@ object MeasurementStore { } val typeface = applyStyles(defaultView.typeface, fontStyle, fontWeight, fontFamily, context.assets) - val paintParams = PaintParams(typeface, fontSize) + val paintParams = PaintParams(typeface, fontSize, defaultView.usesLocalePreferredLineHeight()) val size = measure(width, text, paintParams) if (id != null) { @@ -208,7 +224,7 @@ object MeasurementStore { textSize = value.paintParams.fontSize } - val size = measure(width, value.spannable, paint) + val size = measure(width, value.spannable, paint, value.paintParams.useLocalePreferredLineHeight) data[id] = MeasurementParams(true, width, size, value.spannable, value.paintParams) return size } @@ -222,6 +238,18 @@ object MeasurementStore { props: ReadableMap?, ): Long { val size = getMeasureById(context, id, width, props) + + if (id != null) { + val maxTextHeight = + when (heightMode) { + YogaMeasureMode.AT_MOST -> height + YogaMeasureMode.UNDEFINED -> Float.POSITIVE_INFINITY + else -> null + } + // Atomic, so a concurrent store() from the UI thread isn't overwritten with stale text measurements + data.computeIfPresent(id) { _, params -> params.copy(maxTextHeight = maxTextHeight) } + } + if (heightMode !== YogaMeasureMode.AT_MOST) { return size }