Add preference to adjust wallpaper blur radius

This commit is contained in:
MM20
2023-09-30 14:49:35 +02:00
parent 8fb83f9729
commit 5a821eaabe
12 changed files with 58 additions and 12 deletions
@@ -121,10 +121,6 @@ fun AssistantScaffold(
}
}
WallpaperBlur {
true
}
LaunchedEffect(darkNavBarIcons, showNavBarScrim) {
if (showNavBarScrim) {
systemUiController.setNavigationBarColor(
@@ -122,6 +122,10 @@ class LauncherScaffoldVM : ViewModel(), KoinComponent {
val wallpaperBlur = dataStore.data.map { it.appearance.blurWallpaper }
.stateIn(viewModelScope, SharingStarted.WhileSubscribed(), true)
val wallpaperBlurRadius = dataStore.data.map { it.appearance.blurWallpaperRadius }
.stateIn(viewModelScope, SharingStarted.WhileSubscribed(), 32)
val fillClockHeight = dataStore.data.map { it.clockWidget.fillHeight }
.stateIn(viewModelScope, SharingStarted.WhileSubscribed(), true)
val searchBarColor = dataStore.data.map { it.searchBar.color }
@@ -217,6 +217,7 @@ fun PagerScaffold(
}
val blurEnabled by viewModel.wallpaperBlur.collectAsState()
val blurRadius by viewModel.wallpaperBlurRadius.collectAsState()
val blurWallpaper by remember {
derivedStateOf {
@@ -225,7 +226,7 @@ fun PagerScaffold(
}
WallpaperBlur {
blurWallpaper
if (blurWallpaper) blurRadius else 0
}
val currentPage = pagerState.currentPage
@@ -211,6 +211,7 @@ fun PullDownScaffold(
val maxSearchBarOffset = with(density) { 128.dp.toPx() }
val blurEnabled by viewModel.wallpaperBlur.collectAsState()
val blurRadius by viewModel.wallpaperBlurRadius.collectAsState()
val blurWallpaper by remember {
derivedStateOf {
@@ -219,7 +220,7 @@ fun PullDownScaffold(
}
WallpaperBlur {
blurWallpaper
if (blurWallpaper) blurRadius else 0
}
@@ -10,15 +10,15 @@ import androidx.compose.ui.unit.dp
import de.mm20.launcher2.ktx.isAtLeastApiLevel
@Composable
fun WallpaperBlur(blur: () -> Boolean) {
fun WallpaperBlur(blurRadius: () -> Int) {
val context = LocalContext.current
val density = LocalDensity.current
val blurWallpaper = blur()
LaunchedEffect(blurWallpaper) {
val radius = blurRadius()
LaunchedEffect(radius) {
if (!isAtLeastApiLevel(31)) return@LaunchedEffect
(context as Activity).window.attributes = context.window.attributes.also {
if (blurWallpaper) {
it.blurBehindRadius = with(density) { 32.dp.toPx().toInt() }
if (radius > 0) {
it.blurBehindRadius = with(density) { radius.dp.toPx().toInt() }
it.flags = it.flags or WindowManager.LayoutParams.FLAG_BLUR_BEHIND
} else {
it.blurBehindRadius = 0
@@ -34,6 +34,7 @@ import de.mm20.launcher2.ui.component.preferences.ListPreference
import de.mm20.launcher2.ui.component.preferences.Preference
import de.mm20.launcher2.ui.component.preferences.PreferenceCategory
import de.mm20.launcher2.ui.component.preferences.PreferenceScreen
import de.mm20.launcher2.ui.component.preferences.SliderPreference
import de.mm20.launcher2.ui.component.preferences.SwitchPreference
import de.mm20.launcher2.ui.locals.LocalNavController
import kotlinx.coroutines.delay
@@ -57,6 +58,7 @@ fun HomescreenSettingsScreen() {
val lightStatusBar by viewModel.statusBarIcons.collectAsStateWithLifecycle(null)
val dimWallpaper by viewModel.dimWallpaper.collectAsStateWithLifecycle()
val blurWallpaper by viewModel.blurWallpaper.collectAsStateWithLifecycle()
val blurWallpaperRadius by viewModel.blurWallpaperRadius.collectAsStateWithLifecycle()
val lightNavBar by viewModel.navBarIcons.collectAsStateWithLifecycle(null)
val hideStatusBar by viewModel.hideStatusBar.collectAsStateWithLifecycle(null)
val hideNavBar by viewModel.hideNavBar.collectAsStateWithLifecycle(null)
@@ -172,6 +174,18 @@ fun HomescreenSettingsScreen() {
},
enabled = isBlurSupported
)
AnimatedVisibility(blurWallpaper && isBlurSupported) {
SliderPreference(
title = stringResource(R.string.preference_blur_wallpaper_radius),
value = blurWallpaperRadius,
onValueChanged = {
viewModel.setBlurWallpaperRadius(it)
},
min = 4,
max = 64,
step = 4,
)
}
}
}
item {
@@ -52,6 +52,20 @@ class HomescreenSettingsScreenVM(
}
}
val blurWallpaperRadius = dataStore.data.map { it.appearance.blurWallpaperRadius }
.stateIn(viewModelScope, SharingStarted.WhileSubscribed(), 32)
fun setBlurWallpaperRadius(blurWallpaperRadius: Int) {
viewModelScope.launch {
dataStore.updateData {
it.toBuilder()
.setAppearance(
it.appearance.toBuilder()
.setBlurWallpaperRadius(blurWallpaperRadius)
).build()
}
}
}
fun openWallpaperChooser(context: AppCompatActivity) {
context.startActivity(Intent.createChooser(Intent(Intent.ACTION_SET_WALLPAPER), null))
}