Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit c24f4371 authored by Chaohui Wang's avatar Chaohui Wang Committed by Android (Google) Code Review
Browse files

Merge "Clean up SettingsTheme.colorScheme" into main

parents 120a8107 2e8c907c
Loading
Loading
Loading
Loading
+0 −122
Original line number Diff line number Diff line
/*
 * Copyright (C) 2022 The Android Open Source Project
 *
 * 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
 *
 *      http://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.
 */

package com.android.settingslib.spa.framework.theme

import android.content.Context
import android.os.Build
import androidx.annotation.VisibleForTesting
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.runtime.staticCompositionLocalOf
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext

data class SettingsColorScheme(
    val categoryTitle: Color = Color.Unspecified,
    val surface: Color = Color.Unspecified,
    val surfaceHeader: Color = Color.Unspecified,
    val secondaryText: Color = Color.Unspecified,
    val primaryContainer: Color = Color.Unspecified,
    val onPrimaryContainer: Color = Color.Unspecified,
)

internal val LocalColorScheme = staticCompositionLocalOf { SettingsColorScheme() }

@Composable
internal fun settingsColorScheme(isDarkTheme: Boolean): SettingsColorScheme {
    val context = LocalContext.current
    return remember(isDarkTheme) {
        when {
            Build.VERSION.SDK_INT >= Build.VERSION_CODES.S -> {
                if (isDarkTheme) dynamicDarkColorScheme(context)
                else dynamicLightColorScheme(context)
            }
            isDarkTheme -> darkColorScheme()
            else -> lightColorScheme()
        }
    }
}

/**
 * Creates a light dynamic color scheme.
 *
 * Use this function to create a color scheme based off the system wallpaper. If the developer
 * changes the wallpaper this color scheme will change accordingly. This dynamic scheme is a
 * light theme variant.
 *
 * @param context The context required to get system resource data.
 */
@VisibleForTesting
internal fun dynamicLightColorScheme(context: Context): SettingsColorScheme {
    val tonalPalette = dynamicTonalPalette(context)
    return SettingsColorScheme(
        categoryTitle = tonalPalette.primary40,
        surface = tonalPalette.neutral99,
        surfaceHeader = tonalPalette.neutral90,
        secondaryText = tonalPalette.neutralVariant30,
        primaryContainer = tonalPalette.primary90,
        onPrimaryContainer = tonalPalette.neutral10,
    )
}

/**
 * Creates a dark dynamic color scheme.
 *
 * Use this function to create a color scheme based off the system wallpaper. If the developer
 * changes the wallpaper this color scheme will change accordingly. This dynamic scheme is a dark
 * theme variant.
 *
 * @param context The context required to get system resource data.
 */
@VisibleForTesting
internal fun dynamicDarkColorScheme(context: Context): SettingsColorScheme {
    val tonalPalette = dynamicTonalPalette(context)
    return SettingsColorScheme(
        categoryTitle = tonalPalette.primary90,
        surface = tonalPalette.neutral20,
        surfaceHeader = tonalPalette.neutral30,
        secondaryText = tonalPalette.neutralVariant80,
        primaryContainer = tonalPalette.secondary90,
        onPrimaryContainer = tonalPalette.neutral10,
    )
}

@VisibleForTesting
internal fun darkColorScheme(): SettingsColorScheme {
    val tonalPalette = tonalPalette()
    return SettingsColorScheme(
        categoryTitle = tonalPalette.primary90,
        surface = tonalPalette.neutral20,
        surfaceHeader = tonalPalette.neutral30,
        secondaryText = tonalPalette.neutralVariant80,
        primaryContainer = tonalPalette.secondary90,
        onPrimaryContainer = tonalPalette.neutral10,
    )
}

@VisibleForTesting
internal fun lightColorScheme(): SettingsColorScheme {
    val tonalPalette = tonalPalette()
    return SettingsColorScheme(
        categoryTitle = tonalPalette.primary40,
        surface = tonalPalette.neutral99,
        surfaceHeader = tonalPalette.neutral90,
        secondaryText = tonalPalette.neutralVariant30,
        primaryContainer = tonalPalette.primary90,
        onPrimaryContainer = tonalPalette.neutral10,
    )
}
+0 −9
Original line number Diff line number Diff line
@@ -21,7 +21,6 @@ import androidx.compose.material3.LocalContentColor
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.ReadOnlyComposable

/**
 * The Material 3 Theme for Settings.
@@ -35,17 +34,9 @@ fun SettingsTheme(content: @Composable () -> Unit) {
        typography = rememberSettingsTypography(),
    ) {
        CompositionLocalProvider(
            LocalColorScheme provides settingsColorScheme(isDarkTheme),
            LocalContentColor provides MaterialTheme.colorScheme.onSurface,
        ) {
            content()
        }
    }
}

object SettingsTheme {
    val colorScheme: SettingsColorScheme
        @Composable
        @ReadOnlyComposable
        get() = LocalColorScheme.current
}
+3 −3
Original line number Diff line number Diff line
@@ -88,9 +88,9 @@ private fun RowScope.ActionButton(actionButton: ActionButton) {
        interactionSource = remember(actionButton) { MutableInteractionSource() },
        shape = RectangleShape,
        colors = ButtonDefaults.filledTonalButtonColors(
            containerColor = SettingsTheme.colorScheme.surface,
            contentColor = SettingsTheme.colorScheme.categoryTitle,
            disabledContainerColor = SettingsTheme.colorScheme.surface,
            containerColor = MaterialTheme.colorScheme.surface,
            contentColor = MaterialTheme.colorScheme.primary,
            disabledContainerColor = MaterialTheme.colorScheme.surface,
        ),
        contentPadding = PaddingValues(horizontal = 4.dp, vertical = 20.dp),
    ) {
+1 −1
Original line number Diff line number Diff line
@@ -83,7 +83,7 @@ fun SettingsCardContent(
    Card(
        shape = CornerExtraSmall,
        colors = CardDefaults.cardColors(
            containerColor = containerColor.takeOrElse { SettingsTheme.colorScheme.surface },
            containerColor = containerColor.takeOrElse { MaterialTheme.colorScheme.surface },
        ),
        modifier = Modifier
            .fillMaxWidth()
+1 −2
Original line number Diff line number Diff line
@@ -74,7 +74,6 @@ import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.Velocity
import androidx.compose.ui.unit.dp
import com.android.settingslib.spa.framework.theme.SettingsDimension
import com.android.settingslib.spa.framework.theme.SettingsTheme
import com.android.settingslib.spa.framework.theme.settingsBackground
import kotlin.math.abs
import kotlin.math.max
@@ -142,7 +141,7 @@ private fun Title(title: String, maxLines: Int = Int.MAX_VALUE) {
@Composable
private fun topAppBarColors() = TopAppBarColors(
    containerColor = MaterialTheme.colorScheme.settingsBackground,
    scrolledContainerColor = SettingsTheme.colorScheme.surfaceHeader,
    scrolledContainerColor = MaterialTheme.colorScheme.surfaceVariant,
    navigationIconContentColor = MaterialTheme.colorScheme.onSurface,
    titleContentColor = MaterialTheme.colorScheme.onSurface,
    actionIconContentColor = MaterialTheme.colorScheme.onSurfaceVariant,
Loading