Skip to content

Commit

Permalink
Merge pull request #266 from NUmeroAndDev/impl_color_contrast
Browse files Browse the repository at this point in the history
Support color constrast theming
  • Loading branch information
takahirom authored Aug 11, 2024
2 parents 982d761 + 0e5a83a commit 3f48752
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.github.droidkaigi.confsched

import android.annotation.SuppressLint
import android.app.UiModeManager
import android.content.ActivityNotFoundException
import android.content.Context
import android.content.Intent
Expand All @@ -14,6 +15,7 @@ import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.windowsizeclass.WindowSizeClass
import androidx.compose.runtime.Composable
import androidx.compose.runtime.ReadOnlyComposable
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
Expand All @@ -31,6 +33,7 @@ import io.github.droidkaigi.confsched.about.aboutScreenRoute
import io.github.droidkaigi.confsched.about.navigateAboutScreen
import io.github.droidkaigi.confsched.contributors.contributorsScreenRoute
import io.github.droidkaigi.confsched.contributors.contributorsScreens
import io.github.droidkaigi.confsched.designsystem.theme.ColorContrast
import io.github.droidkaigi.confsched.designsystem.theme.KaigiTheme
import io.github.droidkaigi.confsched.eventmap.eventMapScreens
import io.github.droidkaigi.confsched.eventmap.navigateEventMapScreen
Expand Down Expand Up @@ -69,7 +72,9 @@ fun KaigiApp(
displayFeatures: PersistentList<DisplayFeature>,
modifier: Modifier = Modifier,
) {
KaigiTheme {
KaigiTheme(
colorContrast = colorContrast(),
) {
Surface(
modifier = modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background,
Expand Down Expand Up @@ -344,3 +349,20 @@ private class ExternalNavController(
.launchUrl(context, uri)
}
}

@Composable
@ReadOnlyComposable
private fun colorContrast(): ColorContrast {
val uiModeManager =
LocalContext.current.getSystemService(Context.UI_MODE_SERVICE) as UiModeManager
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
when (uiModeManager.contrast) {
in 0.0f..0.33f -> ColorContrast.Default
in 0.34f..0.66f -> ColorContrast.Medium
in 0.67f..1.0f -> ColorContrast.High
else -> ColorContrast.Default
}
} else {
ColorContrast.Default
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import androidx.compose.material3.lightColorScheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.ui.graphics.Color
import io.github.droidkaigi.confsched.designsystem.theme.ColorContrast.Default
import io.github.droidkaigi.confsched.designsystem.theme.ColorContrast.High
import io.github.droidkaigi.confsched.designsystem.theme.ColorContrast.Medium

private val lightScheme = lightColorScheme(
primary = primaryLight,
Expand Down Expand Up @@ -251,9 +254,14 @@ private val fixedAccentColors = FixedAccentColors(

@Composable
fun KaigiTheme(
colorContrast: ColorContrast = Default,
content: @Composable () -> Unit,
) {
val colorScheme = darkScheme
val colorScheme = when (colorContrast) {
Default -> darkScheme
Medium -> mediumContrastDarkColorScheme
High -> highContrastDarkColorScheme
}

MaterialTheme(
colorScheme = colorScheme,
Expand All @@ -266,3 +274,7 @@ fun KaigiTheme(
},
)
}

enum class ColorContrast {
Default, Medium, High
}

0 comments on commit 3f48752

Please sign in to comment.