mirror of https://github.com/tuskyapp/Tusky.git
Browse Source
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/2461pull/3783/head
9 changed files with 398 additions and 2 deletions
@ -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 [androidx.preference.SeekBarPreference], but better because: |
||||
* |
||||
* - 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. |
||||
* - 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 = androidx.preference.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.SliderPreference_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 { |
||||
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" |
||||
} |
||||
} |
||||
@ -0,0 +1,124 @@
|
||||
<?xml version="1.0" encoding="utf-8"?> |
||||
<!-- |
||||
~ Copyright (C) 2018 The Android Open Source Project |
||||
~ |
||||
~ Licensed under the Apache License, Version 2.0 (the "License"); |
||||
~ you may not use this file except in compliance with the License. |
||||
~ You may obtain a copy of the License at |
||||
~ |
||||
~ http://www.apache.org/licenses/LICENSE-2.0 |
||||
~ |
||||
~ Unless required by applicable law or agreed to in writing, software |
||||
~ distributed under the License is distributed on an "AS IS" BASIS, |
||||
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
~ See the License for the specific language governing permissions and |
||||
~ limitations under the License. |
||||
--> |
||||
|
||||
<!-- Layout used by SeekBarPreference for the seekbar widget style. Minimally adapted for use |
||||
with Slider instead of SeekBar. --> |
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" |
||||
xmlns:tools="http://schemas.android.com/tools" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="wrap_content" |
||||
android:minHeight="?android:attr/listPreferredItemHeightSmall" |
||||
android:gravity="center_vertical" |
||||
android:paddingStart="?android:attr/listPreferredItemPaddingStart" |
||||
android:paddingEnd="?android:attr/listPreferredItemPaddingEnd" |
||||
android:clipChildren="false" |
||||
android:clipToPadding="false" |
||||
android:baselineAligned="false"> |
||||
|
||||
<include layout="@layout/image_frame"/> |
||||
|
||||
<LinearLayout |
||||
android:layout_width="match_parent" |
||||
android:layout_height="wrap_content" |
||||
android:orientation="vertical" |
||||
android:layout_marginTop="8dp" |
||||
android:layout_marginBottom="8dp" |
||||
android:clipChildren="false" |
||||
android:clipToPadding="false"> |
||||
|
||||
<RelativeLayout |
||||
android:layout_width="wrap_content" |
||||
android:layout_height="0dp" |
||||
android:layout_weight="1"> |
||||
|
||||
<TextView |
||||
android:id="@android:id/title" |
||||
android:labelFor="@id/slider" |
||||
android:layout_width="wrap_content" |
||||
android:layout_height="wrap_content" |
||||
android:singleLine="true" |
||||
android:textAppearance="?android:attr/textAppearanceListItem" |
||||
android:ellipsize="marquee" |
||||
tools:ignore="LabelFor,SelectableText" /> |
||||
|
||||
<TextView |
||||
android:id="@android:id/summary" |
||||
android:layout_width="wrap_content" |
||||
android:layout_height="wrap_content" |
||||
android:layout_below="@android:id/title" |
||||
android:layout_alignStart="@android:id/title" |
||||
android:layout_gravity="start" |
||||
android:textAlignment="viewStart" |
||||
android:textColor="?android:attr/textColorSecondary" |
||||
android:maxLines="4" |
||||
style="@style/PreferenceSummaryTextStyle" |
||||
tools:ignore="SelectableText" /> |
||||
|
||||
</RelativeLayout> |
||||
|
||||
<!-- Using UnPressableLinearLayout as a workaround to disable the pressed state propagation |
||||
to the children of this container layout. Otherwise, the animated pressed state will also |
||||
play for the thumb in the AbsSeekBar in addition to the preference's ripple background. |
||||
The background of the SeekBar is also set to null to disable the ripple background --> |
||||
<androidx.preference.UnPressableLinearLayout |
||||
android:layout_width="match_parent" |
||||
android:layout_height="wrap_content" |
||||
android:gravity="center_vertical" |
||||
android:paddingLeft="0dp" |
||||
android:paddingStart="0dp" |
||||
android:paddingRight="0dp" |
||||
android:paddingEnd="0dp" |
||||
android:clipChildren="false" |
||||
android:clipToPadding="false"> |
||||
|
||||
<com.google.android.material.button.MaterialButton |
||||
android:id="@+id/decrement" |
||||
android:layout_width="wrap_content" |
||||
android:layout_height="wrap_content" |
||||
android:visibility="gone" |
||||
style="@style/Widget.Material3.Button.IconButton" /> |
||||
|
||||
<!-- The total height of the Seekbar widget's area should be 48dp - this allows for an |
||||
increased touch area so you do not need to exactly tap the thumb to move it. However, |
||||
setting the Seekbar height directly causes the thumb and seekbar to be misaligned on |
||||
API 22 and 23 - so instead we just set 15dp padding above and below, to account for the |
||||
18dp default height of the Seekbar thumb for a total of 48dp. |
||||
Note: we set 0dp padding at the start and end of this seekbar to allow it to properly |
||||
fit into the layout, but this means that there's no leeway on either side for touch |
||||
input - this might be something we should reconsider down the line. --> |
||||
<com.google.android.material.slider.Slider |
||||
android:id="@+id/slider" |
||||
android:layout_width="0dp" |
||||
android:layout_weight="1" |
||||
android:layout_height="wrap_content" |
||||
android:paddingLeft="@dimen/preference_seekbar_padding_horizontal" |
||||
android:paddingStart="@dimen/preference_seekbar_padding_horizontal" |
||||
android:paddingRight="@dimen/preference_seekbar_padding_horizontal" |
||||
android:paddingEnd="@dimen/preference_seekbar_padding_horizontal" |
||||
android:paddingTop="@dimen/preference_seekbar_padding_vertical" |
||||
android:paddingBottom="@dimen/preference_seekbar_padding_vertical" |
||||
android:background="@null"/> |
||||
|
||||
<com.google.android.material.button.MaterialButton |
||||
android:id="@+id/increment" |
||||
android:layout_width="wrap_content" |
||||
android:layout_height="wrap_content" |
||||
android:visibility="gone" |
||||
style="@style/Widget.Material3.Button.IconButton" /> |
||||
</androidx.preference.UnPressableLinearLayout> |
||||
</LinearLayout> |
||||
</LinearLayout> |
||||
Loading…
Reference in new issue