session-android/app/src/main/java/org/thoughtcrime/securesms/ui/Themes.kt

154 lines
4.6 KiB
Kotlin
Raw Normal View History

2023-06-29 09:41:11 +02:00
package org.thoughtcrime.securesms.ui
import android.content.Context
import androidx.annotation.AttrRes
2023-07-09 16:42:42 +02:00
import androidx.appcompat.view.ContextThemeWrapper
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
2023-09-25 06:31:24 +02:00
import androidx.compose.material.LocalContentColor
2023-07-09 16:42:42 +02:00
import androidx.compose.material.MaterialTheme
2023-09-25 06:31:24 +02:00
import androidx.compose.material.Shapes
import androidx.compose.material.Typography
2023-06-29 09:41:11 +02:00
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
2023-09-25 06:31:24 +02:00
import androidx.compose.runtime.remember
2023-06-29 09:41:11 +02:00
import androidx.compose.runtime.staticCompositionLocalOf
2023-07-09 16:42:42 +02:00
import androidx.compose.ui.Modifier
2023-06-29 09:41:11 +02:00
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
2023-09-25 06:31:24 +02:00
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontWeight
2023-07-09 16:42:42 +02:00
import androidx.compose.ui.tooling.preview.PreviewParameterProvider
2023-09-25 06:31:24 +02:00
import androidx.compose.ui.unit.TextUnit
import androidx.compose.ui.unit.sp
import com.google.accompanist.themeadapter.appcompat.createAppCompatTheme
2023-06-29 09:41:11 +02:00
import com.google.android.material.color.MaterialColors
import network.loki.messenger.R
val LocalExtraColors = staticCompositionLocalOf<ExtraColors> { error("No Custom Attribute value provided") }
2023-07-09 17:35:09 +02:00
2023-06-29 09:41:11 +02:00
data class ExtraColors(
val settingsBackground: Color,
2023-11-16 15:40:27 +01:00
val prominentButtonColor: Color,
val lightCell: Color,
val onLightCell: Color,
2023-06-29 09:41:11 +02:00
)
2023-07-09 17:35:09 +02:00
/**
* Converts current Theme to Compose Theme.
*/
2023-06-29 09:41:11 +02:00
@Composable
fun AppTheme(
content: @Composable () -> Unit
) {
2023-10-19 02:13:09 +02:00
val context = LocalContext.current
val extraColors = context.run {
2023-06-29 09:41:11 +02:00
ExtraColors(
2023-07-09 16:42:42 +02:00
settingsBackground = getColorFromTheme(R.attr.colorSettingsBackground),
2023-09-26 07:27:28 +02:00
prominentButtonColor = getColorFromTheme(R.attr.prominentButtonColor),
2023-11-16 15:40:27 +01:00
lightCell = getColorFromTheme(R.attr.lightCell),
onLightCell = getColorFromTheme(R.attr.onLightCell),
2023-06-29 09:41:11 +02:00
)
}
2023-10-19 02:13:09 +02:00
val surface = context.getColorFromTheme(R.attr.colorSettingsBackground)
2023-06-29 09:41:11 +02:00
CompositionLocalProvider(LocalExtraColors provides extraColors) {
2023-10-19 02:13:09 +02:00
AppCompatTheme(surface = surface) {
2023-06-29 09:41:11 +02:00
content()
}
}
}
2023-07-09 16:42:42 +02:00
2023-09-25 06:31:24 +02:00
@Composable
fun AppCompatTheme(
context: Context = LocalContext.current,
readColors: Boolean = true,
typography: Typography = sessionTypography,
shapes: Shapes = MaterialTheme.shapes,
2023-10-19 02:13:09 +02:00
surface: Color? = null,
2023-09-25 06:31:24 +02:00
content: @Composable () -> Unit
) {
val themeParams = remember(context.theme) {
context.createAppCompatTheme(
readColors = readColors,
readTypography = false
)
}
2023-10-19 02:13:09 +02:00
val colors = themeParams.colors ?: MaterialTheme.colors
2023-09-25 06:31:24 +02:00
MaterialTheme(
2023-10-19 02:13:09 +02:00
colors = colors.copy(
surface = surface ?: colors.surface
),
2023-09-25 06:31:24 +02:00
typography = typography,
shapes = shapes,
) {
// We update the LocalContentColor to match our onBackground. This allows the default
// content color to be more appropriate to the theme background
CompositionLocalProvider(
LocalContentColor provides MaterialTheme.colors.onBackground,
content = content
)
}
}
fun boldStyle(size: TextUnit) = TextStyle.Default.copy(
fontWeight = FontWeight.Bold,
fontSize = size
)
fun defaultStyle(size: TextUnit) = TextStyle.Default.copy(fontSize = size)
val sessionTypography = Typography(
h1 = boldStyle(36.sp),
h2 = boldStyle(32.sp),
h3 = boldStyle(29.sp),
h4 = boldStyle(26.sp),
h5 = boldStyle(23.sp),
h6 = boldStyle(20.sp),
)
val Typography.base get() = defaultStyle(14.sp)
2023-09-26 07:27:28 +02:00
val Typography.baseBold get() = boldStyle(14.sp)
2023-09-25 06:31:24 +02:00
val Typography.small get() = defaultStyle(12.sp)
2023-10-19 17:15:33 +02:00
val Typography.extraSmall get() = defaultStyle(11.sp)
2023-09-25 06:31:24 +02:00
val Typography.h7 get() = boldStyle(18.sp)
val Typography.h8 get() = boldStyle(16.sp)
val Typography.h9 get() = boldStyle(14.sp)
2023-07-09 17:35:09 +02:00
fun Context.getColorFromTheme(@AttrRes attr: Int, defaultValue: Int = 0x0): Color =
MaterialColors.getColor(this, attr, defaultValue).let(::Color)
2023-07-09 16:42:42 +02:00
2023-07-09 17:35:09 +02:00
/**
* Set the theme and a background for Compose Previews.
*/
2023-07-09 16:42:42 +02:00
@Composable
2023-07-09 17:35:09 +02:00
fun PreviewTheme(
themeResId: Int,
content: @Composable () -> Unit
) {
2023-07-09 16:42:42 +02:00
CompositionLocalProvider(
LocalContext provides ContextThemeWrapper(LocalContext.current, themeResId)
) {
AppTheme {
2023-07-09 17:35:09 +02:00
Box(modifier = Modifier.background(color = MaterialTheme.colors.background)) {
content()
}
2023-07-09 16:42:42 +02:00
}
}
}
class ThemeResPreviewParameterProvider : PreviewParameterProvider<Int> {
override val values = sequenceOf(
R.style.Classic_Dark,
R.style.Classic_Light,
R.style.Ocean_Dark,
R.style.Ocean_Light,
)
}