Skip to content

Commit

Permalink
refactor Colors to be immutable
Browse files Browse the repository at this point in the history
ColorScheme is now Immutable, making individual color updates less efficient, but making more common usage of colors more efficient

The reasoning behind this change is that the majority of apps wouldn't have updating individual colors as a main use case.

This is still possible but it will recompose more than before, in turn we significantly decrease the amount of state subscriptions through all of material code and will impact initialization and runtime cost of more standard use cases.
  • Loading branch information
hrach committed Oct 23, 2023
1 parent 3acd6dc commit eed3860
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 190 deletions.
13 changes: 4 additions & 9 deletions ui/src/androidMain/kotlin/kiwi/orbit/compose/ui/OrbitTheme.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.ReadOnlyComposable
import androidx.compose.runtime.remember
import kiwi.orbit.compose.ui.foundation.Colors
import kiwi.orbit.compose.ui.foundation.ContentEmphasis
import kiwi.orbit.compose.ui.foundation.ElevationLevels
Expand All @@ -18,7 +17,6 @@ import kiwi.orbit.compose.ui.foundation.ProvideMergedTextStyle
import kiwi.orbit.compose.ui.foundation.Shapes
import kiwi.orbit.compose.ui.foundation.Typography
import kiwi.orbit.compose.ui.foundation.rememberTextSelectionColors
import kiwi.orbit.compose.ui.foundation.updateColorsFrom

@Composable
public fun OrbitTheme(
Expand All @@ -27,24 +25,21 @@ public fun OrbitTheme(
shapes: Shapes = OrbitTheme.shapes,
content: @Composable () -> Unit,
) {
val rememberedColors = remember { colors.copy() }.apply {
updateColorsFrom(colors)
}
val selectionColors = rememberTextSelectionColors(rememberedColors)
val selectionColors = rememberTextSelectionColors(colors)

MaterialTheme(
colorScheme = rememberedColors.toMaterial3Colors(),
colorScheme = colors.toMaterial3Colors(),
typography = typography.toMaterial3Typography(),
shapes = shapes.toMaterial3Shapes(),
) {
CompositionLocalProvider(
// Orbit
LocalColors provides rememberedColors,
LocalColors provides colors,
LocalContentEmphasis provides ContentEmphasis.Normal,
LocalShapes provides shapes,
LocalTypography provides typography,
// Foundation
LocalContentColor provides rememberedColors.content.normal,
LocalContentColor provides colors.content.normal,
LocalTextSelectionColors provides selectionColors,
) {
ProvideMergedTextStyle(typography.bodyNormal, content)
Expand Down
Loading

0 comments on commit eed3860

Please sign in to comment.