Created
October 24, 2021 01:09
-
-
Save hakanai/987b501d0de72b256e8733db06470e10 to your computer and use it in GitHub Desktop.
Minimal(?) example of custom theme in Jetpack Compose
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import androidx.compose.desktop.Window | |
import androidx.compose.material.* | |
import androidx.compose.runtime.* | |
import androidx.compose.ui.graphics.Color | |
fun isSystemInDarkTheme(): Boolean { | |
// TODO: On Android, presumably this is `AppCompatDelegate.getDefaultNightMode()`. | |
// What is it on Desktop? | |
return false | |
} | |
@Composable | |
fun MyTheme( | |
darkTheme: Boolean = isSystemInDarkTheme(), | |
content: @Composable () -> Unit | |
) { | |
// The only two values I actually have in the brand | |
val desire = Color(0xe8, 0x37, 0x55) | |
val tuftsBlue = Color(0x41, 0x98, 0xdf) | |
// The rest I still have to provide for some reason, presumably Compose | |
// is too lazy to apply whatever function is necessary to darken or lighten | |
// a colour slightly? | |
val primaryColor = desire | |
val primaryLightColor = Color(0xff, 0x6f, 0x81) | |
val primaryDarkColor = Color(0xaf, 0x00, 0x2d) | |
val secondaryColor = tuftsBlue | |
val secondaryLightColor = Color(0x7c, 0xc8, 0xff) | |
val secondaryDarkColor = Color(0x00, 0x6a, 0xad) | |
// Also note how we have to figure out for ourselves how the terminology | |
// in the theme creation tools maps to the terminology in the APIs. | |
val colors = if (darkTheme) { | |
darkColors( | |
primary = primaryColor, | |
primaryVariant = primaryLightColor, | |
secondary = secondaryColor, | |
secondaryVariant = secondaryLightColor | |
) | |
} else { | |
lightColors( | |
primary = primaryColor, | |
primaryVariant = primaryDarkColor, | |
secondary = secondaryColor, | |
secondaryVariant = secondaryDarkColor | |
) | |
} | |
MaterialTheme(colors = colors) { | |
content() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment