diff --git a/CHANGELOG.md b/CHANGELOG.md index 4904edad6..1a0fc7583 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,10 @@ Emojis for the following are chosen based on [gitmoji](https://gitmoji.dev/). ## [Upcoming] Scribe-Android v1.X.Y +### 🎨 Design + +- Compose theme colors now come from the shared `scribe_*` resources. Dark mode button text stays orange on orange. The dark caption uses the named token `#CAC4D0`, the same grey this slot already used ([#782](https://github.com/scribe-org/Scribe-Android/issues/782)). + ### 🔒 Security - The unused `WRITE_EXTERNAL_STORAGE` permission was removed so the app requests only network access ([#791](https://github.com/scribe-org/Scribe-Android/issues/791)). diff --git a/app/src/main/java/be/scri/ui/theme/Colors.kt b/app/src/main/java/be/scri/ui/theme/Colors.kt index 86c72bfe0..5624a6f03 100644 --- a/app/src/main/java/be/scri/ui/theme/Colors.kt +++ b/app/src/main/java/be/scri/ui/theme/Colors.kt @@ -2,42 +2,76 @@ package be.scri.ui.theme +import android.content.Context +import android.content.res.Configuration +import androidx.compose.material3.ColorScheme +import androidx.compose.material3.darkColorScheme +import androidx.compose.material3.lightColorScheme import androidx.compose.ui.graphics.Color - -val theme_light_card_view_color = Color(color = 0xFFFFFFFF) -val theme_light_button_color = Color(color = 0xFFFDAD0D) -val theme_light_button_text_color = Color(color = 0xFF000000) -val theme_light_background = Color(color = 0xFF5EB5E8) -val theme_light_text_color = Color(color = 0xFF000000) -val theme_light_selected_button_color = Color(color = 0xFF235270) -val theme_light_switch_container_color = Color(color = 0xFFAAAAAA) -val theme_light_switch_selector_color = Color(color = 0xFFFEDE9E) -val theme_light_unchecked_switch_selector_color = Color(color = 0xFFEEEEEE) -val theme_light_corner_button_color = Color(color = 0xFFFDAD0D) -val theme_light_success_color = Color(color = 0xFF9BC53D) -val theme_light_caption_color = Color(color = 0xE558564B) - -val theme_dark_card_view_color = Color(color = 0xFF000000) -val theme_dark_button_color = Color(color = 0xFFD17B0F) -val theme_dark_button_text_color = Color(color = 0xFFD17B0F) -val theme_dark_background = Color(color = 0xFF235270) -val theme_dark_text_color = Color(color = 0xFFFFFFFF) -val theme_dark_button_outline_color = Color(color = 0xFFD17B0F) -val theme_dark_selected_button_color = Color(color = 0xFF5EB5E8) -val theme_dark_switch_container_color = Color(color = 0xFFAAAAAA) -val theme_dark_switch_selector_color = Color(color = 0xFF2A1903) -val theme_dark_unchecked_switch_selector_color = Color(color = 0xFF2D2D2D) -val theme_dark_corner_button_color = Color(color = 0xFF2A1903) -val theme_dark_success_color = Color(color = 0xFF08A045) +import androidx.core.content.ContextCompat +import be.scri.R /** - * Custom colors for use throughout the application. + * Compose color scheme from the shared `scribe_*` resources. + * [dark] follows the app theme argument, not the device night mode. */ -@Suppress("MagicNumber") -val BlueSky = Color(0xFF4478a9) +fun Context.scribeColorScheme(dark: Boolean): ColorScheme { + val themed = configurationContext(dark) + val button = themed.scribeColor(R.color.scribe_button) + val onButton = themed.scribeColor(R.color.scribe_on_button) + val background = themed.scribeColor(R.color.scribe_background) + val text = themed.scribeColor(R.color.scribe_text) + val surface = themed.scribeColor(R.color.scribe_surface) + val outline = themed.scribeColor(R.color.scribe_outline) + val selected = themed.scribeColor(R.color.scribe_selected) + val switchThumb = themed.scribeColor(R.color.scribe_switch_thumb) + val switchTrack = themed.scribeColor(R.color.scribe_switch_track) + val switchThumbOff = themed.scribeColor(R.color.scribe_switch_thumb_off) + val cornerButton = themed.scribeColor(R.color.scribe_corner_button) + val success = themed.scribeColor(R.color.scribe_success) + val caption = themed.scribeColor(R.color.scribe_caption) + return if (dark) { + darkColorScheme( + primary = button, + onPrimary = onButton, + background = background, + onBackground = text, + surface = surface, + onSurface = text, + outline = outline, + secondary = selected, + tertiary = switchThumb, + tertiaryContainer = switchTrack, + outlineVariant = switchThumbOff, + surfaceContainer = cornerButton, + surfaceVariant = success, + onSurfaceVariant = caption, + ) + } else { + lightColorScheme( + primary = button, + onPrimary = onButton, + background = background, + onBackground = text, + surface = surface, + onSurface = text, + outline = outline, + secondary = selected, + tertiary = switchThumb, + tertiaryContainer = switchTrack, + outlineVariant = switchThumbOff, + surfaceContainer = cornerButton, + surfaceVariant = success, + onSurfaceVariant = caption, + ) + } +} -@Suppress("MagicNumber") -val NightSky = Color(0xFF333333) +private fun Context.configurationContext(dark: Boolean): Context { + val nightMode = if (dark) Configuration.UI_MODE_NIGHT_YES else Configuration.UI_MODE_NIGHT_NO + val config = Configuration(resources.configuration) + config.uiMode = (config.uiMode and Configuration.UI_MODE_NIGHT_MASK.inv()) or nightMode + return createConfigurationContext(config) +} -@Suppress("MagicNumber") -val BorderColor = Color(0x40000000) +private fun Context.scribeColor(id: Int): Color = Color(color = ContextCompat.getColor(this, id)) diff --git a/app/src/main/java/be/scri/ui/theme/ScribeTheme.kt b/app/src/main/java/be/scri/ui/theme/ScribeTheme.kt index 644648cbb..2114b2fdf 100644 --- a/app/src/main/java/be/scri/ui/theme/ScribeTheme.kt +++ b/app/src/main/java/be/scri/ui/theme/ScribeTheme.kt @@ -3,44 +3,9 @@ package be.scri.ui.theme import androidx.compose.material3.MaterialTheme -import androidx.compose.material3.darkColorScheme -import androidx.compose.material3.lightColorScheme import androidx.compose.runtime.Composable - -private val LightColors = - lightColorScheme( - primary = theme_light_button_color, - onPrimary = theme_light_button_text_color, - background = theme_light_background, - onBackground = theme_light_text_color, - surface = theme_light_card_view_color, - onSurface = theme_light_text_color, - outline = theme_light_button_color, - secondary = theme_light_selected_button_color, - tertiary = theme_light_switch_selector_color, - tertiaryContainer = theme_light_switch_container_color, - outlineVariant = theme_light_unchecked_switch_selector_color, - surfaceContainer = theme_light_corner_button_color, - surfaceVariant = theme_light_success_color, - onSurfaceVariant = theme_light_caption_color, - ) - -private val DarkColors = - darkColorScheme( - primary = theme_dark_button_color, - onPrimary = theme_dark_button_text_color, - background = theme_dark_background, - onBackground = theme_dark_text_color, - surface = theme_dark_card_view_color, - onSurface = theme_dark_text_color, - outline = theme_dark_button_outline_color, - secondary = theme_dark_selected_button_color, - tertiary = theme_dark_switch_selector_color, - tertiaryContainer = theme_dark_switch_container_color, - outlineVariant = theme_dark_unchecked_switch_selector_color, - surfaceContainer = theme_dark_corner_button_color, - surfaceVariant = theme_dark_success_color, - ) +import androidx.compose.runtime.remember +import androidx.compose.ui.platform.LocalContext /** * Light and dark mode themes for the application. @@ -52,12 +17,8 @@ fun ScribeTheme( @Suppress("ktlint:standard:annotation") content: @Composable() () -> Unit, ) { - val colors = - if (!useDarkTheme) { - LightColors - } else { - DarkColors - } + val context = LocalContext.current + val colors = remember(context, useDarkTheme) { context.scribeColorScheme(useDarkTheme) } val typography = if (isIncreaseTextSize) createTypography(1.25f) else ScribeTypography diff --git a/app/src/test/kotlin/be/scri/ui/theme/ScribeColorSchemeTest.kt b/app/src/test/kotlin/be/scri/ui/theme/ScribeColorSchemeTest.kt new file mode 100644 index 000000000..f17449950 --- /dev/null +++ b/app/src/test/kotlin/be/scri/ui/theme/ScribeColorSchemeTest.kt @@ -0,0 +1,86 @@ +// SPDX-License-Identifier: GPL-3.0-or-later + +package be.scri.ui.theme + +import android.content.Context +import androidx.compose.material3.ColorScheme +import androidx.compose.ui.graphics.toArgb +import androidx.test.core.app.ApplicationProvider +import org.junit.Assert.assertEquals +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.annotation.Config + +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [34]) +class ScribeColorSchemeTest { + @Test + @Config(qualifiers = "night") + fun lightSchemeFollowsTheArgumentWhenTheDeviceIsNight() { + val context = ApplicationProvider.getApplicationContext() + val scheme = context.scribeColorScheme(dark = false) + // All 14 slots set in Colors.kt, from the light scribe_* resources. + scheme.assertSlots( + "primary" to 0xFFFDAD0D.toInt(), + "onPrimary" to 0xFF000000.toInt(), + "background" to 0xFF5EB5E8.toInt(), + "onBackground" to 0xFF000000.toInt(), + "surface" to 0xFFFFFFFF.toInt(), + "onSurface" to 0xFF000000.toInt(), + "outline" to 0xFFFDAD0D.toInt(), + "secondary" to 0xFF235270.toInt(), + "tertiary" to 0xFFFEDE9E.toInt(), + "tertiaryContainer" to 0xFFAAAAAA.toInt(), + "outlineVariant" to 0xFFEEEEEE.toInt(), + "surfaceContainer" to 0xFFFDAD0D.toInt(), + "surfaceVariant" to 0xFF9BC53D.toInt(), + "onSurfaceVariant" to 0xE558564B.toInt(), + ) + } + + @Test + fun darkSchemeFollowsTheArgumentWhenTheDeviceIsDay() { + val context = ApplicationProvider.getApplicationContext() + val scheme = context.scribeColorScheme(dark = true) + // All 14 slots set in Colors.kt, from the night scribe_* resources. + scheme.assertSlots( + "primary" to 0xFFD17B0F.toInt(), + "onPrimary" to 0xFFD17B0F.toInt(), + "background" to 0xFF235270.toInt(), + "onBackground" to 0xFFFFFFFF.toInt(), + "surface" to 0xFF000000.toInt(), + "onSurface" to 0xFFFFFFFF.toInt(), + "outline" to 0xFFD17B0F.toInt(), + "secondary" to 0xFF5EB5E8.toInt(), + "tertiary" to 0xFF2A1903.toInt(), + "tertiaryContainer" to 0xFFAAAAAA.toInt(), + "outlineVariant" to 0xFF2D2D2D.toInt(), + "surfaceContainer" to 0xFF2A1903.toInt(), + "surfaceVariant" to 0xFF08A045.toInt(), + "onSurfaceVariant" to 0xFFCAC4D0.toInt(), + ) + } +} + +private fun ColorScheme.assertSlots(vararg expected: Pair) { + val actual = + listOf( + "primary" to primary.toArgb(), + "onPrimary" to onPrimary.toArgb(), + "background" to background.toArgb(), + "onBackground" to onBackground.toArgb(), + "surface" to surface.toArgb(), + "onSurface" to onSurface.toArgb(), + "outline" to outline.toArgb(), + "secondary" to secondary.toArgb(), + "tertiary" to tertiary.toArgb(), + "tertiaryContainer" to tertiaryContainer.toArgb(), + "outlineVariant" to outlineVariant.toArgb(), + "surfaceContainer" to surfaceContainer.toArgb(), + "surfaceVariant" to surfaceVariant.toArgb(), + "onSurfaceVariant" to onSurfaceVariant.toArgb(), + ) + assertEquals(14, actual.size) + assertEquals(expected.toList(), actual) +}