From 68c62208ce666eb82738d5f2016de2f6cd0d5891 Mon Sep 17 00:00:00 2001 From: Horis <8674809+821938089@users.noreply.github.com> Date: Sat, 17 May 2025 11:15:01 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../legado/app/ui/widget/ReaderInfoBarView.kt | 73 ++++--------------- 1 file changed, 15 insertions(+), 58 deletions(-) diff --git a/app/src/main/java/io/legado/app/ui/widget/ReaderInfoBarView.kt b/app/src/main/java/io/legado/app/ui/widget/ReaderInfoBarView.kt index d9425b095..fdd3758bc 100644 --- a/app/src/main/java/io/legado/app/ui/widget/ReaderInfoBarView.kt +++ b/app/src/main/java/io/legado/app/ui/widget/ReaderInfoBarView.kt @@ -10,20 +10,15 @@ import android.graphics.Paint import android.graphics.Rect import android.util.AttributeSet import android.view.View -import android.view.WindowInsets import androidx.annotation.AttrRes import androidx.core.content.ContextCompat import androidx.core.content.res.use import androidx.core.graphics.ColorUtils -import androidx.core.view.ViewCompat import androidx.core.view.WindowInsetsCompat -import io.legado.app.R -import splitties.dimensions.dp -import java.text.DecimalFormat -import java.text.NumberFormat +import io.legado.app.utils.dpToPx +import io.legado.app.utils.setOnApplyWindowInsetsListenerCompat import java.text.SimpleDateFormat import java.util.Date -import java.util.Locale import kotlin.math.min import com.google.android.material.R as materialR @@ -74,13 +69,23 @@ class ReaderInfoBarView @JvmOverloads constructor( get() = width - paddingLeft - paddingRight - insetLeft - insetRight init { - val insetStart = dp(10f).toInt() - val insetEnd = dp(10f).toInt() - paint.strokeWidth = dp(2f) + val insetStart = 10.dpToPx() + val insetEnd = 10.dpToPx() + paint.strokeWidth = 2f.dpToPx() paint.setShadowLayer(2f, 1f, 1f, Color.GRAY) insetLeft = insetStart insetRight = insetEnd insetTop = minOf(insetLeft, insetRight) + setOnApplyWindowInsetsListenerCompat { _, windowInsets -> + val insets = windowInsets.getInsets(WindowInsetsCompat.Type.systemBars()) + if (insets.left >= paddingLeft) { + cutoutInsetLeft = insets.left + } + if (insets.right >= paddingRight) { + cutoutInsetRight = insets.right + } + windowInsets + } } override fun onDraw(canvas: Canvas) { @@ -119,15 +124,9 @@ class ReaderInfoBarView @JvmOverloads constructor( override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) { super.onSizeChanged(w, h, oldw, oldh) - updateCutoutInsets(ViewCompat.getRootWindowInsets(this)) updateTextSize() } - override fun onApplyWindowInsets(insets: WindowInsets): WindowInsets { - updateCutoutInsets(WindowInsetsCompat.toWindowInsetsCompat(insets)) - return super.onApplyWindowInsets(insets) - } - override fun onAttachedToWindow() { super.onAttachedToWindow() ContextCompat.registerReceiver( @@ -136,7 +135,6 @@ class ReaderInfoBarView @JvmOverloads constructor( IntentFilter(Intent.ACTION_TIME_TICK), ContextCompat.RECEIVER_EXPORTED, ) - updateCutoutInsets(ViewCompat.getRootWindowInsets(this)) } override fun onDetachedFromWindow() { @@ -150,33 +148,6 @@ class ReaderInfoBarView @JvmOverloads constructor( invalidate() } - private fun Number.format( - decimals: Int = 0, - decPoint: Char = '.', - thousandsSep: Char? = ' ' - ): String { - val formatter = NumberFormat.getInstance(Locale.US) as DecimalFormat - val symbols = formatter.decimalFormatSymbols - if (thousandsSep != null) { - symbols.groupingSeparator = thousandsSep - formatter.isGroupingUsed = true - } else { - formatter.isGroupingUsed = false - } - symbols.decimalSeparator = decPoint - formatter.decimalFormatSymbols = symbols - formatter.minimumFractionDigits = decimals - formatter.maximumFractionDigits = decimals - return when (this) { - is Float, - is Double, - -> formatter.format(this.toDouble()) - - else -> formatter.format(this.toLong()) - } - } - - private fun updateTextSize() { val testTextSize = 48f paint.textSize = testTextSize @@ -213,20 +184,6 @@ class ReaderInfoBarView @JvmOverloads constructor( drawText(text, x, y, paint) } - private fun updateCutoutInsets(insetsCompat: WindowInsetsCompat?) { - val cutouts = (insetsCompat ?: return).displayCutout?.boundingRects.orEmpty() - cutoutInsetLeft = 0 - cutoutInsetRight = 0 - for (rect in cutouts) { - if (rect.left <= paddingLeft) { - cutoutInsetLeft += rect.width() - } - if (rect.right >= width - paddingRight) { - cutoutInsetRight += rect.width() - } - } - } - private inner class TimeReceiver : BroadcastReceiver() { override fun onReceive(context: Context?, intent: Intent?) {