Add dark mode to search bar

This commit is contained in:
MM20 2022-10-21 18:07:09 +02:00
parent 02bb12549b
commit 6859948bd8
No known key found for this signature in database
GPG Key ID: 0B61A8F2DEAFA389
6 changed files with 51 additions and 2 deletions

View File

@ -617,6 +617,7 @@
<string name="preference_music_filter_sources_summary">Ignore media sessions of apps that are not music apps</string>
<string name="preference_search_bar_style">Style</string>
<string name="preference_search_bar_style_summary">Customize search bar appearance</string>
<string name="preference_search_bar_color">Color</string>
<string name="preference_search_bar_auto_focus">Open keyboard</string>
<string name="preference_search_bar_auto_focus_summary">Automatically show the keyboard when opening the search</string>
<string name="preference_hidden_items">Hidden search results</string>

View File

@ -2,6 +2,7 @@ package de.mm20.launcher2.preferences
import android.content.Context
import android.graphics.Color
import de.mm20.launcher2.preferences.Settings.SearchBarSettings.SearchBarColors
import scheme.Scheme
fun createFactorySettings(context: Context): Settings {
@ -133,6 +134,7 @@ fun createFactorySettings(context: Context): Settings {
Settings.SearchBarSettings.newBuilder()
.setSearchBarStyle(Settings.SearchBarSettings.SearchBarStyle.Transparent)
.setAutoFocus(false)
.setColor(SearchBarColors.Auto)
.build()
)
.setIcons(

View File

@ -216,6 +216,12 @@ message Settings {
}
SearchBarStyle search_bar_style = 1;
bool auto_focus = 2;
enum SearchBarColors {
Auto = 0;
Light = 1;
Dark = 2;
}
SearchBarColors color = 3;
}
SearchBarSettings search_bar = 20;

View File

@ -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
}
}

View File

@ -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()

View File

@ -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 {