-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Custom theming #77
base: main
Are you sure you want to change the base?
Custom theming #77
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package nl.q42.template.ui.compose.composables.widgets | ||
|
||
import androidx.compose.material3.Button | ||
import androidx.compose.material3.ButtonDefaults | ||
import androidx.compose.material3.Text | ||
import androidx.compose.runtime.Composable | ||
import nl.q42.template.ui.theme.AppTheme | ||
import nl.q42.template.ui.theme.PreviewLightDark | ||
|
||
@Composable | ||
fun TemplateButton(text: String, onClick: () -> Unit) { | ||
Button( | ||
onClick = onClick, | ||
colors = ButtonDefaults.buttonColors( | ||
containerColor = AppTheme.colors.accent, | ||
contentColor = AppTheme.colors.buttonText | ||
) | ||
) { | ||
Text( | ||
text = text, | ||
style = AppTheme.typography.body, | ||
color = AppTheme.colors.buttonText | ||
) | ||
} | ||
} | ||
|
||
@Composable | ||
@PreviewLightDark | ||
private fun TemplateButtonPreview() { | ||
AppTheme { | ||
TemplateButton("Button", {}) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package nl.q42.template.ui.theme | ||
|
||
import androidx.compose.ui.graphics.Color | ||
|
||
interface AppColorTokens { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i would name this |
||
val buttonText: Color | ||
val accent: Color | ||
val textPrimary: Color | ||
val surface: Color | ||
val error: Color | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package nl.q42.template.ui.theme | ||
|
||
import androidx.compose.ui.graphics.Color | ||
|
||
object AppColorTokensDark: AppColorTokens { | ||
override val buttonText: Color = White | ||
override val accent: Color = PurpleGrey80 | ||
override val textPrimary = White | ||
override val surface = White | ||
override val error = Pink80 | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package nl.q42.template.ui.theme | ||
|
||
import androidx.compose.ui.graphics.Color | ||
|
||
object AppColorTokensLight : AppColorTokens { | ||
override val buttonText: Color = White | ||
override val accent: Color = Purple40 | ||
override val textPrimary = Black | ||
override val surface: Color = White | ||
override val error: Color = Red80 | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package nl.q42.template.ui.theme | ||
|
||
import androidx.compose.ui.unit.dp | ||
|
||
object Dimens { | ||
object Containers { | ||
val cornerRadius = 8.dp | ||
val cornerRadiusLarge = 16.dp | ||
} | ||
Comment on lines
+6
to
+9
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if we only want this for the shapes, should we move it there instead of leaving it public here? it might be nice to force the usage of the |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,19 +2,17 @@ package nl.q42.template.ui.theme | |
|
||
import android.annotation.SuppressLint | ||
import android.app.Activity | ||
import android.os.Build | ||
import androidx.compose.foundation.isSystemInDarkTheme | ||
import androidx.compose.material3.ExperimentalMaterial3Api | ||
import androidx.compose.material3.MaterialTheme | ||
import androidx.compose.foundation.shape.RoundedCornerShape | ||
import androidx.compose.material3.Scaffold | ||
import androidx.compose.material3.darkColorScheme | ||
import androidx.compose.material3.dynamicDarkColorScheme | ||
import androidx.compose.material3.dynamicLightColorScheme | ||
import androidx.compose.material3.lightColorScheme | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.CompositionLocalProvider | ||
import androidx.compose.runtime.Immutable | ||
import androidx.compose.runtime.ReadOnlyComposable | ||
import androidx.compose.runtime.SideEffect | ||
import androidx.compose.runtime.staticCompositionLocalOf | ||
import androidx.compose.ui.graphics.Shape | ||
import androidx.compose.ui.graphics.toArgb | ||
import androidx.compose.ui.platform.LocalContext | ||
import androidx.compose.ui.platform.LocalView | ||
import androidx.core.view.WindowCompat | ||
|
||
|
@@ -23,64 +21,62 @@ import androidx.core.view.WindowCompat | |
* README file of our project. | ||
*/ | ||
|
||
private val DarkColorScheme = darkColorScheme( | ||
primary = Purple80, | ||
secondary = PurpleGrey80, | ||
tertiary = Pink80, | ||
background = Black, | ||
) | ||
|
||
private val LightColorScheme = lightColorScheme( | ||
primary = Purple40, | ||
secondary = PurpleGrey40, | ||
tertiary = Pink40, | ||
background = White | ||
|
||
/* Other default colors to override | ||
background = Color(0xFFFFFBFE), | ||
surface = Color(0xFFFFFBFE), | ||
onPrimary = Color.White, | ||
onSecondary = Color.White, | ||
onTertiary = Color.White, | ||
onBackground = Color(0xFF1C1B1F), | ||
onSurface = Color(0xFF1C1B1F), | ||
*/ | ||
) | ||
private val LocalAppTypography = staticCompositionLocalOf { AppTypography() } | ||
private val LocalAppColorTokens = staticCompositionLocalOf<AppColorTokens> { | ||
// Dummy default, will be replaced for the actual tokens by the Provider | ||
AppColorTokensLight | ||
} | ||
private val LocalAppShapes = staticCompositionLocalOf { AppShapes() } | ||
|
||
@Composable | ||
fun AppTheme( | ||
darkTheme: Boolean = isSystemInDarkTheme(), | ||
// Dynamic color is available on Android 12+ | ||
dynamicColor: Boolean = true, | ||
typography: AppTypography = AppTheme.typography, | ||
colors: AppColorTokens = AppTheme.colors, | ||
shapes: AppShapes = AppTheme.shapes, | ||
|
||
content: @Composable () -> Unit | ||
) { | ||
val colorScheme = when { | ||
dynamicColor && Build.VERSION.SDK_INT >= Build.VERSION_CODES.S -> { | ||
val context = LocalContext.current | ||
if (darkTheme) dynamicDarkColorScheme(context) else dynamicLightColorScheme(context) | ||
} | ||
|
||
darkTheme -> DarkColorScheme | ||
else -> LightColorScheme | ||
} | ||
val view = LocalView.current | ||
if (!view.isInEditMode) { | ||
SideEffect { | ||
val window = (view.context as Activity).window | ||
window.statusBarColor = colorScheme.primary.toArgb() | ||
window.statusBarColor = colors.accent.toArgb() | ||
WindowCompat.getInsetsController(window, view).isAppearanceLightStatusBars = darkTheme | ||
Comment on lines
42
to
45
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. any reason we are using this instead of https://developer.android.com/develop/ui/views/layout/edge-to-edge#enable-edge-to-edge-display |
||
} | ||
} | ||
|
||
MaterialTheme( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please revert: We still need the MaterialTheme wrapper for system Composables like AlertDialog |
||
colorScheme = colorScheme, | ||
typography = Typography, | ||
CompositionLocalProvider( | ||
LocalAppTypography provides typography, | ||
LocalAppColorTokens provides if (darkTheme) AppColorTokensDark else AppColorTokensLight, | ||
LocalAppShapes provides shapes, | ||
content = content | ||
) | ||
Comment on lines
-75
to
54
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i would expand this a little bit to also provide some crucial elements used thought the app (namely ripples & indicators):
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. potentially also a default |
||
} | ||
|
||
object AppTheme { | ||
val typography: AppTypography | ||
@Composable | ||
@ReadOnlyComposable | ||
get() = LocalAppTypography.current | ||
val colors: AppColorTokens | ||
@Composable | ||
@ReadOnlyComposable | ||
get() = LocalAppColorTokens.current | ||
val shapes: AppShapes | ||
@Composable | ||
@ReadOnlyComposable | ||
get() = LocalAppShapes.current | ||
} | ||
|
||
@Immutable | ||
data class AppShapes( | ||
val small: Shape = RoundedCornerShape(Dimens.Containers.cornerRadius), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should we also define a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should we move the shapes & type to its own files similar to colors? |
||
val medium: Shape = RoundedCornerShape(Dimens.Containers.cornerRadius), | ||
val large: Shape = RoundedCornerShape(Dimens.Containers.cornerRadiusLarge) | ||
) | ||
|
||
@SuppressLint("UnusedMaterial3ScaffoldPaddingParameter") | ||
@OptIn(ExperimentalMaterial3Api::class) | ||
@Composable | ||
fun PreviewAppTheme(content: @Composable () -> Unit) { | ||
AppTheme { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should we define every button color? not doing it could mean that the usage of this button is expanded without defining the colours for it and thus ending with a weirdly themed state