Skip to content

Commit

Permalink
Feature: Add state object for Dynamic theme (#143)
Browse files Browse the repository at this point in the history
  • Loading branch information
jordond committed May 14, 2024
1 parent d05eba0 commit 2a36e0d
Show file tree
Hide file tree
Showing 7 changed files with 320 additions and 84 deletions.
28 changes: 15 additions & 13 deletions demo/composeApp/src/commonMain/kotlin/com/materialkolor/demo/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ import com.materialkolor.ktx.from
import com.materialkolor.ktx.harmonize
import com.materialkolor.ktx.lighten
import com.materialkolor.palettes.TonalPalette
import com.materialkolor.rememberDynamicMaterialThemeState
import kotlin.math.round

val SampleColors = listOf(
Expand Down Expand Up @@ -106,12 +107,9 @@ fun colorSchemePairs() = listOf(
@Composable
internal fun App() {
val isDarkTheme = isSystemInDarkTheme()
val state = rememberDynamicMaterialThemeState(SampleColors[0], isDarkTheme)

var seedColor: Color by remember { mutableStateOf(SampleColors[0]) }
var style by remember { mutableStateOf(PaletteStyle.TonalSpot) }
var darkTheme by remember { mutableStateOf(isDarkTheme) }

AppTheme(seedColor, style, darkTheme) {
AppTheme(state) {
Column(
modifier = Modifier
.fillMaxSize()
Expand All @@ -122,10 +120,10 @@ internal fun App() {
modifier = Modifier.align(Alignment.End)
) {
IconButton(
onClick = { darkTheme = !darkTheme },
onClick = { state.isDark = !state.isDark },
modifier = Modifier.align(Alignment.TopEnd),
) {
val icon = if (darkTheme) Icons.Filled.LightMode else Icons.Filled.DarkMode
val icon = if (state.isDark) Icons.Filled.LightMode else Icons.Filled.DarkMode
Icon(icon, contentDescription = null)
}
}
Expand All @@ -138,8 +136,8 @@ internal fun App() {
PaletteStyle.entries.forEach { paletteStyle ->
FilterChip(
label = { Text(text = paletteStyle.name) },
selected = style == paletteStyle,
onClick = { style = paletteStyle },
selected = state.style == paletteStyle,
onClick = { state.style = paletteStyle },
)
}
}
Expand All @@ -159,7 +157,7 @@ internal fun App() {
.size(32.dp)
.clip(RoundedCornerShape(100.dp))
.background(color)
.clickable { seedColor = color }
.clickable { state.seedColor = color }
)
}
}
Expand Down Expand Up @@ -322,7 +320,7 @@ internal fun App() {
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.spacedBy(8.dp),
) {
var color by remember(seedColor) { mutableStateOf(seedColor) }
var color by remember(state.seedColor) { mutableStateOf(state.seedColor) }

Button(onClick = { color = color.lighten(1.1f) }) {
Text("Lighten")
Expand Down Expand Up @@ -351,8 +349,12 @@ internal fun App() {
modifier = Modifier.fillMaxWidth(),
) {
val ratio = 1.1f + it / 10f
val darker by remember(seedColor) { mutableStateOf(seedColor.darken(ratio)) }
val lighter by remember(seedColor) { mutableStateOf(seedColor.lighten(ratio)) }
val darker by remember(state.seedColor) {
mutableStateOf(state.seedColor.darken(ratio))
}
val lighter by remember(state.seedColor) {
mutableStateOf(state.seedColor.lighten(ratio))
}

Text(text = "Darken ${ratio.roundToTwoDecimalPlaces()}")
Spacer(modifier = Modifier.height(4.dp))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
package com.materialkolor.demo.theme

import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Shapes
import androidx.compose.material3.Surface
import androidx.compose.material3.Typography
import androidx.compose.runtime.Composable
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.materialkolor.DynamicMaterialTheme
import com.materialkolor.PaletteStyle
import com.materialkolor.DynamicMaterialThemeState

private val AppShapes = Shapes(
extraSmall = RoundedCornerShape(2.dp),
Expand All @@ -34,24 +31,16 @@ private val AppTypography = Typography(

@Composable
internal fun AppTheme(
seedColor: Color,
paletteStyle: PaletteStyle = PaletteStyle.TonalSpot,
useDarkTheme: Boolean = isSystemInDarkTheme(),
state: DynamicMaterialThemeState,
content: @Composable () -> Unit,
) {
MaterialTheme(
DynamicMaterialTheme(
state = state,
animate = true,
typography = AppTypography,
shapes = AppShapes,
content = {
DynamicMaterialTheme(
seedColor = seedColor,
useDarkTheme = useDarkTheme,
style = paletteStyle,
animate = true,
content = {
Surface(content = content)
}
)
Surface(content = content)
}
)
}
31 changes: 28 additions & 3 deletions material-kolor/api/android/material-kolor.api
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,41 @@ public final class com/materialkolor/Contrast : java/lang/Enum {
}

public final class com/materialkolor/DynamicColorSchemeKt {
public static final fun dynamicColorScheme-3J-VO9M (JZLcom/materialkolor/PaletteStyle;DZ)Landroidx/compose/material3/ColorScheme;
public static synthetic fun dynamicColorScheme-3J-VO9M$default (JZLcom/materialkolor/PaletteStyle;DZILjava/lang/Object;)Landroidx/compose/material3/ColorScheme;
public static final fun rememberDynamicColorScheme-yWKOrZg (JZLcom/materialkolor/PaletteStyle;DZLandroidx/compose/runtime/Composer;II)Landroidx/compose/material3/ColorScheme;
public static final fun dynamicColorScheme-KTwxG1Y (JZLcom/materialkolor/PaletteStyle;DZLkotlin/jvm/functions/Function1;)Landroidx/compose/material3/ColorScheme;
public static synthetic fun dynamicColorScheme-KTwxG1Y$default (JZLcom/materialkolor/PaletteStyle;DZLkotlin/jvm/functions/Function1;ILjava/lang/Object;)Landroidx/compose/material3/ColorScheme;
public static final fun rememberDynamicColorScheme-ZPw9REg (JZLcom/materialkolor/PaletteStyle;DZLkotlin/jvm/functions/Function1;Landroidx/compose/runtime/Composer;II)Landroidx/compose/material3/ColorScheme;
}

public final class com/materialkolor/DynamicMaterialThemeKt {
public static final fun AnimatedDynamicMaterialTheme-AnklqjM (JZLcom/materialkolor/PaletteStyle;DLandroidx/compose/animation/core/AnimationSpec;Landroidx/compose/material3/Shapes;Landroidx/compose/material3/Typography;ZLkotlin/jvm/functions/Function2;Landroidx/compose/runtime/Composer;II)V
public static final fun DynamicMaterialTheme (Lcom/materialkolor/DynamicMaterialThemeState;Landroidx/compose/material3/Shapes;Landroidx/compose/material3/Typography;ZLandroidx/compose/animation/core/AnimationSpec;Lkotlin/jvm/functions/Function2;Landroidx/compose/runtime/Composer;II)V
public static final fun DynamicMaterialTheme-PSU77Us (JZLcom/materialkolor/PaletteStyle;DLandroidx/compose/material3/Shapes;Landroidx/compose/material3/Typography;ZZLandroidx/compose/animation/core/AnimationSpec;Lkotlin/jvm/functions/Function2;Landroidx/compose/runtime/Composer;II)V
}

public final class com/materialkolor/DynamicMaterialThemeState {
public static final field $stable I
public static final field Companion Lcom/materialkolor/DynamicMaterialThemeState$Companion;
public final fun getColorScheme (Landroidx/compose/runtime/Composer;I)Landroidx/compose/material3/ColorScheme;
public final fun getContrastLevel ()D
public final fun getModifyColorScheme ()Lkotlin/jvm/functions/Function2;
public final fun getSeedColor-0d7_KjU ()J
public final fun getStyle ()Lcom/materialkolor/PaletteStyle;
public final fun isDark ()Z
public final fun isExtendedFidelity ()Z
public final fun setContrastLevel (D)V
public final fun setDark (Z)V
public final fun setExtendedFidelity (Z)V
public final fun setSeedColor-8_81llA (J)V
public final fun setStyle (Lcom/materialkolor/PaletteStyle;)V
}

public final class com/materialkolor/DynamicMaterialThemeState$Companion {
}

public final class com/materialkolor/DynamicMaterialThemeStateKt {
public static final fun rememberDynamicMaterialThemeState-ZPw9REg (JZLcom/materialkolor/PaletteStyle;DZLkotlin/jvm/functions/Function2;Landroidx/compose/runtime/Composer;II)Lcom/materialkolor/DynamicMaterialThemeState;
}

public final class com/materialkolor/LocalDynamicMaterialThemeSeedKt {
public static final fun getLocalDynamicMaterialThemeSeed ()Landroidx/compose/runtime/ProvidableCompositionLocal;
}
Expand Down
31 changes: 28 additions & 3 deletions material-kolor/api/jvm/material-kolor.api
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,41 @@ public final class com/materialkolor/Contrast : java/lang/Enum {
}

public final class com/materialkolor/DynamicColorSchemeKt {
public static final fun dynamicColorScheme-3J-VO9M (JZLcom/materialkolor/PaletteStyle;DZ)Landroidx/compose/material3/ColorScheme;
public static synthetic fun dynamicColorScheme-3J-VO9M$default (JZLcom/materialkolor/PaletteStyle;DZILjava/lang/Object;)Landroidx/compose/material3/ColorScheme;
public static final fun rememberDynamicColorScheme-yWKOrZg (JZLcom/materialkolor/PaletteStyle;DZLandroidx/compose/runtime/Composer;II)Landroidx/compose/material3/ColorScheme;
public static final fun dynamicColorScheme-KTwxG1Y (JZLcom/materialkolor/PaletteStyle;DZLkotlin/jvm/functions/Function1;)Landroidx/compose/material3/ColorScheme;
public static synthetic fun dynamicColorScheme-KTwxG1Y$default (JZLcom/materialkolor/PaletteStyle;DZLkotlin/jvm/functions/Function1;ILjava/lang/Object;)Landroidx/compose/material3/ColorScheme;
public static final fun rememberDynamicColorScheme-ZPw9REg (JZLcom/materialkolor/PaletteStyle;DZLkotlin/jvm/functions/Function1;Landroidx/compose/runtime/Composer;II)Landroidx/compose/material3/ColorScheme;
}

public final class com/materialkolor/DynamicMaterialThemeKt {
public static final fun AnimatedDynamicMaterialTheme-AnklqjM (JZLcom/materialkolor/PaletteStyle;DLandroidx/compose/animation/core/AnimationSpec;Landroidx/compose/material3/Shapes;Landroidx/compose/material3/Typography;ZLkotlin/jvm/functions/Function2;Landroidx/compose/runtime/Composer;II)V
public static final fun DynamicMaterialTheme (Lcom/materialkolor/DynamicMaterialThemeState;Landroidx/compose/material3/Shapes;Landroidx/compose/material3/Typography;ZLandroidx/compose/animation/core/AnimationSpec;Lkotlin/jvm/functions/Function2;Landroidx/compose/runtime/Composer;II)V
public static final fun DynamicMaterialTheme-PSU77Us (JZLcom/materialkolor/PaletteStyle;DLandroidx/compose/material3/Shapes;Landroidx/compose/material3/Typography;ZZLandroidx/compose/animation/core/AnimationSpec;Lkotlin/jvm/functions/Function2;Landroidx/compose/runtime/Composer;II)V
}

public final class com/materialkolor/DynamicMaterialThemeState {
public static final field $stable I
public static final field Companion Lcom/materialkolor/DynamicMaterialThemeState$Companion;
public final fun getColorScheme (Landroidx/compose/runtime/Composer;I)Landroidx/compose/material3/ColorScheme;
public final fun getContrastLevel ()D
public final fun getModifyColorScheme ()Lkotlin/jvm/functions/Function2;
public final fun getSeedColor-0d7_KjU ()J
public final fun getStyle ()Lcom/materialkolor/PaletteStyle;
public final fun isDark ()Z
public final fun isExtendedFidelity ()Z
public final fun setContrastLevel (D)V
public final fun setDark (Z)V
public final fun setExtendedFidelity (Z)V
public final fun setSeedColor-8_81llA (J)V
public final fun setStyle (Lcom/materialkolor/PaletteStyle;)V
}

public final class com/materialkolor/DynamicMaterialThemeState$Companion {
}

public final class com/materialkolor/DynamicMaterialThemeStateKt {
public static final fun rememberDynamicMaterialThemeState-ZPw9REg (JZLcom/materialkolor/PaletteStyle;DZLkotlin/jvm/functions/Function2;Landroidx/compose/runtime/Composer;II)Lcom/materialkolor/DynamicMaterialThemeState;
}

public final class com/materialkolor/LocalDynamicMaterialThemeSeedKt {
public static final fun getLocalDynamicMaterialThemeSeed ()Landroidx/compose/runtime/ProvidableCompositionLocal;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,25 @@ public fun rememberDynamicColorScheme(
seedColor: Color,
isDark: Boolean,
style: PaletteStyle = PaletteStyle.TonalSpot,
contrastLevel: Double = 0.0,
contrastLevel: Double = Contrast.Default.value,
isExtendedFidelity: Boolean = false,
): ColorScheme = remember(seedColor, isDark, style, contrastLevel, isExtendedFidelity) {
dynamicColorScheme(seedColor, isDark, style, contrastLevel, isExtendedFidelity)
modifyColorScheme: ((ColorScheme) -> ColorScheme)? = null,
): ColorScheme = remember(
seedColor,
isDark,
style,
contrastLevel,
isExtendedFidelity,
modifyColorScheme,
) {
dynamicColorScheme(
seedColor = seedColor,
isDark = isDark,
style = style,
contrastLevel = contrastLevel,
isExtendedFidelity = isExtendedFidelity,
modifyColorScheme = modifyColorScheme,
)
}

/**
Expand All @@ -43,6 +58,7 @@ public fun dynamicColorScheme(
style: PaletteStyle = PaletteStyle.TonalSpot,
contrastLevel: Double = Contrast.Default.value,
isExtendedFidelity: Boolean = false,
modifyColorScheme: ((ColorScheme) -> ColorScheme)? = null,
): ColorScheme {
val scheme = seedColor.toDynamicScheme(isDark, style, contrastLevel)
val colors = MaterialDynamicColors(isExtendedFidelity)
Expand Down Expand Up @@ -84,5 +100,5 @@ public fun dynamicColorScheme(
surfaceVariant = colors.surfaceVariant().getColor(scheme),
tertiary = colors.tertiary().getColor(scheme),
tertiaryContainer = colors.tertiaryContainer().getColor(scheme),
)
).let { modifyColorScheme?.invoke(it) ?: it }
}
Loading

0 comments on commit 2a36e0d

Please sign in to comment.