From 3c9b0f1946cd5f285de5555a388f00f4a77790ee Mon Sep 17 00:00:00 2001 From: David Schachter Date: Fri, 13 Nov 2026 19:42:50 -0800 Subject: [PATCH 1/6] ADFA-7142: Never shrink plugin text below its own size; scale text added later ADFA-4212 scales plugin tab text by the editor's font size / 14 when a plugin fragment's view is created. On the test phone the code had been pinched to 6.38sp, so every plugin tab drew at 0.456x (measured 12.761933 px for a 15sp label, = 28 px x 6.3809667 / 14). Rows and chips added after the view was built were never scaled, so one tab mixed two sizes. PluginTextScaler, moved out of EditorHandlerActivity: - scale = max(1, editor font size / 14): plugin text still grows with the editor font, never shrinks below the plugin's sp size. - Text views added under a tracked plugin view are scaled at the next layout pass. The global-layout listener is held only while the view is attached, so it can always be removed; at scale 1 it does nothing. PluginTextScalerTest: 8 Robolectric tests. Without the floor, the 6.38 case fails at 12.761933 px; without the watcher, the three tests of text added later fail; without the detach cleanup or the scale-once guard, their tests fail. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Vh2DAZWMkFWJh7CY7P4kCH --- .../editor/EditorHandlerActivity.kt | 32 +--- .../activities/editor/PluginTextScaler.kt | 84 ++++++++++ .../activities/editor/PluginTextScalerTest.kt | 148 ++++++++++++++++++ 3 files changed, 240 insertions(+), 24 deletions(-) create mode 100644 app/src/main/java/com/itsaky/androidide/activities/editor/PluginTextScaler.kt create mode 100644 app/src/test/java/com/itsaky/androidide/activities/editor/PluginTextScalerTest.kt diff --git a/app/src/main/java/com/itsaky/androidide/activities/editor/EditorHandlerActivity.kt b/app/src/main/java/com/itsaky/androidide/activities/editor/EditorHandlerActivity.kt index eb62c37071..a564bb959f 100644 --- a/app/src/main/java/com/itsaky/androidide/activities/editor/EditorHandlerActivity.kt +++ b/app/src/main/java/com/itsaky/androidide/activities/editor/EditorHandlerActivity.kt @@ -25,7 +25,6 @@ import android.content.res.Configuration import android.os.Bundle import android.text.TextUtils import android.util.Log -import android.util.TypedValue import android.view.KeyEvent import android.view.View import android.view.ViewGroup.LayoutParams @@ -134,7 +133,6 @@ import com.itsaky.androidide.utils.UniqueNameBuilder import com.itsaky.androidide.utils.capHeightToSpaceBelow import com.itsaky.androidide.utils.flashError import com.itsaky.androidide.utils.flashSuccess -import com.itsaky.androidide.utils.forEachViewRecursively import com.itsaky.androidide.utils.hasVisibleDialog import com.itsaky.androidide.utils.isDeepLinkTargetOfOpenProject import com.itsaky.androidide.utils.recordProjectOpenedBookkeeping @@ -153,7 +151,6 @@ import org.greenrobot.eventbus.ThreadMode import org.koin.android.ext.android.inject import org.koin.core.qualifier.named import java.io.File -import java.util.WeakHashMap import java.util.concurrent.ConcurrentHashMap import java.util.concurrent.CopyOnWriteArrayList import java.util.concurrent.atomic.AtomicBoolean @@ -182,8 +179,8 @@ open class EditorHandlerActivity : private val pluginTabIndices = mutableMapOf() private val tabIndexToPluginId = mutableMapOf() - private var lastAppliedPluginFontScale = EditorPreferences.editorFontScale - private val pluginTextBaseSizes = WeakHashMap() + private val pluginTextScaler = PluginTextScaler { EditorPreferences.editorFontScale } + private var lastAppliedPluginTextScale = pluginTextScaler.scale() private val pluginFontScalingListener = object : FragmentManager.FragmentLifecycleCallbacks() { @@ -193,9 +190,8 @@ open class EditorHandlerActivity : view: View, savedInstanceState: Bundle?, ) { - val scale = EditorPreferences.editorFontScale - if (scale != 1f && isPluginFragment(mFragment)) { - applyPluginFontScale(view, scale) + if (isPluginFragment(mFragment)) { + pluginTextScaler.track(view) } } } @@ -2022,33 +2018,21 @@ open class EditorHandlerActivity : } private fun syncPluginUiFontSize() { - val scale = EditorPreferences.editorFontScale - if (scale == lastAppliedPluginFontScale) { + val scale = pluginTextScaler.scale() + if (scale == lastAppliedPluginTextScale) { return } - lastAppliedPluginFontScale = scale + lastAppliedPluginTextScale = scale val pluginFragments = mutableListOf() collectPluginFragments(supportFragmentManager, pluginFragments) pluginFragments.forEach { fragment -> - fragment.view?.let { applyPluginFontScale(it, scale) } + fragment.view?.let(pluginTextScaler::rescale) } } private fun isPluginFragment(fragment: Fragment): Boolean = fragment.javaClass.classLoader !== javaClass.classLoader - private fun applyPluginFontScale( - root: View, - scale: Float, - ) { - root.forEachViewRecursively { view -> - if (view is TextView) { - val baseSize = pluginTextBaseSizes.getOrPut(view) { view.textSize } - view.setTextSize(TypedValue.COMPLEX_UNIT_PX, baseSize * scale) - } - } - } - private fun collectPluginFragments( manager: FragmentManager, into: MutableList, diff --git a/app/src/main/java/com/itsaky/androidide/activities/editor/PluginTextScaler.kt b/app/src/main/java/com/itsaky/androidide/activities/editor/PluginTextScaler.kt new file mode 100644 index 0000000000..f46f4f0a8c --- /dev/null +++ b/app/src/main/java/com/itsaky/androidide/activities/editor/PluginTextScaler.kt @@ -0,0 +1,84 @@ +package com.itsaky.androidide.activities.editor + +import android.util.TypedValue +import android.view.View +import android.view.ViewTreeObserver +import android.widget.TextView +import com.itsaky.androidide.utils.forEachViewRecursively +import java.util.WeakHashMap +import kotlin.math.max + +/** + * Scales the text in plugin views by the editor's font-size preference (ADFA-4212). + * + * The scale never goes below 1 (ADFA-7142). The editor's size goes down to 6sp, often by pinching + * the code, and plugin tabs followed it to under half their size. Views a plugin adds after its + * view was built (list rows, chips) are scaled too; without that, one panel mixed two sizes. + * + * Sizes are px, scaled from the size each [TextView] had when first seen, so applying the same + * scale twice changes nothing. A plugin that resets a size it already set (say, on rebinding a + * recycled row) gets its own size back until the preference next changes. + */ +internal class PluginTextScaler( + private val editorFontScale: () -> Float, +) { + private val baseSizes = WeakHashMap() + + fun scale(): Float = max(1f, editorFontScale()) + + /** Scales the text under [root] now, and in the views added under it later. */ + fun track(root: View) { + scaleNewText(root) + root.addOnAttachStateChangeListener(AddedViewWatcher(root)) + } + + /** Applies the current [scale] to all the text under [root], once the preference has changed. */ + fun rescale(root: View) { + val scale = scale() + root.forEachViewRecursively { view -> + if (view is TextView) { + val base = baseSizes.getOrPut(view) { view.textSize } + view.setTextSize(TypedValue.COMPLEX_UNIT_PX, base * scale) + } + } + } + + private fun scaleNewText(root: View) { + val scale = scale() + // At 1 nothing needs scaling, and a view left unrecorded still has its own size for rescale(). + if (scale == 1f) return + root.forEachViewRecursively { view -> + if (view is TextView && view !in baseSizes) { + baseSizes[view] = view.textSize + view.setTextSize(TypedValue.COMPLEX_UNIT_PX, view.textSize * scale) + } + } + } + + /** + * Catches views added under [root] at the layout pass that follows. The listener lives on the + * window's observer only while [root] is attached: once detached, [View.getViewTreeObserver] + * returns a different observer, which could no longer remove it. + */ + private inner class AddedViewWatcher( + private val root: View, + ) : View.OnAttachStateChangeListener, + ViewTreeObserver.OnGlobalLayoutListener { + private var observer: ViewTreeObserver? = null + + init { + if (root.isAttachedToWindow) onViewAttachedToWindow(root) + } + + override fun onViewAttachedToWindow(v: View) { + observer = v.viewTreeObserver.also { it.addOnGlobalLayoutListener(this) } + } + + override fun onViewDetachedFromWindow(v: View) { + observer?.takeIf { it.isAlive }?.removeOnGlobalLayoutListener(this) + observer = null + } + + override fun onGlobalLayout() = scaleNewText(root) + } +} diff --git a/app/src/test/java/com/itsaky/androidide/activities/editor/PluginTextScalerTest.kt b/app/src/test/java/com/itsaky/androidide/activities/editor/PluginTextScalerTest.kt new file mode 100644 index 0000000000..ff98b26de9 --- /dev/null +++ b/app/src/test/java/com/itsaky/androidide/activities/editor/PluginTextScalerTest.kt @@ -0,0 +1,148 @@ +package com.itsaky.androidide.activities.editor + +import android.app.Activity +import android.app.Application +import android.util.TypedValue +import android.view.ViewGroup +import android.widget.LinearLayout +import android.widget.TextView +import com.google.common.truth.Truth.assertThat +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.Robolectric +import org.robolectric.RobolectricTestRunner +import org.robolectric.annotation.Config + +/** + * Plugin text follows the editor's font size up, never down (ADFA-7142), and text a plugin adds + * after its view was built gets the same scale as the rest. + */ +@RunWith(RobolectricTestRunner::class) +@Config(application = Application::class, sdk = [33]) +class PluginTextScalerTest { + private var editorScale = 1f + private val scaler = PluginTextScaler { editorScale } + private lateinit var activity: Activity + private lateinit var root: LinearLayout + + @Before + fun setUp() { + activity = Robolectric.buildActivity(Activity::class.java).setup().get() + root = LinearLayout(activity) + } + + private fun text(px: Float) = TextView(activity).apply { setTextSize(TypedValue.COMPLEX_UNIT_PX, px) } + + private fun attach() = activity.setContentView(root) + + // The layout pass that follows adding a view, as the window runs it. + private fun layOut() = + activity.window.decorView.viewTreeObserver + .dispatchOnGlobalLayout() + + @Test + fun textIsScaledWhenTheViewIsTracked() { + editorScale = 1.5f + val label = text(20f).also(root::addView) + + scaler.track(root) + + assertThat(label.textSize).isEqualTo(30f) + } + + @Test + fun textAddedAfterTheViewWasBuiltIsScaledToo() { + editorScale = 1.5f + // As a fragment's view: tracked when created, attached to the window after. + scaler.track(root) + attach() + + val row = text(20f).also(root::addView) + layOut() + + assertThat(row.textSize).isEqualTo(30f) + } + + @Test + fun aViewAlreadyAttachedWhenTrackedStillCatchesLaterText() { + editorScale = 1.5f + attach() + scaler.track(root) + + val row = text(20f).also(root::addView) + layOut() + + assertThat(row.textSize).isEqualTo(30f) + } + + @Test + fun laterLayoutsDoNotScaleTextAgain() { + editorScale = 1.5f + scaler.track(root) + attach() + val row = text(20f).also(root::addView) + + layOut() + layOut() + + assertThat(row.textSize).isEqualTo(30f) + } + + @Test + fun theEditorsSmallestSizeLeavesPluginTextAtItsOwnSize() { + // The phone ADFA-7142 was found on: the code pinched down to 6.38sp, against a default of 14. + editorScale = 6.3809667f / 14f + val label = text(28f).also(root::addView) + scaler.track(root) + attach() + val row = text(28f).also(root::addView) + + layOut() + + assertThat(label.textSize).isEqualTo(28f) + assertThat(row.textSize).isEqualTo(28f) + } + + @Test + fun aPreferenceChangeScalesFromEachViewsOwnSize() { + editorScale = 2f + val label = text(20f).also(root::addView) + scaler.track(root) + + editorScale = 1.5f + scaler.rescale(root) + assertThat(label.textSize).isEqualTo(30f) + + editorScale = 0.5f + scaler.rescale(root) + assertThat(label.textSize).isEqualTo(20f) + } + + @Test + fun textAddedAtTheDefaultSizeIsScaledWhenThePreferenceGrows() { + scaler.track(root) + attach() + val row = text(20f).also(root::addView) + layOut() + assertThat(row.textSize).isEqualTo(20f) + + editorScale = 2f + scaler.rescale(root) + + assertThat(row.textSize).isEqualTo(40f) + } + + @Test + fun aDetachedViewStopsWatchingTheWindow() { + editorScale = 1.5f + scaler.track(root) + attach() + (root.parent as ViewGroup).removeView(root) + + val row = text(20f).also(root::addView) + layOut() + + assertThat(row.textSize).isEqualTo(20f) + } +} From f22142684f2901670b9b435860bd61992b44d273 Mon Sep 17 00:00:00 2001 From: David Schachter Date: Sun, 15 Nov 2026 20:26:07 -0800 Subject: [PATCH 2/6] ADFA-7142: Rescale plugin text that was detached when the preference changed rescale() only walks attached views, so a TextView detached at that moment (a pooled RecyclerView row) kept its old size on return, because the scaleNewText guard skipped any view already recorded. Record the scale last applied next to each baseline and re-apply when it differs, including back to 1. The layout-pass walk still returns at once until some view has been scaled. Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_01Vh2DAZWMkFWJh7CY7P4kCH --- .../activities/editor/PluginTextScaler.kt | 38 ++++++++++++++----- .../activities/editor/PluginTextScalerTest.kt | 37 ++++++++++++++++++ 2 files changed, 65 insertions(+), 10 deletions(-) diff --git a/app/src/main/java/com/itsaky/androidide/activities/editor/PluginTextScaler.kt b/app/src/main/java/com/itsaky/androidide/activities/editor/PluginTextScaler.kt index f46f4f0a8c..1d69ee396f 100644 --- a/app/src/main/java/com/itsaky/androidide/activities/editor/PluginTextScaler.kt +++ b/app/src/main/java/com/itsaky/androidide/activities/editor/PluginTextScaler.kt @@ -22,7 +22,15 @@ import kotlin.math.max internal class PluginTextScaler( private val editorFontScale: () -> Float, ) { - private val baseSizes = WeakHashMap() + private class Sizing( + val base: Float, + var applied: Float, + ) + + private val sizings = WeakHashMap() + + // Until some view is scaled, none can be stale, so the default skips the walk. + private var scaledAny = false fun scale(): Float = max(1f, editorFontScale()) @@ -36,25 +44,35 @@ internal class PluginTextScaler( fun rescale(root: View) { val scale = scale() root.forEachViewRecursively { view -> - if (view is TextView) { - val base = baseSizes.getOrPut(view) { view.textSize } - view.setTextSize(TypedValue.COMPLEX_UNIT_PX, base * scale) - } + if (view is TextView) apply(view, sizings.getOrPut(view) { Sizing(view.textSize, 1f) }, scale) } } + // A view detached while the preference changed, a pooled row say, is stale when it returns. private fun scaleNewText(root: View) { val scale = scale() - // At 1 nothing needs scaling, and a view left unrecorded still has its own size for rescale(). - if (scale == 1f) return + if (scale == 1f && !scaledAny) return root.forEachViewRecursively { view -> - if (view is TextView && view !in baseSizes) { - baseSizes[view] = view.textSize - view.setTextSize(TypedValue.COMPLEX_UNIT_PX, view.textSize * scale) + if (view !is TextView) return@forEachViewRecursively + val sizing = sizings[view] + if (sizing == null) { + apply(view, Sizing(view.textSize, 1f).also { sizings[view] = it }, scale) + } else if (sizing.applied != scale) { + apply(view, sizing, scale) } } } + private fun apply( + view: TextView, + sizing: Sizing, + scale: Float, + ) { + if (scale != 1f) scaledAny = true + view.setTextSize(TypedValue.COMPLEX_UNIT_PX, sizing.base * scale) + sizing.applied = scale + } + /** * Catches views added under [root] at the layout pass that follows. The listener lives on the * window's observer only while [root] is attached: once detached, [View.getViewTreeObserver] diff --git a/app/src/test/java/com/itsaky/androidide/activities/editor/PluginTextScalerTest.kt b/app/src/test/java/com/itsaky/androidide/activities/editor/PluginTextScalerTest.kt index ff98b26de9..78cb768c7a 100644 --- a/app/src/test/java/com/itsaky/androidide/activities/editor/PluginTextScalerTest.kt +++ b/app/src/test/java/com/itsaky/androidide/activities/editor/PluginTextScalerTest.kt @@ -145,4 +145,41 @@ class PluginTextScalerTest { assertThat(row.textSize).isEqualTo(20f) } + + @Test + fun aViewReinsertedAfterThePreferenceChangedMatchesAFreshOne() { + editorScale = 1.5f + attach() + scaler.track(root) + val pooled = text(20f).also(root::addView) + layOut() + assertThat(pooled.textSize).isEqualTo(30f) + root.removeView(pooled) + + editorScale = 2f + scaler.rescale(root) + root.addView(pooled) + val fresh = text(20f).also(root::addView) + layOut() + + assertThat(pooled.textSize).isEqualTo(40f) + assertThat(fresh.textSize).isEqualTo(40f) + } + + @Test + fun aViewReinsertedAfterThePreferenceFellToTheDefaultGoesBackToItsOwnSize() { + editorScale = 1.5f + attach() + scaler.track(root) + val pooled = text(20f).also(root::addView) + layOut() + root.removeView(pooled) + + editorScale = 1f + scaler.rescale(root) + root.addView(pooled) + layOut() + + assertThat(pooled.textSize).isEqualTo(20f) + } } From fff493436be21c6b4a2c9075006f60712c383eee Mon Sep 17 00:00:00 2001 From: David Schachter Date: Sun, 15 Nov 2026 22:56:08 -0800 Subject: [PATCH 3/6] ADFA-7142: Scale plugin text before each frame, not after each layout Fixes the three MINOR findings and one NITPICK of review 5915984952. - Text added later is caught by a pre-draw listener, not a global-layout one. A scrolling RecyclerView or ListView adds rows and draws them with no layout pass, so AI Core's chat rows created by a scroll stayed at the plugin's own size. A frame in which something was resized is skipped, so its text is never drawn at the old size (the one-frame flash). The walk runs only while the root is shown. - A size the plugin sets again after the scaler recorded one becomes the view's new base, on the next frame and on a preference change. Auto- sized text is skipped: setTextSize does nothing to it, so it would look resized every frame and hold drawing up. - A root tracked again keeps its one watcher. PluginTextScalerTest: 16 tests (6 new); the helper now runs the window's pre-draw step. Under mutation, each new guard's test fails: no pre-draw scaling (7 tests stay at 20 px), plugin sizes ignored (30 px, not 60 and 45), a second watcher (2 listeners), auto-size not skipped (both frames held), and hidden roots walked or frames never skipped. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Vh2DAZWMkFWJh7CY7P4kCH --- .../activities/editor/PluginTextScaler.kt | 64 ++++++--- .../activities/editor/PluginTextScalerTest.kt | 129 ++++++++++++++++-- 2 files changed, 157 insertions(+), 36 deletions(-) diff --git a/app/src/main/java/com/itsaky/androidide/activities/editor/PluginTextScaler.kt b/app/src/main/java/com/itsaky/androidide/activities/editor/PluginTextScaler.kt index 1d69ee396f..94f4cda40a 100644 --- a/app/src/main/java/com/itsaky/androidide/activities/editor/PluginTextScaler.kt +++ b/app/src/main/java/com/itsaky/androidide/activities/editor/PluginTextScaler.kt @@ -5,6 +5,7 @@ import android.view.View import android.view.ViewTreeObserver import android.widget.TextView import com.itsaky.androidide.utils.forEachViewRecursively +import java.util.Collections import java.util.WeakHashMap import kotlin.math.max @@ -15,9 +16,9 @@ import kotlin.math.max * the code, and plugin tabs followed it to under half their size. Views a plugin adds after its * view was built (list rows, chips) are scaled too; without that, one panel mixed two sizes. * - * Sizes are px, scaled from the size each [TextView] had when first seen, so applying the same - * scale twice changes nothing. A plugin that resets a size it already set (say, on rebinding a - * recycled row) gets its own size back until the preference next changes. + * Sizes are px, scaled from the size each [TextView] was given by its plugin, so applying the same + * scale twice changes nothing. A size the plugin sets again later becomes that view's new base. + * Auto-sized text is left alone: [TextView.setTextSize] does nothing to it. */ internal class PluginTextScaler( private val editorFontScale: () -> Float, @@ -28,39 +29,54 @@ internal class PluginTextScaler( ) private val sizings = WeakHashMap() + private val trackedRoots: MutableSet = Collections.newSetFromMap(WeakHashMap()) // Until some view is scaled, none can be stale, so the default skips the walk. private var scaledAny = false fun scale(): Float = max(1f, editorFontScale()) - /** Scales the text under [root] now, and in the views added under it later. */ + /** + * Scales the text under [root] now, and in the views added under it later. A root tracked + * again, as a fragment that reuses its view causes, keeps its one watcher. + */ fun track(root: View) { scaleNewText(root) - root.addOnAttachStateChangeListener(AddedViewWatcher(root)) + if (trackedRoots.add(root)) root.addOnAttachStateChangeListener(AddedViewWatcher(root)) } /** Applies the current [scale] to all the text under [root], once the preference has changed. */ fun rescale(root: View) { val scale = scale() root.forEachViewRecursively { view -> - if (view is TextView) apply(view, sizings.getOrPut(view) { Sizing(view.textSize, 1f) }, scale) + if (view is TextView && !view.isAutoSized()) apply(view, sizingFor(view), scale) } } - // A view detached while the preference changed, a pooled row say, is stale when it returns. - private fun scaleNewText(root: View) { + /** + * Brings every text view under [root] to the current scale: new ones, ones the plugin resized, + * and ones detached while the preference changed (a pooled row, say). Returns whether any changed. + */ + private fun scaleNewText(root: View): Boolean { val scale = scale() - if (scale == 1f && !scaledAny) return + if (scale == 1f && !scaledAny) return false + var changed = false root.forEachViewRecursively { view -> - if (view !is TextView) return@forEachViewRecursively - val sizing = sizings[view] - if (sizing == null) { - apply(view, Sizing(view.textSize, 1f).also { sizings[view] = it }, scale) - } else if (sizing.applied != scale) { + if (view !is TextView || view.isAutoSized()) return@forEachViewRecursively + val sizing = sizingFor(view) + if (sizing.applied != scale) { apply(view, sizing, scale) + changed = true } } + return changed + } + + // Re-recorded when the size is no longer the one this class set: the plugin has set its own. + private fun sizingFor(view: TextView): Sizing { + val known = sizings[view] + if (known != null && view.textSize == known.base * known.applied) return known + return Sizing(view.textSize, 1f).also { sizings[view] = it } } private fun apply( @@ -73,15 +89,20 @@ internal class PluginTextScaler( sizing.applied = scale } + private fun TextView.isAutoSized() = autoSizeTextType != TextView.AUTO_SIZE_TEXT_TYPE_NONE + /** - * Catches views added under [root] at the layout pass that follows. The listener lives on the - * window's observer only while [root] is attached: once detached, [View.getViewTreeObserver] - * returns a different observer, which could no longer remove it. + * Catches views added under [root] before the frame that would draw them. A pre-draw listener, + * not a layout one: a scrolling RecyclerView or ListView adds rows without any layout pass. A + * frame in which something was resized is skipped, so its text is never drawn at the old size; + * the next one is laid out for the new size. The listener lives on the window's observer only + * while [root] is attached: once detached, [View.getViewTreeObserver] returns a different + * observer, which could no longer remove it. */ private inner class AddedViewWatcher( private val root: View, ) : View.OnAttachStateChangeListener, - ViewTreeObserver.OnGlobalLayoutListener { + ViewTreeObserver.OnPreDrawListener { private var observer: ViewTreeObserver? = null init { @@ -89,14 +110,15 @@ internal class PluginTextScaler( } override fun onViewAttachedToWindow(v: View) { - observer = v.viewTreeObserver.also { it.addOnGlobalLayoutListener(this) } + observer = v.viewTreeObserver.also { it.addOnPreDrawListener(this) } } override fun onViewDetachedFromWindow(v: View) { - observer?.takeIf { it.isAlive }?.removeOnGlobalLayoutListener(this) + observer?.takeIf { it.isAlive }?.removeOnPreDrawListener(this) observer = null } - override fun onGlobalLayout() = scaleNewText(root) + // Hidden text is caught on the frame that first shows it. + override fun onPreDraw(): Boolean = !root.isShown || !scaleNewText(root) } } diff --git a/app/src/test/java/com/itsaky/androidide/activities/editor/PluginTextScalerTest.kt b/app/src/test/java/com/itsaky/androidide/activities/editor/PluginTextScalerTest.kt index 78cb768c7a..c6396cab88 100644 --- a/app/src/test/java/com/itsaky/androidide/activities/editor/PluginTextScalerTest.kt +++ b/app/src/test/java/com/itsaky/androidide/activities/editor/PluginTextScalerTest.kt @@ -3,7 +3,9 @@ package com.itsaky.androidide.activities.editor import android.app.Activity import android.app.Application import android.util.TypedValue +import android.view.View import android.view.ViewGroup +import android.view.ViewTreeObserver import android.widget.LinearLayout import android.widget.TextView import com.google.common.truth.Truth.assertThat @@ -36,10 +38,20 @@ class PluginTextScalerTest { private fun attach() = activity.setContentView(root) - // The layout pass that follows adding a view, as the window runs it. - private fun layOut() = + // The window's step before it draws a frame. Returns whether that frame was skipped. + private fun drawFrame() = activity.window.decorView.viewTreeObserver - .dispatchOnGlobalLayout() + .dispatchOnPreDraw() + + // How many pre-draw listeners the window has, read where ViewTreeObserver keeps them. + private fun preDrawListeners(): Int { + val field = ViewTreeObserver::class.java.getDeclaredField("mOnPreDrawListeners").apply { isAccessible = true } + val listeners = field.get(activity.window.decorView.viewTreeObserver) ?: return 0 + return listeners.javaClass + .getDeclaredMethod("size") + .apply { isAccessible = true } + .invoke(listeners) as Int + } @Test fun textIsScaledWhenTheViewIsTracked() { @@ -59,7 +71,7 @@ class PluginTextScalerTest { attach() val row = text(20f).also(root::addView) - layOut() + drawFrame() assertThat(row.textSize).isEqualTo(30f) } @@ -71,20 +83,20 @@ class PluginTextScalerTest { scaler.track(root) val row = text(20f).also(root::addView) - layOut() + drawFrame() assertThat(row.textSize).isEqualTo(30f) } @Test - fun laterLayoutsDoNotScaleTextAgain() { + fun laterFramesDoNotScaleTextAgain() { editorScale = 1.5f scaler.track(root) attach() val row = text(20f).also(root::addView) - layOut() - layOut() + assertThat(drawFrame()).isTrue() + assertThat(drawFrame()).isFalse() assertThat(row.textSize).isEqualTo(30f) } @@ -98,7 +110,7 @@ class PluginTextScalerTest { attach() val row = text(28f).also(root::addView) - layOut() + drawFrame() assertThat(label.textSize).isEqualTo(28f) assertThat(row.textSize).isEqualTo(28f) @@ -124,7 +136,7 @@ class PluginTextScalerTest { scaler.track(root) attach() val row = text(20f).also(root::addView) - layOut() + drawFrame() assertThat(row.textSize).isEqualTo(20f) editorScale = 2f @@ -141,7 +153,7 @@ class PluginTextScalerTest { (root.parent as ViewGroup).removeView(root) val row = text(20f).also(root::addView) - layOut() + drawFrame() assertThat(row.textSize).isEqualTo(20f) } @@ -152,7 +164,7 @@ class PluginTextScalerTest { attach() scaler.track(root) val pooled = text(20f).also(root::addView) - layOut() + drawFrame() assertThat(pooled.textSize).isEqualTo(30f) root.removeView(pooled) @@ -160,7 +172,7 @@ class PluginTextScalerTest { scaler.rescale(root) root.addView(pooled) val fresh = text(20f).also(root::addView) - layOut() + drawFrame() assertThat(pooled.textSize).isEqualTo(40f) assertThat(fresh.textSize).isEqualTo(40f) @@ -172,14 +184,101 @@ class PluginTextScalerTest { attach() scaler.track(root) val pooled = text(20f).also(root::addView) - layOut() + drawFrame() root.removeView(pooled) editorScale = 1f scaler.rescale(root) root.addView(pooled) - layOut() + drawFrame() assertThat(pooled.textSize).isEqualTo(20f) } + + @Test + fun textAddedWithNoLayoutPassIsScaledBeforeItIsDrawn() { + // A scrolling RecyclerView or ListView adds rows and draws them with no layout pass in + // between, so only the step before drawing sees them. + editorScale = 1.5f + scaler.track(root) + attach() + val row = text(20f).also(root::addView) + + val skipped = drawFrame() + + assertThat(row.textSize).isEqualTo(30f) + // The frame that found it is skipped, so the row is never drawn at its old size. + assertThat(skipped).isTrue() + } + + @Test + fun hiddenTextIsScaledOnTheFrameThatShowsIt() { + editorScale = 1.5f + scaler.track(root) + attach() + root.visibility = View.GONE + val row = text(20f).also(root::addView) + + assertThat(drawFrame()).isFalse() + assertThat(row.textSize).isEqualTo(20f) + + root.visibility = View.VISIBLE + drawFrame() + assertThat(row.textSize).isEqualTo(30f) + } + + @Test + fun aSizeThePluginSetsLaterIsScaledFromThere() { + // Say a recycled row whose binding sets its text size again. + editorScale = 2f + scaler.track(root) + attach() + val row = text(20f).also(root::addView) + drawFrame() + assertThat(row.textSize).isEqualTo(40f) + + row.setTextSize(TypedValue.COMPLEX_UNIT_PX, 30f) + drawFrame() + + assertThat(row.textSize).isEqualTo(60f) + } + + @Test + fun aPreferenceChangeKeepsASizeThePluginSetLater() { + editorScale = 2f + val row = text(20f).also(root::addView) + scaler.track(root) + row.setTextSize(TypedValue.COMPLEX_UNIT_PX, 30f) + + editorScale = 1.5f + scaler.rescale(root) + + assertThat(row.textSize).isEqualTo(45f) + } + + @Test + fun autoSizedTextIsLeftAloneAndNeverHoldsUpAFrame() { + // setTextSize does nothing to auto-sized text, so it would look resized on every frame. + editorScale = 2f + val fitted = text(20f).apply { setAutoSizeTextTypeWithDefaults(TextView.AUTO_SIZE_TEXT_TYPE_UNIFORM) } + root.addView(fitted) + scaler.track(root) + attach() + val size = fitted.textSize + + assertThat(listOf(drawFrame(), drawFrame())).containsExactly(false, false) + assertThat(fitted.textSize).isEqualTo(size) + } + + @Test + fun aRootTrackedAgainKeepsOneWatcher() { + // A fragment that hands back the same view each time its view is recreated. + attach() + val before = preDrawListeners() + + scaler.track(root) + scaler.track(root) + + assertThat(preDrawListeners()).isEqualTo(before + 1) + } } From 1b96e26df6bad71fb2b1387d1cf7f6c5727080fe Mon Sep 17 00:00:00 2001 From: David Schachter Date: Sun, 15 Nov 2026 23:07:41 -0800 Subject: [PATCH 4/6] ADFA-7142: Never skip two frames in a row From the independent review of fff493436 (F01, F04b). A plugin that sets its own text size on every layout, such as a fit-to-width routine, made every frame find a size to rescale, so every frame was skipped and the editor window could stop drawing. The watcher now draws the frame after one it skipped; such a plugin and the scaler then resize the text in turn, but the window keeps drawing. No plugin in addons does this. New tests: a plugin resizing its text every frame never gets two frames skipped in a row (without the guard, it does), and a root taken off the window and put back still catches text added afterward (without re-registering on attach, the row stays at 20 px). 18 tests pass. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Vh2DAZWMkFWJh7CY7P4kCH --- .../activities/editor/PluginTextScaler.kt | 15 ++++++--- .../activities/editor/PluginTextScalerTest.kt | 33 +++++++++++++++++++ 2 files changed, 44 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/com/itsaky/androidide/activities/editor/PluginTextScaler.kt b/app/src/main/java/com/itsaky/androidide/activities/editor/PluginTextScaler.kt index 94f4cda40a..959a204ef2 100644 --- a/app/src/main/java/com/itsaky/androidide/activities/editor/PluginTextScaler.kt +++ b/app/src/main/java/com/itsaky/androidide/activities/editor/PluginTextScaler.kt @@ -95,15 +95,18 @@ internal class PluginTextScaler( * Catches views added under [root] before the frame that would draw them. A pre-draw listener, * not a layout one: a scrolling RecyclerView or ListView adds rows without any layout pass. A * frame in which something was resized is skipped, so its text is never drawn at the old size; - * the next one is laid out for the new size. The listener lives on the window's observer only - * while [root] is attached: once detached, [View.getViewTreeObserver] returns a different - * observer, which could no longer remove it. + * the next one is laid out for the new size. Never two in a row, though: a plugin that sets its + * own size on every layout would otherwise have every frame skipped, and the window would stop + * drawing. The listener lives on the window's observer only while [root] is attached: once + * detached, [View.getViewTreeObserver] returns a different observer, which could no longer + * remove it. */ private inner class AddedViewWatcher( private val root: View, ) : View.OnAttachStateChangeListener, ViewTreeObserver.OnPreDrawListener { private var observer: ViewTreeObserver? = null + private var skippedLast = false init { if (root.isAttachedToWindow) onViewAttachedToWindow(root) @@ -119,6 +122,10 @@ internal class PluginTextScaler( } // Hidden text is caught on the frame that first shows it. - override fun onPreDraw(): Boolean = !root.isShown || !scaleNewText(root) + override fun onPreDraw(): Boolean { + val skip = root.isShown && scaleNewText(root) && !skippedLast + skippedLast = skip + return !skip + } } } diff --git a/app/src/test/java/com/itsaky/androidide/activities/editor/PluginTextScalerTest.kt b/app/src/test/java/com/itsaky/androidide/activities/editor/PluginTextScalerTest.kt index c6396cab88..9e9d284773 100644 --- a/app/src/test/java/com/itsaky/androidide/activities/editor/PluginTextScalerTest.kt +++ b/app/src/test/java/com/itsaky/androidide/activities/editor/PluginTextScalerTest.kt @@ -281,4 +281,37 @@ class PluginTextScalerTest { assertThat(preDrawListeners()).isEqualTo(before + 1) } + + @Test + fun aPluginThatResizesItsTextEveryFrameNeverStopsTheWindowDrawing() { + // Say a fit-to-width routine that sets the size again on every layout. + editorScale = 2f + scaler.track(root) + attach() + val fitted = text(25f).also(root::addView) + + val skipped = + (1..6).map { + fitted.setTextSize(TypedValue.COMPLEX_UNIT_PX, 25f) + drawFrame() + } + + assertThat(skipped.zipWithNext().none { (first, second) -> first && second }).isTrue() + assertThat(skipped).contains(true) + } + + @Test + fun aRootAttachedAgainCatchesTextAddedAfterward() { + // A tab whose view is taken off the window and put back, as a pager does. + editorScale = 1.5f + scaler.track(root) + attach() + (root.parent as ViewGroup).removeView(root) + attach() + + val row = text(20f).also(root::addView) + drawFrame() + + assertThat(row.textSize).isEqualTo(30f) + } } From 3a39c9c8a226a351157e77705929fd314a6f52f4 Mon Sep 17 00:00:00 2001 From: David Schachter Date: Mon, 16 Nov 2026 10:21:11 -0800 Subject: [PATCH 5/6] ADFA-7142: Decide frame skips once per window, not once per root From CodeRabbit's review of 1b96e26df. Each plugin root had its own pre-draw listener and its own record of the frame it last skipped, but a window skips a frame if any of its listeners asks. Two plugin tabs that each resize their text on every frame, starting a frame apart, would take turns skipping, so every frame was skipped and the editor window stopped drawing: the failure 1b96e26df set out to prevent. The scaler now keeps one listener per window. It walks every tracked root in that window and makes one skip decision per frame. Plugin dialogs and bottom sheets (AI Core's approval dialog, Client Time Tracker's sheets) are windows of their own, so each window decides for itself. A window's listener is removed when its last tracked root leaves it. No plugin in addons resizes its text every frame. New tests: two roots resizing a frame apart still let every other frame draw (fails with the per-root listeners of 1b96e26df); closing one tab leaves the other watched (fails if the listener goes with the first root); a plugin dialog's skips don't decide the editor window's (fails with one skip flag for the whole scaler); and detaching the last root removes the listener (fails if it is never removed). 21 tests pass. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Vh2DAZWMkFWJh7CY7P4kCH --- .../activities/editor/PluginTextScaler.kt | 78 +++++++++++------- .../activities/editor/PluginTextScalerTest.kt | 80 ++++++++++++++++++- 2 files changed, 127 insertions(+), 31 deletions(-) diff --git a/app/src/main/java/com/itsaky/androidide/activities/editor/PluginTextScaler.kt b/app/src/main/java/com/itsaky/androidide/activities/editor/PluginTextScaler.kt index 959a204ef2..6ec6bcff41 100644 --- a/app/src/main/java/com/itsaky/androidide/activities/editor/PluginTextScaler.kt +++ b/app/src/main/java/com/itsaky/androidide/activities/editor/PluginTextScaler.kt @@ -31,6 +31,9 @@ internal class PluginTextScaler( private val sizings = WeakHashMap() private val trackedRoots: MutableSet = Collections.newSetFromMap(WeakHashMap()) + // Keyed by the window's observer; an entry lasts while some tracked root is in that window. + private val windowWatchers = HashMap() + // Until some view is scaled, none can be stale, so the default skips the walk. private var scaledAny = false @@ -38,11 +41,14 @@ internal class PluginTextScaler( /** * Scales the text under [root] now, and in the views added under it later. A root tracked - * again, as a fragment that reuses its view causes, keeps its one watcher. + * again, as a fragment that reuses its view causes, is still watched once. */ fun track(root: View) { scaleNewText(root) - if (trackedRoots.add(root)) root.addOnAttachStateChangeListener(AddedViewWatcher(root)) + if (trackedRoots.add(root)) { + root.addOnAttachStateChangeListener(attachListener) + if (root.isAttachedToWindow) startWatching(root) + } } /** Applies the current [scale] to all the text under [root], once the preference has changed. */ @@ -91,39 +97,57 @@ internal class PluginTextScaler( private fun TextView.isAutoSized() = autoSizeTextType != TextView.AUTO_SIZE_TEXT_TYPE_NONE - /** - * Catches views added under [root] before the frame that would draw them. A pre-draw listener, - * not a layout one: a scrolling RecyclerView or ListView adds rows without any layout pass. A - * frame in which something was resized is skipped, so its text is never drawn at the old size; - * the next one is laid out for the new size. Never two in a row, though: a plugin that sets its - * own size on every layout would otherwise have every frame skipped, and the window would stop - * drawing. The listener lives on the window's observer only while [root] is attached: once - * detached, [View.getViewTreeObserver] returns a different observer, which could no longer - * remove it. - */ - private inner class AddedViewWatcher( - private val root: View, - ) : View.OnAttachStateChangeListener, - ViewTreeObserver.OnPreDrawListener { - private var observer: ViewTreeObserver? = null - private var skippedLast = false + private val attachListener = + object : View.OnAttachStateChangeListener { + override fun onViewAttachedToWindow(v: View) = startWatching(v) - init { - if (root.isAttachedToWindow) onViewAttachedToWindow(root) + override fun onViewDetachedFromWindow(v: View) = stopWatching(v) } - override fun onViewAttachedToWindow(v: View) { - observer = v.viewTreeObserver.also { it.addOnPreDrawListener(this) } - } + private fun startWatching(root: View) { + val observer = root.viewTreeObserver + val watcher = + windowWatchers.getOrPut(observer) { + WindowWatcher().also { observer.addOnPreDrawListener(it) } + } + watcher.roots.add(root) + } - override fun onViewDetachedFromWindow(v: View) { - observer?.takeIf { it.isAlive }?.removeOnPreDrawListener(this) - observer = null + // Found by its roots: once a view has left its window, root.viewTreeObserver is a different + // observer, which could not remove the listener. + private fun stopWatching(root: View) { + val entry = windowWatchers.entries.firstOrNull { root in it.value.roots } ?: return + val (observer, watcher) = entry + watcher.roots.remove(root) + if (watcher.roots.isEmpty()) { + if (observer.isAlive) observer.removeOnPreDrawListener(watcher) + windowWatchers.remove(observer) } + } + + /** + * Catches views added under the tracked roots in one window before the frame that would draw + * them. A pre-draw listener, not a layout one: a scrolling RecyclerView or ListView adds rows + * without any layout pass. A frame in which something was resized is skipped, so its text is + * never drawn at the old size; the next one is laid out for the new size. Never two in a row, + * though: a plugin that sets its own size on every layout would otherwise have every frame + * skipped, and the window would stop drawing. + * + * One per window, not one per root: the window skips a frame if any of its listeners asks it + * to, so two roots each skipping every other frame, a frame apart, would skip them all. Plugin + * dialogs and bottom sheets are windows of their own, so each window decides for itself. + */ + private inner class WindowWatcher : ViewTreeObserver.OnPreDrawListener { + val roots = LinkedHashSet() + private var skippedLast = false // Hidden text is caught on the frame that first shows it. override fun onPreDraw(): Boolean { - val skip = root.isShown && scaleNewText(root) && !skippedLast + var changed = false + for (root in roots) { + if (root.isShown && scaleNewText(root)) changed = true + } + val skip = changed && !skippedLast skippedLast = skip return !skip } diff --git a/app/src/test/java/com/itsaky/androidide/activities/editor/PluginTextScalerTest.kt b/app/src/test/java/com/itsaky/androidide/activities/editor/PluginTextScalerTest.kt index 9e9d284773..0fa298028a 100644 --- a/app/src/test/java/com/itsaky/androidide/activities/editor/PluginTextScalerTest.kt +++ b/app/src/test/java/com/itsaky/androidide/activities/editor/PluginTextScalerTest.kt @@ -2,6 +2,8 @@ package com.itsaky.androidide.activities.editor import android.app.Activity import android.app.Application +import android.app.Dialog +import android.os.Looper import android.util.TypedValue import android.view.View import android.view.ViewGroup @@ -14,6 +16,7 @@ import org.junit.Test import org.junit.runner.RunWith import org.robolectric.Robolectric import org.robolectric.RobolectricTestRunner +import org.robolectric.Shadows.shadowOf import org.robolectric.annotation.Config /** @@ -39,9 +42,7 @@ class PluginTextScalerTest { private fun attach() = activity.setContentView(root) // The window's step before it draws a frame. Returns whether that frame was skipped. - private fun drawFrame() = - activity.window.decorView.viewTreeObserver - .dispatchOnPreDraw() + private fun drawFrame(window: View = activity.window.decorView) = window.viewTreeObserver.dispatchOnPreDraw() // How many pre-draw listeners the window has, read where ViewTreeObserver keeps them. private fun preDrawListeners(): Int { @@ -148,14 +149,33 @@ class PluginTextScalerTest { @Test fun aDetachedViewStopsWatchingTheWindow() { editorScale = 1.5f - scaler.track(root) attach() + val before = preDrawListeners() + scaler.track(root) (root.parent as ViewGroup).removeView(root) val row = text(20f).also(root::addView) drawFrame() assertThat(row.textSize).isEqualTo(20f) + assertThat(preDrawListeners()).isEqualTo(before) + } + + @Test + fun aRootLeavingTheWindowLeavesTheOtherRootsWatched() { + // Two plugin tabs: one is closed, the other stays. + editorScale = 1.5f + val closed = LinearLayout(activity).also(root::addView) + val open = LinearLayout(activity).also(root::addView) + scaler.track(closed) + scaler.track(open) + attach() + + root.removeView(closed) + val row = text(20f).also(open::addView) + drawFrame() + + assertThat(row.textSize).isEqualTo(30f) } @Test @@ -314,4 +334,56 @@ class PluginTextScalerTest { assertThat(row.textSize).isEqualTo(30f) } + + @Test + fun twoPluginsResizingTheirTextEveryFrameStillLetEveryOtherFrameDraw() { + // The window skips a frame if any listener asks. Two plugin tabs, each rescaled on every + // frame and starting a frame apart, must not take turns skipping every one of them. + editorScale = 2f + val first = LinearLayout(activity).also(root::addView) + val second = LinearLayout(activity).also(root::addView) + val fittedFirst = text(25f).also(first::addView) + val fittedSecond = text(25f).also(second::addView) + scaler.track(first) + scaler.track(second) + attach() + + val skipped = + (1..6).map { frame -> + fittedFirst.setTextSize(TypedValue.COMPLEX_UNIT_PX, 25f) + if (frame > 1) fittedSecond.setTextSize(TypedValue.COMPLEX_UNIT_PX, 25f) + drawFrame() + } + + assertThat(skipped.zipWithNext().none { (first, second) -> first && second }).isTrue() + assertThat(skipped).contains(true) + } + + @Test + fun aPluginDialogSkipsFramesApartFromTheEditorWindow() { + // A plugin's dialog or bottom sheet is a window of its own, drawn in turn with the editor's. + // What one window skipped must not decide whether the other may skip. + editorScale = 2f + val sheet = LinearLayout(activity) + val dialog = Dialog(activity).apply { setContentView(sheet) } + dialog.show() + attach() + shadowOf(Looper.getMainLooper()).idle() + assertThat(sheet.isAttachedToWindow).isTrue() + assertThat(sheet.viewTreeObserver).isNotSameInstanceAs(root.viewTreeObserver) + val inEditor = text(25f).also(root::addView) + val inSheet = text(25f).also(sheet::addView) + scaler.track(root) + scaler.track(sheet) + + val editorSkipped = + (1..6).map { + inEditor.setTextSize(TypedValue.COMPLEX_UNIT_PX, 25f) + inSheet.setTextSize(TypedValue.COMPLEX_UNIT_PX, 25f) + drawFrame().also { drawFrame(dialog.window!!.decorView) } + } + + assertThat(editorSkipped.zipWithNext().none { (first, second) -> first && second }).isTrue() + assertThat(editorSkipped).contains(true) + } } From dec31e390eb316514dd6f69db80a643a9423d67a Mon Sep 17 00:00:00 2001 From: David Schachter Date: Mon, 16 Nov 2026 11:32:18 -0800 Subject: [PATCH 6/6] ADFA-7142: Keep the scale in the scaler; walk nested and scale-1 views less From the three NITPICKs in the PR review. The scaler now holds the plugin text scale. The activity passes it the editor's font scale on a font-size event and in onStart, which covers a change made while Preferences was in front. The scaler rescales only when the clamped scale changes. That removes the activity's lastAppliedPluginTextScale, its collectPluginFragments walk, and the preference read on every frame. On a change, the scaler also rescales every text view it has seen that is off the window now, such as a pooled list row. No view is left at an old scale, so at scale 1 no frame walks the plugin views. Before, once anything had been scaled, every later frame walked them for the rest of the session. A plugin fragment nested inside another plugin fragment's view is tracked as well. The per-frame walk now skips it, since the outer root's walk covers it. Registration still tracks both, because a nested plugin dialog lives in a window of its own. New tests: a nested plugin view is read once per frame, not twice; frames read no plugin view once the scale is back to 1; and a change that stays at scale 1 touches nothing. Each fails with its guard removed, as does the existing fell-to-1 reinsertion test without the sweep of off-window views. 24 tests pass. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Vh2DAZWMkFWJh7CY7P4kCH --- .../editor/EditorHandlerActivity.kt | 29 +---- .../activities/editor/PluginTextScaler.kt | 52 +++++--- .../activities/editor/PluginTextScalerTest.kt | 121 +++++++++++++----- 3 files changed, 127 insertions(+), 75 deletions(-) diff --git a/app/src/main/java/com/itsaky/androidide/activities/editor/EditorHandlerActivity.kt b/app/src/main/java/com/itsaky/androidide/activities/editor/EditorHandlerActivity.kt index a564bb959f..f8c712ca84 100644 --- a/app/src/main/java/com/itsaky/androidide/activities/editor/EditorHandlerActivity.kt +++ b/app/src/main/java/com/itsaky/androidide/activities/editor/EditorHandlerActivity.kt @@ -179,8 +179,7 @@ open class EditorHandlerActivity : private val pluginTabIndices = mutableMapOf() private val tabIndexToPluginId = mutableMapOf() - private val pluginTextScaler = PluginTextScaler { EditorPreferences.editorFontScale } - private var lastAppliedPluginTextScale = pluginTextScaler.scale() + private val pluginTextScaler = PluginTextScaler(EditorPreferences.editorFontScale) private val pluginFontScalingListener = object : FragmentManager.FragmentLifecycleCallbacks() { @@ -2017,35 +2016,13 @@ open class EditorHandlerActivity : } } + // Also run from onStart: the event is missed while another activity, such as Preferences, is in front. private fun syncPluginUiFontSize() { - val scale = pluginTextScaler.scale() - if (scale == lastAppliedPluginTextScale) { - return - } - lastAppliedPluginTextScale = scale - - val pluginFragments = mutableListOf() - collectPluginFragments(supportFragmentManager, pluginFragments) - pluginFragments.forEach { fragment -> - fragment.view?.let(pluginTextScaler::rescale) - } + pluginTextScaler.onEditorFontScaleChanged(EditorPreferences.editorFontScale) } private fun isPluginFragment(fragment: Fragment): Boolean = fragment.javaClass.classLoader !== javaClass.classLoader - private fun collectPluginFragments( - manager: FragmentManager, - into: MutableList, - ) { - manager.fragments.forEach { fragment -> - if (isPluginFragment(fragment)) { - into.add(fragment) - } else { - collectPluginFragments(fragment.childFragmentManager, into) - } - } - } - /** * Shows [dialog] above any floating windows, tracking it when it was actually raised. * diff --git a/app/src/main/java/com/itsaky/androidide/activities/editor/PluginTextScaler.kt b/app/src/main/java/com/itsaky/androidide/activities/editor/PluginTextScaler.kt index 6ec6bcff41..557d9275ee 100644 --- a/app/src/main/java/com/itsaky/androidide/activities/editor/PluginTextScaler.kt +++ b/app/src/main/java/com/itsaky/androidide/activities/editor/PluginTextScaler.kt @@ -21,7 +21,7 @@ import kotlin.math.max * Auto-sized text is left alone: [TextView.setTextSize] does nothing to it. */ internal class PluginTextScaler( - private val editorFontScale: () -> Float, + editorFontScale: Float, ) { private class Sizing( val base: Float, @@ -34,10 +34,7 @@ internal class PluginTextScaler( // Keyed by the window's observer; an entry lasts while some tracked root is in that window. private val windowWatchers = HashMap() - // Until some view is scaled, none can be stale, so the default skips the walk. - private var scaledAny = false - - fun scale(): Float = max(1f, editorFontScale()) + private var scale = max(1f, editorFontScale) /** * Scales the text under [root] now, and in the views added under it later. A root tracked @@ -51,21 +48,35 @@ internal class PluginTextScaler( } } - /** Applies the current [scale] to all the text under [root], once the preference has changed. */ - fun rescale(root: View) { - val scale = scale() - root.forEachViewRecursively { view -> - if (view is TextView && !view.isAutoSized()) apply(view, sizingFor(view), scale) + /** + * Follows a change to the editor's font scale. Unless the plugin text scale stays the same (any + * size below the editor's default leaves it at 1), the text under every tracked root is rescaled, + * and so is every text view seen before that is now off the window, such as a pooled list row. + * After that no text is left at an old scale, so at scale 1 no frame needs a walk. + */ + fun onEditorFontScaleChanged(editorFontScale: Float) { + val newScale = max(1f, editorFontScale) + if (newScale == scale) return + scale = newScale + for (root in trackedRoots.toList()) { + root.forEachViewRecursively { view -> + if (view is TextView && !view.isAutoSized()) apply(view, sizingFor(view), scale) + } + } + for (view in sizings.keys.toList()) { + if (view.isAutoSized()) continue + val sizing = sizingFor(view) + if (sizing.applied != scale) apply(view, sizing, scale) } } /** - * Brings every text view under [root] to the current scale: new ones, ones the plugin resized, - * and ones detached while the preference changed (a pooled row, say). Returns whether any changed. + * Brings every text view under [root] to the current scale: new ones, and ones the plugin + * resized. At scale 1 there is nothing to do: plugin text is drawn at its own size, and a scale + * change leaves no view at an old one. Returns whether any changed. */ private fun scaleNewText(root: View): Boolean { - val scale = scale() - if (scale == 1f && !scaledAny) return false + if (scale == 1f) return false var changed = false root.forEachViewRecursively { view -> if (view !is TextView || view.isAutoSized()) return@forEachViewRecursively @@ -90,7 +101,6 @@ internal class PluginTextScaler( sizing: Sizing, scale: Float, ) { - if (scale != 1f) scaledAny = true view.setTextSize(TypedValue.COMPLEX_UNIT_PX, sizing.base * scale) sizing.applied = scale } @@ -145,11 +155,21 @@ internal class PluginTextScaler( override fun onPreDraw(): Boolean { var changed = false for (root in roots) { - if (root.isShown && scaleNewText(root)) changed = true + if (root.isShown && !isInsideAnotherRoot(root) && scaleNewText(root)) changed = true } val skip = changed && !skippedLast skippedLast = skip return !skip } + + // A plugin fragment nested in another's view is tracked too; the outer walk covers it. + private fun isInsideAnotherRoot(root: View): Boolean { + var parent = root.parent + while (parent is View) { + if (parent in roots) return true + parent = parent.parent + } + return false + } } } diff --git a/app/src/test/java/com/itsaky/androidide/activities/editor/PluginTextScalerTest.kt b/app/src/test/java/com/itsaky/androidide/activities/editor/PluginTextScalerTest.kt index 0fa298028a..04a11c072a 100644 --- a/app/src/test/java/com/itsaky/androidide/activities/editor/PluginTextScalerTest.kt +++ b/app/src/test/java/com/itsaky/androidide/activities/editor/PluginTextScalerTest.kt @@ -3,6 +3,7 @@ package com.itsaky.androidide.activities.editor import android.app.Activity import android.app.Application import android.app.Dialog +import android.content.Context import android.os.Looper import android.util.TypedValue import android.view.View @@ -26,8 +27,7 @@ import org.robolectric.annotation.Config @RunWith(RobolectricTestRunner::class) @Config(application = Application::class, sdk = [33]) class PluginTextScalerTest { - private var editorScale = 1f - private val scaler = PluginTextScaler { editorScale } + private val scaler = PluginTextScaler(1f) private lateinit var activity: Activity private lateinit var root: LinearLayout @@ -39,6 +39,20 @@ class PluginTextScalerTest { private fun text(px: Float) = TextView(activity).apply { setTextSize(TypedValue.COMPLEX_UNIT_PX, px) } + // Counts how often its size is read: each walk over it reads the size once. + private class CountingTextView( + context: Context, + ) : TextView(context) { + var sizeReads = 0 + + override fun getTextSize(): Float { + sizeReads++ + return super.getTextSize() + } + } + + private fun countingText(px: Float) = CountingTextView(activity).apply { setTextSize(TypedValue.COMPLEX_UNIT_PX, px) } + private fun attach() = activity.setContentView(root) // The window's step before it draws a frame. Returns whether that frame was skipped. @@ -56,7 +70,7 @@ class PluginTextScalerTest { @Test fun textIsScaledWhenTheViewIsTracked() { - editorScale = 1.5f + scaler.onEditorFontScaleChanged(1.5f) val label = text(20f).also(root::addView) scaler.track(root) @@ -66,7 +80,7 @@ class PluginTextScalerTest { @Test fun textAddedAfterTheViewWasBuiltIsScaledToo() { - editorScale = 1.5f + scaler.onEditorFontScaleChanged(1.5f) // As a fragment's view: tracked when created, attached to the window after. scaler.track(root) attach() @@ -79,7 +93,7 @@ class PluginTextScalerTest { @Test fun aViewAlreadyAttachedWhenTrackedStillCatchesLaterText() { - editorScale = 1.5f + scaler.onEditorFontScaleChanged(1.5f) attach() scaler.track(root) @@ -91,7 +105,7 @@ class PluginTextScalerTest { @Test fun laterFramesDoNotScaleTextAgain() { - editorScale = 1.5f + scaler.onEditorFontScaleChanged(1.5f) scaler.track(root) attach() val row = text(20f).also(root::addView) @@ -105,7 +119,7 @@ class PluginTextScalerTest { @Test fun theEditorsSmallestSizeLeavesPluginTextAtItsOwnSize() { // The phone ADFA-7142 was found on: the code pinched down to 6.38sp, against a default of 14. - editorScale = 6.3809667f / 14f + scaler.onEditorFontScaleChanged(6.3809667f / 14f) val label = text(28f).also(root::addView) scaler.track(root) attach() @@ -119,16 +133,14 @@ class PluginTextScalerTest { @Test fun aPreferenceChangeScalesFromEachViewsOwnSize() { - editorScale = 2f + scaler.onEditorFontScaleChanged(2f) val label = text(20f).also(root::addView) scaler.track(root) - editorScale = 1.5f - scaler.rescale(root) + scaler.onEditorFontScaleChanged(1.5f) assertThat(label.textSize).isEqualTo(30f) - editorScale = 0.5f - scaler.rescale(root) + scaler.onEditorFontScaleChanged(0.5f) assertThat(label.textSize).isEqualTo(20f) } @@ -140,15 +152,14 @@ class PluginTextScalerTest { drawFrame() assertThat(row.textSize).isEqualTo(20f) - editorScale = 2f - scaler.rescale(root) + scaler.onEditorFontScaleChanged(2f) assertThat(row.textSize).isEqualTo(40f) } @Test fun aDetachedViewStopsWatchingTheWindow() { - editorScale = 1.5f + scaler.onEditorFontScaleChanged(1.5f) attach() val before = preDrawListeners() scaler.track(root) @@ -164,7 +175,7 @@ class PluginTextScalerTest { @Test fun aRootLeavingTheWindowLeavesTheOtherRootsWatched() { // Two plugin tabs: one is closed, the other stays. - editorScale = 1.5f + scaler.onEditorFontScaleChanged(1.5f) val closed = LinearLayout(activity).also(root::addView) val open = LinearLayout(activity).also(root::addView) scaler.track(closed) @@ -180,7 +191,7 @@ class PluginTextScalerTest { @Test fun aViewReinsertedAfterThePreferenceChangedMatchesAFreshOne() { - editorScale = 1.5f + scaler.onEditorFontScaleChanged(1.5f) attach() scaler.track(root) val pooled = text(20f).also(root::addView) @@ -188,8 +199,7 @@ class PluginTextScalerTest { assertThat(pooled.textSize).isEqualTo(30f) root.removeView(pooled) - editorScale = 2f - scaler.rescale(root) + scaler.onEditorFontScaleChanged(2f) root.addView(pooled) val fresh = text(20f).also(root::addView) drawFrame() @@ -200,15 +210,14 @@ class PluginTextScalerTest { @Test fun aViewReinsertedAfterThePreferenceFellToTheDefaultGoesBackToItsOwnSize() { - editorScale = 1.5f + scaler.onEditorFontScaleChanged(1.5f) attach() scaler.track(root) val pooled = text(20f).also(root::addView) drawFrame() root.removeView(pooled) - editorScale = 1f - scaler.rescale(root) + scaler.onEditorFontScaleChanged(1f) root.addView(pooled) drawFrame() @@ -219,7 +228,7 @@ class PluginTextScalerTest { fun textAddedWithNoLayoutPassIsScaledBeforeItIsDrawn() { // A scrolling RecyclerView or ListView adds rows and draws them with no layout pass in // between, so only the step before drawing sees them. - editorScale = 1.5f + scaler.onEditorFontScaleChanged(1.5f) scaler.track(root) attach() val row = text(20f).also(root::addView) @@ -233,7 +242,7 @@ class PluginTextScalerTest { @Test fun hiddenTextIsScaledOnTheFrameThatShowsIt() { - editorScale = 1.5f + scaler.onEditorFontScaleChanged(1.5f) scaler.track(root) attach() root.visibility = View.GONE @@ -250,7 +259,7 @@ class PluginTextScalerTest { @Test fun aSizeThePluginSetsLaterIsScaledFromThere() { // Say a recycled row whose binding sets its text size again. - editorScale = 2f + scaler.onEditorFontScaleChanged(2f) scaler.track(root) attach() val row = text(20f).also(root::addView) @@ -265,13 +274,12 @@ class PluginTextScalerTest { @Test fun aPreferenceChangeKeepsASizeThePluginSetLater() { - editorScale = 2f + scaler.onEditorFontScaleChanged(2f) val row = text(20f).also(root::addView) scaler.track(root) row.setTextSize(TypedValue.COMPLEX_UNIT_PX, 30f) - editorScale = 1.5f - scaler.rescale(root) + scaler.onEditorFontScaleChanged(1.5f) assertThat(row.textSize).isEqualTo(45f) } @@ -279,7 +287,7 @@ class PluginTextScalerTest { @Test fun autoSizedTextIsLeftAloneAndNeverHoldsUpAFrame() { // setTextSize does nothing to auto-sized text, so it would look resized on every frame. - editorScale = 2f + scaler.onEditorFontScaleChanged(2f) val fitted = text(20f).apply { setAutoSizeTextTypeWithDefaults(TextView.AUTO_SIZE_TEXT_TYPE_UNIFORM) } root.addView(fitted) scaler.track(root) @@ -305,7 +313,7 @@ class PluginTextScalerTest { @Test fun aPluginThatResizesItsTextEveryFrameNeverStopsTheWindowDrawing() { // Say a fit-to-width routine that sets the size again on every layout. - editorScale = 2f + scaler.onEditorFontScaleChanged(2f) scaler.track(root) attach() val fitted = text(25f).also(root::addView) @@ -323,7 +331,7 @@ class PluginTextScalerTest { @Test fun aRootAttachedAgainCatchesTextAddedAfterward() { // A tab whose view is taken off the window and put back, as a pager does. - editorScale = 1.5f + scaler.onEditorFontScaleChanged(1.5f) scaler.track(root) attach() (root.parent as ViewGroup).removeView(root) @@ -339,7 +347,7 @@ class PluginTextScalerTest { fun twoPluginsResizingTheirTextEveryFrameStillLetEveryOtherFrameDraw() { // The window skips a frame if any listener asks. Two plugin tabs, each rescaled on every // frame and starting a frame apart, must not take turns skipping every one of them. - editorScale = 2f + scaler.onEditorFontScaleChanged(2f) val first = LinearLayout(activity).also(root::addView) val second = LinearLayout(activity).also(root::addView) val fittedFirst = text(25f).also(first::addView) @@ -363,7 +371,7 @@ class PluginTextScalerTest { fun aPluginDialogSkipsFramesApartFromTheEditorWindow() { // A plugin's dialog or bottom sheet is a window of its own, drawn in turn with the editor's. // What one window skipped must not decide whether the other may skip. - editorScale = 2f + scaler.onEditorFontScaleChanged(2f) val sheet = LinearLayout(activity) val dialog = Dialog(activity).apply { setContentView(sheet) } dialog.show() @@ -386,4 +394,51 @@ class PluginTextScalerTest { assertThat(editorSkipped.zipWithNext().none { (first, second) -> first && second }).isTrue() assertThat(editorSkipped).contains(true) } + + @Test + fun aPluginViewNestedInAnotherIsWalkedOncePerFrame() { + // A plugin fragment inside another plugin fragment's view: both views are tracked, the inner + // one first, as its view is created first. + scaler.onEditorFontScaleChanged(2f) + val inner = LinearLayout(activity).also(root::addView) + val label = countingText(20f).also(inner::addView) + scaler.track(inner) + scaler.track(root) + attach() + drawFrame() + label.sizeReads = 0 + + drawFrame() + + assertThat(label.sizeReads).isEqualTo(1) + } + + @Test + fun onceTheScaleIsBackTo1FramesNoLongerWalkThePluginViews() { + scaler.onEditorFontScaleChanged(2f) + val label = countingText(20f).also(root::addView) + scaler.track(root) + attach() + drawFrame() + + scaler.onEditorFontScaleChanged(1f) + label.sizeReads = 0 + drawFrame() + + assertThat(label.sizeReads).isEqualTo(0) + assertThat(label.textSize).isEqualTo(20f) + } + + @Test + fun aScaleChangeThatStaysAt1LeavesThePluginTextAlone() { + // Pinching the code from 10 to 8: both below the editor's default. + scaler.onEditorFontScaleChanged(10f / 14f) + val label = countingText(20f).also(root::addView) + scaler.track(root) + label.sizeReads = 0 + + scaler.onEditorFontScaleChanged(8f / 14f) + + assertThat(label.sizeReads).isEqualTo(0) + } }