Repository navigation
ADFA-7142: Never shrink plugin text below its own size; scale text added later #2255
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
davidschachterADFA
wants to merge
6
commits into
stage
Choose a base branch
from
fix/ADFA-7142-plugin-text-scale
base: stage
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
3c9b0f1
ADFA-7142: Never shrink plugin text below its own size; scale text ad…
davidschachterADFA f221426
ADFA-7142: Rescale plugin text that was detached when the preference …
davidschachterADFA fff4934
ADFA-7142: Scale plugin text before each frame, not after each layout
davidschachterADFA 1b96e26
ADFA-7142: Never skip two frames in a row
davidschachterADFA 3a39c9c
ADFA-7142: Decide frame skips once per window, not once per root
davidschachterADFA dec31e3
ADFA-7142: Keep the scale in the scaler; walk nested and scale-1 view…
davidschachterADFA File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
175 changes: 175 additions & 0 deletions
175
app/src/main/java/com/itsaky/androidide/activities/editor/PluginTextScaler.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,175 @@ | ||
| 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.Collections | ||
| 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] 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( | ||
| editorFontScale: Float, | ||
| ) { | ||
| private class Sizing( | ||
| val base: Float, | ||
| var applied: Float, | ||
| ) | ||
|
|
||
| private val sizings = WeakHashMap<TextView, Sizing>() | ||
| private val trackedRoots: MutableSet<View> = Collections.newSetFromMap(WeakHashMap()) | ||
|
|
||
| // Keyed by the window's observer; an entry lasts while some tracked root is in that window. | ||
| private val windowWatchers = HashMap<ViewTreeObserver, WindowWatcher>() | ||
|
|
||
| private var scale = max(1f, editorFontScale) | ||
|
|
||
| /** | ||
| * 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, is still watched once. | ||
| */ | ||
| fun track(root: View) { | ||
| scaleNewText(root) | ||
| if (trackedRoots.add(root)) { | ||
| root.addOnAttachStateChangeListener(attachListener) | ||
| if (root.isAttachedToWindow) startWatching(root) | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * 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, 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 { | ||
| if (scale == 1f) return false | ||
| var changed = false | ||
| root.forEachViewRecursively { view -> | ||
| 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 | ||
|
davidschachterADFA marked this conversation as resolved.
Resolved
|
||
| return Sizing(view.textSize, 1f).also { sizings[view] = it } | ||
| } | ||
|
|
||
| private fun apply( | ||
| view: TextView, | ||
| sizing: Sizing, | ||
| scale: Float, | ||
| ) { | ||
| view.setTextSize(TypedValue.COMPLEX_UNIT_PX, sizing.base * scale) | ||
| sizing.applied = scale | ||
| } | ||
|
|
||
| private fun TextView.isAutoSized() = autoSizeTextType != TextView.AUTO_SIZE_TEXT_TYPE_NONE | ||
|
|
||
| private val attachListener = | ||
| object : View.OnAttachStateChangeListener { | ||
| override fun onViewAttachedToWindow(v: View) = startWatching(v) | ||
|
|
||
| override fun onViewDetachedFromWindow(v: View) = stopWatching(v) | ||
| } | ||
|
|
||
| private fun startWatching(root: View) { | ||
| val observer = root.viewTreeObserver | ||
| val watcher = | ||
| windowWatchers.getOrPut(observer) { | ||
| WindowWatcher().also { observer.addOnPreDrawListener(it) } | ||
| } | ||
| watcher.roots.add(root) | ||
| } | ||
|
|
||
| // 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<View>() | ||
| private var skippedLast = false | ||
|
|
||
| // Hidden text is caught on the frame that first shows it. | ||
| override fun onPreDraw(): Boolean { | ||
| var changed = false | ||
| for (root in roots) { | ||
| 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 | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.