Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 37 additions & 10 deletions app/src/main/java/com/mangoloads/currentlens/DictionaryActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -221,17 +221,41 @@ class DictionaryActivity : ComponentActivity() {
window?.setBackgroundDrawableResource(android.R.color.transparent)
window?.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND)
window?.attributes = window?.attributes?.apply { dimAmount = 0.08f }
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
// Blur-behind samples the whole app window, unlike a blur confined to this popup.
window?.addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND)
window?.attributes = window?.attributes?.apply {
blurBehindRadius = (resources.displayMetrics.density * 24f).toInt()
}
}
window?.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT)
window?.decorView?.performHapticFeedback(HapticFeedbackConstants.CLOCK_TICK)
window?.setGravity(Gravity.BOTTOM)
setContent {
// Fail closed to an opaque, unblurred surface until the saved preference is
// restored. SharedPreferences is read on IO because it shares a file with
// the app's article and study data.
var reduceTransparency by remember { mutableStateOf(true) }
LaunchedEffect(Unit) {
val restored = withContext(Dispatchers.IO) {
LocalStore.reduceTransparency(applicationContext)
}
reduceTransparency = restored
val appearance = DictionarySurfacePolicy.resolve(
reduceTransparency = restored,
supportsBlurBehind = Build.VERSION.SDK_INT >= Build.VERSION_CODES.S
)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
if (appearance.blurBehindEnabled) {
// Blur-behind samples the whole app window, unlike a blur confined to this popup.
window?.addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND)
window?.attributes = window?.attributes?.apply {
blurBehindRadius = (resources.displayMetrics.density * 24f).toInt()
}
} else {
window?.clearFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND)
}
}
}
val surfaceAppearance = remember(reduceTransparency) {
DictionarySurfacePolicy.resolve(
reduceTransparency = reduceTransparency,
supportsBlurBehind = Build.VERSION.SDK_INT >= Build.VERSION_CODES.S
)
}
val dark = androidx.compose.foundation.isSystemInDarkTheme()
MaterialTheme(
colorScheme = if (dark) {
Expand All @@ -258,14 +282,17 @@ class DictionaryActivity : ComponentActivity() {
)
}
) {
DictionaryPopup(word.ifBlank { "Select one word" }) { finish() }
DictionaryPopup(
word = word.ifBlank { "Select one word" },
surfaceAlpha = surfaceAppearance.surfaceAlpha
) { finish() }
}
}
}
}

@Composable
private fun DictionaryPopup(word: String, onClose: () -> Unit) {
private fun DictionaryPopup(word: String, surfaceAlpha: Float, onClose: () -> Unit) {
val context = LocalContext.current
var result by remember(word) { mutableStateOf<Definition?>(null) }
var loading by remember(word) { mutableStateOf(true) }
Expand Down Expand Up @@ -309,7 +336,7 @@ private fun DictionaryPopup(word: String, onClose: () -> Unit) {
.then(contentSizeMotion),
shape = RoundedCornerShape(26.dp),
color = MaterialTheme.colorScheme.surface.copy(
alpha = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) 0.86f else 0.94f
alpha = surfaceAlpha
),
contentColor = MaterialTheme.colorScheme.onSurface,
tonalElevation = 0.dp,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.mangoloads.currentlens

/**
* Resolves the dictionary popup's surface treatment from the app's saved
* Reduce Transparency preference and the platform's blur-behind capability.
*
* The reduced mode is deliberately opaque and never requests window blur.
*/
internal data class DictionarySurfaceAppearance(
val blurBehindEnabled: Boolean,
val surfaceAlpha: Float
)

internal object DictionarySurfacePolicy {
const val REDUCED_TRANSPARENCY_ALPHA = 0.985f
const val MODERN_GLASS_ALPHA = 0.86f
const val LEGACY_SURFACE_ALPHA = 0.94f

fun resolve(
reduceTransparency: Boolean,
supportsBlurBehind: Boolean
): DictionarySurfaceAppearance = when {
reduceTransparency -> DictionarySurfaceAppearance(
blurBehindEnabled = false,
surfaceAlpha = REDUCED_TRANSPARENCY_ALPHA
)
supportsBlurBehind -> DictionarySurfaceAppearance(
blurBehindEnabled = true,
surfaceAlpha = MODERN_GLASS_ALPHA
)
else -> DictionarySurfaceAppearance(
blurBehindEnabled = false,
surfaceAlpha = LEGACY_SURFACE_ALPHA
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.mangoloads.currentlens

import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Test

class DictionarySurfacePolicyTest {
@Test
fun reducedTransparencyDisablesBlurAndUsesOpaqueSurfaceOnModernAndroid() {
val appearance = DictionarySurfacePolicy.resolve(
reduceTransparency = true,
supportsBlurBehind = true
)

assertFalse(appearance.blurBehindEnabled)
assertEquals(0.985f, appearance.surfaceAlpha, 0f)
}

@Test
fun normalAppearanceKeepsBlurBehindOnSupportedAndroid() {
val appearance = DictionarySurfacePolicy.resolve(
reduceTransparency = false,
supportsBlurBehind = true
)

assertTrue(appearance.blurBehindEnabled)
assertEquals(0.86f, appearance.surfaceAlpha, 0f)
}

@Test
fun normalAppearanceKeepsExistingOpaqueFallbackOnOlderAndroid() {
val appearance = DictionarySurfacePolicy.resolve(
reduceTransparency = false,
supportsBlurBehind = false
)

assertFalse(appearance.blurBehindEnabled)
assertEquals(0.94f, appearance.surfaceAlpha, 0f)
}

@Test
fun reducedTransparencyNeverEnablesBlurOnOlderAndroid() {
val appearance = DictionarySurfacePolicy.resolve(
reduceTransparency = true,
supportsBlurBehind = false
)

assertFalse(appearance.blurBehindEnabled)
assertEquals(0.985f, appearance.surfaceAlpha, 0f)
}
}
Loading