refactor: decoupling dynamic colors to SystemColors

This commit is contained in:
2024-01-06 15:19:29 +08:00
parent a713ae44a3
commit 9bec3df68b
6 changed files with 199 additions and 165 deletions

View File

@@ -1,89 +0,0 @@
/*
* Flexi UI - A flexible and useful UI component library.
* Copyright (C) 2019-2024 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
import com.highcapable.betterandroid.ui.extension.graphics.mixColorOf
/**
* Whether dynamic color is available for current system.
* @return [Boolean]
*/
@Composable
@ReadOnlyComposable
actual fun isDynamicColorAvailable() = SystemColors.isAvailable
internal actual val DynamicLightColors
@Composable
@ReadOnlyComposable
get() = if (SystemColors.isAvailable) Colors(
backgroundPrimary = Color(DynamicColors.materialDynamicNeutral(95)),
backgroundSecondary = Color(mixColorOf(DynamicColors.materialDynamicNeutral(95), DynamicColors.materialDynamicNeutral(99))),
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.materialDynamicPrimary(60)).copy(alpha = 0.15f),
textPrimary = DefaultLightColors.textPrimary,
textSecondary = DefaultLightColors.textSecondary
) else DefaultLightColors
internal actual val DynamicDarkColors
@Composable
@ReadOnlyComposable
get() = if (SystemColors.isAvailable) Colors(
backgroundPrimary = Color(DynamicColors.materialDynamicNeutral(10)),
backgroundSecondary = Color(mixColorOf(DynamicColors.materialDynamicNeutral(10), 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
) else DefaultDarkColors
internal actual val DynamicBlackColors
@Composable
@ReadOnlyComposable
get() = if (SystemColors.isAvailable) Colors(
backgroundPrimary = Color(DynamicColors.materialDynamicNeutral(0)),
backgroundSecondary = Color(mixColorOf(DynamicColors.materialDynamicNeutral(0), DynamicColors.materialDynamicNeutral(10))),
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
) else DefaultBlackColors
private val DynamicColors
@Composable
@ReadOnlyComposable
get() = SystemColors.from(LocalContext.current)

View File

@@ -0,0 +1,108 @@
/*
* Flexi UI - A flexible and useful UI component library.
* Copyright (C) 2019-2024 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 2024/1/6.
*/
@file:Suppress("unused")
package com.highcapable.flexiui
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import com.highcapable.betterandroid.ui.extension.component.feature.SystemColors
import com.highcapable.betterandroid.ui.extension.graphics.mixColorOf
internal actual val SystemLightColors
@Composable
get() = defaultSystemLightColors()
internal actual val SystemDarkColors
@Composable
get() = defaultSystemDarkColors()
internal actual val SystemBlackColors
@Composable
get() = defaultSystemBlackColors()
/**
* Whether system color is available.
* @return [Boolean]
*/
@Composable
actual fun isSystemColorAvailable() = SystemColors.isAvailable
@Composable
private fun defaultSystemLightColors(): Colors {
val systemColors = rememberSystemColors()
if (!SystemColors.isAvailable) return DefaultLightColors
return Colors(
backgroundPrimary = Color(systemColors.materialDynamicNeutral(95)),
backgroundSecondary = Color(mixColorOf(systemColors.materialDynamicNeutral(95), systemColors.materialDynamicNeutral(99))),
foregroundPrimary = Color(systemColors.materialDynamicNeutral(99)),
foregroundSecondary = Color(systemColors.materialDynamicNeutral(95)),
themePrimary = Color(systemColors.materialDynamicPrimary(60)),
themeSecondary = Color(systemColors.materialDynamicPrimary(60)).copy(alpha = 0.65f),
themeTertiary = Color(systemColors.materialDynamicPrimary(60)).copy(alpha = 0.15f),
textPrimary = DefaultLightColors.textPrimary,
textSecondary = DefaultLightColors.textSecondary
)
}
@Composable
private fun defaultSystemDarkColors(): Colors {
val systemColors = rememberSystemColors()
if (!SystemColors.isAvailable) return DefaultDarkColors
return Colors(
backgroundPrimary = Color(systemColors.materialDynamicNeutral(10)),
backgroundSecondary = Color(mixColorOf(systemColors.materialDynamicNeutral(10), systemColors.materialDynamicNeutral(20))),
foregroundPrimary = Color(systemColors.materialDynamicNeutral(20)),
foregroundSecondary = Color(systemColors.materialDynamicNeutral(30)),
themePrimary = Color(systemColors.materialDynamicSecondary(60)),
themeSecondary = Color(systemColors.materialDynamicSecondary(60)).copy(alpha = 0.65f),
themeTertiary = Color(systemColors.materialDynamicSecondary(60)).copy(alpha = 0.25f),
textPrimary = DefaultDarkColors.textPrimary,
textSecondary = DefaultDarkColors.textSecondary
)
}
@Composable
private fun defaultSystemBlackColors(): Colors {
val systemColors = rememberSystemColors()
if (!SystemColors.isAvailable) return DefaultBlackColors
return Colors(
backgroundPrimary = Color(systemColors.materialDynamicNeutral(0)),
backgroundSecondary = Color(mixColorOf(systemColors.materialDynamicNeutral(0), systemColors.materialDynamicNeutral(10))),
foregroundPrimary = Color(systemColors.materialDynamicNeutral(10)),
foregroundSecondary = Color(systemColors.materialDynamicNeutral(20)),
themePrimary = Color(systemColors.materialDynamicSecondary(60)),
themeSecondary = Color(systemColors.materialDynamicSecondary(60)).copy(alpha = 0.65f),
themeTertiary = Color(systemColors.materialDynamicSecondary(60)).copy(alpha = 0.27f),
textPrimary = DefaultBlackColors.textPrimary,
textSecondary = DefaultBlackColors.textSecondary
)
}
@Composable
private fun rememberSystemColors(): SystemColors {
val context = LocalContext.current
val systemColors = remember { SystemColors.from(context) }
return systemColors
}

View File

@@ -23,8 +23,6 @@
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
@@ -45,18 +43,6 @@ data class Colors(
var textSecondary: Color
)
@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(0xFFFAFAFA),
@@ -345,28 +331,6 @@ private val BlueBlackColors = Colors(
textSecondary = DefaultBlackColors.textSecondary
)
/**
* Whether dynamic color is available for current system.
* @return [Boolean]
*/
@Composable
@ReadOnlyComposable
expect fun isDynamicColorAvailable(): Boolean
/**
* Returns a dynamic color scheme provided by system.
*
* You can use [isDynamicColorAvailable] check it first, otherwise it will return default colors.
* @param darkMode whether to use dark mode color scheme.
* @param blackDarkMode requires [darkMode] is true, whether to use a pure black mode scheme.
*/
@Composable
@ReadOnlyComposable
fun dynamicColors(darkMode: Boolean = false, blackDarkMode: Boolean = false) = when {
darkMode -> if (blackDarkMode) DynamicBlackColors else DynamicDarkColors
else -> DynamicLightColors
}
/**
* Returns a default color scheme.
* @param darkMode whether to use dark mode color scheme.

View File

@@ -0,0 +1,55 @@
/*
* Flexi UI - A flexible and useful UI component library.
* Copyright (C) 2019-2024 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 2024/1/6.
*/
@file:Suppress("unused")
package com.highcapable.flexiui
import androidx.compose.runtime.Composable
@get:Composable
internal expect val SystemLightColors: Colors
@get:Composable
internal expect val SystemDarkColors: Colors
@get:Composable
internal expect val SystemBlackColors: Colors
/**
* Whether system color is available.
* @return [Boolean]
*/
@Composable
expect fun isSystemColorAvailable(): Boolean
/**
* Returns a color scheme provided by system.
*
* You can use [isSystemColorAvailable] check it first, otherwise it will return default colors.
* @param darkMode whether to use dark mode color scheme.
* @param blackDarkMode requires [darkMode] is true, whether to use a pure black mode scheme.
*/
@Composable
fun systemColors(darkMode: Boolean = false, blackDarkMode: Boolean = false) = when {
darkMode -> if (blackDarkMode) SystemBlackColors else SystemDarkColors
else -> SystemLightColors
}

View File

@@ -17,34 +17,32 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*
* This file is created by fankes on 2023/11/18.
* This file is created by fankes on 2024/1/6.
*/
@file:Suppress("unused")
package com.highcapable.flexiui
import androidx.compose.runtime.Composable
import androidx.compose.runtime.ReadOnlyComposable
internal actual val SystemLightColors
@Composable
get() = DefaultLightColors
internal actual val SystemDarkColors
@Composable
get() = DefaultDarkColors
internal actual val SystemBlackColors
@Composable
get() = DefaultBlackColors
/**
* Whether dynamic color is available for current system.
* Whether system color is available.
* @return [Boolean]
*/
@Composable
@ReadOnlyComposable
actual fun isDynamicColorAvailable() = false
internal actual val DynamicLightColors
@Composable
@ReadOnlyComposable
get() = DefaultLightColors
internal actual val DynamicDarkColors
@Composable
@ReadOnlyComposable
get() = DefaultDarkColors
internal actual val DynamicBlackColors
@Composable
@ReadOnlyComposable
get() = DefaultBlackColors
actual fun isSystemColorAvailable(): Boolean {
// Platform desktop: No-op.
return false
}

View File

@@ -17,34 +17,32 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*
* This file is created by fankes on 2023/11/18.
* This file is created by fankes on 2024/1/6.
*/
@file:Suppress("unused")
package com.highcapable.flexiui
import androidx.compose.runtime.Composable
import androidx.compose.runtime.ReadOnlyComposable
internal actual val SystemLightColors
@Composable
get() = DefaultLightColors
internal actual val SystemDarkColors
@Composable
get() = DefaultDarkColors
internal actual val SystemBlackColors
@Composable
get() = DefaultBlackColors
/**
* Whether dynamic color is available for current system.
* Whether system color is available.
* @return [Boolean]
*/
@Composable
@ReadOnlyComposable
actual fun isDynamicColorAvailable() = false
internal actual val DynamicLightColors
@Composable
@ReadOnlyComposable
get() = DefaultLightColors
internal actual val DynamicDarkColors
@Composable
@ReadOnlyComposable
get() = DefaultDarkColors
internal actual val DynamicBlackColors
@Composable
@ReadOnlyComposable
get() = DefaultBlackColors
actual fun isSystemColorAvailable(): Boolean {
// Platform iOS: No-op.
return false
}