1 /*
2  * Copyright (C) 2022 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.compose.theme
18 
19 import androidx.compose.foundation.isSystemInDarkTheme
20 import androidx.compose.material3.MaterialTheme
21 import androidx.compose.material3.dynamicDarkColorScheme
22 import androidx.compose.material3.dynamicLightColorScheme
23 import androidx.compose.runtime.Composable
24 import androidx.compose.runtime.CompositionLocalProvider
25 import androidx.compose.runtime.remember
26 import androidx.compose.ui.platform.LocalContext
27 import com.android.compose.theme.typography.TypeScaleTokens
28 import com.android.compose.theme.typography.TypefaceNames
29 import com.android.compose.theme.typography.TypefaceTokens
30 import com.android.compose.theme.typography.TypographyTokens
31 import com.android.compose.theme.typography.platformTypography
32 import com.android.compose.windowsizeclass.LocalWindowSizeClass
33 import com.android.compose.windowsizeclass.calculateWindowSizeClass
34 
35 /** The Material 3 theme that should wrap all Platform Composables. */
36 @Composable
PlatformThemenull37 fun PlatformTheme(
38     isDarkTheme: Boolean = isSystemInDarkTheme(),
39     content: @Composable () -> Unit,
40 ) {
41     val context = LocalContext.current
42 
43     // TODO(b/230605885): Define our color scheme.
44     val colorScheme =
45         if (isDarkTheme) {
46             dynamicDarkColorScheme(context)
47         } else {
48             dynamicLightColorScheme(context)
49         }
50     val androidColorScheme = AndroidColorScheme(context)
51     val typefaceNames = remember(context) { TypefaceNames.get(context) }
52     val typography =
53         remember(typefaceNames) {
54             platformTypography(TypographyTokens(TypeScaleTokens(TypefaceTokens(typefaceNames))))
55         }
56     val windowSizeClass = calculateWindowSizeClass()
57 
58     MaterialTheme(colorScheme, typography = typography) {
59         CompositionLocalProvider(
60             LocalAndroidColorScheme provides androidColorScheme,
61             LocalWindowSizeClass provides windowSizeClass,
62         ) {
63             content()
64         }
65     }
66 }
67