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 pathAppBar.kt
1581 lines (1496 loc) · 68.3 KB
/
AppBar.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
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* 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.animation.animateColorAsState
import androidx.compose.animation.core.AnimationState
import androidx.compose.animation.core.DecayAnimationSpec
import androidx.compose.animation.core.FastOutLinearInEasing
import androidx.compose.animation.core.LinearOutSlowInEasing
import androidx.compose.animation.core.animateDecay
import androidx.compose.animation.core.animateTo
import androidx.compose.animation.core.tween
import androidx.compose.foundation.interaction.Interaction
import androidx.compose.foundation.interaction.InteractionSource
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.RowScope
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.BottomAppBarDefaults.FloatingActionButton
import androidx.compose.material3.tokens.BottomAppBarTokens
import androidx.compose.material3.tokens.FabSecondaryTokens
import androidx.compose.material3.tokens.TopAppBarLargeTokens
import androidx.compose.material3.tokens.TopAppBarMediumTokens
import androidx.compose.material3.tokens.TopAppBarSmallCenteredTokens
import androidx.compose.material3.tokens.TopAppBarSmallTokens
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.SideEffect
import androidx.compose.runtime.Stable
import androidx.compose.runtime.State
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberUpdatedState
import androidx.compose.runtime.saveable.Saver
import androidx.compose.runtime.saveable.listSaver
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clipToBounds
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.Shape
import androidx.compose.ui.graphics.lerp
import androidx.compose.ui.input.nestedscroll.NestedScrollConnection
import androidx.compose.ui.input.nestedscroll.NestedScrollSource
import androidx.compose.ui.layout.AlignmentLine
import androidx.compose.ui.layout.LastBaseline
import androidx.compose.ui.layout.Layout
import androidx.compose.ui.layout.layoutId
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.semantics.clearAndSetSemantics
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.unit.Constraints
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.Velocity
import androidx.compose.ui.unit.dp
import kotlin.math.abs
import kotlin.math.max
import kotlin.math.roundToInt
/**
* <a href="https://m3.material.io/components/top-app-bar/overview" class="external" target="_blank">Material Design small top app bar</a>.
*
* Top app bars display information and actions at the top of a screen.
*
* This SmallTopAppBar has slots for a title, navigation icon, and actions.
*
* ![Small top app bar image](https://developer.android.com/images/reference/androidx/compose/material3/small-top-app-bar.png)
*
* A simple top app bar looks like:
* @sample androidx.compose.material3.samples.SimpleSmallTopAppBar
* A top app bar that uses a [scrollBehavior] to customize its nested scrolling behavior when
* working in conjunction with a scrolling content looks like:
* @sample androidx.compose.material3.samples.PinnedSmallTopAppBar
* @sample androidx.compose.material3.samples.EnterAlwaysSmallTopAppBar
*
* @param title the title to be displayed in the top app bar
* @param modifier the [Modifier] to be applied to this top app bar
* @param navigationIcon the navigation icon displayed at the start of the top app bar. This should
* typically be an [IconButton] or [IconToggleButton].
* @param actions the actions displayed at the end of the top app bar. This should typically be
* [IconButton]s. The default layout here is a [Row], so icons inside will be placed horizontally.
* @param colors [TopAppBarColors] that will be used to resolve the colors used for this top app
* bar in different states. See [TopAppBarDefaults.smallTopAppBarColors].
* @param scrollBehavior a [TopAppBarScrollBehavior] which holds various offset values that will be
* applied by this top app bar to set up its height and colors. A scroll behavior is designed to
* work in conjunction with a scrolled content to change the top app bar appearance as the content
* scrolls. See [TopAppBarScrollBehavior.nestedScrollConnection].
*/
@Composable
fun SmallTopAppBar(
title: @Composable () -> Unit,
modifier: Modifier = Modifier,
navigationIcon: @Composable () -> Unit = {},
actions: @Composable RowScope.() -> Unit = {},
colors: TopAppBarColors = TopAppBarDefaults.smallTopAppBarColors(),
scrollBehavior: TopAppBarScrollBehavior? = null
) {
SingleRowTopAppBar(
modifier = modifier,
title = title,
titleTextStyle = MaterialTheme.typography.fromToken(TopAppBarSmallTokens.HeadlineFont),
centeredTitle = false,
navigationIcon = navigationIcon,
actions = actions,
colors = colors,
scrollBehavior = scrollBehavior
)
}
/**
* <a href="https://m3.material.io/components/top-app-bar/overview" class="external" target="_blank">Material Design center-aligned small top app bar</a>.
*
* Top app bars display information and actions at the top of a screen.
*
* This small top app bar has a header title that is horizontally aligned to the center.
*
* ![Center-aligned top app bar image](https://developer.android.com/images/reference/androidx/compose/material3/center-aligned-top-app-bar.png)
*
* This CenterAlignedTopAppBar has slots for a title, navigation icon, and actions.
*
* A center aligned top app bar that uses a [scrollBehavior] to customize its nested scrolling
* behavior when working in conjunction with a scrolling content looks like:
* @sample androidx.compose.material3.samples.SimpleCenterAlignedTopAppBar
*
* @param title the title to be displayed in the top app bar
* @param modifier the [Modifier] to be applied to this top app bar
* @param navigationIcon the navigation icon displayed at the start of the top app bar. This should
* typically be an [IconButton] or [IconToggleButton].
* @param actions the actions displayed at the end of the top app bar. This should typically be
* [IconButton]s. The default layout here is a [Row], so icons inside will be placed horizontally.
* @param colors [TopAppBarColors] that will be used to resolve the colors used for this top app
* bar in different states. See [TopAppBarDefaults.centerAlignedTopAppBarColors].
* @param scrollBehavior a [TopAppBarScrollBehavior] which holds various offset values that will be
* applied by this top app bar to set up its height and colors. A scroll behavior is designed to
* work in conjunction with a scrolled content to change the top app bar appearance as the content
* scrolls. See [TopAppBarScrollBehavior.nestedScrollConnection].
*/
@Composable
fun CenterAlignedTopAppBar(
title: @Composable () -> Unit,
modifier: Modifier = Modifier,
navigationIcon: @Composable () -> Unit = {},
actions: @Composable RowScope.() -> Unit = {},
colors: TopAppBarColors = TopAppBarDefaults.centerAlignedTopAppBarColors(),
scrollBehavior: TopAppBarScrollBehavior? = null
) {
SingleRowTopAppBar(
modifier = modifier,
title = title,
titleTextStyle =
MaterialTheme.typography.fromToken(TopAppBarSmallTokens.HeadlineFont),
centeredTitle = true,
navigationIcon = navigationIcon,
actions = actions,
colors = colors,
scrollBehavior = scrollBehavior
)
}
/**
* <a href="https://m3.material.io/components/top-app-bar/overview" class="external" target="_blank">Material Design medium top app bar</a>.
*
* Top app bars display information and actions at the top of a screen.
*
* ![Medium top app bar image](https://developer.android.com/images/reference/androidx/compose/material3/medium-top-app-bar.png)
*
* This MediumTopAppBar has slots for a title, navigation icon, and actions. In its default expanded
* state, the title is displayed in a second row under the navigation and actions.
*
* A medium top app bar that uses a [scrollBehavior] to customize its nested scrolling behavior when
* working in conjunction with scrolling content looks like:
* @sample androidx.compose.material3.samples.ExitUntilCollapsedMediumTopAppBar
*
* @param title the title to be displayed in the top app bar. This title will be used in the app
* bar's expanded and collapsed states, although in its collapsed state it will be composed with a
* smaller sized [TextStyle]
* @param modifier the [Modifier] to be applied to this top app bar
* @param navigationIcon the navigation icon displayed at the start of the top app bar. This should
* typically be an [IconButton] or [IconToggleButton].
* @param actions the actions displayed at the end of the top app bar. This should typically be
* [IconButton]s. The default layout here is a [Row], so icons inside will be placed horizontally.
* @param colors [TopAppBarColors] that will be used to resolve the colors used for this top app
* bar in different states. See [TopAppBarDefaults.mediumTopAppBarColors].
* @param scrollBehavior a [TopAppBarScrollBehavior] which holds various offset values that will be
* applied by this top app bar to set up its height and colors. A scroll behavior is designed to
* work in conjunction with a scrolled content to change the top app bar appearance as the content
* scrolls. See [TopAppBarScrollBehavior.nestedScrollConnection].
*/
@Composable
fun MediumTopAppBar(
title: @Composable () -> Unit,
modifier: Modifier = Modifier,
navigationIcon: @Composable () -> Unit = {},
actions: @Composable RowScope.() -> Unit = {},
colors: TopAppBarColors = TopAppBarDefaults.mediumTopAppBarColors(),
scrollBehavior: TopAppBarScrollBehavior? = null
) {
TwoRowsTopAppBar(
modifier = modifier,
title = title,
titleTextStyle = MaterialTheme.typography.fromToken(TopAppBarMediumTokens.HeadlineFont),
smallTitleTextStyle = MaterialTheme.typography.fromToken(TopAppBarSmallTokens.HeadlineFont),
titleBottomPadding = MediumTitleBottomPadding,
smallTitle = title,
navigationIcon = navigationIcon,
actions = actions,
colors = colors,
maxHeight = TopAppBarMediumTokens.ContainerHeight,
pinnedHeight = TopAppBarSmallTokens.ContainerHeight,
scrollBehavior = scrollBehavior
)
}
/**
* <a href="https://m3.material.io/components/top-app-bar/overview" class="external" target="_blank">Material Design large top app bar</a>.
*
* Top app bars display information and actions at the top of a screen.
*
* ![Large top app bar image](https://developer.android.com/images/reference/androidx/compose/material3/large-top-app-bar.png)
*
* This LargeTopAppBar has slots for a title, navigation icon, and actions. In its default expanded
* state, the title is displayed in a second row under the navigation and actions.
*
* A large top app bar that uses a [scrollBehavior] to customize its nested scrolling behavior when
* working in conjunction with scrolling content looks like:
* @sample androidx.compose.material3.samples.ExitUntilCollapsedLargeTopAppBar
*
* @param title the title to be displayed in the top app bar. This title will be used in the app
* bar's expanded and collapsed states, although in its collapsed state it will be composed with a
* smaller sized [TextStyle]
* @param modifier the [Modifier] to be applied to this top app bar
* @param navigationIcon the navigation icon displayed at the start of the top app bar. This should
* typically be an [IconButton] or [IconToggleButton].
* @param actions the actions displayed at the end of the top app bar. This should typically be
* [IconButton]s. The default layout here is a [Row], so icons inside will be placed horizontally.
* @param colors [TopAppBarColors] that will be used to resolve the colors used for this top app
* bar in different states. See [TopAppBarDefaults.largeTopAppBarColors].
* @param scrollBehavior a [TopAppBarScrollBehavior] which holds various offset values that will be
* applied by this top app bar to set up its height and colors. A scroll behavior is designed to
* work in conjunction with a scrolled content to change the top app bar appearance as the content
* scrolls. See [TopAppBarScrollBehavior.nestedScrollConnection].
*/
@Composable
fun LargeTopAppBar(
title: @Composable () -> Unit,
modifier: Modifier = Modifier,
navigationIcon: @Composable () -> Unit = {},
actions: @Composable RowScope.() -> Unit = {},
colors: TopAppBarColors = TopAppBarDefaults.largeTopAppBarColors(),
scrollBehavior: TopAppBarScrollBehavior? = null
) {
TwoRowsTopAppBar(
title = title,
titleTextStyle = MaterialTheme.typography.fromToken(TopAppBarLargeTokens.HeadlineFont),
smallTitleTextStyle = MaterialTheme.typography.fromToken(TopAppBarSmallTokens.HeadlineFont),
titleBottomPadding = LargeTitleBottomPadding,
smallTitle = title,
modifier = modifier,
navigationIcon = navigationIcon,
actions = actions,
colors = colors,
maxHeight = TopAppBarLargeTokens.ContainerHeight,
pinnedHeight = TopAppBarSmallTokens.ContainerHeight,
scrollBehavior = scrollBehavior
)
}
/**
* <a href="https://m3.material.io/components/bottom-app-bar/overview" class="external" target="_blank">Material Design bottom app bar</a>.
*
* A bottom app bar displays navigation and key actions at the bottom of mobile screens.
*
* ![Bottom app bar image](https://developer.android.com/images/reference/androidx/compose/material3/bottom-app-bar.png)
*
* @sample androidx.compose.material3.samples.SimpleBottomAppBar
*
* It can optionally display a [FloatingActionButton] embedded at the end of the BottomAppBar.
*
* @sample androidx.compose.material3.samples.BottomAppBarWithFAB
*
* Also see [NavigationBar].
*
* @param icons the icon content of this BottomAppBar. The default layout here is a [Row],
* so content inside will be placed horizontally.
* @param modifier the [Modifier] to be applied to this BottomAppBar
* @param floatingActionButton optional floating action button at the end of this BottomAppBar
* @param containerColor the color used for the background of this BottomAppBar. Use
* [Color.Transparent] to have no color.
* @param contentColor the preferred color for content inside this BottomAppBar. Defaults to either
* the matching content color for [containerColor], or to the current [LocalContentColor] if
* [containerColor] is not a color from the theme.
* @param tonalElevation when [containerColor] is [ColorScheme.surface], a translucent primary color
* overlay is applied on top of the container. A higher tonal elevation value will result in a
* darker color in light theme and lighter color in dark theme. See also: [Surface].
* @param contentPadding the padding applied to the content of this BottomAppBar
*/
@Composable
fun BottomAppBar(
icons: @Composable RowScope.() -> Unit,
modifier: Modifier = Modifier,
floatingActionButton: @Composable (() -> Unit)? = null,
containerColor: Color = BottomAppBarDefaults.ContainerColor,
contentColor: Color = contentColorFor(containerColor),
tonalElevation: Dp = BottomAppBarDefaults.ContainerElevation,
contentPadding: PaddingValues = BottomAppBarDefaults.ContentPadding,
) = BottomAppBar(
modifier = modifier,
containerColor = containerColor,
contentColor = contentColor,
tonalElevation = tonalElevation,
contentPadding = contentPadding
) {
icons()
if (floatingActionButton != null) {
Spacer(Modifier.weight(1f, true))
Box(
Modifier
.fillMaxHeight()
.padding(
top = FABVerticalPadding,
end = FABHorizontalPadding
),
contentAlignment = Alignment.TopStart
) {
floatingActionButton()
}
}
}
/**
* <a href="https://m3.material.io/components/bottom-app-bar/overview" class="external" target="_blank">Material Design bottom app bar</a>.
*
* A bottom app bar displays navigation and key actions at the bottom of mobile screens.
*
* ![Bottom app bar image](https://developer.android.com/images/reference/androidx/compose/material3/bottom-app-bar.png)
*
* If you are interested in displaying a [FloatingActionButton], consider using another overload.
*
* Also see [NavigationBar].
*
* @param modifier the [Modifier] to be applied to this BottomAppBar
* @param containerColor the color used for the background of this BottomAppBar. Use
* [Color.Transparent] to have no color.
* @param contentColor the preferred color for content inside this BottomAppBar. Defaults to either
* the matching content color for [containerColor], or to the current [LocalContentColor] if
* [containerColor] is not a color from the theme.
* @param tonalElevation when [containerColor] is [ColorScheme.surface], a translucent primary color
* overlay is applied on top of the container. A higher tonal elevation value will result in a
* darker color in light theme and lighter color in dark theme. See also: [Surface].
* @param contentPadding the padding applied to the content of this BottomAppBar
* @param content the content of this BottomAppBar. The default layout here is a [Row],
* so content inside will be placed horizontally.
*/
@Composable
fun BottomAppBar(
modifier: Modifier = Modifier,
containerColor: Color = BottomAppBarDefaults.ContainerColor,
contentColor: Color = contentColorFor(containerColor),
tonalElevation: Dp = BottomAppBarDefaults.ContainerElevation,
contentPadding: PaddingValues = BottomAppBarDefaults.ContentPadding,
content: @Composable RowScope.() -> Unit
) {
Surface(
color = containerColor,
contentColor = contentColor,
tonalElevation = tonalElevation,
// TODO(b/209583788): Consider adding a shape parameter if updated design guidance allows
shape = BottomAppBarTokens.ContainerShape.toShape(),
modifier = modifier
) {
Row(
Modifier
.fillMaxWidth()
.height(BottomAppBarTokens.ContainerHeight)
.padding(contentPadding),
horizontalArrangement = Arrangement.Start,
verticalAlignment = Alignment.CenterVertically,
content = content
)
}
}
/**
* A TopAppBarScrollBehavior defines how an app bar should behave when the content under it is
* scrolled.
*
* @see [TopAppBarDefaults.pinnedScrollBehavior]
* @see [TopAppBarDefaults.enterAlwaysScrollBehavior]
* @see [TopAppBarDefaults.exitUntilCollapsedScrollBehavior]
*/
@Stable
interface TopAppBarScrollBehavior {
/**
* A [TopAppBarScrollState] that is attached to this behavior and is read and updated when
* scrolling happens.
*/
val state: TopAppBarScrollState
/**
* A [NestedScrollConnection] that should be attached to a [Modifier.nestedScroll] in order to
* keep track of the scroll events.
*/
val nestedScrollConnection: NestedScrollConnection
/**
* Returns the top app bar's current scroll fraction.
*
* A scrollFraction is a value between `0.0` to `1.0` that provides a percentage of the app
* bar scroll position when the content is scrolled. `0.0` represents an expanded app bar,
* while `1.0` represents a collapsed one (e.g. the app bar is scrolled to its target offset).
* Note that this value will be updated on scroll even if the [TopAppBarScrollState.offset] is
* pinned to a specific value (see [TopAppBarDefaults.pinnedScrollBehavior]). In this case a
* value of 1.0 represents that the scroll value has exceeded the height of the pinned app bar,
* as if the app bar was collapsing.
*/
val scrollFraction: Float
}
/**
* Represents the colors used by a top app bar in different states.
*
* Each app bar has their own default implementation available in [TopAppBarDefaults], such as
* [TopAppBarDefaults.smallTopAppBarColors] for [SmallTopAppBar].
*/
@Stable
interface TopAppBarColors {
/**
* Represents the container color used for the top app bar, depending on whether the app bar is
* scrolled, and the percentage of its area that is scrolled.
*
* @param scrollFraction the scroll percentage of the top app bar (0.0 when the app bar is
* considered expanded to 1.0 when the app bar is scrolled to its target offset)
*/
@Composable
fun containerColor(scrollFraction: Float): State<Color>
/**
* Represents the content color used for the top app bar's navigation icon depending on whether
* the app bar is scrolled, and the percentage of its area that is scrolled.
*
* @param scrollFraction the scroll percentage of the top app bar (0.0 when the app bar is
* considered expanded to 1.0 when the app bar is scrolled to its target offset)
*/
@Composable
fun navigationIconContentColor(scrollFraction: Float): State<Color>
/**
* Represents the content color used for the top app bar's title depending on whether the app
* bar is scrolled, and the percentage of its area that is scrolled.
*
* @param scrollFraction the scroll percentage of the top app bar (0.0 when the app bar is
* considered expanded to 1.0 when the app bar is scrolled to its target offset)
*/
@Composable
fun titleContentColor(scrollFraction: Float): State<Color>
/**
* Represents the content color used for the top app bar's action icons depending on whether the
* app bar is scrolled, and the percentage of its area that is scrolled.
*
* @param scrollFraction the scroll percentage of the top app bar (0.0 when the app bar is
* considered expanded to 1.0 when the app bar is scrolled to its target offset)
*/
@Composable
fun actionIconContentColor(scrollFraction: Float): State<Color>
}
/** Contains default values used for the top app bar implementations. */
object TopAppBarDefaults {
/**
* Creates a [TopAppBarColors] for small top app bars. The default implementation animates
* between the provided colors according to the Material Design specification.
*
* @param containerColor the container color
* @param scrolledContainerColor the container color when content is scrolled behind it
* @param navigationIconContentColor the content color used for the navigation icon
* @param titleContentColor the content color used for the title
* @param actionIconContentColor the content color used for actions
* @return the resulting [TopAppBarColors] used for the top app bar
*/
@Composable
fun smallTopAppBarColors(
containerColor: Color = TopAppBarSmallTokens.ContainerColor.toColor(),
scrolledContainerColor: Color = MaterialTheme.colorScheme.applyTonalElevation(
backgroundColor = containerColor,
elevation = TopAppBarSmallTokens.OnScrollContainerElevation
),
navigationIconContentColor: Color = TopAppBarSmallTokens.LeadingIconColor.toColor(),
titleContentColor: Color = TopAppBarSmallTokens.HeadlineColor.toColor(),
actionIconContentColor: Color = TopAppBarSmallTokens.TrailingIconColor.toColor(),
): TopAppBarColors {
return remember(
containerColor,
scrolledContainerColor,
navigationIconContentColor,
titleContentColor,
actionIconContentColor
) {
AnimatingTopAppBarColors(
containerColor,
scrolledContainerColor,
navigationIconContentColor,
titleContentColor,
actionIconContentColor
)
}
}
/**
* Creates a [TopAppBarColors] for center aligned top app bars. The default implementation
* animates between the provided colors according to the Material Design specification.
*
* @param containerColor the container color
* @param scrolledContainerColor the container color when content is scrolled behind it
* @param navigationIconContentColor the content color used for the navigation icon
* @param titleContentColor the content color used for the title
* @param actionIconContentColor the content color used for actions
* @return the resulting [TopAppBarColors] used for the top app bar
*/
@Composable
fun centerAlignedTopAppBarColors(
containerColor: Color = TopAppBarSmallCenteredTokens.ContainerColor.toColor(),
scrolledContainerColor: Color = MaterialTheme.colorScheme.applyTonalElevation(
backgroundColor = containerColor,
elevation = TopAppBarSmallTokens.OnScrollContainerElevation
),
navigationIconContentColor: Color = TopAppBarSmallCenteredTokens.LeadingIconColor.toColor(),
titleContentColor: Color = TopAppBarSmallCenteredTokens.HeadlineColor.toColor(),
actionIconContentColor: Color = TopAppBarSmallCenteredTokens.TrailingIconColor.toColor(),
): TopAppBarColors {
return remember(
containerColor,
scrolledContainerColor,
navigationIconContentColor,
titleContentColor,
actionIconContentColor
) {
AnimatingTopAppBarColors(
containerColor,
scrolledContainerColor,
navigationIconContentColor,
titleContentColor,
actionIconContentColor
)
}
}
/**
* Creates a [TopAppBarColors] for medium top app bars. The default implementation interpolates
* between the provided colors as the top app bar scrolls according to the Material Design
* specification.
*
* @param containerColor the container color
* @param scrolledContainerColor the container color when content is scrolled behind it
* @param navigationIconContentColor the content color used for the navigation icon
* @param titleContentColor the content color used for the title
* @param actionIconContentColor the content color used for actions
* @return the resulting [TopAppBarColors] used for the top app bar
*/
@Composable
fun mediumTopAppBarColors(
containerColor: Color = TopAppBarMediumTokens.ContainerColor.toColor(),
scrolledContainerColor: Color = MaterialTheme.colorScheme.applyTonalElevation(
backgroundColor = containerColor,
elevation = TopAppBarSmallTokens.OnScrollContainerElevation
),
navigationIconContentColor: Color = TopAppBarMediumTokens.LeadingIconColor.toColor(),
titleContentColor: Color = TopAppBarMediumTokens.HeadlineColor.toColor(),
actionIconContentColor: Color = TopAppBarMediumTokens.TrailingIconColor.toColor(),
): TopAppBarColors {
return remember(
containerColor,
scrolledContainerColor,
navigationIconContentColor,
titleContentColor,
actionIconContentColor
) {
InterpolatingTopAppBarColors(
containerColor,
scrolledContainerColor,
navigationIconContentColor,
titleContentColor,
actionIconContentColor
)
}
}
/**
* Creates a [TopAppBarColors] for large top app bars. The default implementation interpolates
* between the provided colors as the top app bar scrolls according to the Material Design
* specification.
*
* @param containerColor the container color
* @param scrolledContainerColor the container color when content is scrolled behind it
* @param navigationIconContentColor the content color used for the navigation icon
* @param titleContentColor the content color used for the title
* @param actionIconContentColor the content color used for actions
* @return the resulting [TopAppBarColors] used for the top app bar
*/
@Composable
fun largeTopAppBarColors(
containerColor: Color = TopAppBarLargeTokens.ContainerColor.toColor(),
scrolledContainerColor: Color = MaterialTheme.colorScheme.applyTonalElevation(
backgroundColor = containerColor,
elevation = TopAppBarSmallTokens.OnScrollContainerElevation
),
navigationIconContentColor: Color = TopAppBarLargeTokens.LeadingIconColor.toColor(),
titleContentColor: Color = TopAppBarLargeTokens.HeadlineColor.toColor(),
actionIconContentColor: Color = TopAppBarLargeTokens.TrailingIconColor.toColor(),
): TopAppBarColors {
return remember(
containerColor,
scrolledContainerColor,
navigationIconContentColor,
titleContentColor,
actionIconContentColor
) {
InterpolatingTopAppBarColors(
containerColor,
scrolledContainerColor,
navigationIconContentColor,
titleContentColor,
actionIconContentColor
)
}
}
/**
* Returns a pinned [TopAppBarScrollBehavior] that tracks nested-scroll callbacks and
* updates its [TopAppBarScrollState.contentOffset] accordingly.
*
* @param state the state object to be used to control or observe the top app bar's scroll
* state. See also [rememberTopAppBarScrollState] to create a state that is remembered across
* compositions.
* @param canScroll a callback used to determine whether scroll events are to be handled by this
* pinned [TopAppBarScrollBehavior]
*/
@ExperimentalMaterial3Api
fun pinnedScrollBehavior(
state: TopAppBarScrollState,
canScroll: () -> Boolean = { true }
): TopAppBarScrollBehavior = PinnedScrollBehavior(state, canScroll)
/**
* Returns a [TopAppBarScrollBehavior]. A top app bar that is set up with this
* [TopAppBarScrollBehavior] will immediately collapse when the content is pulled up, and will
* immediately appear when the content is pulled down.
*
* @param state the state object to be used to control or observe the top app bar's scroll
* state. See also [rememberTopAppBarScrollState] to create a state that is remembered across
* compositions.
* @param canScroll a callback used to determine whether scroll events are to be
* handled by this [EnterAlwaysScrollBehavior]
*/
@ExperimentalMaterial3Api
fun enterAlwaysScrollBehavior(
state: TopAppBarScrollState,
canScroll: () -> Boolean = { true }
): TopAppBarScrollBehavior = EnterAlwaysScrollBehavior(state, canScroll)
/**
* Returns a [TopAppBarScrollBehavior] that adjusts its properties to affect the colors and
* height of the top app bar.
*
* A top app bar that is set up with this [TopAppBarScrollBehavior] will immediately collapse
* when the nested content is pulled up, and will expand back the collapsed area when the
* content is pulled all the way down.
*
* @param decayAnimationSpec a [DecayAnimationSpec] that will be used by the top app bar motion
* when the user flings the content. Preferably, this should match the animation spec used by
* the scrollable content. See also [androidx.compose.animation.rememberSplineBasedDecay] for a
* default [DecayAnimationSpec] that can be used with this behavior.
* @param state the state object to be used to control or observe the top app bar's scroll
* state. See also [rememberTopAppBarScrollState] to create a state that is remembered across
* compositions.
* @param canScroll a callback used to determine whether scroll events are to be
* handled by this [ExitUntilCollapsedScrollBehavior]
*/
@ExperimentalMaterial3Api
fun exitUntilCollapsedScrollBehavior(
decayAnimationSpec: DecayAnimationSpec<Float>,
state: TopAppBarScrollState,
canScroll: () -> Boolean = { true }
): TopAppBarScrollBehavior =
ExitUntilCollapsedScrollBehavior(
state,
decayAnimationSpec,
canScroll
)
}
/**
* Creates a [TopAppBarScrollState] that is remembered across compositions.
*
* Changes to the provided initial values will **not** result in the state being recreated or
* changed in any way if it has already been created.
*
* @param initialOffsetLimit the initial value for [TopAppBarScrollState.offsetLimit], which
* represents the offset that a top app bar is allowed to scroll when the scrollable content is
* scrolled
* @param initialOffset the initial value for [TopAppBarScrollState.offset]. The initial offset
* should be between zero and [initialOffsetLimit].
* @param initialContentOffset the initial value for [TopAppBarScrollState.contentOffset]
*/
@Composable
fun rememberTopAppBarScrollState(
initialOffsetLimit: Float = -Float.MAX_VALUE,
initialOffset: Float = 0f,
initialContentOffset: Float = 0f
): TopAppBarScrollState {
return rememberSaveable(saver = TopAppBarScrollState.Saver) {
TopAppBarScrollState(
initialOffsetLimit,
initialOffset,
initialContentOffset
)
}
}
/**
* A state object that can be hoisted to control and observe the top app bar scroll state. The state
* is read and updated by a [TopAppBarScrollBehavior] implementation.
*
* In most cases, this will be created via [rememberTopAppBarScrollState].
*
* @param offsetLimit the initial value for [TopAppBarScrollState.offsetLimit]
* @param offset the initial value for [TopAppBarScrollState.offset]
* @param contentOffset the initial value for [TopAppBarScrollState.contentOffset]
*/
@Stable
class TopAppBarScrollState(offsetLimit: Float, offset: Float, contentOffset: Float) {
/**
* The top app bar's offset limit in pixels, which represents the offset that a top app bar is
* allowed to scroll when the scrollable content is scrolled.
*
* This limit is represented by a negative [Float], and used to coerce the [offset] value when
* the content is scrolled.
*/
var offsetLimit by mutableStateOf(offsetLimit)
/**
* The top app bar's current offset in pixels.
*
* The offset is usually between zero and the [offsetLimit].
*/
var offset by mutableStateOf(offset)
/**
* The current content offset that is updated when the nested scroll connection consumes scroll
* events.
*
* A common behavior implementation would update this value to be the sum of all
* [NestedScrollConnection.onPostScroll] `consumed.y` values.
*/
var contentOffset by mutableStateOf(contentOffset)
companion object {
/**
* The default [Saver] implementation for [TopAppBarScrollState].
*/
val Saver: Saver<TopAppBarScrollState, *> = listSaver(
save = { listOf(it.offsetLimit, it.offset, it.contentOffset) },
restore = {
TopAppBarScrollState(
offsetLimit = it[0],
offset = it[1],
contentOffset = it[2]
)
}
)
}
}
/** Contains default values used for the bottom app bar implementations. */
object BottomAppBarDefaults {
/** Default color used for [BottomAppBar] container **/
val ContainerColor: Color @Composable get() = BottomAppBarTokens.ContainerColor.toColor()
/** Default elevation used for [BottomAppBar] **/
val ContainerElevation: Dp = BottomAppBarTokens.ContainerElevation
/**
* Default padding used for [BottomAppBar] when content are default size (24dp) icons in
* [IconButton] that meet the minimum touch target (48.dp).
*/
val ContentPadding = PaddingValues(
start = BottomAppBarHorizontalPadding,
top = BottomAppBarVerticalPadding,
end = BottomAppBarHorizontalPadding
)
// Bottom App Bar FAB Defaults
/**
* Creates a [FloatingActionButtonElevation] that represents the default elevation of a
* [FloatingActionButton] used for [BottomAppBar] in different states.
*/
object FloatingActionButtonElevation :
androidx.compose.material3.FloatingActionButtonElevation {
val elevation = mutableStateOf(0.dp)
@Composable
override fun shadowElevation(interactionSource: InteractionSource) = elevation
@Composable
override fun tonalElevation(interactionSource: InteractionSource) = elevation
}
/** The color of a [BottomAppBar]'s [FloatingActionButton] */
val FloatingActionButtonContainerColor: Color @Composable get() =
FabSecondaryTokens.ContainerColor.toColor()
/** The shape of a [BottomAppBar]'s [FloatingActionButton] */
val FloatingActionButtonShape: Shape @Composable get() =
FabSecondaryTokens.ContainerShape.toShape()
/**
* The default [FloatingActionButton] for [BottomAppBar]
*
* A [BottomAppBar]'s FAB follows a secondary color style, as well as an elevation of zero.
*
* @sample androidx.compose.material3.samples.BottomAppBarWithFAB
*
* @param onClick callback invoked when this FAB is clicked
* @param modifier [Modifier] to be applied to this FAB.
* @param interactionSource the [MutableInteractionSource] representing the stream of
* [Interaction]s for this FAB. You can create and pass in your own `remember`ed instance to
* observe [Interaction]s and customize the appearance / behavior of this FAB in different
* states.
* @param shape defines the shape of this FAB's container and shadow (when using [elevation])
* @param containerColor the color used for the background of this FAB. Use [Color.Transparent]
* to have no color.
* @param contentColor the preferred color for content inside this FAB. Defaults to either the
* matching content color for [containerColor], or to the current [LocalContentColor] if
* [containerColor] is not a color from the theme.
* @param elevation [FloatingActionButtonElevation] used to resolve the elevation for this FAB
* in different states. This controls the size of the shadow below the FAB. Additionally, when
* the container color is [ColorScheme.surface], this controls the amount of primary color
* applied as an overlay. See also: [Surface].
* @param content the content of this FAB - this is typically an [Icon].
*/
@Composable
fun FloatingActionButton(
onClick: () -> Unit,
modifier: Modifier = Modifier,
interactionSource: MutableInteractionSource = remember { MutableInteractionSource() },
shape: Shape = FloatingActionButtonShape,
containerColor: Color = FloatingActionButtonContainerColor,
contentColor: Color = contentColorFor(containerColor),
elevation: androidx.compose.material3.FloatingActionButtonElevation =
FloatingActionButtonElevation,
content: @Composable () -> Unit,
) = androidx.compose.material3.FloatingActionButton(
onClick = onClick,
modifier = modifier,
interactionSource = interactionSource,
shape = shape,
containerColor = containerColor,
contentColor = contentColor,
elevation = elevation,
content = content
)
}
// Padding minus IconButton's min touch target expansion
private val BottomAppBarHorizontalPadding = 16.dp - 12.dp
// Padding minus IconButton's min touch target expansion
private val BottomAppBarVerticalPadding = 16.dp - 12.dp
// Padding minus content padding
private val FABHorizontalPadding = 16.dp - BottomAppBarHorizontalPadding
private val FABVerticalPadding = 12.dp - BottomAppBarVerticalPadding
/**
* A single-row top app bar that is designed to be called by the small and center aligned top app
* bar composables.
*
* This SingleRowTopAppBar has slots for a title, navigation icon, and actions. When the
* [centeredTitle] flag is true, the title will be horizontally aligned to the center of the top app
* bar width.
*/
@Composable
private fun SingleRowTopAppBar(
modifier: Modifier = Modifier,
title: @Composable () -> Unit,
titleTextStyle: TextStyle,
centeredTitle: Boolean,
navigationIcon: @Composable () -> Unit,
actions: @Composable RowScope.() -> Unit,
colors: TopAppBarColors,
scrollBehavior: TopAppBarScrollBehavior?
) {
// TODO(b/182393826): Check if there is a better place to set the offsetLimit.
// Set a scroll offset limit to hide the entire app bar area when scrolling.
val offsetLimit = with(LocalDensity.current) { -TopAppBarSmallTokens.ContainerHeight.toPx() }
SideEffect {
if (scrollBehavior?.state?.offsetLimit != offsetLimit) {
scrollBehavior?.state?.offsetLimit = offsetLimit
}
}
// Obtain the container color from the TopAppBarColors.
// This may potentially animate or interpolate a transition between the container-color and the
// container's scrolled-color according to the app bar's scroll state.
val scrollFraction = scrollBehavior?.scrollFraction ?: 0f
val appBarContainerColor by colors.containerColor(scrollFraction)
// Wrap the given actions in a Row.
val actionsRow = @Composable {
Row(
horizontalArrangement = Arrangement.End,
verticalAlignment = Alignment.CenterVertically,
content = actions
)
}
// Compose a Surface with a TopAppBarLayout content. The surface's background color will be
// animated as specified above, and the height of the app bar will be determined by the current
// scroll-state offset.
Surface(modifier = modifier, color = appBarContainerColor) {
val height = LocalDensity.current.run {
TopAppBarSmallTokens.ContainerHeight.toPx() + (scrollBehavior?.state?.offset ?: 0f)
}
TopAppBarLayout(
modifier = Modifier,
heightPx = height,
navigationIconContentColor = colors.navigationIconContentColor(scrollFraction).value,
titleContentColor = colors.titleContentColor(scrollFraction).value,
actionIconContentColor = colors.actionIconContentColor(scrollFraction).value,
title = title,
titleTextStyle = titleTextStyle,
titleAlpha = 1f,
titleVerticalArrangement = Arrangement.Center,
titleHorizontalArrangement =
if (centeredTitle) Arrangement.Center else Arrangement.Start,
titleBottomPadding = 0,
hideTitleSemantics = false,
navigationIcon = navigationIcon,
actions = actionsRow,
)
}
}
/**
* A two-rows top app bar that is designed to be called by the Large and Medium top app bar
* composables.
*
* @throws [IllegalArgumentException] if the given [maxHeight] is equal or smaller than the
* [pinnedHeight]
*/
@Composable
private fun TwoRowsTopAppBar(
modifier: Modifier = Modifier,
title: @Composable () -> Unit,
titleTextStyle: TextStyle,
titleBottomPadding: Dp,
smallTitle: @Composable () -> Unit,
smallTitleTextStyle: TextStyle,
navigationIcon: @Composable () -> Unit,
actions: @Composable RowScope.() -> Unit,
colors: TopAppBarColors,
maxHeight: Dp,
pinnedHeight: Dp,
scrollBehavior: TopAppBarScrollBehavior?
) {
if (maxHeight <= pinnedHeight) {
throw IllegalArgumentException(
"A TwoRowsTopAppBar max height should be greater than its pinned height"
)
}
val pinnedHeightPx: Float
val maxHeightPx: Float
val titleBottomPaddingPx: Int