This commit is contained in:
Koitharu
2022-08-09 13:31:19 +03:00
parent 6e71f20470
commit 905c4b362c
28 changed files with 302 additions and 67 deletions

View File

@@ -15,14 +15,11 @@ import android.net.Uri
import android.os.Build
import android.provider.Settings
import android.view.View
import android.view.ViewGroup
import android.view.ViewPropertyAnimator
import android.view.Window
import androidx.activity.result.ActivityResultLauncher
import androidx.annotation.IntegerRes
import androidx.core.app.ActivityOptionsCompat
import androidx.core.view.children
import androidx.core.view.descendants
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.coroutineScope
import androidx.work.CoroutineWorker
@@ -104,7 +101,7 @@ fun SyncResult.onError(error: Throwable) {
error.printStackTraceDebug()
}
fun Window.setNavigationBarTransparentCompat(context: Context, elevation: Float = 0F) {
fun Window.setNavigationBarTransparentCompat(context: Context, elevation: Float, alphaFactor: Float = 0.7f) {
navigationBarColor = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q &&
!InternalResourceHelper.getBoolean(context, "config_navBarNeedsScrim", true)
) {
@@ -112,7 +109,7 @@ fun Window.setNavigationBarTransparentCompat(context: Context, elevation: Float
} else {
// Set navbar scrim 70% of navigationBarColor
ElevationOverlayProvider(context).compositeOverlayIfNeeded(
context.getResourceColor(android.R.attr.navigationBarColor, 0.7F),
context.getThemeColor(android.R.attr.navigationBarColor, alphaFactor),
elevation,
)
}

View File

@@ -1,15 +1,7 @@
package org.koitharu.kotatsu.utils.ext
import android.content.Context
import android.content.res.Resources
import android.graphics.Color
import androidx.annotation.AttrRes
import androidx.annotation.ColorInt
import androidx.annotation.Px
import androidx.core.graphics.alpha
import androidx.core.graphics.blue
import androidx.core.graphics.green
import androidx.core.graphics.red
import kotlin.math.roundToInt
@Px
@@ -17,17 +9,3 @@ fun Resources.resolveDp(dp: Int) = (dp * displayMetrics.density).roundToInt()
@Px
fun Resources.resolveDp(dp: Float) = dp * displayMetrics.density
@ColorInt
fun Context.getResourceColor(@AttrRes resource: Int, alphaFactor: Float = 1f): Int {
val typedArray = obtainStyledAttributes(intArrayOf(resource))
val color = typedArray.getColor(0, 0)
typedArray.recycle()
if (alphaFactor < 1f) {
val alpha = (color.alpha * alphaFactor).roundToInt()
return Color.argb(alpha, color.red, color.green, color.blue)
}
return color
}

View File

@@ -4,7 +4,9 @@ import android.content.Context
import android.graphics.Color
import androidx.annotation.AttrRes
import androidx.annotation.ColorInt
import androidx.annotation.FloatRange
import androidx.core.content.res.use
import androidx.core.graphics.ColorUtils
fun Context.getThemeDrawable(
@AttrRes resId: Int,
@@ -15,13 +17,29 @@ fun Context.getThemeDrawable(
@ColorInt
fun Context.getThemeColor(
@AttrRes resId: Int,
@ColorInt default: Int = Color.TRANSPARENT
@ColorInt fallback: Int = Color.TRANSPARENT,
) = obtainStyledAttributes(intArrayOf(resId)).use {
it.getColor(0, default)
it.getColor(0, fallback)
}
@ColorInt
fun Context.getThemeColor(
@AttrRes resId: Int,
@FloatRange(from = 0.0, to = 1.0) alphaFactor: Float,
@ColorInt fallback: Int = Color.TRANSPARENT,
): Int {
if (alphaFactor <= 0f) {
return Color.TRANSPARENT
}
val color = getThemeColor(resId, fallback)
if (alphaFactor >= 1f) {
return color
}
return ColorUtils.setAlphaComponent(color, (0xFF * alphaFactor).toInt())
}
fun Context.getThemeColorStateList(
@AttrRes resId: Int,
) = obtainStyledAttributes(intArrayOf(resId)).use {
it.getColorStateList(0)
}
}