forked from microsoft/fluentui-android
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TabItem.kt
214 lines (198 loc) · 8.01 KB
/
TabItem.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
package com.microsoft.fluentui.tokenized.tabItem
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.text.BasicText
import androidx.compose.material.ripple.rememberRipple
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.layout.Layout
import androidx.compose.ui.layout.layoutId
import androidx.compose.ui.semantics.Role
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import androidx.constraintlayout.compose.ChainStyle
import androidx.constraintlayout.compose.ConstraintLayout
import androidx.constraintlayout.compose.Dimension
import com.microsoft.fluentui.theme.FluentTheme
import com.microsoft.fluentui.theme.token.ControlTokens
import com.microsoft.fluentui.theme.token.FluentStyle
import com.microsoft.fluentui.theme.token.Icon
import com.microsoft.fluentui.theme.token.controlTokens.TabItemInfo
import com.microsoft.fluentui.theme.token.controlTokens.TabItemTokens
import com.microsoft.fluentui.theme.token.controlTokens.TabTextAlignment
@Composable
fun TabItem(
title: String,
icon: ImageVector,
onClick: () -> Unit,
accessory: (@Composable () -> Unit)?,
modifier: Modifier = Modifier,
style: FluentStyle = FluentStyle.Neutral,
textAlignment: TabTextAlignment = TabTextAlignment.VERTICAL,
enabled: Boolean = true,
selected: Boolean = false,
fixedWidth: Boolean = false,
interactionSource: MutableInteractionSource = remember { MutableInteractionSource() },
tabItemTokens: TabItemTokens? = null
) {
val themeID =
FluentTheme.themeID //Adding This only for recomposition in case of Token Updates. Unused otherwise.
val token =
tabItemTokens
?: FluentTheme.controlTokens.tokens[ControlTokens.ControlType.TabItemControlType] as TabItemTokens
val tabItemInfo = TabItemInfo(textAlignment, style)
val textColor =
token.textColor(tabItemInfo = tabItemInfo).getColorByState(
enabled = enabled,
selected = selected,
interactionSource = interactionSource
)
val iconColor =
token.iconColor(tabItemInfo = tabItemInfo).getColorByState(
enabled = enabled,
selected = selected,
interactionSource = interactionSource
)
val padding = token.padding(tabItemInfo = tabItemInfo)
val backgroundColor = token.backgroundBrush(tabItemInfo = tabItemInfo).getBrushByState(
enabled = enabled, selected = selected, interactionSource = interactionSource
)
val rippleColor = token.rippleColor(tabItemInfo = tabItemInfo)
val clickableModifier = Modifier
.clickable(
interactionSource = interactionSource,
indication = rememberRipple(color = rippleColor),
onClickLabel = null,
enabled = enabled,
onClick = onClick,
role = Role.Tab,
)
val widthModifier = if (fixedWidth) {
Modifier.width(token.width(tabItemInfo = tabItemInfo))
} else {
Modifier
}
val iconContent: @Composable () -> Unit = {
Icon(
imageVector = icon,
modifier = Modifier.size(if (textAlignment == TabTextAlignment.NO_TEXT) 28.dp else 24.dp),
contentDescription = if (textAlignment == TabTextAlignment.NO_TEXT) title else null,
tint = iconColor
)
}
if (textAlignment == TabTextAlignment.HORIZONTAL) {
ConstraintLayout(
modifier = modifier
.then(clickableModifier)
.background(backgroundColor)
.padding(padding)
.then(widthModifier)
)
{
val (iconConstrain, textConstrain, badgeConstrain) = createRefs()
Box(modifier = Modifier.constrainAs(iconConstrain) {
start.linkTo(parent.start)
end.linkTo(textConstrain.start)
}
) {
iconContent()
}
BasicText(
text = title,
modifier = Modifier
.constrainAs(textConstrain) {
start.linkTo(iconConstrain.end)
end.linkTo(badgeConstrain.start)
width = Dimension.preferredWrapContent
}
.padding(start = 8.dp),
style = TextStyle(color = textColor, textAlign = TextAlign.Center),
overflow = TextOverflow.Ellipsis,
maxLines = 1
)
if (accessory != null) {
Box(modifier = Modifier
.constrainAs(badgeConstrain) {
start.linkTo(textConstrain.end)
end.linkTo(parent.end)
}
) {
accessory()
}
}
createHorizontalChain(
iconConstrain,
textConstrain,
badgeConstrain,
chainStyle = ChainStyle.Packed
)
}
} else {
val badgeWithIcon: @Composable () -> Unit = {
Layout(
{
Box(
modifier = Modifier.layoutId("anchor"),
contentAlignment = Alignment.Center
) {
iconContent()
}
Box(modifier = Modifier.layoutId("badge")) {
if (accessory != null)
accessory()
}
}
) { measurables, constraints ->
val badgePlaceable = measurables.first { it.layoutId == "badge" }.measure(
// Measure with loose constraints for height as we don't want the text to take up more
// space than it needs.
constraints.copy(minHeight = 0)
)
val anchorPlaceable =
measurables.first { it.layoutId == "anchor" }.measure(constraints)
// Use the width of the badge to infer whether it has any content (based on radius used
// in [Badge]) and determine its horizontal offset.
val hasContent = badgePlaceable.width > 16.dp.roundToPx()
val contentOffset = if (hasContent) -2.dp.roundToPx() else 0
val badgeHorizontalOffset = -anchorPlaceable.width / 2 + contentOffset
val badgeVerticalOffset = (-4).dp
val totalWidth = anchorPlaceable.width
val totalHeight = anchorPlaceable.height
layout(
totalWidth,
totalHeight
) {
anchorPlaceable.placeRelative(0, 0)
val badgeX = anchorPlaceable.width + badgeHorizontalOffset
val badgeY = badgeVerticalOffset.roundToPx()
badgePlaceable.placeRelative(badgeX, badgeY)
}
}
}
Column(
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center,
modifier = modifier
.then(clickableModifier)
.background(backgroundColor)
.padding(padding)
.then(widthModifier)
) {
badgeWithIcon()
if (textAlignment == TabTextAlignment.VERTICAL) {
Spacer(modifier = Modifier.height(2.dp))
BasicText(
text = title,
style = TextStyle(color = textColor, textAlign = TextAlign.Center)
)
}
}
}
}