xref: /aosp_15_r20/external/accompanist/insets-ui/src/main/java/com/google/accompanist/insets/ui/BottomNavigation.kt (revision fa44fe6ae8e729aa3cfe5c03eedbbf98fb44e2c6)
1 /*
2  * Copyright 2021 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  *      https://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 @file:Suppress("DEPRECATION")
18 
19 package com.google.accompanist.insets.ui
20 
21 import androidx.compose.foundation.layout.Arrangement
22 import androidx.compose.foundation.layout.PaddingValues
23 import androidx.compose.foundation.layout.Row
24 import androidx.compose.foundation.layout.RowScope
25 import androidx.compose.foundation.layout.fillMaxWidth
26 import androidx.compose.foundation.layout.height
27 import androidx.compose.foundation.layout.padding
28 import androidx.compose.foundation.selection.selectableGroup
29 import androidx.compose.material.BottomNavigation
30 import androidx.compose.material.BottomNavigationDefaults
31 import androidx.compose.material.MaterialTheme
32 import androidx.compose.material.Surface
33 import androidx.compose.material.contentColorFor
34 import androidx.compose.material.primarySurface
35 import androidx.compose.runtime.Composable
36 import androidx.compose.ui.Modifier
37 import androidx.compose.ui.graphics.Color
38 import androidx.compose.ui.unit.Dp
39 import androidx.compose.ui.unit.dp
40 
41 /**
42  * A wrapper around [BottomNavigation] which supports the setting of [contentPadding] to add
43  * internal padding. This is especially useful in conjunction with insets.
44  *
45  * For an edge-to-edge layout, typically you would use the
46  * [com.google.accompanist.insets.WindowInsets.navigationBars] insets like so below:
47  *
48  * @sample com.google.accompanist.sample.insets.BottomNavigation_Insets
49  */
50 @Deprecated(
51     """
52         accompanist/insets-ui has been deprecated.
53         This functionality has been upstreamed to Material.
54         For more migration information, please visit https://google.github.io/accompanist/insets/#migration
55     """
56 )
57 @Composable
BottomNavigationnull58 public fun BottomNavigation(
59     modifier: Modifier = Modifier,
60     contentPadding: PaddingValues = PaddingValues(0.dp),
61     backgroundColor: Color = MaterialTheme.colors.primarySurface,
62     contentColor: Color = contentColorFor(backgroundColor),
63     elevation: Dp = BottomNavigationDefaults.Elevation,
64     content: @Composable RowScope.() -> Unit,
65 ) {
66     BottomNavigationSurface(modifier, backgroundColor, contentColor, elevation) {
67         BottomNavigationContent(Modifier.padding(contentPadding)) {
68             content()
69         }
70     }
71 }
72 
73 @Deprecated(
74     """
75         accompanist/insets-ui has been deprecated.
76         This functionality has been upstreamed to Material.
77         For more migration information, please visit https://google.github.io/accompanist/insets/#migration
78     """
79 )
80 @Composable
BottomNavigationSurfacenull81 public fun BottomNavigationSurface(
82     modifier: Modifier = Modifier,
83     backgroundColor: Color = MaterialTheme.colors.primarySurface,
84     contentColor: Color = contentColorFor(backgroundColor),
85     elevation: Dp = BottomNavigationDefaults.Elevation,
86     content: @Composable () -> Unit
87 ) {
88     Surface(
89         color = backgroundColor,
90         contentColor = contentColor,
91         elevation = elevation,
92         modifier = modifier,
93     ) {
94         content()
95     }
96 }
97 
98 @Deprecated(
99     """
100         accompanist/insets-ui has been deprecated.
101         This functionality has been upstreamed to Material.
102         For more migration information, please visit https://google.github.io/accompanist/insets/#migration
103     """
104 )
105 @Composable
BottomNavigationContentnull106 public fun BottomNavigationContent(
107     modifier: Modifier = Modifier,
108     content: @Composable RowScope.() -> Unit,
109 ) {
110     Row(
111         modifier = modifier
112             .fillMaxWidth()
113             .height(BottomNavigationHeight)
114             .selectableGroup(),
115         horizontalArrangement = Arrangement.SpaceBetween,
116         content = content,
117     )
118 }
119 
120 /**
121  * Copied from [androidx.compose.material.BottomNavigationHeight]
122  * Height of a [BottomNavigation] component
123  */
124 private val BottomNavigationHeight = 56.dp
125