Adjust calendar color according to theme
This commit is contained in:
parent
44ef3c46ae
commit
a16c6ea226
@ -48,5 +48,6 @@ dependencies {
|
||||
implementation(project(":base"))
|
||||
implementation(project(":hiddenitems"))
|
||||
implementation(project(":permissions"))
|
||||
implementation(project(":material-color-utilities"))
|
||||
|
||||
}
|
||||
@ -18,6 +18,9 @@ import de.mm20.launcher2.calendar.R
|
||||
import de.mm20.launcher2.graphics.TextDrawable
|
||||
import de.mm20.launcher2.icons.LauncherIcon
|
||||
import de.mm20.launcher2.ktx.dp
|
||||
import hct.Hct
|
||||
import palettes.TonalPalette
|
||||
import scheme.Scheme
|
||||
import java.text.SimpleDateFormat
|
||||
import java.util.*
|
||||
|
||||
@ -48,7 +51,7 @@ class CalendarEvent(
|
||||
typeface = Typeface.DEFAULT_BOLD,
|
||||
height = s
|
||||
)
|
||||
val background = ColorDrawable(getDisplayColor(context, color))
|
||||
val background = ColorDrawable(getDisplayColor())
|
||||
return LauncherIcon(
|
||||
foreground = foreground,
|
||||
background = background,
|
||||
@ -61,50 +64,9 @@ class CalendarEvent(
|
||||
return Intent(Intent.ACTION_VIEW).setData(uri).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
|
||||
}
|
||||
|
||||
companion object {
|
||||
fun getDisplayColor(context: Context, color: Int): Int {
|
||||
val hsl = FloatArray(3).let {
|
||||
ColorUtils.RGBToHSL(color.red, color.green, color.blue, it)
|
||||
it
|
||||
}
|
||||
return if (context.resources.getBoolean(R.bool.is_dark_theme)) {
|
||||
if (ColorUtils.calculateContrast(
|
||||
ContextCompat.getColor(
|
||||
context,
|
||||
R.color.calendar_foreground_color
|
||||
), color
|
||||
) < 2.5 || true
|
||||
) {
|
||||
if (color.red == color.green && color.red == color.blue) {
|
||||
val level = 0xFF - ((0xFF - color.red) * 0.7f).toInt()
|
||||
Color.rgb(level, level, level)
|
||||
} else {
|
||||
hsl[2] = hsl[2] + (1 - hsl[2]) * 0.2f
|
||||
hsl[1] = 1 - (1 - hsl[1]) * 0.9f
|
||||
ColorUtils.HSLToColor(hsl)
|
||||
}
|
||||
} else return color
|
||||
} else {
|
||||
if (ColorUtils.calculateContrast(
|
||||
ContextCompat.getColor(
|
||||
context,
|
||||
R.color.calendar_foreground_color
|
||||
), color
|
||||
) < 1.8
|
||||
) {
|
||||
if (color.red == color.green && color.red == color.blue) {
|
||||
val level = (color.red * 0.7f).toInt()
|
||||
Color.rgb(level, level, level)
|
||||
} else {
|
||||
hsl[2] = (0.5f - hsl[2]) * 0.8f + hsl[2]
|
||||
hsl[1] = 1 - (1 - hsl[1]) * 0.8f
|
||||
ColorUtils.HSLToColor(hsl)
|
||||
}
|
||||
} else return color
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fun getDisplayColor(): Int {
|
||||
val palette = TonalPalette.fromInt(color)
|
||||
return palette.tone(70)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -35,9 +35,11 @@ import de.mm20.launcher2.ui.component.DefaultToolbarAction
|
||||
import de.mm20.launcher2.ui.component.Toolbar
|
||||
import de.mm20.launcher2.ui.component.ToolbarAction
|
||||
import de.mm20.launcher2.ui.ktx.toDp
|
||||
import de.mm20.launcher2.ui.locals.LocalDarkTheme
|
||||
import de.mm20.launcher2.ui.locals.LocalFavoritesEnabled
|
||||
import de.mm20.launcher2.ui.locals.LocalSnackbarHostState
|
||||
import kotlinx.coroutines.launch
|
||||
import palettes.TonalPalette
|
||||
|
||||
@Composable
|
||||
fun CalendarItem(
|
||||
@ -52,10 +54,15 @@ fun CalendarItem(
|
||||
val lifecycleOwner = LocalLifecycleOwner.current
|
||||
val snackbarHostState = LocalSnackbarHostState.current
|
||||
|
||||
val darkMode = LocalDarkTheme.current
|
||||
|
||||
Row(
|
||||
modifier = modifier
|
||||
.drawBehind {
|
||||
drawRect(Color(calendar.color), Offset.Zero, this.size.copy(width = 8.dp.toPx()))
|
||||
val color = TonalPalette.fromInt(calendar.color).tone(
|
||||
if (darkMode) 80 else 40
|
||||
)
|
||||
drawRect(Color(color), Offset.Zero, this.size.copy(width = 8.dp.toPx()))
|
||||
}
|
||||
.padding(start = 8.dp),
|
||||
) {
|
||||
|
||||
@ -4,7 +4,6 @@ import android.appwidget.AppWidgetHost
|
||||
import androidx.compose.material3.SnackbarHostState
|
||||
import androidx.compose.runtime.compositionLocalOf
|
||||
import androidx.compose.ui.geometry.Size
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.navigation.NavController
|
||||
import de.mm20.launcher2.preferences.Settings
|
||||
@ -25,6 +24,8 @@ val LocalGridIconSize = compositionLocalOf { 48.dp }
|
||||
|
||||
val LocalSnackbarHostState = compositionLocalOf { SnackbarHostState() }
|
||||
|
||||
val LocalDarkTheme = compositionLocalOf { false }
|
||||
|
||||
/**
|
||||
* Workaround a bug in Jetpack Compose which incorrectly places popups
|
||||
* that are nested inside other popups.
|
||||
|
||||
@ -22,6 +22,7 @@ import de.mm20.launcher2.ui.R
|
||||
import de.mm20.launcher2.ui.component.preferences.Preference
|
||||
import de.mm20.launcher2.ui.component.preferences.PreferenceCategory
|
||||
import de.mm20.launcher2.ui.component.preferences.PreferenceScreen
|
||||
import de.mm20.launcher2.ui.locals.LocalDarkTheme
|
||||
import de.mm20.launcher2.ui.locals.LocalNavController
|
||||
import de.mm20.launcher2.ui.theme.colorSchemeAsState
|
||||
|
||||
@ -46,7 +47,7 @@ fun ColorSchemeSettingsScreen() {
|
||||
}
|
||||
|
||||
for (cs in items) {
|
||||
val scheme by colorSchemeAsState(cs.first)
|
||||
val scheme by colorSchemeAsState(cs.first, LocalDarkTheme.current)
|
||||
Preference(
|
||||
title = cs.second,
|
||||
icon = if (colorScheme == cs.first) Icons.Rounded.RadioButtonChecked else Icons.Rounded.RadioButtonUnchecked,
|
||||
|
||||
@ -12,6 +12,7 @@ import de.mm20.launcher2.preferences.LauncherDataStore
|
||||
import de.mm20.launcher2.preferences.Settings
|
||||
import de.mm20.launcher2.preferences.Settings.AppearanceSettings
|
||||
import de.mm20.launcher2.preferences.Settings.AppearanceSettings.Theme
|
||||
import de.mm20.launcher2.ui.locals.LocalDarkTheme
|
||||
import de.mm20.launcher2.ui.theme.colorscheme.*
|
||||
import de.mm20.launcher2.ui.theme.typography.DefaultTypography
|
||||
import kotlinx.coroutines.flow.map
|
||||
@ -34,26 +35,29 @@ fun LauncherTheme(
|
||||
AppearanceSettings.ColorScheme.Default
|
||||
)
|
||||
|
||||
val colorScheme by colorSchemeAsState(colorSchemePreference)
|
||||
|
||||
MaterialTheme(
|
||||
colorScheme = colorScheme,
|
||||
typography = DefaultTypography,
|
||||
content = content
|
||||
)
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun colorSchemeAsState(colorScheme: AppearanceSettings.ColorScheme): MutableState<ColorScheme> {
|
||||
val context = LocalContext.current
|
||||
val dataStore: LauncherDataStore by inject()
|
||||
|
||||
val themePreference by remember { dataStore.data.map { it.appearance.theme } }.collectAsState(
|
||||
Theme.System
|
||||
)
|
||||
val darkTheme =
|
||||
themePreference == Theme.Dark || themePreference == Theme.System && isSystemInDarkTheme()
|
||||
|
||||
val colorScheme by colorSchemeAsState(colorSchemePreference, darkTheme)
|
||||
|
||||
CompositionLocalProvider(
|
||||
LocalDarkTheme provides darkTheme
|
||||
) {
|
||||
MaterialTheme(
|
||||
colorScheme = colorScheme,
|
||||
typography = DefaultTypography,
|
||||
content = content
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun colorSchemeAsState(colorScheme: AppearanceSettings.ColorScheme, darkTheme: Boolean): MutableState<ColorScheme> {
|
||||
val context = LocalContext.current
|
||||
val dataStore: LauncherDataStore by inject()
|
||||
|
||||
when (colorScheme) {
|
||||
AppearanceSettings.ColorScheme.BlackAndWhite -> {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user