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 pathSurface.kt
496 lines (487 loc) · 23.7 KB
/
Surface.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
/*
* 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.BorderStroke
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
import androidx.compose.foundation.interaction.Interaction
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.interaction.PressInteraction
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.selection.selectable
import androidx.compose.foundation.selection.toggleable
import androidx.compose.material.ripple.rememberRipple
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.NonRestartableComposable
import androidx.compose.runtime.compositionLocalOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.draw.shadow
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.RectangleShape
import androidx.compose.ui.graphics.Shape
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.semantics.Role
import androidx.compose.ui.semantics.semantics
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
// TODO(b/197880751): Add url to spec on Material.io.
/**
* Material surface is the central metaphor in material design. Each surface exists at a given
* elevation, which influences how that piece of surface visually relates to other surfaces and how
* that surface is modified by tonal variance.
*
* See the other overloads for clickable, selectable, and toggleable surfaces.
*
* The Surface is responsible for:
*
* 1) Clipping: Surface clips its children to the shape specified by [shape]
*
* 2) Borders: If [shape] has a border, then it will also be drawn.
*
* 3) Background: Surface fills the shape specified by [shape] with the [color]. If [color] is
* [ColorScheme.surface] a color overlay will be applied. The color of the overlay depends on the
* [tonalElevation] of this Surface, and the [LocalAbsoluteTonalElevation] set by any parent
* surfaces. This ensures that a Surface never appears to have a lower elevation overlay than its
* ancestors, by summing the elevation of all previous Surfaces.
*
* 4) Content color: Surface uses [contentColor] to specify a preferred color for the content of
* this surface - this is used by the [Text] and [Icon] components as a default color.
*
* If no [contentColor] is set, this surface will try and match its background color to a color
* defined in the theme [ColorScheme], and return the corresponding content color. For example, if
* the [color] of this surface is [ColorScheme.surface], [contentColor] will be set to
* [ColorScheme.onSurface]. If [color] is not part of the theme palette, [contentColor] will keep
* the same value set above this Surface.
*
* To manually retrieve the content color inside a surface, use [LocalContentColor].
*
* 5) Blocking touch propagation behind the surface.
*
* Surface sample:
* @sample androidx.compose.material3.samples.SurfaceSample
*
* @param modifier Modifier to be applied to the layout corresponding to the surface
* @param shape Defines the surface's shape as well its shadow.
* @param color The background color. Use [Color.Transparent] to have no color.
* @param contentColor The preferred content color provided by this Surface to its children.
* Defaults to either the matching content color for [color], or if [color] is not a color from the
* theme, this will keep the same value set above this Surface.
* @param tonalElevation When [color] is [ColorScheme.surface], a higher the elevation will result
* in a darker color in light theme and lighter color in dark theme.
* @param shadowElevation The size of the shadow below the surface. To prevent shadow creep, only
* apply shadow elevation when absolutely necessary, such as when the surface requires visual
* separation from a patterned background. Note that It will not affect z index of the Surface.
* If you want to change the drawing order you can use `Modifier.zIndex`.
* @param border Optional border to draw on top of the surface
*/
@Composable
@NonRestartableComposable
fun Surface(
modifier: Modifier = Modifier,
shape: Shape = RectangleShape,
color: Color = MaterialTheme.colorScheme.surface,
contentColor: Color = contentColorFor(color),
tonalElevation: Dp = 0.dp,
shadowElevation: Dp = 0.dp,
border: BorderStroke? = null,
content: @Composable () -> Unit
) {
val absoluteElevation = LocalAbsoluteTonalElevation.current + tonalElevation
CompositionLocalProvider(
LocalContentColor provides contentColor,
LocalAbsoluteTonalElevation provides absoluteElevation
) {
Box(
modifier = modifier
.surface(
shape = shape,
backgroundColor = surfaceColorAtElevation(
color = color,
elevation = absoluteElevation
),
border = border,
shadowElevation = shadowElevation
)
.semantics(mergeDescendants = false) {}
.pointerInput(Unit) {},
propagateMinConstraints = true
) {
content()
}
}
}
/**
* Material surface is the central metaphor in material design. Each surface exists at a given
* elevation, which influences how that piece of surface visually relates to other surfaces and how
* that surface is modified by tonal variance.
*
* This version of Surface is responsible for a click handling as well as everything else that a
* regular Surface does:
*
* This clickable Surface is responsible for:
*
* 1) Clipping: Surface clips its children to the shape specified by [shape]
*
* 2) Borders: If [shape] has a border, then it will also be drawn.
*
* 3) Background: Surface fills the shape specified by [shape] with the [color]. If [color] is
* [ColorScheme.surface] a color overlay may be applied. The color of the overlay depends on the
* [tonalElevation] of this Surface, and the [LocalAbsoluteTonalElevation] set by any
* parent surfaces. This ensures that a Surface never appears to have a lower elevation overlay than
* its ancestors, by summing the elevation of all previous Surfaces.
*
* 4) Content color: Surface uses [contentColor] to specify a preferred color for the content of
* this surface - this is used by the [Text] and [Icon] components as a default color. If no
* [contentColor] is set, this surface will try and match its background color to a color defined in
* the theme [ColorScheme], and return the corresponding content color. For example, if the [color]
* of this surface is [ColorScheme.surface], [contentColor] will be set to [ColorScheme.onSurface].
* If [color] is not part of the theme palette, [contentColor] will keep the same value set above
* this Surface.
*
* 5) Click handling. This version of surface will react to the clicks, calling [onClick] lambda,
* updating the [interactionSource] when [PressInteraction] occurs, and showing ripple indication in
* response to press events. If you don't need click handling, consider using the Surface function
* that doesn't require [onClick] param. If you need to set a custom label for the [onClick], apply
* a `Modifier.semantics { onClick(label = "YOUR_LABEL", action = null) }` to the Surface.
*
* 6) Semantics for clicks. Just like with [Modifier.clickable], clickable version of Surface will
* produce semantics to indicate that it is clicked. Also, by default, accessibility services will
* describe the element as [Role.Button]. You may change this by passing a desired [Role] with a
* [Modifier.semantics].
*
* To manually retrieve the content color inside a surface, use [LocalContentColor].
*
* Clickable surface sample:
* @sample androidx.compose.material3.samples.ClickableSurfaceSample
*
* @param onClick callback to be called when the surface is clicked
* @param modifier Modifier to be applied to the layout corresponding to the surface
* @param enabled Controls the enabled state of the surface. When `false`, this surface will not be
* clickable
* @param shape Defines the surface's shape as well its shadow. A shadow is only displayed if the
* [tonalElevation] is greater than zero.
* @param color The background color. Use [Color.Transparent] to have no color.
* @param contentColor The preferred content color provided by this Surface to its children.
* Defaults to either the matching content color for [color], or if [color] is not a color from the
* theme, this will keep the same value set above this Surface.
* @param border Optional border to draw on top of the surface
* @param tonalElevation When [color] is [ColorScheme.surface], a higher the elevation will result
* in a darker color in light theme and lighter color in dark theme.
* @param shadowElevation The size of the shadow below the surface. Note that It will not affect z
* index of the Surface. If you want to change the drawing order you can use `Modifier.zIndex`.
* @param interactionSource the [MutableInteractionSource] representing the stream of [Interaction]s
* for this Surface. You can create and pass in your own remembered [MutableInteractionSource] if
* you want to observe [Interaction]s and customize the appearance / behavior of this Surface in
* different [Interaction]s.
*/
@ExperimentalMaterial3Api
@Composable
@NonRestartableComposable
fun Surface(
onClick: () -> Unit,
modifier: Modifier = Modifier,
enabled: Boolean = true,
shape: Shape = RectangleShape,
color: Color = MaterialTheme.colorScheme.surface,
contentColor: Color = contentColorFor(color),
tonalElevation: Dp = 0.dp,
shadowElevation: Dp = 0.dp,
border: BorderStroke? = null,
interactionSource: MutableInteractionSource = remember { MutableInteractionSource() },
content: @Composable () -> Unit
) {
val absoluteElevation = LocalAbsoluteTonalElevation.current + tonalElevation
CompositionLocalProvider(
LocalContentColor provides contentColor,
LocalAbsoluteTonalElevation provides absoluteElevation
) {
Box(
modifier = modifier
.minimumTouchTargetSize()
.surface(
shape = shape,
backgroundColor = surfaceColorAtElevation(
color = color,
elevation = absoluteElevation
),
border = border,
shadowElevation = shadowElevation
)
.clickable(
interactionSource = interactionSource,
indication = rememberRipple(),
enabled = enabled,
role = Role.Button,
onClick = onClick
),
propagateMinConstraints = true
) {
content()
}
}
}
/**
* Material surface is the central metaphor in material design. Each surface exists at a given
* elevation, which influences how that piece of surface visually relates to other surfaces and how
* that surface is modified by tonal variance.
*
* This version of Surface is responsible for a selection handling as well as everything else that
* a regular Surface does:
*
* This selectable Surface is responsible for:
*
* 1) Clipping: Surface clips its children to the shape specified by [shape]
*
* 2) Borders: If [shape] has a border, then it will also be drawn.
*
* 3) Background: Surface fills the shape specified by [shape] with the [color]. If [color] is
* [ColorScheme.surface] a color overlay may be applied. The color of the overlay depends on the
* [tonalElevation] of this Surface, and the [LocalAbsoluteTonalElevation] set by any
* parent surfaces. This ensures that a Surface never appears to have a lower elevation overlay than
* its ancestors, by summing the elevation of all previous Surfaces.
*
* 4) Content color: Surface uses [contentColor] to specify a preferred color for the content of
* this surface - this is used by the [Text] and [Icon] components as a default color. If no
* [contentColor] is set, this surface will try and match its background color to a color defined in
* the theme [ColorScheme], and return the corresponding content color. For example, if the [color]
* of this surface is [ColorScheme.surface], [contentColor] will be set to [ColorScheme.onSurface].
* If [color] is not part of the theme palette, [contentColor] will keep the same value set above
* this Surface.
*
* 5) Click handling. This version of surface will react to the clicks, calling [onClick] lambda,
* updating the [interactionSource] when [PressInteraction] occurs, and showing ripple indication in
* response to press events. If you don't need click handling, consider using the Surface function
* that doesn't require [onClick] param.
*
* 6) Semantics for selection. Just like with [Modifier.selectable], selectable version of Surface
* will produce semantics to indicate that it is selected. Also, by default, accessibility services
* will describe the element as [Role.Tab]. You may change this by passing a desired [Role] with a
* [Modifier.semantics].
*
* To manually retrieve the content color inside a surface, use [LocalContentColor].
*
* Selectable surface sample:
* @sample androidx.compose.material3.samples.SelectableSurfaceSample
*
* @param selected whether or not this Surface is selected
* @param onClick callback to be called when the surface is clicked
* @param modifier Modifier to be applied to the layout corresponding to the surface
* @param enabled Controls the enabled state of the surface. When `false`, this surface will not be
* clickable
* @param shape Defines the surface's shape as well its shadow. A shadow is only displayed if the
* [tonalElevation] is greater than zero.
* @param color The background color. Use [Color.Transparent] to have no color.
* @param contentColor The preferred content color provided by this Surface to its children.
* Defaults to either the matching content color for [color], or if [color] is not a color from the
* theme, this will keep the same value set above this Surface.
* @param border Optional border to draw on top of the surface
* @param tonalElevation When [color] is [ColorScheme.surface], a higher the elevation will result
* in a darker color in light theme and lighter color in dark theme.
* @param shadowElevation The size of the shadow below the surface. Note that It will not affect z
* index of the Surface. If you want to change the drawing order you can use `Modifier.zIndex`.
* @param interactionSource the [MutableInteractionSource] representing the stream of [Interaction]s
* for this Surface. You can create and pass in your own remembered [MutableInteractionSource] if
* you want to observe [Interaction]s and customize the appearance / behavior of this Surface in
* different [Interaction]s.
*/
@ExperimentalMaterial3Api
@Composable
@NonRestartableComposable
fun Surface(
selected: Boolean,
onClick: () -> Unit,
modifier: Modifier = Modifier,
enabled: Boolean = true,
shape: Shape = RectangleShape,
color: Color = MaterialTheme.colorScheme.surface,
contentColor: Color = contentColorFor(color),
tonalElevation: Dp = 0.dp,
shadowElevation: Dp = 0.dp,
border: BorderStroke? = null,
interactionSource: MutableInteractionSource = remember { MutableInteractionSource() },
content: @Composable () -> Unit
) {
val absoluteElevation = LocalAbsoluteTonalElevation.current + tonalElevation
CompositionLocalProvider(
LocalContentColor provides contentColor,
LocalAbsoluteTonalElevation provides absoluteElevation
) {
Box(
modifier = modifier
.minimumTouchTargetSize()
.surface(
shape = shape,
backgroundColor = surfaceColorAtElevation(
color = color,
elevation = absoluteElevation
),
border = border,
shadowElevation = shadowElevation
)
.selectable(
selected = selected,
interactionSource = interactionSource,
indication = rememberRipple(),
enabled = enabled,
role = Role.Tab,
onClick = onClick
),
propagateMinConstraints = true
) {
content()
}
}
}
/**
* Material surface is the central metaphor in material design. Each surface exists at a given
* elevation, which influences how that piece of surface visually relates to other surfaces and how
* that surface is modified by tonal variance.
*
* This version of Surface is responsible for a toggling its checked state as well as everything
* else that a regular Surface does:
*
* This toggleable Surface is responsible for:
*
* 1) Clipping: Surface clips its children to the shape specified by [shape]
*
* 2) Borders: If [shape] has a border, then it will also be drawn.
*
* 3) Background: Surface fills the shape specified by [shape] with the [color]. If [color] is
* [ColorScheme.surface] a color overlay may be applied. The color of the overlay depends on the
* [tonalElevation] of this Surface, and the [LocalAbsoluteTonalElevation] set by any
* parent surfaces. This ensures that a Surface never appears to have a lower elevation overlay than
* its ancestors, by summing the elevation of all previous Surfaces.
*
* 4) Content color: Surface uses [contentColor] to specify a preferred color for the content of
* this surface - this is used by the [Text] and [Icon] components as a default color. If no
* [contentColor] is set, this surface will try and match its background color to a color defined in
* the theme [ColorScheme], and return the corresponding content color. For example, if the [color]
* of this surface is [ColorScheme.surface], [contentColor] will be set to [ColorScheme.onSurface].
* If [color] is not part of the theme palette, [contentColor] will keep the same value set above
* this Surface.
*
* 5) Click handling. This version of surface will react to the check toggles, calling
* [onCheckedChange] lambda, updating the [interactionSource] when [PressInteraction] occurs, and
* showing ripple indication in response to press events. If you don't need check
* handling, consider using a Surface function that doesn't require [onCheckedChange] param.
*
* 6) Semantics for toggle. Just like with [Modifier.toggleable], toggleable version of Surface
* will produce semantics to indicate that it is checked. Also, by default, accessibility services
* will describe the element as [Role.Switch]. You may change this by passing a desired [Role] with
* a [Modifier.semantics].
*
* To manually retrieve the content color inside a surface, use [LocalContentColor].
*
* Toggleable surface sample:
* @sample androidx.compose.material3.samples.ToggleableSurfaceSample
*
* @param checked whether or not this Surface is toggled on or off
* @param onCheckedChange callback to be invoked when the toggleable Surface is clicked
* @param modifier Modifier to be applied to the layout corresponding to the surface
* @param enabled Controls the enabled state of the surface. When `false`, this surface will not be
* clickable
* @param shape Defines the surface's shape as well its shadow. A shadow is only displayed if the
* [tonalElevation] is greater than zero.
* @param color The background color. Use [Color.Transparent] to have no color.
* @param contentColor The preferred content color provided by this Surface to its children.
* Defaults to either the matching content color for [color], or if [color] is not a color from the
* theme, this will keep the same value set above this Surface.
* @param border Optional border to draw on top of the surface
* @param tonalElevation When [color] is [ColorScheme.surface], a higher the elevation will result
* in a darker color in light theme and lighter color in dark theme.
* @param shadowElevation The size of the shadow below the surface. Note that It will not affect z
* index of the Surface. If you want to change the drawing order you can use `Modifier.zIndex`.
* @param interactionSource the [MutableInteractionSource] representing the stream of [Interaction]s
* for this Surface. You can create and pass in your own remembered [MutableInteractionSource] if
* you want to observe [Interaction]s and customize the appearance / behavior of this Surface in
* different [Interaction]s.
*/
@ExperimentalMaterial3Api
@Composable
@NonRestartableComposable
fun Surface(
checked: Boolean,
onCheckedChange: (Boolean) -> Unit,
modifier: Modifier = Modifier,
enabled: Boolean = true,
shape: Shape = RectangleShape,
color: Color = MaterialTheme.colorScheme.surface,
contentColor: Color = contentColorFor(color),
tonalElevation: Dp = 0.dp,
shadowElevation: Dp = 0.dp,
border: BorderStroke? = null,
interactionSource: MutableInteractionSource = remember { MutableInteractionSource() },
content: @Composable () -> Unit
) {
val absoluteElevation = LocalAbsoluteTonalElevation.current + tonalElevation
CompositionLocalProvider(
LocalContentColor provides contentColor,
LocalAbsoluteTonalElevation provides absoluteElevation
) {
Box(
modifier = modifier
.minimumTouchTargetSize()
.surface(
shape = shape,
backgroundColor = surfaceColorAtElevation(
color = color,
elevation = absoluteElevation
),
border = border,
shadowElevation = shadowElevation
)
.toggleable(
value = checked,
interactionSource = interactionSource,
indication = rememberRipple(),
enabled = enabled,
role = Role.Switch,
onValueChange = onCheckedChange
),
propagateMinConstraints = true
) {
content()
}
}
}
private fun Modifier.surface(
shape: Shape,
backgroundColor: Color,
border: BorderStroke?,
shadowElevation: Dp
) = this.shadow(shadowElevation, shape, clip = false)
.then(if (border != null) Modifier.border(border, shape) else Modifier)
.background(color = backgroundColor, shape = shape)
.clip(shape)
@Composable
private fun surfaceColorAtElevation(color: Color, elevation: Dp): Color {
return if (color == MaterialTheme.colorScheme.surface) {
MaterialTheme.colorScheme.surfaceColorAtElevation(elevation)
} else {
color
}
}
/**
* CompositionLocal containing the current absolute elevation provided by [Surface] components. This
* absolute elevation is a sum of all the previous elevations. Absolute elevation is only used for
* calculating surface tonal colors, and is *not* used for drawing the shadow in a [Surface].
*/
// TODO(b/179787782): Add sample after catalog app lands in aosp.
val LocalAbsoluteTonalElevation = compositionLocalOf { 0.dp }