|
| 1 | +package co.touchlab.kampkit.ui.theme |
| 2 | + |
| 3 | +import android.app.Activity |
| 4 | +import android.os.Build |
| 5 | +import androidx.compose.material3.MaterialTheme |
| 6 | +import androidx.compose.material3.dynamicDarkColorScheme |
| 7 | +import androidx.compose.material3.dynamicLightColorScheme |
| 8 | +import androidx.compose.runtime.Composable |
| 9 | +import androidx.compose.runtime.SideEffect |
| 10 | +import androidx.compose.ui.graphics.toArgb |
| 11 | +import androidx.compose.ui.platform.LocalContext |
| 12 | +import androidx.compose.ui.platform.LocalView |
| 13 | +import androidx.core.view.WindowCompat |
| 14 | + |
| 15 | +@Composable |
| 16 | +actual fun KaMPKitTheme( |
| 17 | + darkTheme: Boolean, |
| 18 | + dynamicColor: Boolean, |
| 19 | + content: @Composable () -> Unit |
| 20 | +) { |
| 21 | + val colorScheme = when { |
| 22 | + // Dynamic color is only supported on Android 12+ |
| 23 | + dynamicColor && Build.VERSION.SDK_INT >= Build.VERSION_CODES.S -> { |
| 24 | + val context = LocalContext.current |
| 25 | + if (darkTheme) dynamicDarkColorScheme(context) else dynamicLightColorScheme(context) |
| 26 | + } |
| 27 | + darkTheme -> DarkColorPalette |
| 28 | + else -> LightColorPalette |
| 29 | + } |
| 30 | + |
| 31 | + // If not in Android Studio's preview then update also the system bars |
| 32 | + val view = LocalView.current |
| 33 | + if (!view.isInEditMode) { |
| 34 | + SideEffect { |
| 35 | + (view.context as Activity).window.apply { |
| 36 | + statusBarColor = colorScheme.primary.toArgb() |
| 37 | + WindowCompat |
| 38 | + .getInsetsController(this, view).apply { |
| 39 | + isAppearanceLightStatusBars = darkTheme |
| 40 | + isAppearanceLightNavigationBars = darkTheme |
| 41 | + } |
| 42 | + } |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + MaterialTheme( |
| 47 | + colorScheme = colorScheme, |
| 48 | + typography = Typography, |
| 49 | + shapes = Shapes, |
| 50 | + content = content |
| 51 | + ) |
| 52 | +} |
0 commit comments