This repository has been archived by the owner on Jul 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMaterialTheme.kt
144 lines (133 loc) · 5.43 KB
/
MaterialTheme.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
* Copyright 2021 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 androidx.compose.material3
import androidx.compose.foundation.LocalIndication
import androidx.compose.foundation.text.selection.LocalTextSelectionColors
import androidx.compose.foundation.text.selection.TextSelectionColors
import androidx.compose.material.ripple.LocalRippleTheme
import androidx.compose.material.ripple.RippleAlpha
import androidx.compose.material.ripple.RippleTheme
import androidx.compose.material.ripple.rememberRipple
import androidx.compose.material3.tokens.StateTokens
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.Immutable
import androidx.compose.runtime.ReadOnlyComposable
import androidx.compose.runtime.remember
// TODO: Create a sample androidx.compose.material3.samples.MaterialThemeSample
// TODO(b/197880751) Update to link M3 Material Theming page (i.e. a <a href="https://material
// .io/design/material-theming/overview.html" class="external" target="_blank">Material
// Theming</a> M3 equivalent).
/**
* Material Theming refers to the customization of your Material Design app to better reflect your
* product’s brand.
*
* Material components such as [Button] and [Checkbox] use values provided here when retrieving
* default values.
*
* All values may be set by providing this component with the [colorScheme][ColorScheme],
* [typography][Typography] attributes. Use this to configure the overall
* theme of elements within this MaterialTheme.
*
* Any values that are not set will inherit the current value from the theme, falling back to the
* defaults if there is no parent MaterialTheme. This allows using a MaterialTheme at the top
* of your application, and then separate MaterialTheme(s) for different screens / parts of your
* UI, overriding only the parts of the theme definition that need to change.
*
* @param colorScheme A complete definition of the Material Color theme for this hierarchy
* @param typography A set of text styles to be used as this hierarchy's typography system
* @param shapes A set of corner shapes to be used as this hierarchy's shape system
*/
@Composable
fun MaterialTheme(
colorScheme: ColorScheme = MaterialTheme.colorScheme,
shapes: Shapes = MaterialTheme.shapes,
typography: Typography = MaterialTheme.typography,
content: @Composable () -> Unit
) {
val rememberedColorScheme = remember {
// Explicitly creating a new object here so we don't mutate the initial [colorScheme]
// provided, and overwrite the values set in it.
colorScheme.copy()
}.apply {
updateColorSchemeFrom(colorScheme)
}
val rippleIndication = rememberRipple()
val selectionColors = rememberTextSelectionColors(rememberedColorScheme)
CompositionLocalProvider(
LocalColorScheme provides rememberedColorScheme,
LocalIndication provides rippleIndication,
LocalRippleTheme provides MaterialRippleTheme,
LocalShapes provides shapes,
LocalTextSelectionColors provides selectionColors,
LocalTypography provides typography,
) {
ProvideTextStyle(value = typography.bodyLarge, content = content)
}
}
/**
* Contains functions to access the current theme values provided at the call site's position in
* the hierarchy.
*/
object MaterialTheme {
/**
* Retrieves the current [ColorScheme] at the call site's position in the hierarchy.
*/
val colorScheme: ColorScheme
@Composable
@ReadOnlyComposable
get() = LocalColorScheme.current
/**
* Retrieves the current [Typography] at the call site's position in the hierarchy.
*/
val typography: Typography
@Composable
@ReadOnlyComposable
get() = LocalTypography.current
/**
* Retrieves the current [Shapes] at the call site's position in the hierarchy.
*/
val shapes: Shapes
@Composable
@ReadOnlyComposable
get() = LocalShapes.current
}
@Immutable
private object MaterialRippleTheme : RippleTheme {
@Composable
override fun defaultColor() = LocalContentColor.current
@Composable
override fun rippleAlpha() = DefaultRippleAlpha
}
private val DefaultRippleAlpha = RippleAlpha(
pressedAlpha = StateTokens.PressedStateLayerOpacity,
focusedAlpha = StateTokens.FocusStateLayerOpacity,
draggedAlpha = StateTokens.DraggedStateLayerOpacity,
hoveredAlpha = StateTokens.HoverStateLayerOpacity
)
@Composable
/*@VisibleForTesting*/
internal fun rememberTextSelectionColors(colorScheme: ColorScheme): TextSelectionColors {
val primaryColor = colorScheme.primary
return remember(primaryColor) {
TextSelectionColors(
handleColor = primaryColor,
backgroundColor = primaryColor.copy(alpha = TextSelectionBackgroundOpacity),
)
}
}
/*@VisibleForTesting*/
internal const val TextSelectionBackgroundOpacity = 0.4f