Add dark mode to search bar

This commit is contained in:
MM20
2022-10-21 18:07:09 +02:00
parent 02bb12549b
commit 6859948bd8
6 changed files with 51 additions and 2 deletions
@@ -44,12 +44,14 @@ import coil.compose.AsyncImage
import de.mm20.launcher2.ktx.tryStartActivity
import de.mm20.launcher2.preferences.LauncherDataStore
import de.mm20.launcher2.preferences.Settings.SearchBarSettings
import de.mm20.launcher2.preferences.Settings.SearchBarSettings.SearchBarColors
import de.mm20.launcher2.search.data.Websearch
import de.mm20.launcher2.ui.R
import de.mm20.launcher2.ui.component.LauncherCard
import de.mm20.launcher2.ui.launcher.LauncherActivityVM
import de.mm20.launcher2.ui.layout.BottomReversed
import de.mm20.launcher2.ui.locals.LocalCardStyle
import de.mm20.launcher2.ui.locals.LocalPreferDarkContentOverWallpaper
import de.mm20.launcher2.ui.settings.SettingsActivity
import kotlinx.coroutines.awaitCancellation
import kotlinx.coroutines.flow.map
@@ -72,6 +74,9 @@ fun SearchBar(
val style by remember { dataStore.data.map { it.searchBar.searchBarStyle } }
.collectAsState(SearchBarSettings.SearchBarStyle.Hidden)
val color by remember { dataStore.data.map { it.searchBar.color } }
.collectAsState(SearchBarSettings.SearchBarColors.Auto)
val focusManager = LocalFocusManager.current
val focusRequester = remember { FocusRequester() }
@@ -135,7 +140,8 @@ fun SearchBar(
onUnfocus = {
onFocusChange(false)
},
reverse = reverse
reverse = reverse,
darkColors = color == SearchBarColors.Dark || color == SearchBarColors.Auto && LocalPreferDarkContentOverWallpaper.current
)
}
@@ -152,6 +158,7 @@ fun SearchBar(
onUnfocus: () -> Unit = {},
focusRequester: FocusRequester = remember { FocusRequester() },
reverse: Boolean = false,
darkColors: Boolean = false,
) {
val context = LocalContext.current
@@ -212,7 +219,7 @@ fun SearchBar(
}) {
when {
style != SearchBarSettings.SearchBarStyle.Transparent -> MaterialTheme.colorScheme.onSurface
it == SearchBarLevel.Resting -> Color.White
it == SearchBarLevel.Resting -> if (darkColors) Color(0,0,0, 180) else Color.White
else -> MaterialTheme.colorScheme.onSurface
}
}
@@ -2,6 +2,7 @@ package de.mm20.launcher2.ui.settings.appearance
import android.graphics.drawable.ColorDrawable
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.grid.GridCells
@@ -33,6 +34,8 @@ import de.mm20.launcher2.ktx.isAtLeastApiLevel
import de.mm20.launcher2.preferences.Settings.*
import de.mm20.launcher2.preferences.Settings.AppearanceSettings.ColorScheme
import de.mm20.launcher2.preferences.Settings.AppearanceSettings.Theme
import de.mm20.launcher2.preferences.Settings.SearchBarSettings.SearchBarColors
import de.mm20.launcher2.preferences.Settings.SearchBarSettings.SearchBarStyle
import de.mm20.launcher2.preferences.Settings.SystemBarsSettings.SystemBarColors
import de.mm20.launcher2.ui.R
import de.mm20.launcher2.ui.component.ShapedLauncherIcon
@@ -250,6 +253,21 @@ fun AppearanceSettingsScreen() {
viewModel.setSearchBarStyle(it)
}
)
AnimatedVisibility(searchBarStyle == SearchBarStyle.Transparent) {
val searchBarColor by viewModel.searchBarColor.observeAsState()
ListPreference(
title = stringResource(R.string.preference_search_bar_color),
value = searchBarColor,
items = listOf(
stringResource(R.string.preference_system_bar_icons_auto) to SearchBarColors.Auto,
stringResource(R.string.preference_system_bar_icons_light) to SearchBarColors.Light,
stringResource(R.string.preference_system_bar_icons_dark) to SearchBarColors.Dark,
),
onValueChanged = {
if (it != null) viewModel.setSearchBarColor(it)
}
)
}
}
PreferenceCategory(stringResource(R.string.preference_category_system_bars)) {
val lightStatusBar by viewModel.statusBarIcons.observeAsState()
@@ -13,6 +13,7 @@ import de.mm20.launcher2.preferences.LauncherDataStore
import de.mm20.launcher2.preferences.Settings
import de.mm20.launcher2.preferences.Settings.AppearanceSettings.*
import de.mm20.launcher2.preferences.Settings.SearchBarSettings
import de.mm20.launcher2.preferences.Settings.SearchBarSettings.SearchBarColors
import de.mm20.launcher2.preferences.Settings.SystemBarsSettings
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.launch
@@ -218,6 +219,20 @@ class AppearanceSettingsScreenVM : ViewModel(), KoinComponent {
}
}
val searchBarColor = dataStore.data.map { it.searchBar.color }.asLiveData()
fun setSearchBarColor(color: SearchBarColors) {
viewModelScope.launch {
dataStore.updateData {
it.toBuilder()
.setSearchBar(
it.searchBar.toBuilder()
.setColor(color)
)
.build()
}
}
}
val statusBarIcons = dataStore.data.map { it.systemBars.statusBarColor }.asLiveData()
fun setLightStatusBar(statusBarColor: SystemBarsSettings.SystemBarColors) {
viewModelScope.launch {