1 /*
2  * Copyright 2023 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 androidx.tv.material3
18 
19 import androidx.compose.foundation.text.selection.LocalTextSelectionColors
20 import androidx.compose.foundation.text.selection.TextSelectionColors
21 import androidx.compose.runtime.Composable
22 import androidx.compose.runtime.CompositionLocalProvider
23 import androidx.compose.runtime.ReadOnlyComposable
24 import androidx.compose.runtime.remember
25 
26 /**
27  * Material Theming refers to the customization of your Material Design app to better reflect your
28  * product’s brand.
29  *
30  * Material components such as Button and Checkbox use values provided here when retrieving default
31  * values.
32  *
33  * All values may be set by providing this component with the [colorScheme][ColorScheme],
34  * [typography][Typography] attributes. Use this to configure the overall theme of elements within
35  * this MaterialTheme.
36  *
37  * Any values that are not set will inherit the current value from the theme, falling back to the
38  * defaults if there is no parent MaterialTheme. This allows using a MaterialTheme at the top of
39  * your application, and then separate MaterialTheme(s) for different screens / parts of your UI,
40  * overriding only the parts of the theme definition that need to change.
41  *
42  * @param colorScheme A complete definition of the Material Color theme for this hierarchy
43  * @param shapes A set of corner shapes to be used as this hierarchy's shape system
44  * @param typography A set of text styles to be used as this hierarchy's typography system
45  * @param content The composable content that will be displayed with this theme
46  */
47 @Composable
MaterialThemenull48 fun MaterialTheme(
49     colorScheme: ColorScheme = MaterialTheme.colorScheme,
50     shapes: Shapes = MaterialTheme.shapes,
51     typography: Typography = MaterialTheme.typography,
52     content: @Composable () -> Unit
53 ) {
54     val rememberedColorScheme =
55         remember {
56                 // Explicitly creating a new object here so we don't mutate the initial
57                 // [colorScheme]
58                 // provided, and overwrite the values set in it.
59                 colorScheme.copy()
60             }
61             .apply { updateColorSchemeFrom(colorScheme) }
62     val selectionColors = rememberTextSelectionColors(rememberedColorScheme)
63     CompositionLocalProvider(
64         LocalColorScheme provides rememberedColorScheme,
65         LocalShapes provides shapes,
66         LocalTextSelectionColors provides selectionColors,
67         LocalTypography provides typography
68     ) {
69         ProvideTextStyle(value = typography.bodyLarge, content = content)
70     }
71 }
72 
73 /**
74  * Contains functions to access the current theme values provided at the call site's position in the
75  * hierarchy.
76  */
77 object MaterialTheme {
78     /** Retrieves the current [ColorScheme] at the call site's position in the hierarchy. */
79     val colorScheme: ColorScheme
80         @Composable
81         @ReadOnlyComposable
82         @SuppressWarnings("HiddenTypeParameter", "UnavailableSymbol")
83         get() = LocalColorScheme.current
84 
85     /** Retrieves the current [Typography] at the call site's position in the hierarchy. */
86     val typography: Typography
87         @Composable @ReadOnlyComposable get() = LocalTypography.current
88 
89     /** Retrieves the current [Shapes] at the call site's position in the hierarchy. */
90     val shapes: Shapes
91         @Composable @ReadOnlyComposable get() = LocalShapes.current
92 }
93 
94 @Composable
95 /*@VisibleForTesting*/
rememberTextSelectionColorsnull96 internal fun rememberTextSelectionColors(colorScheme: ColorScheme): TextSelectionColors {
97     val primaryColor = colorScheme.primary
98     return remember(primaryColor) {
99         TextSelectionColors(
100             handleColor = primaryColor,
101             backgroundColor = primaryColor.copy(alpha = TextSelectionBackgroundOpacity)
102         )
103     }
104 }
105 
106 /*@VisibleForTesting*/
107 internal const val TextSelectionBackgroundOpacity = 0.4f
108