feat: support dynamic colors for Android

This commit is contained in:
2023-11-18 02:57:03 +08:00
parent 1b3d623342
commit 774d9632a9
4 changed files with 167 additions and 5 deletions

View File

@@ -0,0 +1,83 @@
/*
* Flexi UI - A flexible and useful UI component library.
* Copyright (C) 2019-2023 HighCapable
* https://github.com/BetterAndroid/FlexiUI
*
* Apache License Version 2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* This file is created by fankes on 2023/11/18.
*/
@file:Suppress("unused")
package com.highcapable.flexiui
import androidx.compose.runtime.Composable
import androidx.compose.runtime.ReadOnlyComposable
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import com.highcapable.betterandroid.ui.extension.component.feature.SystemColors
internal actual val DynamicLightColors
@Composable
@ReadOnlyComposable
get() = if (SystemColors.isAvailable) Colors(
backgroundPrimary = Color(DynamicColors.materialDynamicNeutral(95)),
backgroundSecondary = Color(DynamicColors.materialDynamicNeutral(90)),
foregroundPrimary = Color(DynamicColors.materialDynamicNeutral(99)),
foregroundSecondary = Color(DynamicColors.materialDynamicNeutral(95)),
themePrimary = Color(DynamicColors.materialDynamicPrimary(60)),
themeSecondary = Color(DynamicColors.materialDynamicPrimary(60)).copy(alpha = 0.65f),
themeTertiary = Color(DynamicColors.materialDynamicNeutral(60)).copy(alpha = 0.15f),
textPrimary = DefaultLightColors.textPrimary,
textSecondary = DefaultLightColors.textSecondary,
isLight = true
) else DefaultLightColors
internal actual val DynamicDarkColors
@Composable
@ReadOnlyComposable
get() = if (SystemColors.isAvailable) Colors(
backgroundPrimary = Color(DynamicColors.materialDynamicNeutral(10)),
backgroundSecondary = Color(DynamicColors.materialDynamicNeutral(20)),
foregroundPrimary = Color(DynamicColors.materialDynamicNeutral(20)),
foregroundSecondary = Color(DynamicColors.materialDynamicNeutral(30)),
themePrimary = Color(DynamicColors.materialDynamicSecondary(60)),
themeSecondary = Color(DynamicColors.materialDynamicSecondary(60)).copy(alpha = 0.65f),
themeTertiary = Color(DynamicColors.materialDynamicSecondary(60)).copy(alpha = 0.25f),
textPrimary = DefaultDarkColors.textPrimary,
textSecondary = DefaultDarkColors.textSecondary,
isLight = false
) else DefaultDarkColors
internal actual val DynamicBlackColors
@Composable
@ReadOnlyComposable
get() = if (SystemColors.isAvailable) Colors(
backgroundPrimary = Color(DynamicColors.materialDynamicNeutral(0)),
backgroundSecondary = Color(DynamicColors.materialDynamicNeutral(0)),
foregroundPrimary = Color(DynamicColors.materialDynamicNeutral(10)),
foregroundSecondary = Color(DynamicColors.materialDynamicNeutral(20)),
themePrimary = Color(DynamicColors.materialDynamicSecondary(60)),
themeSecondary = Color(DynamicColors.materialDynamicSecondary(60)).copy(alpha = 0.65f),
themeTertiary = Color(DynamicColors.materialDynamicSecondary(60)).copy(alpha = 0.27f),
textPrimary = DefaultBlackColors.textPrimary,
textSecondary = DefaultBlackColors.textSecondary,
isLight = false
) else DefaultBlackColors
private val DynamicColors
@Composable
@ReadOnlyComposable
get() = SystemColors.from(LocalContext.current)

View File

@@ -23,12 +23,12 @@
package com.highcapable.flexiui
import androidx.compose.runtime.Composable
import androidx.compose.runtime.ReadOnlyComposable
import androidx.compose.runtime.Stable
import androidx.compose.runtime.staticCompositionLocalOf
import androidx.compose.ui.graphics.Color
// TODO: Dynamic colors support
@Stable
data class Colors(
var backgroundPrimary: Color,
@@ -43,7 +43,19 @@ data class Colors(
var isLight: Boolean
)
private val DefaultLightColors = Colors(
@get:Composable
@get:ReadOnlyComposable
internal expect val DynamicLightColors: Colors
@get:Composable
@get:ReadOnlyComposable
internal expect val DynamicDarkColors: Colors
@get:Composable
@get:ReadOnlyComposable
internal expect val DynamicBlackColors: Colors
internal val DefaultLightColors = Colors(
backgroundPrimary = Color(0xFFF5F5F5),
backgroundSecondary = Color(0xFFEDEDED),
foregroundPrimary = Color(0xFFFFFFFF),
@@ -56,7 +68,7 @@ private val DefaultLightColors = Colors(
isLight = true
)
private val DefaultDarkColors = Colors(
internal val DefaultDarkColors = Colors(
backgroundPrimary = Color(0xFF2D2D2D),
backgroundSecondary = Color(0xFF484848),
foregroundPrimary = Color(0xFF474747),
@@ -69,7 +81,7 @@ private val DefaultDarkColors = Colors(
isLight = false
)
private val DefaultBlackColors = Colors(
internal val DefaultBlackColors = Colors(
backgroundPrimary = Color(0xFF000000),
backgroundSecondary = Color(0xFF1B1B1B),
foregroundPrimary = Color(0xFF1A1A1A),
@@ -355,6 +367,13 @@ private val BlueBlackColors = Colors(
isLight = false
)
@Composable
@ReadOnlyComposable
fun dynamicColors(darkMode: Boolean = false, blackDarkMode: Boolean = false) = when {
darkMode -> if (blackDarkMode) DynamicBlackColors else DynamicDarkColors
else -> DynamicLightColors
}
fun defaultColors(darkMode: Boolean = false, blackDarkMode: Boolean = false) = when {
darkMode -> if (blackDarkMode) DefaultBlackColors else DefaultDarkColors
else -> DefaultLightColors

View File

@@ -0,0 +1,30 @@
/*
* Flexi UI - A flexible and useful UI component library.
* Copyright (C) 2019-2023 HighCapable
* https://github.com/BetterAndroid/FlexiUI
*
* Apache License Version 2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* This file is created by fankes on 2023/11/18.
*/
@file:Suppress("unused")
package com.highcapable.flexiui
internal actual val DynamicLightColors = DefaultLightColors
internal actual val DynamicDarkColors = DefaultDarkColors
internal actual val DynamicBlackColors = DefaultBlackColors

View File

@@ -0,0 +1,30 @@
/*
* Flexi UI - A flexible and useful UI component library.
* Copyright (C) 2019-2023 HighCapable
* https://github.com/BetterAndroid/FlexiUI
*
* Apache License Version 2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* This file is created by fankes on 2023/11/18.
*/
@file:Suppress("unused")
package com.highcapable.flexiui
internal actual val DynamicLightColors = DefaultLightColors
internal actual val DynamicDarkColors = DefaultDarkColors
internal actual val DynamicBlackColors = DefaultBlackColors