Only update color scheme after color picker has been closed

This commit is contained in:
MM20 2023-08-23 21:23:21 +02:00
parent bf9035bb2c
commit ebee33ecf0
No known key found for this signature in database
GPG Key ID: 0B61A8F2DEAFA389
3 changed files with 44 additions and 47 deletions

View File

@ -54,20 +54,24 @@ fun CorePaletteColorPreference(
} }
if (showDialog) { if (showDialog) {
BottomSheetDialog(onDismissRequest = { showDialog = false }) { var currentValue by remember { mutableStateOf(value) }
BottomSheetDialog(onDismissRequest = {
onValueChange(currentValue)
showDialog = false
}) {
Column( Column(
modifier = Modifier.padding(it) modifier = Modifier.padding(it)
) { ) {
SwitchPreference( SwitchPreference(
icon = Icons.Rounded.SettingsSuggest, icon = Icons.Rounded.SettingsSuggest,
title = "Use system default", title = "Use system default",
value = value == null, value = currentValue == null,
onValueChanged = { onValueChanged = {
onValueChange(if (it) null else defaultValue) currentValue = if (it) null else defaultValue
} }
) )
AnimatedVisibility( AnimatedVisibility(
value != null, currentValue != null,
enter = expandVertically( enter = expandVertically(
expandFrom = Alignment.Top, expandFrom = Alignment.Top,
), ),
@ -82,7 +86,7 @@ fun CorePaletteColorPreference(
val colorPickerState = rememberHctColorPickerState( val colorPickerState = rememberHctColorPickerState(
initialColor = Color(value ?: defaultValue), initialColor = Color(value ?: defaultValue),
onColorChanged = { onColorChanged = {
onValueChange(it.toArgb()) currentValue = it.toArgb()
} }
) )
HctColorPicker( HctColorPicker(
@ -104,7 +108,7 @@ fun CorePaletteColorPreference(
contentPadding = ButtonDefaults.TextButtonWithIconContentPadding, contentPadding = ButtonDefaults.TextButtonWithIconContentPadding,
onClick = { onClick = {
val autoGenerated = autoGenerate() val autoGenerated = autoGenerate()
onValueChange(autoGenerated) currentValue = autoGenerated
if (autoGenerated != null) { if (autoGenerated != null) {
colorPickerState.setColor(Color(autoGenerated)) colorPickerState.setColor(Color(autoGenerated))
} }

View File

@ -45,7 +45,6 @@ import androidx.compose.ui.graphics.drawscope.Fill
import androidx.compose.ui.graphics.toArgb import androidx.compose.ui.graphics.toArgb
import androidx.compose.ui.text.style.TextAlign import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import de.mm20.launcher2.themes.Color as ThemeColor
import de.mm20.launcher2.themes.ColorRef import de.mm20.launcher2.themes.ColorRef
import de.mm20.launcher2.themes.CorePaletteColor import de.mm20.launcher2.themes.CorePaletteColor
import de.mm20.launcher2.themes.FullCorePalette import de.mm20.launcher2.themes.FullCorePalette
@ -58,6 +57,7 @@ import de.mm20.launcher2.ui.component.colorpicker.rememberHctColorPickerState
import de.mm20.launcher2.ui.ktx.hct import de.mm20.launcher2.ui.ktx.hct
import hct.Hct import hct.Hct
import kotlin.math.roundToInt import kotlin.math.roundToInt
import de.mm20.launcher2.themes.Color as ThemeColor
@Composable @Composable
fun ThemeColorPreference( fun ThemeColorPreference(
@ -70,11 +70,9 @@ fun ThemeColorPreference(
) { ) {
var showDialog by remember { mutableStateOf(false) } var showDialog by remember { mutableStateOf(false) }
val actualValue = value ?: defaultValue
PlainTooltipBox(tooltip = { Text(title) }) { PlainTooltipBox(tooltip = { Text(title) }) {
ColorSwatch( ColorSwatch(
color = Color(actualValue.get(corePalette)), color = Color((value ?: defaultValue).get(corePalette)),
modifier = modifier modifier = modifier
.size(48.dp) .size(48.dp)
.tooltipTrigger() .tooltipTrigger()
@ -83,7 +81,13 @@ fun ThemeColorPreference(
} }
if (showDialog) { if (showDialog) {
BottomSheetDialog(onDismissRequest = { showDialog = false }) { var currentValue by remember { mutableStateOf(value) }
val actualValue = currentValue ?: defaultValue
BottomSheetDialog(onDismissRequest = {
onValueChange(currentValue)
showDialog = false
}) {
Column( Column(
modifier = Modifier modifier = Modifier
.fillMaxWidth() .fillMaxWidth()
@ -96,7 +100,7 @@ fun ThemeColorPreference(
selected = actualValue is ColorRef, selected = actualValue is ColorRef,
onClick = { onClick = {
if (actualValue is ColorRef) return@SegmentedButton if (actualValue is ColorRef) return@SegmentedButton
onValueChange(defaultValue) currentValue = defaultValue
}, },
icon = { icon = {
SegmentedButtonDefaults.SegmentedButtonIcon( SegmentedButtonDefaults.SegmentedButtonIcon(
@ -117,7 +121,7 @@ fun ThemeColorPreference(
SegmentedButton( SegmentedButton(
selected = actualValue is StaticColor, selected = actualValue is StaticColor,
onClick = { onClick = {
onValueChange(StaticColor(actualValue.get(corePalette))) currentValue = StaticColor(actualValue.get(corePalette))
}, },
icon = { icon = {
SegmentedButtonDefaults.SegmentedButtonIcon( SegmentedButtonDefaults.SegmentedButtonIcon(
@ -146,7 +150,7 @@ fun ThemeColorPreference(
val colorPickerState = rememberHctColorPickerState( val colorPickerState = rememberHctColorPickerState(
initialColor = Color(themeColor.color), initialColor = Color(themeColor.color),
onColorChanged = { onColorChanged = {
onValueChange(StaticColor(it.toArgb())) currentValue = StaticColor(it.toArgb())
} }
) )
HctColorPicker( HctColorPicker(
@ -174,11 +178,9 @@ fun ThemeColorPreference(
.padding(8.dp) .padding(8.dp)
.size(64.dp) .size(64.dp)
.clickable { .clickable {
onValueChange( currentValue = ColorRef(
ColorRef( CorePaletteColor.Primary,
CorePaletteColor.Primary, tone.roundToInt()
tone.roundToInt()
)
) )
}, },
selected = themeColor.color == CorePaletteColor.Primary, selected = themeColor.color == CorePaletteColor.Primary,
@ -194,11 +196,9 @@ fun ThemeColorPreference(
.padding(8.dp) .padding(8.dp)
.size(64.dp) .size(64.dp)
.clickable { .clickable {
onValueChange( currentValue = ColorRef(
ColorRef( CorePaletteColor.Secondary,
CorePaletteColor.Secondary, tone.roundToInt()
tone.roundToInt()
)
) )
}, },
selected = themeColor.color == CorePaletteColor.Secondary, selected = themeColor.color == CorePaletteColor.Secondary,
@ -214,11 +214,9 @@ fun ThemeColorPreference(
.padding(8.dp) .padding(8.dp)
.size(64.dp) .size(64.dp)
.clickable { .clickable {
onValueChange( currentValue = ColorRef(
ColorRef( CorePaletteColor.Tertiary,
CorePaletteColor.Tertiary, tone.roundToInt()
tone.roundToInt()
)
) )
}, },
selected = themeColor.color == CorePaletteColor.Tertiary, selected = themeColor.color == CorePaletteColor.Tertiary,
@ -237,11 +235,9 @@ fun ThemeColorPreference(
.padding(8.dp) .padding(8.dp)
.size(64.dp) .size(64.dp)
.clickable { .clickable {
onValueChange( currentValue = ColorRef(
ColorRef( CorePaletteColor.Neutral,
CorePaletteColor.Neutral, tone.roundToInt()
tone.roundToInt()
)
) )
}, },
selected = themeColor.color == CorePaletteColor.Neutral, selected = themeColor.color == CorePaletteColor.Neutral,
@ -257,11 +253,9 @@ fun ThemeColorPreference(
.padding(8.dp) .padding(8.dp)
.size(64.dp) .size(64.dp)
.clickable { .clickable {
onValueChange( currentValue = ColorRef(
ColorRef( CorePaletteColor.NeutralVariant,
CorePaletteColor.NeutralVariant, tone.roundToInt()
tone.roundToInt()
)
) )
}, },
selected = themeColor.color == CorePaletteColor.NeutralVariant, selected = themeColor.color == CorePaletteColor.NeutralVariant,
@ -277,11 +271,9 @@ fun ThemeColorPreference(
.padding(8.dp) .padding(8.dp)
.size(64.dp) .size(64.dp)
.clickable { .clickable {
onValueChange( currentValue = ColorRef(
ColorRef( CorePaletteColor.Error,
CorePaletteColor.Error, tone.roundToInt()
tone.roundToInt()
)
) )
}, },
selected = themeColor.color == CorePaletteColor.Error, selected = themeColor.color == CorePaletteColor.Error,
@ -305,7 +297,7 @@ fun ThemeColorPreference(
valueRange = 0f..100f, valueRange = 0f..100f,
onValueChange = { onValueChange = {
tone = it tone = it
onValueChange(themeColor.copy(tone = it.roundToInt())) currentValue = themeColor.copy(tone = it.roundToInt())
}, },
track = { track = {
Canvas( Canvas(
@ -409,7 +401,7 @@ fun ThemeColorPreference(
.padding(top = 8.dp) .padding(top = 8.dp)
.align(Alignment.End), .align(Alignment.End),
contentPadding = ButtonDefaults.TextButtonWithIconContentPadding, contentPadding = ButtonDefaults.TextButtonWithIconContentPadding,
onClick = { onValueChange(null) } onClick = { currentValue = null }
) { ) {
Icon( Icon(
Icons.Rounded.RestartAlt, null, Icons.Rounded.RestartAlt, null,

View File

@ -10,6 +10,7 @@ import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.combine import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.flowOf import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.map
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import java.util.UUID import java.util.UUID
@ -29,7 +30,7 @@ class ThemeRepository(
fun getTheme(id: UUID): Flow<Theme?> { fun getTheme(id: UUID): Flow<Theme?> {
if (id == DefaultThemeId) return flowOf(getDefaultTheme()) if (id == DefaultThemeId) return flowOf(getDefaultTheme())
if (id == BlackAndWhiteThemeId) return flowOf(getBlackAndWhiteTheme()) if (id == BlackAndWhiteThemeId) return flowOf(getBlackAndWhiteTheme())
return database.themeDao().get(id).map { it?.let { Theme(it) } } return database.themeDao().get(id).map { it?.let { Theme(it) } }.flowOn(Dispatchers.Default)
} }
fun createTheme(theme: Theme) { fun createTheme(theme: Theme) {