Przeglądaj źródła

feat:亮度实时调节

fansan 4 miesięcy temu
rodzic
commit
3ff86fd8a9

+ 23 - 4
app/src/main/java/io/legado/app/ui/book/read/ReadBookMenuBar.kt

@@ -1,7 +1,10 @@
 package io.legado.app.ui.book.read
 package io.legado.app.ui.book.read
 
 
+import android.app.Activity
 import android.content.Context
 import android.content.Context
+import android.content.ContextWrapper
 import android.os.Build
 import android.os.Build
+import androidx.activity.compose.LocalActivity
 import androidx.compose.animation.AnimatedContent
 import androidx.compose.animation.AnimatedContent
 import androidx.compose.animation.AnimatedVisibility
 import androidx.compose.animation.AnimatedVisibility
 import androidx.compose.animation.SizeTransform
 import androidx.compose.animation.SizeTransform
@@ -3004,6 +3007,7 @@ private fun BrightnessBar(
     buttonGlassEnabled: Boolean = false,
     buttonGlassEnabled: Boolean = false,
     glassThumbEnabled: Boolean = false,
     glassThumbEnabled: Boolean = false,
 ) {
 ) {
+    val activity = LocalActivity.current
     var sliderValue by remember { mutableFloatStateOf(brightness.toFloat()) }
     var sliderValue by remember { mutableFloatStateOf(brightness.toFloat()) }
     var sliderDragging by remember { mutableStateOf(false) }
     var sliderDragging by remember { mutableStateOf(false) }
 
 
@@ -3016,6 +3020,23 @@ private fun BrightnessBar(
         }
         }
     }
     }
 
 
+    fun updateSliderValue(value: Float) {
+        if (brightnessAuto) return
+        sliderDragging = true
+        sliderValue = value.coerceIn(0f, 100f)
+        val target = value.roundToInt().coerceIn(0, 100)
+
+        //直接先改亮度,如果在这里onBrightnessChange,会ANR
+        activity?.let { act ->
+            val lp = act.window.attributes
+            val targetBrightness = target / 100f
+            if (lp.screenBrightness != targetBrightness) {
+                lp.screenBrightness = targetBrightness
+                act.window.attributes = lp
+            }
+        }
+    }
+
     fun commitSliderValue(value: Float) {
     fun commitSliderValue(value: Float) {
         val target = value.roundToInt().coerceIn(0, 100)
         val target = value.roundToInt().coerceIn(0, 100)
         sliderDragging = false
         sliderDragging = false
@@ -3052,8 +3073,7 @@ private fun BrightnessBar(
                 value = sliderValue.coerceIn(0f, 100f),
                 value = sliderValue.coerceIn(0f, 100f),
                 onValueChange = { value ->
                 onValueChange = { value ->
                     if (brightnessAuto) return@AppVerticalSlider
                     if (brightnessAuto) return@AppVerticalSlider
-                    sliderDragging = true
-                    sliderValue = value.coerceIn(0f, 100f)
+                    updateSliderValue(value)
                 },
                 },
                 onValueChangeFinished = {
                 onValueChangeFinished = {
                     commitSliderValue(sliderValue)
                     commitSliderValue(sliderValue)
@@ -3095,8 +3115,7 @@ private fun BrightnessBar(
                 value = sliderValue.coerceIn(0f, 100f),
                 value = sliderValue.coerceIn(0f, 100f),
                 onValueChange = { value ->
                 onValueChange = { value ->
                     if (brightnessAuto) return@ReadMenuSlider
                     if (brightnessAuto) return@ReadMenuSlider
-                    sliderDragging = true
-                    sliderValue = value.coerceIn(0f, 100f)
+                    updateSliderValue(value)
                 },
                 },
                 onValueChangeFinished = {
                 onValueChangeFinished = {
                     commitSliderValue(sliderValue)
                     commitSliderValue(sliderValue)