工具栏自定义与优化 #242、#243

This commit is contained in:
HapeLee
2025-11-08 02:05:02 +08:00
parent b2808c286c
commit 858ba70c09
20 changed files with 312 additions and 116 deletions
@@ -211,4 +211,5 @@ object PreferKey {
const val systemMediaControlCompatibilityChange = "systemMediaControlCompatibilityChange"
const val isPredictiveBackEnabled = "isPredictiveBackEnabled"
const val replaceSortMode = "desc"
const val readBarStyle = "0"
}
@@ -648,12 +648,19 @@ object AppConfig : SharedPreferences.OnSharedPreferenceChangeListener {
set(value) {
appCtx.putPrefBoolean(PreferKey.showReadTitleAddition, value)
}
var readBarStyleFollowPage: Boolean
get() = appCtx.getPrefBoolean(PreferKey.readBarStyleFollowPage, false)
set(value) {
appCtx.putPrefBoolean(PreferKey.readBarStyleFollowPage, value)
}
var readBarStyle: Int
get() = appCtx.getPrefInt(PreferKey.readBarStyle, 0)
set(value) {
appCtx.putPrefInt(PreferKey.readBarStyle, value)
}
var sourceEditMaxLine: Int
get() {
val maxLine = appCtx.getPrefInt(PreferKey.sourceEditMaxLine, Int.MAX_VALUE)
@@ -580,6 +580,10 @@ object ReadBookConfig {
var name: String = "",
var bgStr: String = "#EEEEEE",//白天背景
var bgStrNight: String = "#000000",//夜间背景
var menuBgColor: String = "#EEEFE3",
var menuAcColor: String = "#EEEFE3",
var menuBgColorNight: String = "#BFCBAD",
var menuAcColorNight: String = "#586249",
var bgStrEInk: String = "#FFFFFF",//EInk背景
var bgAlpha: Int = 100,//背景透明度
var bgType: Int = 0,//白天背景类型 0:颜色, 1:assets图片, 2其它图片
@@ -662,6 +666,18 @@ object ReadBookConfig {
@Transient
private var textColorSInt = -1
@Transient
private var menuBgColorInt = -1
@Transient
private var menuBgColorNightInt = -1
@Transient
private var menuAcColorInt = -1
@Transient
private var menuAcColorNightInt = -1
@Transient
private var initColorInt = false
@@ -671,6 +687,10 @@ object ReadBookConfig {
textColorInt = textColor.toColorInt()
textColorSIntNight = shadowColorN.toColorInt()
textColorSInt = shadowColor.toColorInt()
menuBgColorInt = menuBgColor.toColorInt()
menuBgColorNightInt = menuBgColorNight.toColorInt()
menuAcColorInt = menuAcColor.toColorInt()
menuAcColorNightInt = menuAcColorNight.toColorInt()
initColorInt = true
}
@@ -784,6 +804,48 @@ object ReadBookConfig {
}
}
fun curMenuBg(): Int {
return when {
AppConfig.isNightTheme -> menuBgColorNightInt
else -> menuBgColorInt
}
}
fun setMenuCurBg(bg: Int) {
when {
AppConfig.isNightTheme -> {
menuBgColorNight = "#${bg.hexString}"
menuBgColorNightInt = bg
}
else -> {
menuBgColor = "#${bg.hexString}"
menuBgColorInt = bg
}
}
}
fun curMenuAc(): Int {
return when {
AppConfig.isNightTheme -> menuAcColorNightInt
else -> menuAcColorInt
}
}
fun setMenuCurAc(bg: Int) {
when {
AppConfig.isNightTheme -> {
menuAcColorNight = "#${bg.hexString}"
menuAcColorNightInt = bg
}
else -> {
menuAcColor = "#${bg.hexString}"
menuAcColorInt = bg
}
}
}
fun curBgType(): Int {
return when {
AppConfig.isEInkMode -> bgTypeEInk
@@ -79,6 +79,8 @@ import io.legado.app.ui.book.read.config.BgTextConfigDialog.Companion.TEXT_COLOR
import io.legado.app.ui.book.read.config.FontSelectDialog.Companion.S_COLOR
import io.legado.app.ui.book.read.config.ReadAloudDialog
import io.legado.app.ui.book.read.config.ReadStyleDialog
import io.legado.app.ui.book.read.config.TipConfigDialog.Companion.A_COLOR
import io.legado.app.ui.book.read.config.TipConfigDialog.Companion.B_COLOR
import io.legado.app.ui.book.read.config.TipConfigDialog.Companion.TIP_COLOR
import io.legado.app.ui.book.read.config.TipConfigDialog.Companion.TIP_DIVIDER_COLOR
import io.legado.app.ui.book.read.config.ToolButtonConfigDialog
@@ -126,6 +128,7 @@ import io.legado.app.utils.showHelp
import io.legado.app.utils.startActivity
import io.legado.app.utils.startActivityForBook
import io.legado.app.utils.sysScreenOffTime
import io.legado.app.utils.themeColor
import io.legado.app.utils.throttle
import io.legado.app.utils.toastOnUi
import io.legado.app.utils.visible
@@ -278,13 +281,17 @@ class ReadBookActivity : BaseReadBookActivity(),
window.sharedElementReturnTransition = transform
}
super.onCreate(savedInstanceState)
if (AppConfig.sharedElementEnterTransitionEnable){
binding.rootView.transitionName = intent.getStringExtra("transitionName")
}
upScreenTimeOut()
ReadBook.register(this)
binding.readMenu.colorSurfaceContainer =
themeColor(com.google.android.material.R.attr.colorSurfaceContainer)
binding.readMenu.colorSecondary =
themeColor(androidx.appcompat.R.attr.colorPrimary)
binding.readMenu.colorSecondaryContainer =
themeColor(com.google.android.material.R.attr.colorSecondaryContainer)
binding.cursorLeft.setOnTouchListener(this)
binding.cursorRight.setOnTouchListener(this)
@@ -1538,6 +1545,16 @@ class ReadBookActivity : BaseReadBookActivity(),
postEvent(EventBus.TIP_COLOR, "")
postEvent(EventBus.UP_CONFIG, arrayListOf(2))
}
B_COLOR -> {
setMenuCurBg(color)
postEvent(EventBus.UPDATE_READ_ACTION_BAR, true)
}
A_COLOR -> {
setMenuCurAc(color)
postEvent(EventBus.UPDATE_READ_ACTION_BAR, true)
}
}
}
@@ -15,6 +15,7 @@ import android.widget.FrameLayout
import android.widget.SeekBar
import androidx.appcompat.widget.PopupMenu
import androidx.core.graphics.ColorUtils
import androidx.core.graphics.toColorInt
import androidx.core.view.doOnAttach
import androidx.core.view.isGone
import androidx.core.view.isVisible
@@ -27,6 +28,7 @@ import io.legado.app.constant.PreferKey
import io.legado.app.databinding.ViewReadMenuBinding
import io.legado.app.help.config.AppConfig
import io.legado.app.help.config.LocalConfig
import io.legado.app.help.config.ReadBookConfig
import io.legado.app.help.config.ThemeConfig
import io.legado.app.help.coroutine.Coroutine
import io.legado.app.help.source.getSourceType
@@ -87,25 +89,51 @@ class ReadMenu @JvmOverloads constructor(
fillAfter = true
}
// private val immersiveMenu: Boolean
// get() = AppConfig.readBarStyleFollowPage && ReadBookConfig.durConfig.curBgType() == 0
// private var bgColor: Int = if (immersiveMenu) {
// kotlin.runCatching {
// ReadBookConfig.durConfig.curBgStr().toColorInt()
// }.getOrDefault(context.bottomBackground)
// } else {
// context.bottomBackground
// }
// private var textColor: Int = if (immersiveMenu) {
// ReadBookConfig.durConfig.curTextColor()
// } else {
// context.getPrimaryTextColor(ColorUtils.isColorLight(bgColor))
// }
var colorSurfaceContainer: Int = context.themeColor(com.google.android.material.R.attr.colorSurfaceContainer)
set(value) {
field = value
}
var colorSecondary: Int = context.themeColor(androidx.appcompat.R.attr.colorPrimary)
set(value) {
field = value
}
var colorSecondaryContainer: Int = context.themeColor(com.google.android.material.R.attr.colorSecondaryContainer)
set(value) {
field = value
}
private val bgColor: Int
get() = when (AppConfig.readBarStyle) {
0 -> colorSurfaceContainer
1 -> kotlin.runCatching {
ReadBookConfig.durConfig.curBgStr().toColorInt()
}.getOrDefault(colorSurfaceContainer)
else -> ReadBookConfig.durConfig.curMenuBg()
}
private val acColor: Int
get() = when (AppConfig.readBarStyle) {
0 -> colorSecondary
1 -> kotlin.runCatching {
ReadBookConfig.durConfig.curTextColor()
}.getOrDefault(colorSecondary)
else -> ReadBookConfig.durConfig.curMenuAc()
}
private val bgcColor: Int
get() = when (AppConfig.readBarStyle) {
0 -> colorSecondaryContainer
1 -> kotlin.runCatching {
val baseColor = ReadBookConfig.durConfig.curTextColor()
ColorUtils.setAlphaComponent(baseColor, (255 * 0.2f).toInt())
}.getOrDefault(colorSecondaryContainer)
else -> ColorUtils.setAlphaComponent(acColor, (255 * 0.2f).toInt())
}
// private var bottomBackgroundList: ColorStateList = Selector.colorBuild()
// .setDefaultColor(bgColor)
// .setPressedColor(ColorUtils.darkenColor(bgColor))
// .create()
private var onMenuOutEnd: (() -> Unit)? = null
private val showBrightnessView
@@ -182,11 +210,10 @@ class ReadMenu @JvmOverloads constructor(
val brightnessBackground = GradientDrawable()
brightnessBackground.cornerRadius = 5F.dpToPx()
llBrightness.background = brightnessBackground
if (AppConfig.isEInkMode) {
titleBar.setBackgroundResource(R.drawable.bg_eink_border_bottom)
} else {
//llBottomBg.setBackgroundColor(bgColor)
}
// if (AppConfig.isEInkMode) {
// titleBar.setBackgroundResource(R.drawable.bg_eink_border_bottom)
// }
llBrightness.setOnClickListener(null)
seekBrightness.post {
@@ -202,9 +229,17 @@ class ReadMenu @JvmOverloads constructor(
renderButtons(binding.bottomView, allButtons)
}
val alpha = (AppConfig.menuAlpha / 100f * 255).toInt()
val color = context.themeColor(com.google.android.material.R.attr.colorSurfaceContainer)
titleBar.setBackgroundColor(ColorUtils.setAlphaComponent(color, alpha))
cdSlider.setCardBackgroundColor(ColorUtils.setAlphaComponent(color, alpha))
titleBar.setBackgroundColor(ColorUtils.setAlphaComponent(bgColor, alpha))
cdSlider.setCardBackgroundColor(ColorUtils.setAlphaComponent(bgColor, alpha))
seekReadPage.trackInactiveTintList = ColorStateList.valueOf(bgcColor)
seekReadPage.trackActiveTintList = ColorStateList.valueOf(acColor)
seekReadPage.thumbTintList = ColorStateList.valueOf(acColor)
seekReadPage.tickActiveTintList = ColorStateList.valueOf(bgColor)
seekReadPage.tickInactiveTintList = ColorStateList.valueOf(acColor)
tvPre.iconTint = ColorStateList.valueOf(acColor)
tvNext.iconTint = ColorStateList.valueOf(acColor)
tvPre.backgroundTintList = ColorStateList.valueOf(bgColor)
tvNext.backgroundTintList = ColorStateList.valueOf(bgColor)
tvPre.alpha = AppConfig.menuAlpha / 100f * 255
tvNext.alpha = AppConfig.menuAlpha / 100f * 255
upBrightnessVwPos()
@@ -214,7 +249,7 @@ class ReadMenu @JvmOverloads constructor(
if (resources.configuration.orientation == Configuration.ORIENTATION_LANDSCAPE) {
binding.bottomMenu.applyNavigationBarPadding()
} else {
bottomView.setBackgroundColor(ColorUtils.setAlphaComponent(color, alpha))
bottomView.setBackgroundColor(ColorUtils.setAlphaComponent(bgColor, alpha))
binding.bottomView.applyNavigationBarPadding()
}
}
@@ -516,8 +551,8 @@ class ReadMenu @JvmOverloads constructor(
tooltipText = btn.description
strokeWidth = 0
iconGravity = MaterialButton.ICON_GRAVITY_TEXT_START
iconTint = ColorStateList.valueOf(acColor)
maxLines = 1
iconTint = ColorStateList.valueOf(context.themeColor(androidx.appcompat.R.attr.colorPrimary))
setOnClickListener { btn.onClick() }
btn.onLongClick?.let { longAction ->
setOnLongClickListener {
@@ -10,6 +10,7 @@ import io.legado.app.base.BaseBottomSheetDialogFragment
import io.legado.app.constant.EventBus
import io.legado.app.databinding.DialogEditTextBinding
import io.legado.app.databinding.DialogTipConfigBinding
import io.legado.app.help.config.AppConfig
import io.legado.app.help.config.ReadBookConfig
import io.legado.app.help.config.ReadTipConfig
import io.legado.app.lib.dialogs.alert
@@ -27,6 +28,8 @@ class TipConfigDialog : BaseBottomSheetDialogFragment(R.layout.dialog_tip_config
companion object {
const val TIP_COLOR = 7897
const val TIP_DIVIDER_COLOR = 7898
const val B_COLOR = 114
const val A_COLOR = 514
}
private val binding by viewBinding(DialogTipConfigBinding::bind)
@@ -42,6 +45,10 @@ class TipConfigDialog : BaseBottomSheetDialogFragment(R.layout.dialog_tip_config
upTvTipColor()
upTvTipDividerColor()
}
observeEvent<Boolean>(EventBus.UPDATE_READ_ACTION_BAR) {
binding.abtnBackgroundColor.color = ReadBookConfig.durConfig.curMenuBg()
binding.abtnAccentColor.color = ReadBookConfig.durConfig.curMenuAc()
}
}
private fun initView() {
@@ -61,25 +68,47 @@ class TipConfigDialog : BaseBottomSheetDialogFragment(R.layout.dialog_tip_config
weightOptions?.let { options ->
items(options.toList()) { _, i ->
ReadBookConfig.titleBold = weightValues[i]
binding.sliderFontWeight.value =
ReadBookConfig.titleBold.toFloat().coerceAtLeast(100f)
binding.textFontWeightConverter.text = options.getOrNull(i) ?: ""
postEvent(EventBus.UP_CONFIG, arrayListOf(8, 9, 6))
}
}
}
}
binding.abtnBackgroundColor.color = ReadBookConfig.durConfig.curMenuBg()
binding.abtnAccentColor.color = ReadBookConfig.durConfig.curMenuAc()
binding.abtnBackgroundColor.setOnClickListener {
ColorPickerDialog.newBuilder()
.setColor(ReadBookConfig.durConfig.curMenuBg())
.setShowAlphaSlider(false)
.setDialogType(ColorPickerDialog.TYPE_CUSTOM)
.setDialogId(B_COLOR)
.show(requireActivity())
}
binding.sliderFontWeight.apply {
valueFrom = 100f
valueTo = 900f
stepSize = 1f
value = ReadBookConfig.titleBold.toFloat().coerceAtLeast(100f)
addOnChangeListener { _, newValue, _ ->
binding.textFontWeightConverter.text = "自定义"
ReadBookConfig.titleBold = newValue.toInt()
postEvent(EventBus.UP_CONFIG, arrayListOf(5))
binding.abtnAccentColor.setOnClickListener {
ColorPickerDialog.newBuilder()
.setColor(ReadBookConfig.durConfig.curMenuAc())
.setShowAlphaSlider(false)
.setDialogType(ColorPickerDialog.TYPE_CUSTOM)
.setDialogId(A_COLOR)
.show(requireActivity())
}
binding.bottomMode.check(
when (AppConfig.readBarStyle) {
0 -> R.id.bottom_mode1
1 -> R.id.bottom_mode2
else -> R.id.bottom_mode3
}
)
binding.bottomMode.setOnCheckedStateChangeListener { group, checkedIds ->
val checkedId = checkedIds.firstOrNull() ?: return@setOnCheckedStateChangeListener
AppConfig.readBarStyle = when (checkedId) {
R.id.bottom_mode1 -> 0
R.id.bottom_mode2 -> 1
R.id.bottom_mode3 -> 2
else -> 0
}
postEvent(EventBus.UPDATE_READ_ACTION_BAR, true)
}
binding.btnTitleSegType.setOnClickListener {
@@ -65,7 +65,7 @@ class TitleBar @JvmOverloads constructor(
val titleText = a.getString(R.styleable.TitleBar_title)
val subtitleText = a.getString(R.styleable.TitleBar_subtitle)
when (a.getInt(R.styleable.TitleBar_themeMode, 0)) {
when (a.getInt(R.styleable.TitleBar_isNavigationBackground, 0)) {
1 -> inflate(context, R.layout.view_title_bar_dark, this)
else -> inflate(context, R.layout.view_title_bar, this)
}