diff --git a/app/src/main/java/io/legado/app/help/AppConfig.kt b/app/src/main/java/io/legado/app/help/AppConfig.kt index 6eef2bfa0..7e9c05834 100644 --- a/app/src/main/java/io/legado/app/help/AppConfig.kt +++ b/app/src/main/java/io/legado/app/help/AppConfig.kt @@ -111,4 +111,9 @@ object AppConfig { App.INSTANCE.putPrefBoolean("bookGroupAudio", value) } + var elevation: Int + get() = App.INSTANCE.getPrefInt("elevation", -1) + set(value) { + App.INSTANCE.putPrefInt("elevation", value) + } } diff --git a/app/src/main/java/io/legado/app/lib/theme/ATHUtils.kt b/app/src/main/java/io/legado/app/lib/theme/ATHUtils.kt index 73e342713..ddd6fc816 100644 --- a/app/src/main/java/io/legado/app/lib/theme/ATHUtils.kt +++ b/app/src/main/java/io/legado/app/lib/theme/ATHUtils.kt @@ -19,4 +19,16 @@ object ATHUtils { a.recycle() } } + + @JvmOverloads + fun resolveFloat(context: Context, @AttrRes attr: Int, fallback: Float = 0.0f): Float { + val a = context.theme.obtainStyledAttributes(intArrayOf(attr)) + return try { + a.getFloat(0, fallback) + } catch (e: Exception) { + fallback + } finally { + a.recycle() + } + } } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/lib/theme/MaterialValueHelper.kt b/app/src/main/java/io/legado/app/lib/theme/MaterialValueHelper.kt index fab853c6b..bc898d065 100644 --- a/app/src/main/java/io/legado/app/lib/theme/MaterialValueHelper.kt +++ b/app/src/main/java/io/legado/app/lib/theme/MaterialValueHelper.kt @@ -107,4 +107,7 @@ val Context.isDarkTheme: Boolean get() = ColorUtils.isColorLight(ThemeStore.primaryColor(this)) val Fragment.isDarkTheme: Boolean - get() = requireContext().isDarkTheme \ No newline at end of file + get() = requireContext().isDarkTheme + +val Context.elevation: Float + get() = ThemeStore.elevation(this) \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/lib/theme/ThemeStore.kt b/app/src/main/java/io/legado/app/lib/theme/ThemeStore.kt index 927fc2fb0..5b3997081 100644 --- a/app/src/main/java/io/legado/app/lib/theme/ThemeStore.kt +++ b/app/src/main/java/io/legado/app/lib/theme/ThemeStore.kt @@ -284,6 +284,14 @@ private constructor(private val mContext: Context) : ThemeStoreInterface { ) } + @CheckResult + fun elevation(context: Context): Float { + return prefs(context).getFloat( + ThemeStorePrefKeys.KEY_ELEVATION, + ATHUtils.resolveFloat(context, android.R.attr.elevation, context.resources.getDimension(R.dimen.design_appbar_elevation)) + ) + } + @CheckResult @ColorInt fun bottomBackground(context: Context): Int { diff --git a/app/src/main/java/io/legado/app/lib/theme/ThemeStorePrefKeys.kt b/app/src/main/java/io/legado/app/lib/theme/ThemeStorePrefKeys.kt index a2ab19d60..ca8e85fc1 100644 --- a/app/src/main/java/io/legado/app/lib/theme/ThemeStorePrefKeys.kt +++ b/app/src/main/java/io/legado/app/lib/theme/ThemeStorePrefKeys.kt @@ -27,4 +27,6 @@ object ThemeStorePrefKeys { const val KEY_APPLY_PRIMARYDARK_STATUSBAR = "apply_primarydark_statusbar" const val KEY_APPLY_PRIMARY_NAVBAR = "apply_primary_navbar" const val KEY_AUTO_GENERATE_PRIMARYDARK = "auto_generate_primarydark" + + const val KEY_ELEVATION = "elevation" } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/config/ThemeConfigFragment.kt b/app/src/main/java/io/legado/app/ui/config/ThemeConfigFragment.kt index 99d7d1e54..17a3bcfe1 100644 --- a/app/src/main/java/io/legado/app/ui/config/ThemeConfigFragment.kt +++ b/app/src/main/java/io/legado/app/ui/config/ThemeConfigFragment.kt @@ -16,6 +16,8 @@ import io.legado.app.lib.dialogs.noButton import io.legado.app.lib.dialogs.yesButton import io.legado.app.lib.theme.ATH import io.legado.app.lib.theme.ColorUtils +import io.legado.app.lib.theme.elevation +import io.legado.app.ui.widget.number.NumberPickerDialog import io.legado.app.utils.* @@ -25,6 +27,7 @@ class ThemeConfigFragment : PreferenceFragmentCompat(), SharedPreferences.OnShar override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) { addPreferencesFromResource(R.xml.pref_config_theme) + upPreferenceSummary("barElevation", AppConfig.elevation.toString()) } override fun onViewCreated(view: View, savedInstanceState: Bundle?) { @@ -145,6 +148,19 @@ class ThemeConfigFragment : PreferenceFragmentCompat(), SharedPreferences.OnShar recreateActivities() } }.show().applyTint() + "barElevation" -> NumberPickerDialog(requireContext()) + .setTitle(getString(R.string.bar_elevation)) + .setMaxValue(32) + .setMinValue(0) + .setValue(AppConfig.elevation) + .setCustomButton((R.string.btn_default_s)) { + AppConfig.elevation = App.INSTANCE.resources.getDimension(R.dimen.design_appbar_elevation).toInt() + recreateActivities() + } + .show { + AppConfig.elevation = it + recreateActivities() + } } return super.onPreferenceTreeClick(preference) } @@ -180,4 +196,10 @@ class ThemeConfigFragment : PreferenceFragmentCompat(), SharedPreferences.OnShar postEvent(EventBus.RECREATE, "") } + private fun upPreferenceSummary(preferenceKey: String, value: String?) { + val preference = findPreference(preferenceKey) ?: return + when (preferenceKey) { + "barElevation" -> preference.summary = getString(R.string.bar_elevation_s, value) + } + } } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/widget/TitleBar.kt b/app/src/main/java/io/legado/app/ui/widget/TitleBar.kt index d19c8929f..8df952849 100644 --- a/app/src/main/java/io/legado/app/ui/widget/TitleBar.kt +++ b/app/src/main/java/io/legado/app/ui/widget/TitleBar.kt @@ -10,6 +10,8 @@ import androidx.annotation.StyleRes import androidx.appcompat.widget.Toolbar import com.google.android.material.appbar.AppBarLayout import io.legado.app.R +import io.legado.app.help.AppConfig +import io.legado.app.lib.theme.elevation import io.legado.app.lib.theme.primaryColor import io.legado.app.utils.activity import io.legado.app.utils.navigationBarHeight @@ -140,6 +142,13 @@ class TitleBar(context: Context, attrs: AttributeSet?) : AppBarLayout(context, a } backgroundColor = context.primaryColor + // targetElevation = context.elevation + stateListAnimator = null + if (AppConfig.elevation < 0) { + elevation = context.elevation + } else { + elevation = AppConfig.elevation.toFloat() + } a.recycle() } diff --git a/app/src/main/java/io/legado/app/ui/widget/number/NumberPickerDialog.kt b/app/src/main/java/io/legado/app/ui/widget/number/NumberPickerDialog.kt index 17fe9ab53..3b4c8f5db 100644 --- a/app/src/main/java/io/legado/app/ui/widget/number/NumberPickerDialog.kt +++ b/app/src/main/java/io/legado/app/ui/widget/number/NumberPickerDialog.kt @@ -40,6 +40,17 @@ class NumberPickerDialog(context: Context) { return this } + fun setCustomButton(textId: Int, listener: (() -> Unit)?): NumberPickerDialog { + builder.setNeutralButton(textId) { _, _ -> + numberPicker?.let { + it.clearFocus() + it.hideSoftInput() + listener?.invoke() + } + } + return this; + } + fun show(callBack: ((value: Int) -> Unit)?) { builder.setPositiveButton(R.string.ok) { _, _ -> numberPicker?.let { diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 52c7b3b08..d082e75a0 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -643,4 +643,7 @@ 没有默认路径 设置分组 查看目录 + 导航栏阴影 + 当前阴影大小(elevation): %s + 默认 diff --git a/app/src/main/res/xml/pref_config_theme.xml b/app/src/main/res/xml/pref_config_theme.xml index fe0d24ca2..2f34e86ec 100644 --- a/app/src/main/res/xml/pref_config_theme.xml +++ b/app/src/main/res/xml/pref_config_theme.xml @@ -26,6 +26,13 @@ android:title="@string/navigation_bar_color_change" app:iconSpaceReserved="false" /> + +