From 79235ef8e0bc6d7e529c7df847d0d07ca780ae57 Mon Sep 17 00:00:00 2001 From: Nik Clayton Date: Tue, 31 Jan 2023 15:32:56 +0100 Subject: [PATCH 1/4] Provide a preference to scale all UI text Font scaling is applied in addition to any scaling set in Android system preferences. So if the user set the Android font size to largest (a 1.3x increase) and then sets the preference to 120%, the total change is 1.56x. Create SliderPreference to adjust the preference. - Use Slider, which supports float values and step sizes > 1 - Display the selected value in the preference's summary - Provide buttons to increment / decrement the value Restart the activity if the preference changes so that the user sees the impact of the change immediately. Fix a bug in PreferencesActivity where the "EXTRA_RESTART_ON_BACK" intent was never processed. Fix this to ensure that other activities are restarted so the new font scale takes effect. Implement the scaling in BaseActivity by overriding onAttachBaseContext, and providing a wrapped context with the font scaling applied. Fixes https://github.com/tuskyapp/Tusky/issues/2982, https://github.com/tuskyapp/Tusky/issues/2461 --- .../com/keylesspalace/tusky/BaseActivity.java | 17 ++ .../preference/PreferencesActivity.kt | 10 +- .../preference/PreferencesFragment.kt | 14 ++ .../tusky/settings/SettingsConstants.kt | 3 + .../tusky/settings/SettingsDSL.kt | 10 + .../tusky/util/FontScaleContextWrapper.kt | 43 ++++ .../tusky/view/SliderPreference.kt | 185 ++++++++++++++++++ app/src/main/res/layout/pref_slider.xml | 122 ++++++++++++ app/src/main/res/values/attrs.xml | 12 +- app/src/main/res/values/strings.xml | 1 + 10 files changed, 414 insertions(+), 3 deletions(-) create mode 100644 app/src/main/java/com/keylesspalace/tusky/util/FontScaleContextWrapper.kt create mode 100644 app/src/main/java/com/keylesspalace/tusky/view/SliderPreference.kt create mode 100644 app/src/main/res/layout/pref_slider.xml diff --git a/app/src/main/java/com/keylesspalace/tusky/BaseActivity.java b/app/src/main/java/com/keylesspalace/tusky/BaseActivity.java index ad173170a6..b206d6317d 100644 --- a/app/src/main/java/com/keylesspalace/tusky/BaseActivity.java +++ b/app/src/main/java/com/keylesspalace/tusky/BaseActivity.java @@ -16,6 +16,7 @@ package com.keylesspalace.tusky; import android.app.ActivityManager; +import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.content.pm.PackageManager; @@ -45,6 +46,8 @@ import com.keylesspalace.tusky.di.Injectable; import com.keylesspalace.tusky.interfaces.AccountSelectionListener; import com.keylesspalace.tusky.interfaces.PermissionRequester; +import com.keylesspalace.tusky.settings.PrefKeys; +import com.keylesspalace.tusky.util.FontScaleContextWrapper; import com.keylesspalace.tusky.util.ThemeUtils; import java.util.ArrayList; @@ -54,6 +57,7 @@ import javax.inject.Inject; public abstract class BaseActivity extends AppCompatActivity implements Injectable { + private static final String TAG = "BaseActivity"; @Inject public AccountManager accountManager; @@ -93,6 +97,19 @@ protected void onCreate(@Nullable Bundle savedInstanceState) { requesters = new HashMap<>(); } + @Override + protected void attachBaseContext(Context newBase) { + Log.d(TAG, "attachBaseContext()"); + SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(newBase); + + float uiScaleRatio = preferences.getFloat(PrefKeys.UI_SCALE_RATIO, 100F); + Context context = FontScaleContextWrapper.Companion.wrap( + newBase, + uiScaleRatio / 100.f + ); + super.attachBaseContext(context); + } + protected boolean requiresLogin() { return true; } diff --git a/app/src/main/java/com/keylesspalace/tusky/components/preference/PreferencesActivity.kt b/app/src/main/java/com/keylesspalace/tusky/components/preference/PreferencesActivity.kt index 1fdc7a6506..23943768dc 100644 --- a/app/src/main/java/com/keylesspalace/tusky/components/preference/PreferencesActivity.kt +++ b/app/src/main/java/com/keylesspalace/tusky/components/preference/PreferencesActivity.kt @@ -93,7 +93,9 @@ class PreferencesActivity : } onBackPressedDispatcher.addCallback(this, restartActivitiesOnBackPressedCallback) - restartActivitiesOnBackPressedCallback.isEnabled = savedInstanceState?.getBoolean(EXTRA_RESTART_ON_BACK, false) ?: false + restartActivitiesOnBackPressedCallback.isEnabled = intent.extras?.getBoolean( + EXTRA_RESTART_ON_BACK + ) ?: savedInstanceState?.getBoolean(EXTRA_RESTART_ON_BACK, false) ?: false } override fun onPreferenceStartFragment( @@ -149,6 +151,10 @@ class PreferencesActivity : restartActivitiesOnBackPressedCallback.isEnabled = true this.restartCurrentActivity() } + PrefKeys.UI_SCALE_RATIO -> { + restartActivitiesOnBackPressedCallback.isEnabled = true + this.restartCurrentActivity() + } "statusTextSize", "absoluteTimeView", "showBotOverlay", "animateGifAvatars", "useBlurhash", "showSelfUsername", "showCardsInTimelines", "confirmReblogs", "confirmFavourites", "enableSwipeForTabs", "mainNavPosition", PrefKeys.HIDE_TOP_TOOLBAR -> { @@ -172,7 +178,7 @@ class PreferencesActivity : override fun androidInjector() = androidInjector companion object { - + const val TAG = "PreferencesActivity" const val GENERAL_PREFERENCES = 0 const val ACCOUNT_PREFERENCES = 1 const val NOTIFICATION_PREFERENCES = 2 diff --git a/app/src/main/java/com/keylesspalace/tusky/components/preference/PreferencesFragment.kt b/app/src/main/java/com/keylesspalace/tusky/components/preference/PreferencesFragment.kt index 5d6c1ea277..ec4653ee1d 100644 --- a/app/src/main/java/com/keylesspalace/tusky/components/preference/PreferencesFragment.kt +++ b/app/src/main/java/com/keylesspalace/tusky/components/preference/PreferencesFragment.kt @@ -29,6 +29,7 @@ import com.keylesspalace.tusky.settings.listPreference import com.keylesspalace.tusky.settings.makePreferenceScreen import com.keylesspalace.tusky.settings.preference import com.keylesspalace.tusky.settings.preferenceCategory +import com.keylesspalace.tusky.settings.sliderPreference import com.keylesspalace.tusky.settings.switchPreference import com.keylesspalace.tusky.util.LocaleManager import com.keylesspalace.tusky.util.deserialize @@ -98,6 +99,19 @@ class PreferencesFragment : PreferenceFragmentCompat(), Injectable { preferenceDataStore = localeManager } + sliderPreference { + key = PrefKeys.UI_SCALE_RATIO + setDefaultValue(100F) + valueTo = 150F + valueFrom = 50F + stepSize = 5F + setTitle(R.string.pref_ui_text_size) + format = "%.0f%%" + decrementIcon = makeIcon(GoogleMaterial.Icon.gmd_zoom_out) + incrementIcon = makeIcon(GoogleMaterial.Icon.gmd_zoom_in) + icon = makeIcon(GoogleMaterial.Icon.gmd_format_size) + } + listPreference { setDefaultValue("medium") setEntries(R.array.post_text_size_names) diff --git a/app/src/main/java/com/keylesspalace/tusky/settings/SettingsConstants.kt b/app/src/main/java/com/keylesspalace/tusky/settings/SettingsConstants.kt index 6df811d6f2..de6be0dac7 100644 --- a/app/src/main/java/com/keylesspalace/tusky/settings/SettingsConstants.kt +++ b/app/src/main/java/com/keylesspalace/tusky/settings/SettingsConstants.kt @@ -68,4 +68,7 @@ object PrefKeys { const val TAB_FILTER_HOME_REPLIES = "tabFilterHomeReplies_v2" // This was changed once to reset an unintentionally set default. const val TAB_FILTER_HOME_BOOSTS = "tabFilterHomeBoosts" + + /** UI scaling factor, stored as float, 100 = 100% = no scaling */ + const val UI_SCALE_RATIO = "uiScaleRatio" } diff --git a/app/src/main/java/com/keylesspalace/tusky/settings/SettingsDSL.kt b/app/src/main/java/com/keylesspalace/tusky/settings/SettingsDSL.kt index fc7a51c582..720dc817fd 100644 --- a/app/src/main/java/com/keylesspalace/tusky/settings/SettingsDSL.kt +++ b/app/src/main/java/com/keylesspalace/tusky/settings/SettingsDSL.kt @@ -14,6 +14,7 @@ import androidx.preference.PreferenceCategory import androidx.preference.PreferenceFragmentCompat import androidx.preference.PreferenceScreen import androidx.preference.SwitchPreference +import com.keylesspalace.tusky.view.SliderPreference import de.c1710.filemojicompat_ui.views.picker.preference.EmojiPickerPreference class PreferenceParent( @@ -43,6 +44,15 @@ inline fun PreferenceParent.emojiPreference(activity: A, builder: EmojiPicke return pref } +inline fun PreferenceParent.sliderPreference( + builder: SliderPreference.() -> Unit +): SliderPreference { + val pref = SliderPreference(context) + builder(pref) + addPref(pref) + return pref +} + inline fun PreferenceParent.switchPreference( builder: SwitchPreference.() -> Unit ): SwitchPreference { diff --git a/app/src/main/java/com/keylesspalace/tusky/util/FontScaleContextWrapper.kt b/app/src/main/java/com/keylesspalace/tusky/util/FontScaleContextWrapper.kt new file mode 100644 index 0000000000..f461ba6b7b --- /dev/null +++ b/app/src/main/java/com/keylesspalace/tusky/util/FontScaleContextWrapper.kt @@ -0,0 +1,43 @@ +package com.keylesspalace.tusky.util + +import android.content.Context +import android.content.ContextWrapper + +class FontScaleContextWrapper(base: Context?) : ContextWrapper(base) { + companion object { + const val TAG = "FontScaleContextWrapper" + + /** + * Wraps `context`, setting the correct `fontScale` from `fontScaleRatio`. + */ + fun wrap(context: Context, fontScaleRatio: Float): FontScaleContextWrapper { + val configuration = context.resources.configuration + + // Can't simply adjust the `fontScale` in `context`, because that will contain the + // result of previous adjustments. E.g., going from 100% to 80% to 100% does not return + // you to the original 100%, it leaves it at 80%. + // + // The application context is unaffected by changes to the base context, so always use + // the `fontScale` from that as the initial value to multiply. This will also contain + // any changes to the font scale from "Settings > Display > Font size" in the device + // settings. + val appConfiguration = context.applicationContext.resources.configuration + + // This only adjusts the fonts, anything measured in `dp` is unaffected by this. + // You can try to adjust `densityDpi` as shown in the commented out code below. This + // works, to a point. However, dialogs do not react well to this. Beyond a certain + // scale (~ 120%) the right hand edge of the dialog will clip off the right of the + // screen. + // + // So for now, just adjust the font scale + // + // val displayMetrics = appContext.resources.displayMetrics + // configuration.densityDpi = ((displayMetrics.densityDpi * uiScaleRatio).toInt()) + + configuration.fontScale = appConfiguration.fontScale * fontScaleRatio + + val newContext = context.createConfigurationContext(configuration) + return FontScaleContextWrapper(newContext) + } + } +} diff --git a/app/src/main/java/com/keylesspalace/tusky/view/SliderPreference.kt b/app/src/main/java/com/keylesspalace/tusky/view/SliderPreference.kt new file mode 100644 index 0000000000..d376016174 --- /dev/null +++ b/app/src/main/java/com/keylesspalace/tusky/view/SliderPreference.kt @@ -0,0 +1,185 @@ +package com.keylesspalace.tusky.view + +import android.content.Context +import android.content.res.TypedArray +import android.graphics.drawable.Drawable +import android.util.AttributeSet +import android.view.View.VISIBLE +import androidx.appcompat.content.res.AppCompatResources +import androidx.preference.Preference +import androidx.preference.PreferenceViewHolder +import com.google.android.material.slider.LabelFormatter.LABEL_GONE +import com.google.android.material.slider.Slider +import com.keylesspalace.tusky.R +import com.keylesspalace.tusky.databinding.PrefSliderBinding +import java.lang.Float.max +import java.lang.Float.min + +/** + * Slider preference + * + * Similar to [SeekBarPreference], but better because: + * + * - Uses a [Slider] instead of a [SeekBar]. Slider supports float values, and step sizes + * other than 1. + * - Displays the currently selected value in the Preference's summary, for consistency + * with platform norms. + * - Icon buttons can be displayed at the start/end of the slider. Pressing them will + * increment/decrement the slider by `stepSize`. + * - User can supply a custom formatter to format the summary value + */ +class SliderPreference @JvmOverloads constructor( + context: Context, + attrs: AttributeSet? = null, + defStyleAttr: Int = R.attr.preferenceStyle, + defStyleRes: Int = 0 +) : Preference(context, attrs, defStyleAttr, defStyleRes), + Slider.OnChangeListener, + Slider.OnSliderTouchListener { + + /** Backing property for `value` */ + private var _value = 0F + + /** + * @see Slider.getValue + * @see Slider.setValue + */ + var value: Float = defaultValue + get() = _value + set(v) { + val clamped = max(max(v, valueFrom), min(v, valueTo)) + if (clamped == field) return + _value = clamped + persistFloat(v) + notifyChanged() + } + + /** @see Slider.setValueFrom */ + var valueFrom: Float + + /** @see Slider.setValueTo */ + var valueTo: Float + + /** @see Slider.setStepSize */ + var stepSize: Float + + /** + * Format string to be applied to values before setting the summary. For more control set + * [SliderPreference.formatter] + */ + var format: String = defaultFormat + + /** + * Function that will be used to format the summary. The default formatter formats using the + * value of the [SliderPreference.format] property. + */ + var formatter: (Float) -> String = { format.format(it) } + + /** + * Optional icon to show in a button at the start of the slide. If non-null the button is + * shown. Clicking the button decrements the value by one step. + */ + var decrementIcon: Drawable? = null + + /** + * Optional icon to show in a button at the end of the slider. If non-null the button is + * shown. Clicking the button increments the value by one step. + */ + var incrementIcon: Drawable? = null + + /** View binding */ + private lateinit var binding: PrefSliderBinding + + init { + // Using `widgetLayoutResource` here would be incorrect, as that tries to put the entire + // preference layout to the right of the title and summary. + layoutResource = R.layout.pref_slider + + val a = context.obtainStyledAttributes(attrs, R.styleable.SliderPreference, defStyleAttr, defStyleRes) + + value = a.getFloat(R.styleable.Slider_android_value, defaultValue) + valueFrom = a.getFloat(R.styleable.SliderPreference_android_valueFrom, defaultValueFrom) + valueTo = a.getFloat(R.styleable.SliderPreference_android_valueTo, defaultValueTo) + stepSize = a.getFloat(R.styleable.SliderPreference_android_stepSize, defaultStepSize) + format = a.getString(R.styleable.SliderPreference_format) ?: defaultFormat + + val decrementIconResource = a.getResourceId(R.styleable.SliderPreference_iconStart, -1) + if (decrementIconResource != -1) { + decrementIcon = AppCompatResources.getDrawable(context, decrementIconResource) + } + + val incrementIconResource = a.getResourceId(R.styleable.SliderPreference_iconEnd, -1) + if (incrementIconResource != -1) { + incrementIcon = AppCompatResources.getDrawable(context, incrementIconResource) + } + + a.recycle() + } + + override fun onGetDefaultValue(a: TypedArray, i: Int): Any { + return a.getFloat(i, defaultValue) + } + + override fun onSetInitialValue(defaultValue: Any?) { + value = getPersistedFloat((defaultValue ?: Companion.defaultValue) as Float) + } + + override fun onBindViewHolder(holder: PreferenceViewHolder) { + super.onBindViewHolder(holder) + binding = PrefSliderBinding.bind(holder.itemView) + + binding.root.isClickable = false + + binding.slider.addOnChangeListener(this) + binding.slider.addOnSliderTouchListener(this) + binding.slider.value = value // sliderValue + binding.slider.valueTo = valueTo + binding.slider.valueFrom = valueFrom + binding.slider.stepSize = stepSize + + // Disable the label, the value is shown in the preference summary + binding.slider.labelBehavior = LABEL_GONE + binding.slider.isEnabled = isEnabled + + binding.summary.visibility = VISIBLE + binding.summary.text = formatter(value) + + decrementIcon?.let { icon -> + binding.decrement.icon = icon + binding.decrement.visibility = VISIBLE + binding.decrement.setOnClickListener { + value -= stepSize + } + } + + incrementIcon?.let { icon -> + binding.increment.icon = icon + binding.increment.visibility = VISIBLE + binding.increment.setOnClickListener { + value += stepSize + } + } + } + + override fun onValueChange(slider: Slider, value: Float, fromUser: Boolean) { + if (!fromUser) return + binding.summary.text = formatter(value) + } + + override fun onStartTrackingTouch(slider: Slider) { + // Deliberately empty + } + + override fun onStopTrackingTouch(slider: Slider) { + value = slider.value + } + + companion object { + const val TAG = "FloatSeekBarPreference" + const val defaultValueFrom = 0F + const val defaultValueTo = 1F + const val defaultValue = 0.5F + const val defaultStepSize = 0.1F + const val defaultFormat = "%3.1f" + } +} diff --git a/app/src/main/res/layout/pref_slider.xml b/app/src/main/res/layout/pref_slider.xml new file mode 100644 index 0000000000..29e0b9fc98 --- /dev/null +++ b/app/src/main/res/layout/pref_slider.xml @@ -0,0 +1,122 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/app/src/main/res/values/attrs.xml b/app/src/main/res/values/attrs.xml index 6610992805..58dda20189 100644 --- a/app/src/main/res/values/attrs.xml +++ b/app/src/main/res/values/attrs.xml @@ -8,6 +8,16 @@ + + + + + + + + + + @@ -20,4 +30,4 @@ - \ No newline at end of file + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 4f002b9484..3818c0c946 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -311,6 +311,7 @@ Unlisted Followers-only + UI text size Post text size Smallest From 4f901d9df36347259ec543b6518c1c15f5184b6f Mon Sep 17 00:00:00 2001 From: Nik Clayton Date: Thu, 15 Jun 2023 16:35:28 +0200 Subject: [PATCH 2/4] Use the correct default style Prevents a horizontal line appearing between preferences. Update comments with full package names so that links work. --- .../java/com/keylesspalace/tusky/view/SliderPreference.kt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/com/keylesspalace/tusky/view/SliderPreference.kt b/app/src/main/java/com/keylesspalace/tusky/view/SliderPreference.kt index 9d170afb28..b032863978 100644 --- a/app/src/main/java/com/keylesspalace/tusky/view/SliderPreference.kt +++ b/app/src/main/java/com/keylesspalace/tusky/view/SliderPreference.kt @@ -18,9 +18,9 @@ import java.lang.Float.min /** * Slider preference * - * Similar to [SeekBarPreference], but better because: + * Similar to [androidx.preference.SeekBarPreference], but better because: * - * - Uses a [Slider] instead of a [SeekBar]. Slider supports float values, and step sizes + * - Uses a [Slider] instead of a [android.widget.SeekBar]. Slider supports float values, and step sizes * other than 1. * - Displays the currently selected value in the Preference's summary, for consistency * with platform norms. @@ -31,7 +31,7 @@ import java.lang.Float.min class SliderPreference @JvmOverloads constructor( context: Context, attrs: AttributeSet? = null, - defStyleAttr: Int = android.R.attr.preferenceStyle, + defStyleAttr: Int = androidx.preference.R.attr.preferenceStyle, defStyleRes: Int = 0 ) : Preference(context, attrs, defStyleAttr, defStyleRes), Slider.OnChangeListener, From 0588975f85ff90ba1cbf3fab38d1f97f8df6d3af Mon Sep 17 00:00:00 2001 From: Nik Clayton Date: Thu, 15 Jun 2023 16:42:16 +0200 Subject: [PATCH 3/4] Remove log statement. --- app/src/main/java/com/keylesspalace/tusky/BaseActivity.java | 1 - 1 file changed, 1 deletion(-) diff --git a/app/src/main/java/com/keylesspalace/tusky/BaseActivity.java b/app/src/main/java/com/keylesspalace/tusky/BaseActivity.java index 09c01d7a93..6bd1226338 100644 --- a/app/src/main/java/com/keylesspalace/tusky/BaseActivity.java +++ b/app/src/main/java/com/keylesspalace/tusky/BaseActivity.java @@ -99,7 +99,6 @@ protected void onCreate(@Nullable Bundle savedInstanceState) { @Override protected void attachBaseContext(Context newBase) { - Log.d(TAG, "attachBaseContext()"); SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(newBase); float uiScaleRatio = preferences.getFloat(PrefKeys.UI_SCALE_RATIO, 100F); From 50b9e8d9243a0b804f6540cb7580b30c27fc2aa2 Mon Sep 17 00:00:00 2001 From: Nik Clayton Date: Wed, 28 Jun 2023 19:46:53 +0200 Subject: [PATCH 4/4] Code review feedback --- .../com/keylesspalace/tusky/BaseActivity.java | 40 ++++++++++++++--- .../preference/PreferencesActivity.kt | 5 ++- .../preference/PreferencesFragment.kt | 2 +- .../tusky/settings/SettingsConstants.kt | 4 +- .../tusky/util/FontScaleContextWrapper.kt | 43 ------------------- .../tusky/view/SliderPreference.kt | 12 +++--- 6 files changed, 45 insertions(+), 61 deletions(-) delete mode 100644 app/src/main/java/com/keylesspalace/tusky/util/FontScaleContextWrapper.kt diff --git a/app/src/main/java/com/keylesspalace/tusky/BaseActivity.java b/app/src/main/java/com/keylesspalace/tusky/BaseActivity.java index 6bd1226338..62709d7c37 100644 --- a/app/src/main/java/com/keylesspalace/tusky/BaseActivity.java +++ b/app/src/main/java/com/keylesspalace/tusky/BaseActivity.java @@ -20,6 +20,7 @@ import android.content.Intent; import android.content.SharedPreferences; import android.content.pm.PackageManager; +import android.content.res.Configuration; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Color; @@ -47,7 +48,6 @@ import com.keylesspalace.tusky.interfaces.AccountSelectionListener; import com.keylesspalace.tusky.interfaces.PermissionRequester; import com.keylesspalace.tusky.settings.PrefKeys; -import com.keylesspalace.tusky.util.FontScaleContextWrapper; import com.keylesspalace.tusky.util.ThemeUtils; import java.util.ArrayList; @@ -101,12 +101,38 @@ protected void onCreate(@Nullable Bundle savedInstanceState) { protected void attachBaseContext(Context newBase) { SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(newBase); - float uiScaleRatio = preferences.getFloat(PrefKeys.UI_SCALE_RATIO, 100F); - Context context = FontScaleContextWrapper.Companion.wrap( - newBase, - uiScaleRatio / 100.f - ); - super.attachBaseContext(context); + // Scale text in the UI from PrefKeys.UI_TEXT_SCALE_RATIO + float uiScaleRatio = preferences.getFloat(PrefKeys.UI_TEXT_SCALE_RATIO, 100F); + + Configuration configuration = newBase.getResources().getConfiguration(); + + // Adjust `fontScale` in the configuration. + // + // You can't repeatedly adjust the `fontScale` in `newBase` because that will contain the + // result of previous adjustments. E.g., going from 100% to 80% to 100% does not return + // you to the original 100%, it leaves it at 80%. + // + // Instead, calculate the new scale from the application context. This is unaffected by + // changes to the base context. It does contain contain any changes to the font scale from + // "Settings > Display > Font size" in the device settings, so scaling performed here + // is in addition to any scaling in the device settings. + Configuration appConfiguration = newBase.getApplicationContext().getResources().getConfiguration(); + + // This only adjusts the fonts, anything measured in `dp` is unaffected by this. + // You can try to adjust `densityDpi` as shown in the commented out code below. This + // works, to a point. However, dialogs do not react well to this. Beyond a certain + // scale (~ 120%) the right hand edge of the dialog will clip off the right of the + // screen. + // + // So for now, just adjust the font scale + // + // val displayMetrics = appContext.resources.displayMetrics + // configuration.densityDpi = ((displayMetrics.densityDpi * uiScaleRatio).toInt()) + configuration.fontScale = appConfiguration.fontScale * uiScaleRatio / 100F; + + Context fontScaleContext = newBase.createConfigurationContext(configuration); + + super.attachBaseContext(fontScaleContext); } protected boolean requiresLogin() { diff --git a/app/src/main/java/com/keylesspalace/tusky/components/preference/PreferencesActivity.kt b/app/src/main/java/com/keylesspalace/tusky/components/preference/PreferencesActivity.kt index 9ecb6412c3..b47df15960 100644 --- a/app/src/main/java/com/keylesspalace/tusky/components/preference/PreferencesActivity.kt +++ b/app/src/main/java/com/keylesspalace/tusky/components/preference/PreferencesActivity.kt @@ -153,7 +153,7 @@ class PreferencesActivity : restartActivitiesOnBackPressedCallback.isEnabled = true this.restartCurrentActivity() } - PrefKeys.UI_SCALE_RATIO -> { + PrefKeys.UI_TEXT_SCALE_RATIO -> { restartActivitiesOnBackPressedCallback.isEnabled = true this.restartCurrentActivity() } @@ -181,7 +181,8 @@ class PreferencesActivity : override fun androidInjector() = androidInjector companion object { - const val TAG = "PreferencesActivity" + @Suppress("unused") + private const val TAG = "PreferencesActivity" const val GENERAL_PREFERENCES = 0 const val ACCOUNT_PREFERENCES = 1 const val NOTIFICATION_PREFERENCES = 2 diff --git a/app/src/main/java/com/keylesspalace/tusky/components/preference/PreferencesFragment.kt b/app/src/main/java/com/keylesspalace/tusky/components/preference/PreferencesFragment.kt index ea6704391b..e2d29d4959 100644 --- a/app/src/main/java/com/keylesspalace/tusky/components/preference/PreferencesFragment.kt +++ b/app/src/main/java/com/keylesspalace/tusky/components/preference/PreferencesFragment.kt @@ -101,7 +101,7 @@ class PreferencesFragment : PreferenceFragmentCompat(), Injectable { } sliderPreference { - key = PrefKeys.UI_SCALE_RATIO + key = PrefKeys.UI_TEXT_SCALE_RATIO setDefaultValue(100F) valueTo = 150F valueFrom = 50F diff --git a/app/src/main/java/com/keylesspalace/tusky/settings/SettingsConstants.kt b/app/src/main/java/com/keylesspalace/tusky/settings/SettingsConstants.kt index b154f3ec0d..1a64f69b0a 100644 --- a/app/src/main/java/com/keylesspalace/tusky/settings/SettingsConstants.kt +++ b/app/src/main/java/com/keylesspalace/tusky/settings/SettingsConstants.kt @@ -102,6 +102,6 @@ object PrefKeys { const val TAB_FILTER_HOME_REPLIES = "tabFilterHomeReplies_v2" // This was changed once to reset an unintentionally set default. const val TAB_FILTER_HOME_BOOSTS = "tabFilterHomeBoosts" - /** UI scaling factor, stored as float, 100 = 100% = no scaling */ - const val UI_SCALE_RATIO = "uiScaleRatio" + /** UI text scaling factor, stored as float, 100 = 100% = no scaling */ + const val UI_TEXT_SCALE_RATIO = "uiTextScaleRatio" } diff --git a/app/src/main/java/com/keylesspalace/tusky/util/FontScaleContextWrapper.kt b/app/src/main/java/com/keylesspalace/tusky/util/FontScaleContextWrapper.kt deleted file mode 100644 index f461ba6b7b..0000000000 --- a/app/src/main/java/com/keylesspalace/tusky/util/FontScaleContextWrapper.kt +++ /dev/null @@ -1,43 +0,0 @@ -package com.keylesspalace.tusky.util - -import android.content.Context -import android.content.ContextWrapper - -class FontScaleContextWrapper(base: Context?) : ContextWrapper(base) { - companion object { - const val TAG = "FontScaleContextWrapper" - - /** - * Wraps `context`, setting the correct `fontScale` from `fontScaleRatio`. - */ - fun wrap(context: Context, fontScaleRatio: Float): FontScaleContextWrapper { - val configuration = context.resources.configuration - - // Can't simply adjust the `fontScale` in `context`, because that will contain the - // result of previous adjustments. E.g., going from 100% to 80% to 100% does not return - // you to the original 100%, it leaves it at 80%. - // - // The application context is unaffected by changes to the base context, so always use - // the `fontScale` from that as the initial value to multiply. This will also contain - // any changes to the font scale from "Settings > Display > Font size" in the device - // settings. - val appConfiguration = context.applicationContext.resources.configuration - - // This only adjusts the fonts, anything measured in `dp` is unaffected by this. - // You can try to adjust `densityDpi` as shown in the commented out code below. This - // works, to a point. However, dialogs do not react well to this. Beyond a certain - // scale (~ 120%) the right hand edge of the dialog will clip off the right of the - // screen. - // - // So for now, just adjust the font scale - // - // val displayMetrics = appContext.resources.displayMetrics - // configuration.densityDpi = ((displayMetrics.densityDpi * uiScaleRatio).toInt()) - - configuration.fontScale = appConfiguration.fontScale * fontScaleRatio - - val newContext = context.createConfigurationContext(configuration) - return FontScaleContextWrapper(newContext) - } - } -} diff --git a/app/src/main/java/com/keylesspalace/tusky/view/SliderPreference.kt b/app/src/main/java/com/keylesspalace/tusky/view/SliderPreference.kt index b032863978..742a2cd769 100644 --- a/app/src/main/java/com/keylesspalace/tusky/view/SliderPreference.kt +++ b/app/src/main/java/com/keylesspalace/tusky/view/SliderPreference.kt @@ -175,11 +175,11 @@ class SliderPreference @JvmOverloads constructor( } companion object { - const val TAG = "FloatSeekBarPreference" - const val defaultValueFrom = 0F - const val defaultValueTo = 1F - const val defaultValue = 0.5F - const val defaultStepSize = 0.1F - const val defaultFormat = "%3.1f" + private const val TAG = "SliderPreference" + private const val defaultValueFrom = 0F + private const val defaultValueTo = 1F + private const val defaultValue = 0.5F + private const val defaultStepSize = 0.1F + private const val defaultFormat = "%3.1f" } }