mirror of
https://github.com/D-Modem/app.git
synced 2026-11-28 17:38:05 -08:00
App theming: dark mode following the system, palettes, black
- Theme: System default (follows Android), Light or Dark; the system bars' icons and, from Android 12, the app's night mode follow it - Colours: the wallpaper's (Android 12+) or Violet, Phosphor, Amber, Ocean, Teal, Rose, Graphite, each a Material scheme made from tones of its hue (CIE LCh, kept in sRGB) - Black background for dark mode (OLED) - The window's own background light or dark before the first frame - Tests: every palette's text contrast at least 4.5:1 in both modes; the app drawn in each; the choices made in Settings Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0172Hejd12i4aYikYW9vgpZs
This commit is contained in:
8 files changed
+464
-8
No files matched your search
@@ -0,0 +1,68 @@
|
||||
// SPDX-License-Identifier: AGPL-3.0-only
|
||||
// Copyright (C) 2026 Neo-Desktop
|
||||
package com.neodesktop.dmodem.settings
|
||||
|
||||
import android.content.Context
|
||||
import androidx.datastore.core.DataStore
|
||||
import androidx.datastore.preferences.core.Preferences
|
||||
import androidx.datastore.preferences.core.booleanPreferencesKey
|
||||
import androidx.datastore.preferences.core.edit
|
||||
import androidx.datastore.preferences.core.stringPreferencesKey
|
||||
import androidx.datastore.preferences.preferencesDataStore
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.map
|
||||
|
||||
enum class DarkMode(val label: String) {
|
||||
SYSTEM("System default"),
|
||||
LIGHT("Light"),
|
||||
DARK("Dark"),
|
||||
}
|
||||
|
||||
/**
|
||||
* The app's colours (its menus, bars and dialogs; the terminal has its own):
|
||||
* the wallpaper's (Android 12 on), or one made from a hue and its chroma.
|
||||
*/
|
||||
enum class AppPalette(val label: String, val hue: Double, val chroma: Double) {
|
||||
DYNAMIC("Wallpaper", 0.0, 0.0),
|
||||
BASELINE("Violet", 300.0, 40.0),
|
||||
PHOSPHOR("Phosphor", 140.0, 48.0),
|
||||
AMBER("Amber", 70.0, 56.0),
|
||||
OCEAN("Ocean", 255.0, 44.0),
|
||||
TEAL("Teal", 195.0, 36.0),
|
||||
ROSE("Rose", 10.0, 48.0),
|
||||
GRAPHITE("Graphite", 250.0, 6.0),
|
||||
}
|
||||
|
||||
data class Appearance(
|
||||
val dark: DarkMode = DarkMode.SYSTEM,
|
||||
val palette: AppPalette = AppPalette.DYNAMIC,
|
||||
/** dark mode's surfaces black (OLED screens) */
|
||||
val black: Boolean = false,
|
||||
)
|
||||
|
||||
private val Context.appearanceStore: DataStore<Preferences> by preferencesDataStore("appearance")
|
||||
|
||||
class AppearanceStore(private val context: Context) {
|
||||
private object K {
|
||||
val dark = stringPreferencesKey("dark")
|
||||
val palette = stringPreferencesKey("palette")
|
||||
val black = booleanPreferencesKey("black")
|
||||
}
|
||||
|
||||
val appearance: Flow<Appearance> = context.appearanceStore.data.map { p ->
|
||||
val d = Appearance()
|
||||
Appearance(
|
||||
dark = p[K.dark]?.let { runCatching { DarkMode.valueOf(it) }.getOrNull() } ?: d.dark,
|
||||
palette = p[K.palette]?.let { runCatching { AppPalette.valueOf(it) }.getOrNull() } ?: d.palette,
|
||||
black = p[K.black] ?: d.black,
|
||||
)
|
||||
}
|
||||
|
||||
suspend fun save(a: Appearance) {
|
||||
context.appearanceStore.edit { p ->
|
||||
p[K.dark] = a.dark.name
|
||||
p[K.palette] = a.palette.name
|
||||
p[K.black] = a.black
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,116 @@
|
||||
// SPDX-License-Identifier: AGPL-3.0-only
|
||||
// Copyright (C) 2026 Neo-Desktop
|
||||
package com.neodesktop.dmodem.ui
|
||||
|
||||
import android.os.Build
|
||||
import androidx.compose.foundation.background
|
||||
import androidx.compose.foundation.border
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Box
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.ExperimentalLayoutApi
|
||||
import androidx.compose.foundation.layout.FlowRow
|
||||
import androidx.compose.foundation.layout.Row
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.layout.size
|
||||
import androidx.compose.foundation.layout.width
|
||||
import androidx.compose.foundation.selection.selectable
|
||||
import androidx.compose.foundation.shape.CircleShape
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.filled.Check
|
||||
import androidx.compose.material3.Icon
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.SegmentedButton
|
||||
import androidx.compose.material3.SegmentedButtonDefaults
|
||||
import androidx.compose.material3.SingleChoiceSegmentedButtonRow
|
||||
import androidx.compose.material3.Switch
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.material3.dynamicLightColorScheme
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.rememberCoroutineScope
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.draw.clip
|
||||
import androidx.compose.ui.graphics.Color
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.semantics.Role
|
||||
import androidx.compose.ui.text.style.TextAlign
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.lifecycle.compose.collectAsStateWithLifecycle
|
||||
import com.neodesktop.dmodem.settings.AppPalette
|
||||
import com.neodesktop.dmodem.settings.Appearance
|
||||
import com.neodesktop.dmodem.settings.AppearanceStore
|
||||
import com.neodesktop.dmodem.settings.DarkMode
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
/** The app's look: light, dark or the system's; its colours. Each change shown at once. */
|
||||
@OptIn(ExperimentalLayoutApi::class)
|
||||
@Composable
|
||||
fun AppearanceSection() {
|
||||
val context = LocalContext.current
|
||||
val store = remember { AppearanceStore(context.applicationContext) }
|
||||
val scope = rememberCoroutineScope()
|
||||
val current by store.appearance.collectAsStateWithLifecycle(initialValue = null)
|
||||
val a = current ?: return
|
||||
fun set(n: Appearance) {
|
||||
scope.launch { store.save(n) }
|
||||
}
|
||||
|
||||
Column(verticalArrangement = Arrangement.spacedBy(8.dp)) {
|
||||
Text("Appearance", style = MaterialTheme.typography.titleMedium)
|
||||
Text("Theme", style = MaterialTheme.typography.labelMedium)
|
||||
SingleChoiceSegmentedButtonRow(Modifier.fillMaxWidth()) {
|
||||
DarkMode.entries.forEachIndexed { i, m ->
|
||||
SegmentedButton(
|
||||
selected = a.dark == m,
|
||||
onClick = { set(a.copy(dark = m)) },
|
||||
shape = SegmentedButtonDefaults.itemShape(i, DarkMode.entries.size),
|
||||
) { Text(m.label, maxLines = 1) }
|
||||
}
|
||||
}
|
||||
Text("Colours", style = MaterialTheme.typography.labelMedium)
|
||||
val palettes = AppPalette.entries.filter { it != AppPalette.DYNAMIC || Build.VERSION.SDK_INT >= 31 }
|
||||
FlowRow(horizontalArrangement = Arrangement.spacedBy(12.dp), verticalArrangement = Arrangement.spacedBy(8.dp)) {
|
||||
for (p in palettes) {
|
||||
val selected = a.palette == p || (a.palette == AppPalette.DYNAMIC && p == AppPalette.BASELINE && Build.VERSION.SDK_INT < 31)
|
||||
Swatch(p, selected) { set(a.copy(palette = p)) }
|
||||
}
|
||||
}
|
||||
Row(Modifier.fillMaxWidth(), verticalAlignment = Alignment.CenterVertically) {
|
||||
Column(Modifier.weight(1f)) {
|
||||
Text("Black background")
|
||||
Text("In dark mode (for OLED screens)", style = MaterialTheme.typography.bodySmall)
|
||||
}
|
||||
Switch(checked = a.black, onCheckedChange = { set(a.copy(black = it)) })
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun Swatch(p: AppPalette, selected: Boolean, onPick: () -> Unit) {
|
||||
val context = LocalContext.current
|
||||
val (main, second) = if (p == AppPalette.DYNAMIC && Build.VERSION.SDK_INT >= 31) {
|
||||
val s = dynamicLightColorScheme(context)
|
||||
s.primary to s.tertiaryContainer
|
||||
} else {
|
||||
Palettes.swatch(p) to Palettes.scheme(p, dark = false).tertiaryContainer
|
||||
}
|
||||
Column(
|
||||
Modifier.width(64.dp).selectable(selected = selected, role = Role.RadioButton, onClick = onPick).padding(2.dp),
|
||||
horizontalAlignment = Alignment.CenterHorizontally,
|
||||
) {
|
||||
Box(
|
||||
Modifier.size(44.dp).clip(CircleShape).background(second)
|
||||
.border(if (selected) 3.dp else 1.dp, if (selected) MaterialTheme.colorScheme.onSurface else MaterialTheme.colorScheme.outlineVariant, CircleShape),
|
||||
contentAlignment = Alignment.Center,
|
||||
) {
|
||||
Box(Modifier.size(26.dp).clip(CircleShape).background(main), contentAlignment = Alignment.Center) {
|
||||
if (selected) Icon(Icons.Filled.Check, null, tint = Color.White, modifier = Modifier.size(16.dp))
|
||||
}
|
||||
}
|
||||
Text(p.label, style = MaterialTheme.typography.labelSmall, textAlign = TextAlign.Center, maxLines = 1)
|
||||
}
|
||||
}
|
||||
@@ -24,6 +24,11 @@ import androidx.compose.material3.NavigationBarItem
|
||||
import androidx.compose.material3.Scaffold
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.LaunchedEffect
|
||||
import androidx.activity.SystemBarStyle
|
||||
import android.app.UiModeManager
|
||||
import androidx.lifecycle.compose.collectAsStateWithLifecycle
|
||||
import com.neodesktop.dmodem.settings.AppearanceStore
|
||||
import com.neodesktop.dmodem.settings.DarkMode
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableIntStateOf
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
@@ -49,8 +54,18 @@ class MainActivity : ComponentActivity() {
|
||||
) {
|
||||
askNotifications.launch(Manifest.permission.POST_NOTIFICATIONS)
|
||||
}
|
||||
val appearanceStore = AppearanceStore(applicationContext)
|
||||
setContent {
|
||||
DModemTheme {
|
||||
val appearance by appearanceStore.appearance.collectAsStateWithLifecycle(initialValue = null)
|
||||
val a = appearance ?: return@setContent
|
||||
val dark = isDark(a)
|
||||
LaunchedEffect(dark) {
|
||||
// the bars' icons for the app's background, not the system's mode
|
||||
val bars = SystemBarStyle.auto(android.graphics.Color.TRANSPARENT, android.graphics.Color.TRANSPARENT) { dark }
|
||||
enableEdgeToEdge(statusBarStyle = bars, navigationBarStyle = bars)
|
||||
}
|
||||
LaunchedEffect(a.dark) { applyNightMode(a.dark) }
|
||||
DModemTheme(a) {
|
||||
var tab by rememberSaveable { mutableIntStateOf(TAB_MODEM) }
|
||||
var fullscreen by rememberSaveable { mutableStateOf(false) }
|
||||
val full = fullscreen && tab == TAB_TERMINAL
|
||||
@@ -90,6 +105,18 @@ class MainActivity : ComponentActivity() {
|
||||
}
|
||||
}
|
||||
|
||||
/** the system's own UI for the app (its splash screen, its dialogs) in the app's mode, from Android 12 */
|
||||
private fun applyNightMode(mode: DarkMode) {
|
||||
if (Build.VERSION.SDK_INT < 31) return
|
||||
val ui = getSystemService(UiModeManager::class.java) ?: return
|
||||
val want = when (mode) {
|
||||
DarkMode.SYSTEM -> UiModeManager.MODE_NIGHT_AUTO
|
||||
DarkMode.LIGHT -> UiModeManager.MODE_NIGHT_NO
|
||||
DarkMode.DARK -> UiModeManager.MODE_NIGHT_YES
|
||||
}
|
||||
runCatching { ui.setApplicationNightMode(want) }
|
||||
}
|
||||
|
||||
private fun setSystemBars(show: Boolean) {
|
||||
val c = WindowCompat.getInsetsController(window, window.decorView)
|
||||
if (show) {
|
||||
|
||||
@@ -104,6 +104,8 @@ fun SettingsScreen(modifier: Modifier = Modifier) {
|
||||
}
|
||||
}
|
||||
|
||||
androidx.compose.material3.HorizontalDivider(Modifier.padding(vertical = 8.dp))
|
||||
AppearanceSection()
|
||||
androidx.compose.material3.HorizontalDivider(Modifier.padding(vertical = 8.dp))
|
||||
TerminalSettingsSection()
|
||||
androidx.compose.material3.HorizontalDivider(Modifier.padding(vertical = 8.dp))
|
||||
|
||||
@@ -4,24 +4,142 @@ package com.neodesktop.dmodem.ui
|
||||
|
||||
import android.os.Build
|
||||
import androidx.compose.foundation.isSystemInDarkTheme
|
||||
import androidx.compose.material3.ColorScheme
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
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.ui.graphics.Color
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import com.neodesktop.dmodem.settings.AppPalette
|
||||
import com.neodesktop.dmodem.settings.Appearance
|
||||
import com.neodesktop.dmodem.settings.DarkMode
|
||||
import kotlin.math.cos
|
||||
import kotlin.math.pow
|
||||
import kotlin.math.sin
|
||||
|
||||
/** dark or not, as [a] has it (the system's by default) */
|
||||
@Composable
|
||||
fun isDark(a: Appearance): Boolean = when (a.dark) {
|
||||
DarkMode.SYSTEM -> isSystemInDarkTheme()
|
||||
DarkMode.LIGHT -> false
|
||||
DarkMode.DARK -> true
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun DModemTheme(content: @Composable () -> Unit) {
|
||||
val dark = isSystemInDarkTheme()
|
||||
val colors = when {
|
||||
Build.VERSION.SDK_INT >= 31 -> {
|
||||
fun DModemTheme(appearance: Appearance = Appearance(), content: @Composable () -> Unit) {
|
||||
val dark = isDark(appearance)
|
||||
val wallpaper = appearance.palette == AppPalette.DYNAMIC && Build.VERSION.SDK_INT >= 31
|
||||
var colors = when {
|
||||
wallpaper -> {
|
||||
val c = LocalContext.current
|
||||
if (dark) dynamicDarkColorScheme(c) else dynamicLightColorScheme(c)
|
||||
}
|
||||
dark -> darkColorScheme()
|
||||
else -> lightColorScheme()
|
||||
else -> Palettes.scheme(appearance.palette.takeIf { it != AppPalette.DYNAMIC } ?: AppPalette.BASELINE, dark)
|
||||
}
|
||||
if (dark && appearance.black) colors = Palettes.black(colors)
|
||||
MaterialTheme(colorScheme = colors, content = content)
|
||||
}
|
||||
|
||||
/**
|
||||
* Material colour schemes made from a hue: each role a tone (CIE L*) of the
|
||||
* palette's hue at its chroma (in LCh, cut down to what sRGB can show), as
|
||||
* Material's own tonal palettes have them.
|
||||
*/
|
||||
object Palettes {
|
||||
|
||||
/** the colour at L* [tone], chroma [c] and hue [h] (degrees) in CIE LCh(ab) */
|
||||
fun lch(tone: Double, c: Double, h: Double): Color {
|
||||
var lo = 0.0
|
||||
var hi = c
|
||||
var rgb = rgbOf(tone, c, h)
|
||||
if (rgb == null) {
|
||||
// the most chroma sRGB has at this tone and hue
|
||||
repeat(18) {
|
||||
val mid = (lo + hi) / 2
|
||||
if (rgbOf(tone, mid, h) != null) lo = mid else hi = mid
|
||||
}
|
||||
rgb = rgbOf(tone, lo, h)!!
|
||||
}
|
||||
return Color(rgb[0].toFloat(), rgb[1].toFloat(), rgb[2].toFloat())
|
||||
}
|
||||
|
||||
private fun rgbOf(l: Double, c: Double, hDeg: Double): DoubleArray? {
|
||||
val h = Math.toRadians(hDeg)
|
||||
val a = c * cos(h)
|
||||
val b = c * sin(h)
|
||||
val fy = (l + 16) / 116
|
||||
val fx = fy + a / 500
|
||||
val fz = fy - b / 200
|
||||
fun inv(t: Double) = if (t * t * t > 216.0 / 24389) t * t * t else (116 * t - 16) / (24389.0 / 27)
|
||||
val x = 0.95047 * inv(fx)
|
||||
val y = if (l > 8) fy * fy * fy else l / (24389.0 / 27)
|
||||
val z = 1.08883 * inv(fz)
|
||||
val lin = doubleArrayOf(
|
||||
3.2404542 * x - 1.5371385 * y - 0.4985314 * z,
|
||||
-0.9692660 * x + 1.8760108 * y + 0.0415560 * z,
|
||||
0.0556434 * x - 0.2040259 * y + 1.0572252 * z,
|
||||
)
|
||||
val eps = 1e-4
|
||||
if (lin.any { it < -eps || it > 1 + eps }) return null
|
||||
return DoubleArray(3) {
|
||||
val v = lin[it].coerceIn(0.0, 1.0)
|
||||
if (v <= 0.0031308) 12.92 * v else 1.055 * v.pow(1 / 2.4) - 0.055
|
||||
}
|
||||
}
|
||||
|
||||
private class Tones(val h: Double, val c: Double) {
|
||||
operator fun invoke(t: Int) = lch(t.toDouble(), c, h)
|
||||
}
|
||||
|
||||
fun scheme(p: AppPalette, dark: Boolean): ColorScheme {
|
||||
val a = Tones(p.hue, p.chroma)
|
||||
val b = Tones(p.hue, (p.chroma / 3).coerceAtMost(16.0))
|
||||
val c = Tones((p.hue + 60) % 360, (p.chroma / 2).coerceAtLeast(if (p.chroma < 10) p.chroma else 16.0))
|
||||
val n = Tones(p.hue, (p.chroma / 12).coerceAtMost(4.0))
|
||||
val v = Tones(p.hue, (p.chroma / 6).coerceAtMost(8.0))
|
||||
val e = Tones(25.0, 84.0)
|
||||
return if (!dark) {
|
||||
lightColorScheme(
|
||||
primary = a(40), onPrimary = a(100), primaryContainer = a(90), onPrimaryContainer = a(10), inversePrimary = a(80),
|
||||
secondary = b(40), onSecondary = b(100), secondaryContainer = b(90), onSecondaryContainer = b(10),
|
||||
tertiary = c(40), onTertiary = c(100), tertiaryContainer = c(90), onTertiaryContainer = c(10),
|
||||
background = n(98), onBackground = n(10), surface = n(98), onSurface = n(10),
|
||||
surfaceVariant = v(90), onSurfaceVariant = v(30), surfaceTint = a(40),
|
||||
inverseSurface = n(20), inverseOnSurface = n(95),
|
||||
error = e(40), onError = e(100), errorContainer = e(90), onErrorContainer = e(10),
|
||||
outline = v(50), outlineVariant = v(80), scrim = Color.Black,
|
||||
surfaceBright = n(98), surfaceDim = n(87), surfaceContainerLowest = n(100), surfaceContainerLow = n(96),
|
||||
surfaceContainer = n(94), surfaceContainerHigh = n(92), surfaceContainerHighest = n(90),
|
||||
)
|
||||
} else {
|
||||
darkColorScheme(
|
||||
primary = a(80), onPrimary = a(20), primaryContainer = a(30), onPrimaryContainer = a(90), inversePrimary = a(40),
|
||||
secondary = b(80), onSecondary = b(20), secondaryContainer = b(30), onSecondaryContainer = b(90),
|
||||
tertiary = c(80), onTertiary = c(20), tertiaryContainer = c(30), onTertiaryContainer = c(90),
|
||||
background = n(6), onBackground = n(90), surface = n(6), onSurface = n(90),
|
||||
surfaceVariant = v(30), onSurfaceVariant = v(80), surfaceTint = a(80),
|
||||
inverseSurface = n(90), inverseOnSurface = n(20),
|
||||
error = e(80), onError = e(20), errorContainer = e(30), onErrorContainer = e(90),
|
||||
outline = v(60), outlineVariant = v(30), scrim = Color.Black,
|
||||
surfaceBright = n(24), surfaceDim = n(6), surfaceContainerLowest = n(4), surfaceContainerLow = n(10),
|
||||
surfaceContainer = n(12), surfaceContainerHigh = n(17), surfaceContainerHighest = n(22),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/** a dark scheme on black: the surfaces black, their containers a step up each */
|
||||
fun black(s: ColorScheme): ColorScheme {
|
||||
fun up(by: Float) = Color(by, by, by)
|
||||
return s.copy(
|
||||
background = Color.Black, surface = Color.Black, surfaceDim = Color.Black,
|
||||
surfaceContainerLowest = Color.Black, surfaceContainerLow = up(0.04f), surfaceContainer = up(0.06f),
|
||||
surfaceContainerHigh = up(0.10f), surfaceContainerHighest = up(0.14f), surfaceBright = up(0.18f),
|
||||
)
|
||||
}
|
||||
|
||||
/** the swatch for a palette: its primary, as light mode has it */
|
||||
fun swatch(p: AppPalette): Color = Tones(p.hue, p.chroma)(40)
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
<style name="Theme.DModem" parent="android:Theme.Material.NoActionBar">
|
||||
<item name="android:windowBackground">@android:color/black</item>
|
||||
</style>
|
||||
</resources>
|
||||
@@ -1,4 +1,7 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
<style name="Theme.DModem" parent="android:Theme.Material.NoActionBar" />
|
||||
<!-- the window before the first frame: light, or dark in night mode (values-night) -->
|
||||
<style name="Theme.DModem" parent="android:Theme.Material.Light.NoActionBar">
|
||||
<item name="android:windowBackground">@android:color/white</item>
|
||||
</style>
|
||||
</resources>
|
||||
@@ -0,0 +1,116 @@
|
||||
// SPDX-License-Identifier: AGPL-3.0-only
|
||||
// Copyright (C) 2026 Neo-Desktop
|
||||
package com.neodesktop.dmodem.ui
|
||||
|
||||
import android.content.Context
|
||||
import android.graphics.Bitmap
|
||||
import androidx.compose.ui.graphics.asAndroidBitmap
|
||||
import androidx.compose.ui.graphics.luminance
|
||||
import androidx.compose.ui.test.captureToImage
|
||||
import androidx.compose.ui.test.hasText
|
||||
import androidx.compose.ui.test.isSelectable
|
||||
import androidx.compose.ui.test.junit4.createEmptyComposeRule
|
||||
import androidx.compose.ui.test.onNodeWithText
|
||||
import androidx.compose.ui.test.onRoot
|
||||
import androidx.compose.ui.test.performClick
|
||||
import androidx.compose.ui.test.performScrollTo
|
||||
import androidx.test.core.app.ActivityScenario
|
||||
import androidx.test.core.app.ApplicationProvider
|
||||
import com.neodesktop.dmodem.settings.AppPalette
|
||||
import com.neodesktop.dmodem.settings.Appearance
|
||||
import com.neodesktop.dmodem.settings.AppearanceStore
|
||||
import com.neodesktop.dmodem.settings.DarkMode
|
||||
import kotlinx.coroutines.flow.first
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Rule
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.robolectric.RobolectricTestRunner
|
||||
import org.robolectric.annotation.Config
|
||||
import org.robolectric.annotation.GraphicsMode
|
||||
import java.io.File
|
||||
|
||||
/** Light, dark and black, in each palette: the schemes, and the app drawn in them into build/screens/. */
|
||||
@RunWith(RobolectricTestRunner::class)
|
||||
@GraphicsMode(GraphicsMode.Mode.NATIVE)
|
||||
@Config(sdk = [35], qualifiers = "w411dp-h891dp-xxhdpi")
|
||||
class AppearanceTest {
|
||||
@get:Rule
|
||||
val compose = createEmptyComposeRule()
|
||||
|
||||
private val ctx get() = ApplicationProvider.getApplicationContext<Context>()
|
||||
private val store get() = AppearanceStore(ctx)
|
||||
|
||||
private fun shot(name: String) {
|
||||
compose.waitForIdle()
|
||||
val bmp = compose.onRoot().captureToImage().asAndroidBitmap()
|
||||
File("build/screens").mkdirs()
|
||||
File("build/screens/$name.png").outputStream().use { bmp.compress(Bitmap.CompressFormat.PNG, 100, it) }
|
||||
}
|
||||
|
||||
private fun tab(name: String) = compose.onNode(hasText(name) and isSelectable()).performClick()
|
||||
|
||||
@Test
|
||||
fun everyPaletteReadsInLightAndDark() {
|
||||
for (p in AppPalette.entries.filter { it != AppPalette.DYNAMIC }) {
|
||||
val light = Palettes.scheme(p, dark = false)
|
||||
val dark = Palettes.scheme(p, dark = true)
|
||||
assertTrue("$p light surface", light.surface.luminance() > 0.85f)
|
||||
assertTrue("$p dark surface", dark.surface.luminance() < 0.02f)
|
||||
// text on its backgrounds: WCAG contrast of at least 4.5:1
|
||||
fun contrast(a: androidx.compose.ui.graphics.Color, b: androidx.compose.ui.graphics.Color): Float {
|
||||
val (hi, lo) = listOf(a.luminance(), b.luminance()).sortedDescending()
|
||||
return (hi + 0.05f) / (lo + 0.05f)
|
||||
}
|
||||
for (s in listOf(light, dark)) {
|
||||
assertTrue("$p onPrimary", contrast(s.primary, s.onPrimary) >= 4.5f)
|
||||
assertTrue("$p onSurface", contrast(s.surface, s.onSurface) >= 4.5f)
|
||||
assertTrue("$p onPrimaryContainer", contrast(s.primaryContainer, s.onPrimaryContainer) >= 4.5f)
|
||||
}
|
||||
}
|
||||
val black = Palettes.black(Palettes.scheme(AppPalette.AMBER, dark = true))
|
||||
assertEquals(0f, black.surface.luminance())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun theAppInEach() {
|
||||
val looks = listOf(
|
||||
"light-violet" to Appearance(DarkMode.LIGHT, AppPalette.BASELINE),
|
||||
"dark-phosphor" to Appearance(DarkMode.DARK, AppPalette.PHOSPHOR),
|
||||
"black-amber" to Appearance(DarkMode.DARK, AppPalette.AMBER, black = true),
|
||||
"dark-ocean" to Appearance(DarkMode.DARK, AppPalette.OCEAN),
|
||||
)
|
||||
for ((name, a) in looks) {
|
||||
runBlocking { store.save(a) }
|
||||
ActivityScenario.launch(MainActivity::class.java).use {
|
||||
compose.waitForIdle()
|
||||
shot("theme-$name-home")
|
||||
if (name == "dark-ocean" || name == "light-violet") {
|
||||
tab("Settings")
|
||||
compose.onNodeWithText("Black background").performScrollTo()
|
||||
shot("theme-$name-settings")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun chosenInSettings() {
|
||||
runBlocking { store.save(Appearance()) }
|
||||
ActivityScenario.launch(MainActivity::class.java).use {
|
||||
tab("Settings")
|
||||
compose.onNodeWithText("Black background").performScrollTo()
|
||||
compose.onNodeWithText("Dark").performClick()
|
||||
compose.onNodeWithText("Teal").performClick()
|
||||
compose.waitForIdle()
|
||||
val a = runBlocking { store.appearance.first() }
|
||||
assertEquals(DarkMode.DARK, a.dark)
|
||||
assertEquals(AppPalette.TEAL, a.palette)
|
||||
compose.onNodeWithText("System default").performClick()
|
||||
compose.waitForIdle()
|
||||
assertEquals(DarkMode.SYSTEM, runBlocking { store.appearance.first() }.dark)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user