From 6d5ad8cac223940b687b19226dfa7aa42af1fd6e Mon Sep 17 00:00:00 2001 From: MM20 <15646950+MM2-0@users.noreply.github.com> Date: Fri, 10 Jun 2022 20:01:52 +0200 Subject: [PATCH] Add preference for corner shape --- i18n/src/main/res/values/strings.xml | 3 ++ preferences/src/main/proto/settings.proto | 5 +++ .../ui/settings/cards/CardsSettingsScreen.kt | 42 +++++++++++++------ .../settings/cards/CardsSettingsScreenVM.kt | 13 ++++++ .../mm20/launcher2/ui/theme/LauncherTheme.kt | 21 +++++++--- 5 files changed, 66 insertions(+), 18 deletions(-) diff --git a/i18n/src/main/res/values/strings.xml b/i18n/src/main/res/values/strings.xml index ab1a4e1a..c58fac83 100644 --- a/i18n/src/main/res/values/strings.xml +++ b/i18n/src/main/res/values/strings.xml @@ -433,6 +433,9 @@ Corner radius Stroke width Opacity + Shape + Rounded + Cut Shape System default Square diff --git a/preferences/src/main/proto/settings.proto b/preferences/src/main/proto/settings.proto index 135ce0df..d0253a2f 100644 --- a/preferences/src/main/proto/settings.proto +++ b/preferences/src/main/proto/settings.proto @@ -233,6 +233,11 @@ message Settings { float opacity = 1; uint32 radius = 2; uint32 border_width = 3; + Shape shape = 4; + enum Shape { + Rounded = 0; + Cut = 1; + } } CardSettings cards = 24; diff --git a/ui/src/main/java/de/mm20/launcher2/ui/settings/cards/CardsSettingsScreen.kt b/ui/src/main/java/de/mm20/launcher2/ui/settings/cards/CardsSettingsScreen.kt index dfe889fa..49fcaf0a 100644 --- a/ui/src/main/java/de/mm20/launcher2/ui/settings/cards/CardsSettingsScreen.kt +++ b/ui/src/main/java/de/mm20/launcher2/ui/settings/cards/CardsSettingsScreen.kt @@ -18,8 +18,10 @@ import androidx.compose.ui.Modifier import androidx.compose.ui.res.stringResource import androidx.compose.ui.unit.dp import androidx.lifecycle.viewmodel.compose.viewModel +import de.mm20.launcher2.preferences.Settings import de.mm20.launcher2.ui.R import de.mm20.launcher2.ui.component.LauncherCard +import de.mm20.launcher2.ui.component.preferences.ListPreference import de.mm20.launcher2.ui.component.preferences.PreferenceCategory import de.mm20.launcher2.ui.component.preferences.PreferenceScreen import de.mm20.launcher2.ui.component.preferences.SliderPreference @@ -29,9 +31,11 @@ fun CardsSettingsScreen() { val viewModel: CardsSettingsScreenVM = viewModel() PreferenceScreen(title = stringResource(R.string.preference_cards)) { item { - Box(modifier = Modifier - .fillMaxWidth() - .background(MaterialTheme.colorScheme.secondary)) { + Box( + modifier = Modifier + .fillMaxWidth() + .background(MaterialTheme.colorScheme.secondary) + ) { LauncherCard( modifier = Modifier .height(200.dp) @@ -42,17 +46,18 @@ fun CardsSettingsScreen() { } item { PreferenceCategory { - val opacity by viewModel.opacity.observeAsState(0f) - SliderPreference( - title = stringResource(R.string.preference_cards_opacity), - icon = Icons.Rounded.Opacity, - value = opacity, - min = 0f, - max = 1f, + val shape by viewModel.shape.observeAsState() + ListPreference( + icon = Icons.Rounded.BorderStyle, + title = stringResource(R.string.preference_cards_shape), + items = listOf( + stringResource(R.string.preference_cards_shape_rounded) to Settings.CardSettings.Shape.Rounded, + stringResource(R.string.preference_cards_shape_cut) to Settings.CardSettings.Shape.Cut, + ), + value = shape, onValueChanged = { - viewModel.setOpacity(it) - } - ) + if (it != null) viewModel.setShape(it) + }) val radius by viewModel.radius.observeAsState(0) SliderPreference( title = stringResource(R.string.preference_cards_corner_radius), @@ -65,6 +70,17 @@ fun CardsSettingsScreen() { viewModel.setRadius(it) } ) + val opacity by viewModel.opacity.observeAsState(0f) + SliderPreference( + title = stringResource(R.string.preference_cards_opacity), + icon = Icons.Rounded.Opacity, + value = opacity, + min = 0f, + max = 1f, + onValueChanged = { + viewModel.setOpacity(it) + } + ) val borderWidth by viewModel.borderWidth.observeAsState(0) SliderPreference( title = stringResource(R.string.preference_cards_stroke_width), diff --git a/ui/src/main/java/de/mm20/launcher2/ui/settings/cards/CardsSettingsScreenVM.kt b/ui/src/main/java/de/mm20/launcher2/ui/settings/cards/CardsSettingsScreenVM.kt index 41573b71..9c5e46d1 100644 --- a/ui/src/main/java/de/mm20/launcher2/ui/settings/cards/CardsSettingsScreenVM.kt +++ b/ui/src/main/java/de/mm20/launcher2/ui/settings/cards/CardsSettingsScreenVM.kt @@ -4,6 +4,7 @@ import androidx.lifecycle.ViewModel import androidx.lifecycle.asLiveData import androidx.lifecycle.viewModelScope import de.mm20.launcher2.preferences.LauncherDataStore +import de.mm20.launcher2.preferences.Settings import kotlinx.coroutines.flow.map import kotlinx.coroutines.launch import org.koin.core.component.KoinComponent @@ -47,4 +48,16 @@ class CardsSettingsScreenVM: ViewModel(), KoinComponent { } } } + + val shape = dataStore.data.map { it.cards.shape }.asLiveData() + fun setShape(shape: Settings.CardSettings.Shape) { + viewModelScope.launch { + dataStore.updateData { + it.toBuilder() + .setCards(it.cards.toBuilder() + .setShape(shape) + ).build() + } + } + } } \ No newline at end of file diff --git a/ui/src/main/java/de/mm20/launcher2/ui/theme/LauncherTheme.kt b/ui/src/main/java/de/mm20/launcher2/ui/theme/LauncherTheme.kt index 035ff060..abfca97b 100644 --- a/ui/src/main/java/de/mm20/launcher2/ui/theme/LauncherTheme.kt +++ b/ui/src/main/java/de/mm20/launcher2/ui/theme/LauncherTheme.kt @@ -2,6 +2,8 @@ package de.mm20.launcher2.ui.theme import android.os.Build import androidx.compose.foundation.isSystemInDarkTheme +import androidx.compose.foundation.shape.CornerSize +import androidx.compose.foundation.shape.CutCornerShape import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.material3.* import androidx.compose.runtime.* @@ -44,6 +46,15 @@ fun LauncherTheme( dataStore.data.map { it.cards.radius.dp } }.collectAsState(8.dp) + val baseShape by remember { + dataStore.data.map { + when(it.cards.shape) { + Settings.CardSettings.Shape.Cut -> CutCornerShape(0f) + else -> RoundedCornerShape(0f) + } + } + }.collectAsState(RoundedCornerShape(0f)) + val colorScheme by colorSchemeAsState(colorSchemePreference, darkTheme) CompositionLocalProvider( @@ -53,11 +64,11 @@ fun LauncherTheme( colorScheme = colorScheme, typography = DefaultTypography, shapes = Shapes( - extraSmall = RoundedCornerShape(cornerRadius / 3f), - small = RoundedCornerShape(cornerRadius / 3f * 2f), - medium = RoundedCornerShape(cornerRadius), - large = RoundedCornerShape(cornerRadius / 3f * 4f), - extraLarge = RoundedCornerShape(cornerRadius / 3f * 7f), + extraSmall = baseShape.copy(CornerSize(cornerRadius / 3f)), + small = baseShape.copy(CornerSize(cornerRadius / 3f * 2f)), + medium = baseShape.copy(CornerSize(cornerRadius)), + large = baseShape.copy(CornerSize(cornerRadius / 3f * 4f)), + extraLarge = baseShape.copy(CornerSize(cornerRadius / 3f * 7f)), ), content = content )