
Tally
App Bar
package com.composables.tally.components
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.RowScope
import androidx.compose.foundation.layout.WindowInsets
import androidx.compose.foundation.layout.WindowInsetsSides
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.only
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.safeDrawing
import androidx.compose.foundation.layout.statusBars
import androidx.compose.foundation.layout.union
import androidx.compose.foundation.layout.windowInsetsPadding
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.layout.Layout
import androidx.compose.ui.unit.Constraints
import androidx.compose.ui.unit.dp
import com.composables.tally.theme.ProvideTextStyle
import com.composables.tally.theme.Space
import com.composables.tally.theme.Theme
@Composable
fun AppBar(
modifier: Modifier = Modifier,
navigation: @Composable () -> Unit = {},
title: @Composable () -> Unit = {},
actions: @Composable RowScope.() -> Unit = {},
) {
val gap = Space * 2
Layout(
content = {
Box { navigation() }
Box { ProvideTextStyle(Theme.typography.title, title) }
Row(
horizontalArrangement = Arrangement.spacedBy(Space),
verticalAlignment = Alignment.CenterVertically,
) {
actions()
}
},
modifier =
modifier
.fillMaxWidth()
.background(Theme.colors.background)
.blockTouchesToContentBehind()
.windowInsetsPadding(
WindowInsets.statusBars.union(
WindowInsets.safeDrawing.only(WindowInsetsSides.Horizontal)
)
)
.height(56.dp)
.padding(horizontal = Space * 2),
) { (navigationSlot, titleSlot, actionsSlot), constraints ->
val loose = constraints.copy(minWidth = 0, minHeight = 0)
val navigationPlaceable = navigationSlot.measure(loose)
val actionsPlaceable = actionsSlot.measure(loose)
val side = maxOf(navigationPlaceable.width, actionsPlaceable.width) + gap.roundToPx()
val titleWidth = (constraints.maxWidth - side * 2).coerceAtLeast(0)
val titlePlaceable =
titleSlot.measure(Constraints(maxWidth = titleWidth, maxHeight = constraints.maxHeight))
val height = constraints.maxHeight
layout(constraints.maxWidth, height) {
navigationPlaceable.place(0, (height - navigationPlaceable.height) / 2)
titlePlaceable.place(
(constraints.maxWidth - titlePlaceable.width) / 2,
(height - titlePlaceable.height) / 2,
)
actionsPlaceable.place(
constraints.maxWidth - actionsPlaceable.width,
(height - actionsPlaceable.height) / 2,
)
}
}
}
private fun Modifier.blockTouchesToContentBehind() = pointerInput(Unit) {}
package com.composables.tally.theme
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.selection.LocalTextSelectionColors
import androidx.compose.foundation.text.selection.TextSelectionColors
import androidx.compose.runtime.*
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.compositionLocalOf
import androidx.compose.runtime.staticCompositionLocalOf
import androidx.compose.ui.graphics.*
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.*
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.*
import androidx.compose.ui.unit.*
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import com.composables.googlefonts.rememberGoogleFontFamily
@Immutable
data class ThemeColors(
val `background`: Color = Color(0xFFF6F3EC),
val `onBackground`: Color = Color(0xFF1C1B19),
val `surface`: Color = Color(0xFFFFFFFF),
val `onSurface`: Color = Color(0xFF1C1B19),
val `surfaceVariant`: Color = Color(0xFFECE7DC),
val `onSurfaceVariant`: Color = Color(0xFF1C1B19),
val `onBackgroundMuted`: Color = Color(0xFF6B665C),
val `primary`: Color = Color(0xFF1C1B19),
val `onPrimary`: Color = Color(0xFFFFFFFF),
val `border`: Color = Color(0xFFE2DCCF),
val `textSelection`: Color = Color(0x661C1B19),
val `destructive`: Color = Color(0xFFB3261E),
val `onDestructive`: Color = Color(0xFFFFFFFF),
val `amberLight`: Color = Color(0xFFFFD27A),
val `amber`: Color = Color(0xFFFF9F2E),
val `emberLight`: Color = Color(0xFFFFA273),
val `ember`: Color = Color(0xFFF25A2B),
val `orchidLight`: Color = Color(0xFFF0A6F5),
val `orchid`: Color = Color(0xFFC05CE6),
val `skyLight`: Color = Color(0xFF9EDCFF),
val `sky`: Color = Color(0xFF3FA2F2),
val `meadowLight`: Color = Color(0xFFB6EE9A),
val `meadow`: Color = Color(0xFF4CC46A),
)
@Immutable
data class ThemeTypography(
val `display`: TextStyle =
TextStyle(
fontFamily = FontFamily.SansSerif,
fontSize = 36.sp,
lineHeight = 40.sp,
fontWeight = FontWeight(400),
),
val `title`: TextStyle =
TextStyle(
fontFamily = FontFamily.SansSerif,
fontSize = 24.sp,
lineHeight = 30.sp,
fontWeight = FontWeight(400),
),
val `body`: TextStyle =
TextStyle(
fontFamily = FontFamily.SansSerif,
fontSize = 16.sp,
lineHeight = 22.sp,
fontWeight = FontWeight(400),
),
val `label`: TextStyle =
TextStyle(
fontFamily = FontFamily.SansSerif,
fontSize = 14.sp,
lineHeight = 18.sp,
fontWeight = FontWeight(600),
),
val `caption`: TextStyle =
TextStyle(
fontFamily = FontFamily.SansSerif,
fontSize = 11.sp,
lineHeight = 14.sp,
fontWeight = FontWeight(500),
),
)
@Immutable
data class ThemeShapes(
val `small`: Shape = RoundedCornerShape(12.dp),
val `medium`: Shape = RoundedCornerShape(20.dp),
val `large`: Shape = RoundedCornerShape(28.dp),
val `fullyRounded`: Shape = RoundedCornerShape(50),
)
fun themeColors(dark: Boolean = false): ThemeColors = ThemeColors()
@Composable
fun resolveThemeTypography(typography: ThemeTypography = ThemeTypography()): ThemeTypography {
val font0 = rememberGoogleFontFamily("Instrument Serif", weights = intArrayOf(400))
val font1 = rememberGoogleFontFamily("DM Sans", weights = intArrayOf(400, 500, 600))
return typography.copy(
`display` = typography.`display`.copy(fontFamily = font0),
`title` = typography.`title`.copy(fontFamily = font0),
`body` = typography.`body`.copy(fontFamily = font1),
`label` = typography.`label`.copy(fontFamily = font1),
`caption` = typography.`caption`.copy(fontFamily = font1),
)
}
private val LocalColors = staticCompositionLocalOf<ThemeColors> { error("Theme is missing") }
private val LocalTypography =
staticCompositionLocalOf<ThemeTypography> { error("Theme is missing") }
private val LocalSpace = staticCompositionLocalOf { 4.dp }
private val LocalShapes = staticCompositionLocalOf<ThemeShapes> { error("Theme is missing") }
val LocalContentColor = compositionLocalOf<Color> { error("Theme is missing") }
val LocalTextStyle = compositionLocalOf<TextStyle> { error("Theme is missing") }
object Theme {
val colors: ThemeColors
@Composable get() = LocalColors.current
val typography: ThemeTypography
@Composable get() = LocalTypography.current
val shapes: ThemeShapes
@Composable get() = LocalShapes.current
}
val Space: Dp
@Composable get() = LocalSpace.current
@Composable
fun TallyTheme(
colors: ThemeColors = themeColors(isSystemInDarkTheme()),
typography: ThemeTypography = resolveThemeTypography(),
space: Dp = 4.dp,
shapes: ThemeShapes = ThemeShapes(),
contentColor: Color = colors.onBackground,
textStyle: TextStyle = typography.body,
content: @Composable () -> Unit,
) {
CompositionLocalProvider(
LocalColors provides colors,
LocalTypography provides typography,
LocalSpace provides space,
LocalShapes provides shapes,
LocalContentColor provides contentColor,
LocalTextStyle provides textStyle,
LocalTextSelectionColors provides
TextSelectionColors(
handleColor = colors.primary,
backgroundColor = colors.textSelection,
),
content = content,
)
}
@Composable
fun ProvideContentColor(color: Color, content: @Composable () -> Unit) {
CompositionLocalProvider(LocalContentColor provides color, content = content)
}
@Composable
fun ProvideTextStyle(style: TextStyle, content: @Composable () -> Unit) {
CompositionLocalProvider(
LocalTextStyle provides LocalTextStyle.current.merge(style),
content = content,
)
}



















