Skip to content

Commit

Permalink
优化Eink主题效果: 1.移除Eink主题时dialog及UI控件阴影; 2.Eink主题时dialog增加边框. (#4386)
Browse files Browse the repository at this point in the history
* 优化Eink主题效果: 1.移除Eink主题时dialog及UI控件阴影; 2.Eink主题时dialog增加边框.

* fix: 2处与全局不一致的UI效果
  • Loading branch information
wyouflf authored Nov 30, 2024
1 parent aeaed40 commit b7db2a0
Show file tree
Hide file tree
Showing 25 changed files with 273 additions and 22 deletions.
38 changes: 37 additions & 1 deletion app/src/main/java/io/legado/app/base/BaseDialogFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,22 @@ import android.content.DialogInterface
import android.content.DialogInterface.OnDismissListener
import android.os.Build
import android.os.Bundle
import android.view.Gravity
import android.view.View
import android.view.WindowManager
import androidx.annotation.LayoutRes
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.FragmentManager
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleEventObserver
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.lifecycleScope
import io.legado.app.R
import io.legado.app.constant.AppLog
import io.legado.app.help.config.AppConfig
import io.legado.app.help.coroutine.Coroutine
import io.legado.app.lib.theme.ThemeStore
import io.legado.app.utils.dpToPx
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlin.coroutines.CoroutineContext
Expand All @@ -34,6 +41,35 @@ abstract class BaseDialogFragment(
if (adaptationSoftKeyboard) {
dialog?.window?.setBackgroundDrawableResource(R.color.transparent)
}
if (AppConfig.isEInkMode) {
dialog?.window?.let {
it.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND)
val attr = it.attributes
attr.dimAmount = 0.0f
attr.windowAnimations = 0
it.attributes = attr
it.setBackgroundDrawableResource(R.color.transparent)
}
// 修改gravity的时机一般在子类的onStart方法中, 因此需要在onStart之后执行.
this.lifecycle.addObserver(object : LifecycleEventObserver {
override fun onStateChanged(
source: LifecycleOwner,
event: Lifecycle.Event
) {
if (event == Lifecycle.Event.ON_START) {
when (dialog?.window?.attributes?.gravity) {
Gravity.TOP -> view?.setBackgroundResource(R.drawable.bg_eink_border_bottom)
Gravity.BOTTOM -> view?.setBackgroundResource(R.drawable.bg_eink_border_top)
else -> {
val padding = 2.dpToPx();
view?.setPadding(padding, padding, padding, padding)
view?.setBackgroundResource(R.drawable.bg_eink_border_dialog)
}
}
}
}
})
}
}

override fun onCreate(savedInstanceState: Bundle?) {
Expand All @@ -49,7 +85,7 @@ abstract class BaseDialogFragment(
if (adaptationSoftKeyboard) {
view.findViewById<View>(R.id.vw_bg)?.setOnClickListener(null)
view.setOnClickListener { dismiss() }
} else {
} else if (!AppConfig.isEInkMode) {
view.setBackgroundColor(ThemeStore.backgroundColor())
}
onFragmentCreated(view, savedInstanceState)
Expand Down
50 changes: 50 additions & 0 deletions app/src/main/java/io/legado/app/base/BasePrefDialogFragment.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package io.legado.app.base

import android.view.Gravity
import android.view.WindowManager
import androidx.fragment.app.DialogFragment
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleEventObserver
import androidx.lifecycle.LifecycleOwner
import io.legado.app.R
import io.legado.app.help.config.AppConfig
import io.legado.app.utils.dpToPx


abstract class BasePrefDialogFragment(
) : DialogFragment() {

override fun onStart() {
super.onStart()
if (AppConfig.isEInkMode) {
dialog?.window?.let {
it.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND)
val attr = it.attributes
attr.dimAmount = 0.0f
attr.windowAnimations = 0
it.attributes = attr
it.setBackgroundDrawableResource(R.color.transparent)
}

// 修改gravity的时机一般在子类的onStart方法中, 因此需要在onStart之后执行.
this.lifecycle.addObserver(object : LifecycleEventObserver {
override fun onStateChanged(
source: LifecycleOwner,
event: Lifecycle.Event
) {
if (event == Lifecycle.Event.ON_START) {
when (dialog?.window?.attributes?.gravity) {
Gravity.TOP -> view?.setBackgroundResource(R.drawable.bg_eink_border_bottom)
Gravity.BOTTOM -> view?.setBackgroundResource(R.drawable.bg_eink_border_top)
else -> {
val padding = 2.dpToPx();
view?.setPadding(padding, padding, padding, padding)
view?.setBackgroundResource(R.drawable.bg_eink_border_dialog)
}
}
}
}
})
}
}
}
3 changes: 2 additions & 1 deletion app/src/main/java/io/legado/app/constant/AppConst.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import android.provider.Settings
import androidx.annotation.Keep
import cn.hutool.crypto.digest.DigestUtil
import io.legado.app.BuildConfig
import io.legado.app.help.config.AppConfig
import io.legado.app.help.update.AppVariant
import splitties.init.appCtx
import java.text.SimpleDateFormat
Expand Down Expand Up @@ -51,7 +52,7 @@ object AppConst {
)

@SuppressLint("PrivateResource")
val sysElevation = appCtx.resources
val sysElevation = if (AppConfig.isEInkMode) 0 else appCtx.resources
.getDimension(com.google.android.material.R.dimen.design_appbar_elevation)
.toInt()

Expand Down
5 changes: 4 additions & 1 deletion app/src/main/java/io/legado/app/help/config/AppConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,10 @@ object AppConfig : SharedPreferences.OnSharedPreferenceChangeListener {
}

var elevation: Int
get() = appCtx.getPrefInt(PreferKey.barElevation, AppConst.sysElevation)
get() = if (isEInkMode) 0 else appCtx.getPrefInt(
PreferKey.barElevation,
AppConst.sysElevation
)
set(value) {
appCtx.putPrefInt(PreferKey.barElevation, value)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@ object ReadBookConfig {
private var textColorNight: String = "#ADADAD",//夜间文字颜色
private var textColorEInk: String = "#000000",
private var pageAnim: Int = 0,//翻页动画
private var pageAnimEInk: Int = 3,
private var pageAnimEInk: Int = 4,
var textFont: String = "",//字体
var textBold: Int = 0,//是否粗体字 0:正常, 1:粗体, 2:细体
var textSize: Int = 20,//文字大小
Expand Down
20 changes: 18 additions & 2 deletions app/src/main/java/io/legado/app/lib/dialogs/AndroidAlertBuilder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import android.view.KeyEvent
import android.view.View
import androidx.appcompat.app.AlertDialog
import io.legado.app.utils.applyTint
import io.legado.app.R
import io.legado.app.help.config.AppConfig

internal class AndroidAlertBuilder(override val ctx: Context) : AlertBuilder<AlertDialog> {
private val builder = AlertDialog.Builder(ctx)
Expand Down Expand Up @@ -139,7 +141,21 @@ internal class AndroidAlertBuilder(override val ctx: Context) : AlertBuilder<Ale
}
}

override fun build(): AlertDialog = builder.create()
override fun build(): AlertDialog {
var dialog = builder.create()
if (AppConfig.isEInkMode) {
dialog.window?.setDimAmount(0f)
dialog.window?.decorView?.setBackgroundResource(R.drawable.bg_eink_border_dialog)
}
return dialog
}

override fun show(): AlertDialog = builder.show().applyTint()
override fun show(): AlertDialog {
var dialog = builder.show().applyTint()
if (AppConfig.isEInkMode) {
dialog.window?.setDimAmount(0f)
dialog.window?.decorView?.setBackgroundResource(R.drawable.bg_eink_border_dialog)
}
return dialog
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@ package io.legado.app.lib.prefs

import android.app.Dialog
import android.os.Bundle
import android.view.Gravity
import android.view.WindowManager
import androidx.appcompat.app.AlertDialog
import androidx.preference.EditTextPreferenceDialogFragmentCompat
import androidx.preference.PreferenceDialogFragmentCompat
import io.legado.app.R
import io.legado.app.help.config.AppConfig
import io.legado.app.lib.theme.accentColor
import io.legado.app.lib.theme.filletBackground
import io.legado.app.utils.dpToPx

class EditTextPreferenceDialog : EditTextPreferenceDialogFragmentCompat() {

Expand Down Expand Up @@ -35,4 +40,27 @@ class EditTextPreferenceDialog : EditTextPreferenceDialogFragmentCompat() {
return dialog
}

override fun onStart() {
super.onStart()
if (AppConfig.isEInkMode) {
dialog?.window?.let {
it.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND)
val attr = it.attributes
attr.dimAmount = 0.0f
attr.windowAnimations = 0
it.attributes = attr
it.setBackgroundDrawableResource(R.color.transparent)
when (attr.gravity) {
Gravity.TOP -> it.decorView.setBackgroundResource(R.drawable.bg_eink_border_bottom)
Gravity.BOTTOM -> it.decorView.setBackgroundResource(R.drawable.bg_eink_border_top)
else -> {
val padding = 2.dpToPx();
it.decorView.setPadding(padding, padding, padding, padding)
it.decorView.setBackgroundResource(R.drawable.bg_eink_border_dialog)
}
}
}
}
}

}
28 changes: 28 additions & 0 deletions app/src/main/java/io/legado/app/lib/prefs/ListPreferenceDialog.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@ package io.legado.app.lib.prefs

import android.app.Dialog
import android.os.Bundle
import android.view.Gravity
import android.view.WindowManager
import androidx.appcompat.app.AlertDialog
import androidx.core.view.forEach
import androidx.preference.ListPreferenceDialogFragmentCompat
import androidx.preference.PreferenceDialogFragmentCompat
import io.legado.app.R
import io.legado.app.help.config.AppConfig
import io.legado.app.lib.theme.accentColor
import io.legado.app.lib.theme.filletBackground
import io.legado.app.utils.applyTint
import io.legado.app.utils.dpToPx

class ListPreferenceDialog : ListPreferenceDialogFragmentCompat() {

Expand Down Expand Up @@ -40,4 +45,27 @@ class ListPreferenceDialog : ListPreferenceDialogFragmentCompat() {
return dialog
}

override fun onStart() {
super.onStart()
if (AppConfig.isEInkMode) {
dialog?.window?.let {
it.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND)
val attr = it.attributes
attr.dimAmount = 0.0f
attr.windowAnimations = 0
it.attributes = attr
it.setBackgroundDrawableResource(R.color.transparent)
when (attr.gravity) {
Gravity.TOP -> it.decorView.setBackgroundResource(R.drawable.bg_eink_border_bottom)
Gravity.BOTTOM -> it.decorView.setBackgroundResource(R.drawable.bg_eink_border_top)
else -> {
val padding = 2.dpToPx();
it.decorView.setPadding(padding, padding, padding, padding)
it.decorView.setBackgroundResource(R.drawable.bg_eink_border_dialog)
}
}
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@ package io.legado.app.lib.prefs

import android.app.Dialog
import android.os.Bundle
import android.view.Gravity
import android.view.WindowManager
import androidx.appcompat.app.AlertDialog
import androidx.core.view.forEach
import androidx.preference.MultiSelectListPreferenceDialogFragmentCompat
import androidx.preference.PreferenceDialogFragmentCompat
import io.legado.app.R
import io.legado.app.help.config.AppConfig
import io.legado.app.lib.theme.accentColor
import io.legado.app.lib.theme.filletBackground
import io.legado.app.utils.applyTint
import io.legado.app.utils.dpToPx

class MultiSelectListPreferenceDialog : MultiSelectListPreferenceDialogFragmentCompat() {

Expand Down Expand Up @@ -42,5 +47,27 @@ class MultiSelectListPreferenceDialog : MultiSelectListPreferenceDialogFragmentC
return dialog
}

override fun onStart() {
super.onStart()
if (AppConfig.isEInkMode) {
dialog?.window?.let {
it.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND)
val attr = it.attributes
attr.dimAmount = 0.0f
attr.windowAnimations = 0
it.attributes = attr
it.setBackgroundDrawableResource(R.color.transparent)
when (attr.gravity) {
Gravity.TOP -> it.decorView.setBackgroundResource(R.drawable.bg_eink_border_bottom)
Gravity.BOTTOM -> it.decorView.setBackgroundResource(R.drawable.bg_eink_border_top)
else -> {
val padding = 2.dpToPx();
it.decorView.setPadding(padding, padding, padding, padding)
it.decorView.setBackgroundResource(R.drawable.bg_eink_border_dialog)
}
}
}
}
}

}
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package io.legado.app.lib.theme.view

import android.content.Context
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.util.AttributeSet
import android.view.LayoutInflater
import com.google.android.material.bottomnavigation.BottomNavigationItemView
import com.google.android.material.bottomnavigation.BottomNavigationMenuView
import android.view.ViewGroup
import com.google.android.material.bottomnavigation.BottomNavigationView
import io.legado.app.databinding.ViewNavigationBadgeBinding
import io.legado.app.help.config.AppConfig
import io.legado.app.lib.theme.Selector
import io.legado.app.lib.theme.ThemeStore
import io.legado.app.lib.theme.bottomBackground
Expand All @@ -27,13 +29,18 @@ class ThemeBottomNavigationVIew(context: Context, attrs: AttributeSet) :
.setSelectedColor(ThemeStore.accentColor(context)).create()
itemIconTintList = colorStateList
itemTextColor = colorStateList

if (AppConfig.isEInkMode) {
isItemHorizontalTranslationEnabled = false
itemBackground = ColorDrawable(Color.TRANSPARENT)
}
}

fun addBadgeView(index: Int): BadgeView {
//获取底部菜单view
val menuView = getChildAt(0) as BottomNavigationMenuView
val menuView = getChildAt(0) as ViewGroup
//获取第index个itemView
val itemView = menuView.getChildAt(index) as BottomNavigationItemView
val itemView = menuView.getChildAt(index) as ViewGroup
val badgeBinding = ViewNavigationBadgeBinding.inflate(LayoutInflater.from(context))
itemView.addView(badgeBinding.root)
return badgeBinding.viewBadge
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import io.legado.app.R
import io.legado.app.base.BaseDialogFragment
import io.legado.app.constant.AppConst
import io.legado.app.constant.BookType
import io.legado.app.constant.EventBus
import io.legado.app.data.appDb
Expand Down Expand Up @@ -122,6 +123,7 @@ class ChangeBookSourceDialog() : BaseDialogFragment(R.layout.dialog_book_change_
binding.toolBar.subtitle = viewModel.author
binding.toolBar.navigationIcon =
getCompatDrawable(androidx.appcompat.R.drawable.abc_ic_ab_back_material)
binding.toolBar.elevation = AppConfig.elevation.toFloat()
}

private fun initMenu() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ class ChangeChapterSourceDialog() : BaseDialogFragment(R.layout.dialog_chapter_c
binding.ivHideToc.setOnClickListener {
binding.clToc.gone()
}
binding.flHideToc.elevation = requireContext().elevation
binding.flHideToc.elevation = if (AppConfig.isEInkMode) 0f else requireContext().elevation
}

private fun initRecyclerView() {
Expand Down
Loading

0 comments on commit b7db2a0

Please sign in to comment.