13
13
* See the License for the specific language governing permissions and
14
14
* limitations under the License.
15
15
*/
16
+ @file:OptIn(ExperimentalMaterial3ExpressiveApi ::class )
17
+
16
18
package com.android.developers.androidify.customize
17
19
20
+ import androidx.compose.foundation.background
18
21
import androidx.compose.foundation.border
22
+ import androidx.compose.foundation.layout.Box
19
23
import androidx.compose.foundation.layout.Spacer
20
- import androidx.compose.foundation.layout.padding
21
- import androidx.compose.foundation.layout.width
24
+ import androidx.compose.foundation.layout.size
22
25
import androidx.compose.material3.ExperimentalMaterial3ExpressiveApi
23
26
import androidx.compose.material3.FloatingToolbarColors
24
27
import androidx.compose.material3.HorizontalFloatingToolbar
@@ -33,8 +36,8 @@ import androidx.compose.ui.res.painterResource
33
36
import androidx.compose.ui.tooling.preview.Preview
34
37
import androidx.compose.ui.unit.dp
35
38
import com.android.developers.androidify.theme.AndroidifyTheme
39
+ import com.android.developers.androidify.theme.Primary
36
40
37
- @OptIn(ExperimentalMaterial3ExpressiveApi ::class )
38
41
@Composable
39
42
fun ToolSelector (
40
43
tools : List <CustomizeTool >,
@@ -43,103 +46,106 @@ fun ToolSelector(
43
46
horizontal : Boolean ,
44
47
modifier : Modifier = Modifier ,
45
48
) {
49
+ val buttons = @Composable {
50
+ tools.forEachIndexed { index, tool ->
51
+ ToolSelectorToggleButton (
52
+ modifier = Modifier ,
53
+ tool = tool,
54
+ checked = selectedOption == tool,
55
+ onCheckedChange = { onToolSelected(tool) },
56
+ )
57
+ if (index != tools.size - 1 ) {
58
+ Spacer (Modifier .size(8 .dp))
59
+ }
60
+ }
61
+ }
62
+ val toolbarColors = FloatingToolbarColors (
63
+ toolbarContainerColor = MaterialTheme .colorScheme.surface,
64
+ toolbarContentColor = MaterialTheme .colorScheme.onSurfaceVariant,
65
+ fabContainerColor = MaterialTheme .colorScheme.tertiary,
66
+ fabContentColor = MaterialTheme .colorScheme.onTertiary,
67
+ )
68
+
46
69
if (horizontal) {
47
70
HorizontalFloatingToolbar (
48
- modifier = modifier.border(
49
- 2 .dp,
50
- color = MaterialTheme .colorScheme.outline,
51
- shape = MaterialTheme .shapes.large,
52
- ).padding(2 .dp),
53
- colors = FloatingToolbarColors (
54
- toolbarContainerColor = MaterialTheme .colorScheme.surface,
55
- toolbarContentColor = MaterialTheme .colorScheme.onSurfaceVariant,
56
- fabContainerColor = MaterialTheme .colorScheme.tertiary,
57
- fabContentColor = MaterialTheme .colorScheme.onTertiary,
58
- ),
71
+ modifier = modifier.toolbarBorder(),
72
+ shape = MaterialTheme .shapes.large,
73
+ colors = toolbarColors,
59
74
expanded = true ,
60
75
) {
61
- tools.forEachIndexed { index, tool ->
62
- ToggleButton (
63
- modifier = Modifier ,
64
- checked = selectedOption == tool,
65
- onCheckedChange = { onToolSelected(tool) },
66
- shapes = ToggleButtonDefaults .shapes(checkedShape = MaterialTheme .shapes.large),
67
- colors = ToggleButtonDefaults .toggleButtonColors(
68
- checkedContainerColor = MaterialTheme .colorScheme.onSurface,
69
- containerColor = MaterialTheme .colorScheme.surface,
70
- ),
71
- ) {
72
- Icon (
73
- painterResource(tool.icon),
74
- contentDescription = tool.displayName,
75
- )
76
- }
77
- if (index != tools.size - 1 ) {
78
- Spacer (Modifier .width(8 .dp))
79
- }
80
- }
76
+ buttons()
81
77
}
82
78
} else {
83
79
VerticalFloatingToolbar (
84
- modifier = modifier.border(
85
- 2 .dp,
86
- color = MaterialTheme .colorScheme.outline,
87
- shape = MaterialTheme .shapes.large,
88
- ).padding(2 .dp),
89
- colors = FloatingToolbarColors (
90
- toolbarContainerColor = MaterialTheme .colorScheme.surface,
91
- toolbarContentColor = MaterialTheme .colorScheme.onSurfaceVariant,
92
- fabContainerColor = MaterialTheme .colorScheme.tertiary,
93
- fabContentColor = MaterialTheme .colorScheme.onTertiary,
94
- ),
80
+ modifier = modifier.toolbarBorder(),
81
+ shape = MaterialTheme .shapes.large,
82
+ colors = toolbarColors,
95
83
expanded = true ,
96
84
) {
97
- tools.forEachIndexed { index, tool ->
98
- ToggleButton (
99
- modifier = Modifier ,
100
- checked = selectedOption == tool,
101
- onCheckedChange = { onToolSelected(tool) },
102
- shapes = ToggleButtonDefaults .shapes(checkedShape = MaterialTheme .shapes.large),
103
- colors = ToggleButtonDefaults .toggleButtonColors(
104
- checkedContainerColor = MaterialTheme .colorScheme.onSurface,
105
- containerColor = MaterialTheme .colorScheme.surface,
106
- ),
107
- ) {
108
- Icon (
109
- painterResource(tool.icon),
110
- contentDescription = tool.displayName,
111
- )
112
- }
113
- if (index != tools.size - 1 ) {
114
- Spacer (Modifier .width(8 .dp))
115
- }
116
- }
85
+ buttons()
117
86
}
118
87
}
119
88
}
120
89
90
+ @Composable
91
+ private fun ToolSelectorToggleButton (
92
+ tool : CustomizeTool ,
93
+ checked : Boolean ,
94
+ onCheckedChange : (Boolean ) -> Unit ,
95
+ modifier : Modifier = Modifier ,
96
+ ) {
97
+ ToggleButton (
98
+ modifier = modifier,
99
+ checked = checked,
100
+ onCheckedChange = onCheckedChange,
101
+ shapes = ToggleButtonDefaults .shapes(
102
+ checkedShape = MaterialTheme .shapes.large,
103
+ ),
104
+ colors = ToggleButtonDefaults .toggleButtonColors(
105
+ checkedContainerColor = MaterialTheme .colorScheme.onSurface,
106
+ containerColor = MaterialTheme .colorScheme.surface,
107
+ ),
108
+ ) {
109
+ Icon (
110
+ painterResource(tool.icon),
111
+ contentDescription = tool.displayName,
112
+ )
113
+ }
114
+ }
115
+
116
+ @Composable
117
+ private fun Modifier.toolbarBorder () = this .border(
118
+ 2 .dp,
119
+ color = MaterialTheme .colorScheme.outline,
120
+ shape = MaterialTheme .shapes.large,
121
+ )
122
+
121
123
@Preview
122
124
@Composable
123
125
private fun ToolsPreviewHorizontal () {
124
126
AndroidifyTheme {
125
- ToolSelector (
126
- tools = listOf (CustomizeTool .Size , CustomizeTool .Background ),
127
- selectedOption = CustomizeTool .Size ,
128
- horizontal = true ,
129
- onToolSelected = {},
130
- )
127
+ Box (Modifier .background(Primary )) {
128
+ ToolSelector (
129
+ tools = listOf (CustomizeTool .Size , CustomizeTool .Background ),
130
+ selectedOption = CustomizeTool .Size ,
131
+ horizontal = true ,
132
+ onToolSelected = {},
133
+ )
134
+ }
131
135
}
132
136
}
133
137
134
138
@Preview
135
139
@Composable
136
140
private fun ToolsPreviewVertical () {
137
141
AndroidifyTheme {
138
- ToolSelector (
139
- tools = listOf (CustomizeTool .Size , CustomizeTool .Background ),
140
- selectedOption = CustomizeTool .Size ,
141
- horizontal = false ,
142
- onToolSelected = {},
143
- )
142
+ Box (Modifier .background(Primary )) {
143
+ ToolSelector (
144
+ tools = listOf (CustomizeTool .Size , CustomizeTool .Background ),
145
+ selectedOption = CustomizeTool .Size ,
146
+ horizontal = false ,
147
+ onToolSelected = {},
148
+ )
149
+ }
144
150
}
145
151
}
0 commit comments