Redesign accent color selector

This commit is contained in:
Patryk Michalik committed 2021-10-23 13:40:17 +02:00
1 parent ff93c79be1
commit 7849d1ecde
10 files changed
+282 -382

No files matched your search

+2 -1
View File
@@ -160,9 +160,10 @@
<string name="app_title_label">Title</string>
<string name="hide_from_drawer">Hide from App Drawer</string>
<string name="action_lens">Lens</string>
<string name="enable_colorful_theme">Also Apply to Background</string>
<string name="enable_colorful_theme">Tint Backgrounds</string>
<string name="force_rounded_widgets">Round Corners</string>
<string name="suggestion_pref_screen_title">Suggestions</string>
<string name="n_percent">%1$d%%</string>
<string name="label">Label</string>
<string name="presets">Presets</string>
</resources>
@@ -86,12 +86,6 @@ class PreferenceManager private constructor(private val context: Context) : Base
ColorOption::toString,
recreate
)
val lastCustomAccent = ObjectPref(
"pref_lastCustomAccent",
ColorOption.LawnchairBlue,
ColorOption::fromString,
ColorOption::toString
)
val enableColorfulTheme = BoolPref("pref_enableColorfulTheme", true, recreate)
val wallpaperScrolling = BoolPref("pref_wallpaperScrolling", true)
val showSysUiScrim = BoolPref("pref_showSysUiScrim", true)
@@ -3,7 +3,7 @@ package app.lawnchair.theme.color
import android.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import app.lawnchair.ui.preferences.components.ColorPreferenceOption
import app.lawnchair.ui.preferences.AccentColorOption
import app.lawnchair.ui.theme.getSystemAccent
import app.lawnchair.wallpaper.WallpaperManagerCompat
import com.android.launcher3.R
@@ -12,12 +12,12 @@ import com.android.launcher3.Utilities
sealed class ColorOption {
abstract val isSupported: Boolean
abstract val preferenceOption: ColorPreferenceOption<ColorOption>
abstract val accentColorOption: AccentColorOption<ColorOption>
object SystemAccent : ColorOption() {
override val isSupported = true
override val preferenceOption = ColorPreferenceOption<ColorOption>(
override val accentColorOption = AccentColorOption<ColorOption>(
this,
{ stringResource(id = R.string.system) },
{ LocalContext.current.getSystemAccent(false) },
@@ -30,7 +30,7 @@ sealed class ColorOption {
object WallpaperPrimary : ColorOption() {
override val isSupported = Utilities.ATLEAST_O_MR1
override val preferenceOption = ColorPreferenceOption<ColorOption>(
override val accentColorOption = AccentColorOption<ColorOption>(
this,
{ stringResource(id = R.string.wallpaper) },
{
@@ -47,7 +47,7 @@ sealed class ColorOption {
class CustomColor(val color: Int) : ColorOption() {
override val isSupported = true
override val preferenceOption = ColorPreferenceOption<ColorOption>(
override val accentColorOption = AccentColorOption<ColorOption>(
this,
{ stringResource(id = R.string.custom) },
{ color }
@@ -0,0 +1,256 @@
package app.lawnchair.ui.preferences
import androidx.compose.animation.Crossfade
import androidx.compose.animation.ExperimentalAnimationApi
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material.*
import androidx.compose.material.TabRowDefaults.tabIndicatorOffset
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.rounded.Done
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.navigation.NavGraphBuilder
import app.lawnchair.preferences.getAdapter
import app.lawnchair.preferences.preferenceManager
import app.lawnchair.theme.color.ColorOption
import app.lawnchair.ui.preferences.components.PreferenceGroup
import app.lawnchair.ui.preferences.components.PreferenceLayout
import app.lawnchair.ui.preferences.components.PreferenceTemplate
import app.lawnchair.ui.preferences.components.SwitchPreference
import app.lawnchair.ui.theme.lightenColor
import com.android.launcher3.R
val staticColors = listOf(
ColorOption.CustomColor(0xFFF32020),
ColorOption.CustomColor(0xFFF20D69),
ColorOption.CustomColor(0xFF7452FF),
ColorOption.CustomColor(0xFF2C41C9),
ColorOption.LawnchairBlue,
ColorOption.CustomColor(0xFF00BAD6),
ColorOption.CustomColor(0xFF00A399),
ColorOption.CustomColor(0xFF47B84F),
ColorOption.CustomColor(0xFFFFBB00),
ColorOption.CustomColor(0xFFFF9800),
ColorOption.CustomColor(0xFF7C5445),
ColorOption.CustomColor(0xFF67818E)
).map(ColorOption::accentColorOption)
val dynamicColors = listOf(ColorOption.SystemAccent, ColorOption.WallpaperPrimary)
.filter(ColorOption::isSupported)
.map(ColorOption::accentColorOption)
@ExperimentalAnimationApi
fun NavGraphBuilder.accentColorGraph(route: String) {
preferenceGraph(route, { AccentColorPreferences() })
}
@Composable
@OptIn(ExperimentalAnimationApi::class)
fun AccentColorPreferences() {
val pm = preferenceManager()
var accentColor by pm.accentColor.getAdapter()
val defaultTabIndex = if (dynamicColors.any { accentColor == it.value }) 0 else 1
var selectedTabIndex by remember { mutableStateOf(value = defaultTabIndex) }
PreferenceLayout(label = stringResource(id = R.string.accent_color)) {
PreferenceGroup(
isFirstChild = true,
dividersToSkip = 1
) {
TabRow(
selectedTabIndex = selectedTabIndex,
backgroundColor = Color.Transparent,
indicator = { tabPositions ->
TabRowDefaults.Indicator(
modifier = Modifier.tabIndicatorOffset(tabPositions[selectedTabIndex]),
color = MaterialTheme.colors.primary
)
},
) {
CustomTab(
selected = selectedTabIndex == 0,
onClick = { selectedTabIndex = 0 },
label = stringResource(id = R.string.presets)
)
CustomTab(
selected = selectedTabIndex == 1,
onClick = { selectedTabIndex = 1 },
label = stringResource(id = R.string.custom)
)
}
when (selectedTabIndex) {
0 -> {
dynamicColors.map { accentColorOption ->
key(accentColorOption) {
PreferenceTemplate(
title = { Text(text = accentColorOption.label()) },
verticalPadding = 12.dp,
modifier = Modifier
.clickable { accentColor = accentColorOption.value },
startWidget = {
RadioButton(
selected = accentColorOption.value == accentColor,
onClick = null
)
ColorDot(
accentColorOption = accentColorOption,
modifier = Modifier.padding(start = 16.dp)
)
}
)
}
}
}
1 -> {
SwatchGrid(
accentColorOptions = staticColors,
modifier = Modifier.padding(16.dp),
onSwatchClick = { accentColor = it },
isSwatchSelected = { it == accentColor }
)
}
}
}
PreferenceGroup {
SwitchPreference(
adapter = pm.enableColorfulTheme.getAdapter(),
label = stringResource(id = R.string.enable_colorful_theme)
)
}
}
}
@Composable
fun <T> SwatchGrid(
accentColorOptions: List<AccentColorOption<T>>,
onSwatchClick: (T) -> Unit,
modifier: Modifier = Modifier,
isSwatchSelected: (T) -> Boolean
) {
val columnCount = 6
val rowCount = (staticColors.size.toDouble() / 6.0).toInt()
val gutter = 12.dp
Column(modifier = modifier) {
for (rowNo in 1..rowCount) {
val firstIndex = (rowNo - 1) * columnCount
val lastIndex = firstIndex + columnCount - 1
val indices = firstIndex..lastIndex
Row {
accentColorOptions.slice(indices).forEachIndexed { index, colorOption ->
ColorSwatch(
accentColorOption = colorOption,
onClick = { onSwatchClick(colorOption.value) },
modifier = Modifier.weight(1F),
selected = isSwatchSelected(colorOption.value)
)
if (index != columnCount - 1) {
Spacer(modifier = Modifier.width(gutter))
}
}
}
if (rowNo != rowCount) {
Spacer(modifier = Modifier.height(gutter))
}
}
}
}
@Composable
fun <T> ColorSwatch(
accentColorOption: AccentColorOption<T>,
modifier: Modifier = Modifier,
onClick: () -> Unit,
selected: Boolean
) {
val color = if (MaterialTheme.colors.isLight) {
accentColorOption.lightColor()
} else {
accentColorOption.darkColor()
}
Box(
contentAlignment = Alignment.Center,
modifier = modifier
.fillMaxWidth()
.aspectRatio(ratio = 1F)
.clip(CircleShape)
.background(Color(color))
.clickable(onClick = onClick)
) {
Crossfade(targetState = selected) {
if (it) {
Icon(
imageVector = Icons.Rounded.Done,
contentDescription = null,
tint = MaterialTheme.colors.onPrimary
)
}
}
}
}
@Composable
fun <T> ColorDot(
accentColorOption: AccentColorOption<T>,
modifier: Modifier = Modifier
) {
val color = if (MaterialTheme.colors.isLight) {
accentColorOption.lightColor()
} else {
accentColorOption.darkColor()
}
ColorDot(
color = Color(color),
modifier = modifier
)
}
@Composable
fun ColorDot(
color: Color,
modifier: Modifier = Modifier
) {
Box(
modifier = modifier
.size(30.dp)
.clip(CircleShape)
.background(color = color)
)
}
@Composable
fun CustomTab(
selected: Boolean,
onClick: () -> Unit,
label: String
) {
Tab(
selected = selected,
onClick = onClick,
selectedContentColor = MaterialTheme.colors.primary,
unselectedContentColor = MaterialTheme.colors.onBackground.copy(alpha = ContentAlpha.medium)
) {
Text(
text = label,
modifier = Modifier.padding(vertical = 12.dp)
)
}
}
open class AccentColorOption<T>(
val value: T,
val label: @Composable () -> String,
val lightColor: @Composable () -> Int,
val darkColor: @Composable () -> Int = { lightenColor(lightColor()) },
)
@@ -19,6 +19,7 @@ package app.lawnchair.ui.preferences
import android.os.Build
import androidx.compose.animation.*
import androidx.compose.material.ExperimentalMaterialApi
import androidx.compose.material.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.ui.res.stringResource
import androidx.navigation.NavGraphBuilder
@@ -29,6 +30,7 @@ import com.android.launcher3.R
object GeneralRoutes {
const val ICON_PACK = "iconPack"
const val ACCENT_COLOR = "accentColor"
}
@ExperimentalMaterialApi
@@ -36,6 +38,7 @@ object GeneralRoutes {
fun NavGraphBuilder.generalGraph(route: String) {
preferenceGraph(route, { GeneralPreferences() }) { subRoute ->
iconPackGraph(route = subRoute(GeneralRoutes.ICON_PACK))
accentColorGraph(route = subRoute(GeneralRoutes.ACCENT_COLOR))
}
}
@@ -61,7 +64,12 @@ fun GeneralPreferences() {
)
IconShapePreference()
ThemePreference()
AccentColorPreference()
NavigationActionPreference(
label = stringResource(id = R.string.accent_color),
destination = subRoute(name = GeneralRoutes.ACCENT_COLOR),
subtitle = (dynamicColors + staticColors).firstOrNull { it.value == prefs.accentColor.get() }?.label?.invoke(),
endWidget = { ColorDot(color = MaterialTheme.colors.primary) }
)
FontPreference(
adapter = prefs.workspaceFont.getAdapter(),
label = stringResource(id = R.string.font_label)
@@ -1,53 +0,0 @@
package app.lawnchair.ui.preferences.components
import androidx.compose.animation.ExperimentalAnimationApi
import androidx.compose.material.ExperimentalMaterialApi
import androidx.compose.material.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.ui.res.stringResource
import app.lawnchair.preferences.getAdapter
import app.lawnchair.preferences.preferenceManager
import app.lawnchair.theme.color.ColorOption
import com.android.launcher3.R
private val customOptionsValues = listOf(
ColorOption.CustomColor(0xFFF32020),
ColorOption.CustomColor(0xFFF20D69),
ColorOption.CustomColor(0xFF7452FF),
ColorOption.CustomColor(0xFF2C41C9),
ColorOption.LawnchairBlue,
ColorOption.CustomColor(0xFF00BAD6),
ColorOption.CustomColor(0xFF00A399),
ColorOption.CustomColor(0xFF47B84F),
ColorOption.CustomColor(0xFFFFBB00),
ColorOption.CustomColor(0xFFFF9800),
ColorOption.CustomColor(0xFF7C5445),
ColorOption.CustomColor(0xFF67818E)
)
private val options = listOf(ColorOption.SystemAccent, ColorOption.WallpaperPrimary)
.filter(ColorOption::isSupported)
.map(ColorOption::preferenceOption)
private val customOptions = customOptionsValues.map(ColorOption::preferenceOption)
@Composable
@ExperimentalMaterialApi
@ExperimentalAnimationApi
fun AccentColorPreference() {
val prefs = preferenceManager()
ColorPreference(
previewColor = MaterialTheme.colors.primary,
colorAdapter = prefs.accentColor.getAdapter(),
lastCustomColorAdapter = prefs.lastCustomAccent.getAdapter(),
label = stringResource(id = R.string.accent_color),
options = options,
customOptions = customOptions
) {
PreferenceGroup {
SwitchPreference(
adapter = prefs.enableColorfulTheme.getAdapter(),
label = stringResource(id = R.string.enable_colorful_theme)
)
}
}
}
@@ -1,311 +0,0 @@
package app.lawnchair.ui.preferences.components
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.Crossfade
import androidx.compose.animation.ExperimentalAnimationApi
import androidx.compose.animation.core.animateDpAsState
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import app.lawnchair.preferences.PreferenceAdapter
import app.lawnchair.ui.theme.lightenColor
import app.lawnchair.util.BackHandler
import com.android.launcher3.R
import com.google.accompanist.insets.navigationBarsPadding
import kotlinx.coroutines.launch
@Composable
@ExperimentalMaterialApi
@ExperimentalAnimationApi
fun <T> ColorPreference(
previewColor: Color,
colorAdapter: PreferenceAdapter<T>,
lastCustomColorAdapter: PreferenceAdapter<T>,
label: String,
options: List<ColorPreferenceOption<T>>,
customOptions: List<ColorPreferenceOption<T>>,
widget: @Composable () -> Unit = {}
) {
val bottomSheetState = rememberBottomSheetState(initialValue = ModalBottomSheetValue.Hidden)
val coroutineScope = rememberCoroutineScope()
var selectedColor by colorAdapter
var lastCustomColor by lastCustomColorAdapter
PreferenceTemplate(
title = { Text(text = label) },
description = {
val selectedOption = (options + customOptions)
.firstOrNull { it.value == selectedColor }
if (selectedOption != null) {
Text(text = selectedOption.label())
}
},
modifier = Modifier
.clickable { coroutineScope.launch { bottomSheetState.show() } },
endWidget = {
Box(
modifier = Modifier
.size(30.dp)
.clip(CircleShape)
.background(previewColor)
)
}
)
BottomSheet(sheetState = bottomSheetState) {
var selectingCustomColor by remember { mutableStateOf(value = false) }
Column(modifier = Modifier.navigationBarsPadding()) {
TopBar(
label = if (selectingCustomColor) stringResource(id = R.string.custom_color) else label,
showBackArrow = selectingCustomColor,
onBackArrowClick = { selectingCustomColor = false }
)
if (selectingCustomColor) {
BackHandler {
selectingCustomColor = false
}
ColorSwatchGrid(
options = customOptions,
onColorSwatchClicked = {
selectedColor = it
lastCustomColor = it
},
selectedColor = lastCustomColor,
modifier = Modifier.padding(top = 8.dp, bottom = 24.dp)
)
} else {
options.mapIndexed { index, option ->
key(option) {
ModeRow(
title = option.label(),
onClick = { selectedColor = option.value },
selected = selectedColor == option.value,
option = option,
showDivider = index != 0
)
}
}
val customColorOption = customOptions.firstOrNull { it.value == selectedColor }
val lastCustomColorOption = customOptions.first { it.value == lastCustomColor }
ModeRow(
title = stringResource(id = R.string.custom),
onClick = { selectedColor = lastCustomColor },
selected = customColorOption != null,
option = lastCustomColorOption,
onEditClick = { selectingCustomColor = true }
)
widget()
Spacer(modifier = Modifier.requiredHeight(16.dp))
}
Row(
horizontalArrangement = Arrangement.End,
modifier = Modifier
.fillMaxWidth()
.padding(end = 16.dp, bottom = 16.dp)
) {
Button(
shape = MaterialTheme.shapes.small,
colors = ButtonDefaults.buttonColors(
backgroundColor = MaterialTheme.colors.primary,
contentColor = MaterialTheme.colors.onPrimary
),
onClick = {
coroutineScope.launch {
bottomSheetState.hide()
}
}
) {
Text(text = stringResource(id = R.string.done))
}
}
}
}
}
@Composable
@ExperimentalAnimationApi
fun <T> ColorSwatchGrid(
options: List<ColorPreferenceOption<T>>,
onColorSwatchClicked: (T) -> Unit,
selectedColor: T,
modifier: Modifier = Modifier
) {
val columnCount = 6
val rowCount = (options.size.toDouble() / 6.0).toInt()
Column(modifier = modifier) {
for (rowNo in 1..rowCount) {
val firstIndex = (rowNo - 1) * columnCount
val lastIndex = firstIndex + columnCount - 1
val indices = firstIndex..lastIndex
Row(modifier = Modifier.padding(horizontal = 13.dp)) {
options.slice(indices).forEachIndexed { index, entry ->
ColorSwatch(
option = entry,
onClick = { onColorSwatchClicked(entry.value) },
modifier = Modifier.weight(1F),
isSelected = entry.value == selectedColor,
ringColor = MaterialTheme.colors.surface,
elevation = ModalBottomSheetDefaults.Elevation
)
if (index != columnCount - 1) {
Spacer(modifier = Modifier.requiredWidth(8.dp))
}
}
}
if (rowNo != rowCount) {
Spacer(modifier = Modifier.requiredHeight(8.dp))
}
}
}
}
@Composable
@ExperimentalAnimationApi
fun TopBar(
label: String,
showBackArrow: Boolean,
onBackArrowClick: () -> Unit
) {
Row(
verticalAlignment = Alignment.CenterVertically,
modifier = Modifier
.height(56.dp)
.padding(horizontal = 8.dp)
) {
AnimatedVisibility(visible = showBackArrow) {
ClickableIcon(
imageVector = backIcon(),
onClick = onBackArrowClick,
tint = MaterialTheme.colors.onSurface
)
}
Text(
text = label,
style = MaterialTheme.typography.h6,
modifier = Modifier.padding(start = 8.dp)
)
}
}
@Composable
@ExperimentalAnimationApi
fun <T> ModeRow(
title: String,
onClick: () -> Unit,
onEditClick: (() -> Unit)? = null,
option: ColorPreferenceOption<T>,
showDivider: Boolean = true,
selected: Boolean
) {
PreferenceTemplate(
title = { Text(text = title) },
modifier = Modifier
.clickable(onClick = onClick),
startWidget = {
RadioButton(
selected = selected,
onClick = null,
colors = RadioButtonDefaults.colors(
unselectedColor = MaterialTheme.colors.onSurface.copy(alpha = 0.48F)
)
)
Spacer(modifier = Modifier.requiredWidth(16.dp))
Box(
modifier = Modifier
.size(30.dp)
.clip(CircleShape)
.background(Color(if (MaterialTheme.colors.isLight) option.lightColor() else option.darkColor()))
)
},
endWidget = {
if (onEditClick != null) {
CompositionLocalProvider(
LocalContentColor provides MaterialTheme.colors.primary
) {
ClickableIcon(
painter = painterResource(id = R.drawable.ic_edit),
onClick = onEditClick,
enabled = selected
)
}
} else {
Spacer(modifier = Modifier.requiredHeight(40.dp))
}
},
showDivider = showDivider,
verticalPadding = 8.dp
)
}
@Composable
@ExperimentalAnimationApi
fun <T> ColorSwatch(
option: ColorPreferenceOption<T>,
onClick: () -> Unit,
modifier: Modifier = Modifier,
isSelected: Boolean,
ringColor: Color,
elevation: Dp
) {
val swatchPadding by animateDpAsState(targetValue = if (isSelected) 2.dp else 3.dp)
val ringPadding by animateDpAsState(targetValue = if (isSelected) 5.dp else 0.dp)
val darkTheme = !MaterialTheme.colors.isLight
val color = if (darkTheme) option.darkColor() else option.lightColor()
val elevationOverlay = LocalElevationOverlay.current
val absoluteElevation = LocalAbsoluteElevation.current + elevation
val ringColorWithOverlay =
if (ringColor == MaterialTheme.colors.surface && elevationOverlay != null) {
elevationOverlay.apply(ringColor, absoluteElevation)
} else {
ringColor
}
Box(
modifier = modifier
.aspectRatio(1F)
.height(IntrinsicSize.Min)
.width(IntrinsicSize.Min)
) {
Box(
modifier = Modifier
.padding(swatchPadding)
.fillMaxSize()
.clip(CircleShape)
.background(Color(color))
.clickable(onClick = onClick)
)
Crossfade(targetState = isSelected) {
if (it) {
Box(
modifier = Modifier
.fillMaxSize()
.padding(ringPadding)
.border(3.dp, ringColorWithOverlay, CircleShape)
)
}
}
}
}
open class ColorPreferenceOption<T>(
val value: T,
val label: @Composable () -> String,
val lightColor: @Composable () -> Int,
val darkColor: @Composable () -> Int = { lightenColor(lightColor()) },
)
@@ -20,7 +20,8 @@ fun DividerColumn(
thickness: Dp = 1.dp,
startIndent: Dp = 0.dp,
endIndent: Dp = 0.dp,
content: @Composable () -> Unit
content: @Composable () -> Unit,
dividersToSkip: Int
) {
val state = remember { DividersState() }
val density = LocalDensity.current
@@ -50,7 +51,7 @@ fun DividerColumn(
// Place children in the parent layout
placeables.forEachIndexed { index, placeable ->
if (index > 0) {
if (index > dividersToSkip) {
dividerPositions.add(yPosition)
yPosition += thicknessPx.roundToInt()
}
@@ -27,7 +27,8 @@ fun NavigationActionPreference(
label: String,
subtitle: String? = null,
destination: String,
showDivider: Boolean = false
showDivider: Boolean = false,
endWidget: (@Composable () -> Unit)? = null
) {
val navController = LocalNavController.current
@@ -35,6 +36,7 @@ fun NavigationActionPreference(
modifier = Modifier.clickable { navController.navigate(route = destination) },
title = { Text(text = label) },
description = { subtitle?.let { Text(text = it) } },
showDivider = showDivider
showDivider = showDivider,
endWidget = endWidget
)
}
@@ -37,6 +37,7 @@ fun PreferenceGroup(
showDividers: Boolean = true,
dividerStartIndent: Dp = 0.dp,
dividerEndIndent: Dp = 0.dp,
dividersToSkip: Int = 0,
content: @Composable () -> Unit
) {
PreferenceGroupHeading(heading, isFirstChild)
@@ -53,7 +54,8 @@ fun PreferenceGroup(
modifier = columnModifier,
startIndent = dividerStartIndent,
endIndent = dividerEndIndent,
content = content
content = content,
dividersToSkip = dividersToSkip
)
} else {
Column(modifier = columnModifier) {