forked from microsoft/fluentui-android
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pill.kt
284 lines (268 loc) · 11.4 KB
/
Pill.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
package com.microsoft.fluentui.tokenized.segmentedcontrols
import androidx.compose.animation.core.Animatable
import androidx.compose.animation.core.Spring
import androidx.compose.animation.core.spring
import androidx.compose.animation.core.tween
import androidx.compose.foundation.*
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.interaction.collectIsFocusedAsState
import androidx.compose.foundation.interaction.collectIsHoveredAsState
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyRow
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.BasicText
import androidx.compose.material.ripple.rememberRipple
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.draw.scale
import androidx.compose.ui.focus.onFocusEvent
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.SolidColor
import androidx.compose.ui.graphics.drawscope.Fill
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.semantics.Role
import androidx.compose.ui.semantics.clearAndSetSemantics
import androidx.compose.ui.semantics.contentDescription
import androidx.compose.ui.semantics.semantics
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import com.microsoft.fluentui.tablayout.R
import com.microsoft.fluentui.theme.FluentTheme
import com.microsoft.fluentui.theme.token.ControlTokens
import com.microsoft.fluentui.theme.token.FluentGlobalTokens
import com.microsoft.fluentui.theme.token.FluentStyle
import com.microsoft.fluentui.theme.token.Icon
import com.microsoft.fluentui.theme.token.controlTokens.PillBarInfo
import com.microsoft.fluentui.theme.token.controlTokens.PillBarTokens
import com.microsoft.fluentui.theme.token.controlTokens.PillButtonInfo
import com.microsoft.fluentui.theme.token.controlTokens.PillButtonTokens
import com.microsoft.fluentui.util.dpToPx
import kotlinx.coroutines.launch
import kotlin.math.max
data class PillMetaData(
var text: String,
var onClick: (() -> Unit),
var icon: ImageVector? = null,
var enabled: Boolean = true,
var selected: Boolean = false,
var notificationDot: Boolean = false,
)
/**
* API to create Pill shaped Button which will further be used in tabs and bars.
*
* @param pillMetaData Metadata for a single pill. Type: [PillMetaData]
* @param modifier Optional Modifier to customize the design and behaviour of pill button
* @param style Color Scheme of pill shaped button. Default: [FluentStyle.Neutral]
* @param interactionSource Interaction Source Object to handle gestures.
* @param pillButtonTokens Tokens to customize the design of pill button.
*/
@Composable
fun PillButton(
pillMetaData: PillMetaData,
modifier: Modifier = Modifier,
style: FluentStyle = FluentStyle.Neutral,
interactionSource: MutableInteractionSource = remember { MutableInteractionSource() },
pillButtonTokens: PillButtonTokens? = null
) {
val themeID =
FluentTheme.themeID //Adding This only for recomposition in case of Token Updates. Unused otherwise.
val token = pillButtonTokens
?: FluentTheme.controlTokens.tokens[ControlTokens.ControlType.PillButtonControlType] as PillButtonTokens
val pillButtonInfo = PillButtonInfo(
style,
pillMetaData.enabled,
pillMetaData.selected
)
val shape = RoundedCornerShape(50)
val scaleBox = remember { Animatable(1.0F) }
LaunchedEffect(key1 = pillMetaData.selected) {
if (pillMetaData.selected) {
launch {
scaleBox.animateTo(
targetValue = 0.95F,
animationSpec = tween(
durationMillis = 50
)
)
scaleBox.animateTo(
targetValue = 1.0F,
animationSpec = spring(
dampingRatio = Spring.DampingRatioLowBouncy,
stiffness = Spring.StiffnessLow
)
)
}
}
}
val backgroundColor = token.backgroundBrush(pillButtonInfo = pillButtonInfo)
.getBrushByState(
enabled = pillMetaData.enabled,
selected = pillMetaData.selected,
interactionSource = interactionSource
)
val iconColor =
token.iconColor(pillButtonInfo = pillButtonInfo).getColorByState(
enabled = pillMetaData.enabled,
selected = pillMetaData.selected,
interactionSource = interactionSource
)
val textColor =
token.textColor(pillButtonInfo = pillButtonInfo).getColorByState(
enabled = pillMetaData.enabled,
selected = pillMetaData.selected,
interactionSource = interactionSource
)
val fontStyle = token.typography(pillButtonInfo)
val focusStroke = token.focusStroke(pillButtonInfo)
var focusedBorderModifier: Modifier = Modifier
for (borderStroke in focusStroke) {
focusedBorderModifier =
focusedBorderModifier.border(borderStroke, shape)
}
val clickAndSemanticsModifier = modifier.clickable(
interactionSource = interactionSource,
indication = rememberRipple(),
enabled = pillMetaData.enabled,
onClickLabel = null,
role = Role.Button,
onClick = pillMetaData.onClick
)
val selectedString = if (pillMetaData.selected)
LocalContext.current.resources.getString(R.string.fluentui_selected)
else
LocalContext.current.resources.getString(R.string.fluentui_not_selected)
val enabledString = if (pillMetaData.enabled)
LocalContext.current.resources.getString(R.string.fluentui_enabled)
else
LocalContext.current.resources.getString(R.string.fluentui_disabled)
Box(
modifier
.scale(scaleBox.value)
.defaultMinSize(minHeight = token.minHeight(pillButtonInfo))
.clip(shape)
.background(backgroundColor, shape)
.then(clickAndSemanticsModifier)
.then(if (interactionSource.collectIsFocusedAsState().value || interactionSource.collectIsHoveredAsState().value) focusedBorderModifier else Modifier)
.padding(vertical = token.verticalPadding(pillButtonInfo))
.semantics(true) {
contentDescription = if (pillMetaData.enabled) "${pillMetaData.text} $selectedString" else "${pillMetaData.text} $enabledString"
},
contentAlignment = Alignment.Center
) {
Row(Modifier.width(IntrinsicSize.Max)) {
if (pillMetaData.icon != null) {
Spacer(Modifier.requiredWidth(FluentGlobalTokens.size(FluentGlobalTokens.SizeTokens.Size180)))
Icon(
pillMetaData.icon!!,
pillMetaData.text,
modifier = Modifier
.size(token.iconSize(pillButtonInfo))
.clearAndSetSemantics { },
tint = iconColor
)
} else {
Spacer(Modifier.requiredWidth(FluentGlobalTokens.size(FluentGlobalTokens.SizeTokens.Size160)))
BasicText(
pillMetaData.text,
modifier = Modifier
.weight(1F)
.clearAndSetSemantics { },
style = fontStyle.merge(
TextStyle(color = textColor)
),
maxLines = 1,
overflow = TextOverflow.Ellipsis
)
}
if (pillMetaData.notificationDot) {
val notificationDotColor: Color =
token.notificationDotColor(pillButtonInfo)
.getColorByState(
enabled = pillMetaData.enabled,
selected = pillMetaData.selected,
interactionSource = interactionSource
)
Spacer(Modifier.requiredWidth(FluentGlobalTokens.size(FluentGlobalTokens.SizeTokens.Size20)))
Canvas(
modifier = Modifier
.padding(top = 2.dp, bottom = 12.dp)
.sizeIn(minWidth = 6.dp, minHeight = 6.dp)
) {
drawCircle(
color = notificationDotColor, style = Fill, radius = dpToPx(3.dp)
)
}
if (pillMetaData.icon != null)
Spacer(Modifier.requiredWidth(FluentGlobalTokens.size(FluentGlobalTokens.SizeTokens.Size100)))
else
Spacer(Modifier.requiredWidth(FluentGlobalTokens.size(FluentGlobalTokens.SizeTokens.Size80)))
} else {
Spacer(Modifier.requiredWidth(FluentGlobalTokens.size(FluentGlobalTokens.SizeTokens.Size160)))
}
}
}
}
/**
* API to create Bar of Pill button. The PillBar control is a linear set of two or more PillButton, each of which functions as a mutually exclusive button.
* PillBar are commonly used as filter for search results.
*
* @param metadataList
* @param modifier
* @param style
* @param showBackground
* @param pillButtonTokens
* @param pillBarTokens
*/
@Composable
fun PillBar(
metadataList: MutableList<PillMetaData>,
modifier: Modifier = Modifier,
style: FluentStyle = FluentStyle.Neutral,
showBackground: Boolean = false,
pillButtonTokens: PillButtonTokens? = null,
pillBarTokens: PillBarTokens? = null
) {
if (metadataList.size == 0)
return
val themeID =
FluentTheme.themeID //Adding This only for recomposition in case of Token Updates. Unused otherwise.
val token = pillBarTokens
?: FluentTheme.controlTokens.tokens[ControlTokens.ControlType.PillBarControlType] as PillBarTokens
val pillBarInfo = PillBarInfo(style)
val lazyListState = rememberLazyListState()
val scope = rememberCoroutineScope()
LazyRow(
modifier = modifier
.fillMaxWidth()
.background(if (showBackground) token.backgroundBrush(pillBarInfo) else SolidColor(Color.Unspecified))
.focusable(enabled = false),
contentPadding = PaddingValues(horizontal = 16.dp),
horizontalArrangement = Arrangement.spacedBy(8.dp, Alignment.CenterHorizontally),
state = lazyListState
) {
metadataList.forEachIndexed { index, pillMetadata ->
item(index.toString()) {
PillButton(
pillMetadata,
modifier = Modifier.onFocusEvent { focusState ->
if (focusState.isFocused) {
scope.launch {
lazyListState.animateScrollToItem(
max(0, index - 2)
)
}
}
}, style = style, pillButtonTokens = pillButtonTokens
)
}
}
}
}