@@ -17,10 +17,19 @@ import androidx.compose.material3.SingleChoiceSegmentedButtonRow
17
17
import androidx.compose.material3.SingleChoiceSegmentedButtonRowScope
18
18
import androidx.compose.material3.Text
19
19
import androidx.compose.runtime.Composable
20
+ import androidx.compose.runtime.getValue
21
+ import androidx.compose.runtime.mutableStateOf
22
+ import androidx.compose.runtime.remember
23
+ import androidx.compose.runtime.setValue
20
24
import androidx.compose.ui.Modifier
21
25
import androidx.compose.ui.graphics.Color
26
+ import androidx.compose.ui.layout.onGloballyPositioned
27
+ import androidx.compose.ui.platform.LocalDensity
28
+ import androidx.compose.ui.unit.Density
29
+ import androidx.compose.ui.unit.Dp
22
30
import androidx.compose.ui.unit.dp
23
31
import com.x8bit.bitwarden.ui.platform.base.util.nullableTestTag
32
+ import com.x8bit.bitwarden.ui.platform.base.util.toDp
24
33
import com.x8bit.bitwarden.ui.platform.components.segment.color.bitwardenSegmentedButtonColors
25
34
import com.x8bit.bitwarden.ui.platform.theme.BitwardenTheme
26
35
import kotlinx.collections.immutable.ImmutableList
@@ -31,6 +40,9 @@ import kotlinx.collections.immutable.ImmutableList
31
40
* @param options List of options to display.
32
41
* @param modifier Modifier.
33
42
* @param windowInsets The insets to be applied to this composable.
43
+ * @param optionContent For outer context the content lambda passes back the index of the option,
44
+ * the weighted width (in [Dp]) per option (total width / # number of options), and the
45
+ * corresponding [SegmentedButtonState].
34
46
*/
35
47
@Composable
36
48
fun BitwardenSegmentedButton (
@@ -39,10 +51,12 @@ fun BitwardenSegmentedButton(
39
51
windowInsets : WindowInsets = WindowInsets .displayCutout
40
52
.union(WindowInsets .navigationBars)
41
53
.only(WindowInsetsSides .Horizontal ),
54
+ density : Density = LocalDensity .current,
42
55
optionContent : @Composable SingleChoiceSegmentedButtonRowScope .(
43
56
Int ,
57
+ Dp ,
44
58
SegmentedButtonState ,
45
- ) -> Unit = { _, optionState ->
59
+ ) -> Unit = { _, _, optionState ->
46
60
this.SegmentedButtonOptionContent (
47
61
option = optionState,
48
62
)
@@ -55,18 +69,24 @@ fun BitwardenSegmentedButton(
55
69
.padding(top = 4 .dp, bottom = 8 .dp, start = 16 .dp, end = 16 .dp)
56
70
.windowInsetsPadding(insets = windowInsets),
57
71
) {
72
+ var weightedWidth by remember {
73
+ mutableStateOf(0 .dp)
74
+ }
58
75
SingleChoiceSegmentedButtonRow (
59
76
modifier = Modifier
60
77
.fillMaxWidth()
61
78
.background(
62
79
color = BitwardenTheme .colorScheme.background.primary,
63
80
shape = BitwardenTheme .shapes.segmentedControl,
64
81
)
82
+ .onGloballyPositioned {
83
+ weightedWidth = (it.size.width / options.size).toDp(density)
84
+ }
65
85
.padding(horizontal = 4 .dp),
66
86
space = 0 .dp,
67
87
) {
68
88
options.forEachIndexed { index, option ->
69
- optionContent(index, option)
89
+ optionContent(index, weightedWidth, option)
70
90
}
71
91
}
72
92
}
@@ -78,6 +98,7 @@ fun BitwardenSegmentedButton(
78
98
@Composable
79
99
fun SingleChoiceSegmentedButtonRowScope.SegmentedButtonOptionContent (
80
100
option : SegmentedButtonState ,
101
+ modifier : Modifier = Modifier ,
81
102
) {
82
103
SegmentedButton (
83
104
enabled = option.isEnabled,
@@ -95,7 +116,7 @@ fun SingleChoiceSegmentedButtonRowScope.SegmentedButtonOptionContent(
95
116
icon = {
96
117
// No icon required
97
118
},
98
- modifier = Modifier .nullableTestTag(tag = option.testTag),
119
+ modifier = modifier .nullableTestTag(tag = option.testTag),
99
120
)
100
121
}
101
122
0 commit comments