Skip to content

Commit 8917fca

Browse files
authored
ADFA-4212: scale plugin tab UI text to editor font-size preference (#1386)
* ADFA-4212: scale plugin tab UI text to editor font-size preference
1 parent 9196356 commit 8917fca

3 files changed

Lines changed: 86 additions & 4 deletions

File tree

app/src/main/java/com/itsaky/androidide/activities/editor/EditorHandlerActivity.kt

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import android.content.res.Configuration
2424
import android.os.Bundle
2525
import android.text.TextUtils
2626
import android.util.Log
27+
import android.util.TypedValue
2728
import android.view.KeyEvent
2829
import android.view.View
2930
import android.view.ViewGroup.LayoutParams
@@ -32,6 +33,8 @@ import androidx.collection.MutableIntObjectMap
3233
import androidx.core.content.res.ResourcesCompat
3334
import androidx.core.view.GravityCompat
3435
import androidx.core.view.doOnNextLayout
36+
import androidx.fragment.app.Fragment
37+
import androidx.fragment.app.FragmentManager
3538
import androidx.lifecycle.lifecycleScope
3639
import com.blankj.utilcode.util.ImageUtils
3740
import com.google.android.material.tabs.TabLayout
@@ -63,6 +66,7 @@ import com.itsaky.androidide.eventbus.events.editor.DocumentChangeEvent
6366
import com.itsaky.androidide.eventbus.events.file.FileRenameEvent
6467
import com.itsaky.androidide.activities.PluginManagerActivity
6568
import com.itsaky.androidide.eventbus.events.plugin.PluginCrashedEvent
69+
import com.itsaky.androidide.eventbus.events.preferences.PreferenceChangeEvent
6670
import com.itsaky.androidide.idetooltips.TooltipManager
6771
import com.itsaky.androidide.idetooltips.TooltipTag
6872
import com.itsaky.androidide.interfaces.IEditorHandler
@@ -73,6 +77,7 @@ import com.itsaky.androidide.models.Range
7377
import com.itsaky.androidide.models.SaveResult
7478
import com.itsaky.androidide.plugins.manager.build.PluginBuildActionManager
7579
import com.itsaky.androidide.plugins.manager.fragment.PluginFragmentFactory
80+
import com.itsaky.androidide.preferences.internal.EditorPreferences
7681
import com.itsaky.androidide.plugins.manager.ui.PluginDrawableResolver
7782
import com.itsaky.androidide.plugins.manager.ui.PluginEditorTabManager
7883
import com.itsaky.androidide.projects.ProjectManagerImpl
@@ -90,6 +95,7 @@ import com.itsaky.androidide.utils.EditorSidebarActions
9095
import com.itsaky.androidide.utils.IntentUtils.openImage
9196
import com.itsaky.androidide.utils.UniqueNameBuilder
9297
import com.itsaky.androidide.utils.flashSuccess
98+
import com.itsaky.androidide.utils.forEachViewRecursively
9399
import kotlinx.coroutines.Dispatchers
94100
import kotlinx.coroutines.NonCancellable
95101
import kotlinx.coroutines.launch
@@ -98,6 +104,7 @@ import org.adfa.constants.CONTENT_KEY
98104
import org.greenrobot.eventbus.Subscribe
99105
import org.greenrobot.eventbus.ThreadMode
100106
import java.io.File
107+
import java.util.WeakHashMap
101108
import java.util.concurrent.ConcurrentHashMap
102109
import java.util.concurrent.CopyOnWriteArrayList
103110
import java.util.concurrent.atomic.AtomicBoolean
@@ -125,6 +132,22 @@ open class EditorHandlerActivity :
125132

126133
private val pluginTabIndices = mutableMapOf<String, Int>()
127134
private val tabIndexToPluginId = mutableMapOf<Int, String>()
135+
private var lastAppliedPluginFontScale = EditorPreferences.editorFontScale
136+
private val pluginTextBaseSizes = WeakHashMap<TextView, Float>()
137+
138+
private val pluginFontScalingListener = object : FragmentManager.FragmentLifecycleCallbacks() {
139+
override fun onFragmentViewCreated(
140+
mFragmentManager: FragmentManager,
141+
mFragment: Fragment,
142+
view: View,
143+
savedInstanceState: Bundle?
144+
) {
145+
val scale = EditorPreferences.editorFontScale
146+
if (scale != 1f && isPluginFragment(mFragment)) {
147+
applyPluginFontScale(view, scale)
148+
}
149+
}
150+
}
128151
private val shortcutManager by lazy { ShortcutManager(applicationContext) }
129152

130153
private var pluginEditorProvider: EditorProviderImpl? = null
@@ -194,6 +217,8 @@ open class EditorHandlerActivity :
194217
mBuildEventListener.setActivity(this)
195218
super.onCreate(savedInstanceState)
196219

220+
supportFragmentManager.registerFragmentLifecycleCallbacks(pluginFontScalingListener, true)
221+
197222
editorViewModel._displayedFile.observe(
198223
this,
199224
) { fileIndex ->
@@ -392,6 +417,7 @@ open class EditorHandlerActivity :
392417
}
393418

394419
restoreOpenedPluginTabs()
420+
syncPluginUiFontSize()
395421
}
396422

397423
private fun restoreOpenedPluginTabs() {
@@ -1113,6 +1139,49 @@ open class EditorHandlerActivity :
11131139
showPluginCrashDialog(event)
11141140
}
11151141

1142+
@Subscribe(threadMode = ThreadMode.MAIN)
1143+
fun onPreferenceChanged(event: PreferenceChangeEvent) {
1144+
if (event.key == EditorPreferences.FONT_SIZE) {
1145+
syncPluginUiFontSize()
1146+
}
1147+
}
1148+
1149+
private fun syncPluginUiFontSize() {
1150+
val scale = EditorPreferences.editorFontScale
1151+
if (scale == lastAppliedPluginFontScale) {
1152+
return
1153+
}
1154+
lastAppliedPluginFontScale = scale
1155+
1156+
val pluginFragments = mutableListOf<Fragment>()
1157+
collectPluginFragments(supportFragmentManager, pluginFragments)
1158+
pluginFragments.forEach { fragment ->
1159+
fragment.view?.let { applyPluginFontScale(it, scale) }
1160+
}
1161+
}
1162+
1163+
private fun isPluginFragment(fragment: Fragment): Boolean =
1164+
fragment.javaClass.classLoader !== javaClass.classLoader
1165+
1166+
private fun applyPluginFontScale(root: View, scale: Float) {
1167+
root.forEachViewRecursively { view ->
1168+
if (view is TextView) {
1169+
val baseSize = pluginTextBaseSizes.getOrPut(view) { view.textSize }
1170+
view.setTextSize(TypedValue.COMPLEX_UNIT_PX, baseSize * scale)
1171+
}
1172+
}
1173+
}
1174+
1175+
private fun collectPluginFragments(manager: FragmentManager, into: MutableList<Fragment>) {
1176+
manager.fragments.forEach { fragment ->
1177+
if (isPluginFragment(fragment)) {
1178+
into.add(fragment)
1179+
} else {
1180+
collectPluginFragments(fragment.childFragmentManager, into)
1181+
}
1182+
}
1183+
}
1184+
11161185
private fun showPluginCrashDialog(event: PluginCrashedEvent) {
11171186
val dialogView = layoutInflater.inflate(R.layout.dialog_plugin_crash, null)
11181187
dialogView.findViewById<TextView>(R.id.plugin_crash_message).text =

app/src/main/java/com/itsaky/androidide/ui/CodeEditorView.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,9 @@ import java.io.Closeable
8484
import java.io.File
8585
import kotlin.math.abs
8686

87-
private const val MIN_FONT_SIZE = 6f
88-
private const val DEFAULT_FONT_SIZE = 14f
89-
private const val MAX_FONT_SIZE = 32f
87+
private const val MIN_FONT_SIZE = EditorPreferences.FONT_SIZE_MIN
88+
private const val DEFAULT_FONT_SIZE = EditorPreferences.FONT_SIZE_DEFAULT
89+
private const val MAX_FONT_SIZE = EditorPreferences.FONT_SIZE_MAX
9090
private val ARCHIVE_EXTENSIONS = setOf("apk", "cgp", "zip")
9191

9292
/**

preferences/src/main/java/com/itsaky/androidide/preferences/internal/EditorPreferences.kt

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ object EditorPreferences {
4949
const val COLOR_SCHEME = "idepref_editor_colorScheme"
5050
const val DEFAULT_COLOR_SCHEME = "default"
5151

52+
const val FONT_SIZE_MIN = 6f
53+
const val FONT_SIZE_DEFAULT = 14f
54+
const val FONT_SIZE_MAX = 32f
55+
5256
var completionsMatchLower: Boolean
5357
get() = prefManager.getBoolean(COMPLETIONS_MATCH_LOWER, false)
5458
set(value) {
@@ -86,11 +90,20 @@ object EditorPreferences {
8690
}
8791

8892
var fontSize: Float
89-
get() = prefManager.getFloat(FONT_SIZE, 14f)
93+
get() = prefManager.getFloat(FONT_SIZE, FONT_SIZE_DEFAULT)
9094
set(value) {
9195
prefManager.putFloat(FONT_SIZE, value)
9296
}
9397

98+
val effectiveFontSize: Float
99+
get() {
100+
val size = fontSize
101+
return if (size !in FONT_SIZE_MIN..FONT_SIZE_MAX) FONT_SIZE_DEFAULT else size
102+
}
103+
104+
val editorFontScale: Float
105+
get() = effectiveFontSize / FONT_SIZE_DEFAULT
106+
94107
var tabSize: Int
95108
get() = prefManager.getInt(TAB_SIZE, 4)
96109
set(value) {

0 commit comments

Comments
 (0)