forked from microsoft/fluentui-android
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PillSwitch.kt
100 lines (94 loc) · 4.04 KB
/
PillSwitch.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
package com.microsoft.fluentui.tokenized.segmentedcontrols
import androidx.compose.foundation.background
import androidx.compose.foundation.focusable
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.wrapContentWidth
import androidx.compose.foundation.lazy.LazyRow
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.runtime.Composable
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.focus.onFocusEvent
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
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.controlTokens.PillButtonTokens
import com.microsoft.fluentui.theme.token.controlTokens.PillSwitchInfo
import com.microsoft.fluentui.theme.token.controlTokens.PillSwitchTokens
import kotlinx.coroutines.launch
import kotlin.math.max
/**
* API to create PillSwitches. The PillSwitch control is a linear set of two or more PillButton, each of which functions as a mutually exclusive button.
* PillSwitches are used to toggling between two views.
*
* @param metadataList List of [PillMetaData] which contains information for all buttons in Tab
* @param modifier Optional Modifier for Tabs
* @param selectedIndex Index of the PillButton to be selected. Default: [0]
* @param style Style of Tabs and inherent PillButtons. Default: [FluentStyle.Neutral]
* @param pillButtonTokens Tokens to provide appearance value to PillButton
* @param pillSwitchTokens Tokens to provide appearance value to PillSwitch
*/
@Composable
fun PillSwitch(
metadataList: MutableList<PillMetaData>,
modifier: Modifier = Modifier,
selectedIndex: Int = 0,
style: FluentStyle = FluentStyle.Neutral,
pillButtonTokens: PillButtonTokens? = null,
pillSwitchTokens: PillSwitchTokens? = null
) {
if (metadataList.size == 0)
return
val themeID =
FluentTheme.themeID //Adding This only for recomposition in case of Token Updates. Unused otherwise.
val token = pillSwitchTokens
?: FluentTheme.controlTokens.tokens[ControlTokens.ControlType.PillSwitchControlType] as PillSwitchTokens
val pillSwitchInfo = PillSwitchInfo(style)
val shape = RoundedCornerShape(50)
val lazyListState = rememberLazyListState()
val scope = rememberCoroutineScope()
Row(
modifier = Modifier
.wrapContentWidth()
.padding(horizontal = 16.dp)
.background(Color.Transparent)
.focusable(false)
) {
LazyRow(
modifier = modifier
.wrapContentWidth()
.padding(horizontal = 16.dp)
.focusable(enabled = false)
.clip(shape)
.background(token.backgroundBrush(pillSwitchInfo), shape),
state = lazyListState
) {
metadataList.forEachIndexed { index, pillMetadata ->
item(index.toString()) {
pillMetadata.selected = (selectedIndex == index)
PillButton(
pillMetadata,
modifier = Modifier
.wrapContentWidth()
.onFocusEvent { focusState ->
if (focusState.isFocused) {
scope.launch {
lazyListState.animateScrollToItem(
max(0, index - 2)
)
}
}
},
style = style,
pillButtonTokens = pillButtonTokens
)
}
}
}
}
}