Skip to content

Commit a68b370

Browse files
PM-18083 Ensure segmented buttons on generator fill entire width evenly. (#4713)
1 parent 60a6b2a commit a68b370

File tree

2 files changed

+42
-8
lines changed

2 files changed

+42
-8
lines changed

app/src/main/java/com/x8bit/bitwarden/ui/platform/components/segment/BitwardenSegmentedButton.kt

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,19 @@ import androidx.compose.material3.SingleChoiceSegmentedButtonRow
1717
import androidx.compose.material3.SingleChoiceSegmentedButtonRowScope
1818
import androidx.compose.material3.Text
1919
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
2024
import androidx.compose.ui.Modifier
2125
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
2230
import androidx.compose.ui.unit.dp
2331
import com.x8bit.bitwarden.ui.platform.base.util.nullableTestTag
32+
import com.x8bit.bitwarden.ui.platform.base.util.toDp
2433
import com.x8bit.bitwarden.ui.platform.components.segment.color.bitwardenSegmentedButtonColors
2534
import com.x8bit.bitwarden.ui.platform.theme.BitwardenTheme
2635
import kotlinx.collections.immutable.ImmutableList
@@ -31,6 +40,9 @@ import kotlinx.collections.immutable.ImmutableList
3140
* @param options List of options to display.
3241
* @param modifier Modifier.
3342
* @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].
3446
*/
3547
@Composable
3648
fun BitwardenSegmentedButton(
@@ -39,10 +51,12 @@ fun BitwardenSegmentedButton(
3951
windowInsets: WindowInsets = WindowInsets.displayCutout
4052
.union(WindowInsets.navigationBars)
4153
.only(WindowInsetsSides.Horizontal),
54+
density: Density = LocalDensity.current,
4255
optionContent: @Composable SingleChoiceSegmentedButtonRowScope.(
4356
Int,
57+
Dp,
4458
SegmentedButtonState,
45-
) -> Unit = { _, optionState ->
59+
) -> Unit = { _, _, optionState ->
4660
this.SegmentedButtonOptionContent(
4761
option = optionState,
4862
)
@@ -55,18 +69,24 @@ fun BitwardenSegmentedButton(
5569
.padding(top = 4.dp, bottom = 8.dp, start = 16.dp, end = 16.dp)
5670
.windowInsetsPadding(insets = windowInsets),
5771
) {
72+
var weightedWidth by remember {
73+
mutableStateOf(0.dp)
74+
}
5875
SingleChoiceSegmentedButtonRow(
5976
modifier = Modifier
6077
.fillMaxWidth()
6178
.background(
6279
color = BitwardenTheme.colorScheme.background.primary,
6380
shape = BitwardenTheme.shapes.segmentedControl,
6481
)
82+
.onGloballyPositioned {
83+
weightedWidth = (it.size.width / options.size).toDp(density)
84+
}
6585
.padding(horizontal = 4.dp),
6686
space = 0.dp,
6787
) {
6888
options.forEachIndexed { index, option ->
69-
optionContent(index, option)
89+
optionContent(index, weightedWidth, option)
7090
}
7191
}
7292
}
@@ -78,6 +98,7 @@ fun BitwardenSegmentedButton(
7898
@Composable
7999
fun SingleChoiceSegmentedButtonRowScope.SegmentedButtonOptionContent(
80100
option: SegmentedButtonState,
101+
modifier: Modifier = Modifier,
81102
) {
82103
SegmentedButton(
83104
enabled = option.isEnabled,
@@ -95,7 +116,7 @@ fun SingleChoiceSegmentedButtonRowScope.SegmentedButtonOptionContent(
95116
icon = {
96117
// No icon required
97118
},
98-
modifier = Modifier.nullableTestTag(tag = option.testTag),
119+
modifier = modifier.nullableTestTag(tag = option.testTag),
99120
)
100121
}
101122

app/src/main/java/com/x8bit/bitwarden/ui/tools/feature/generator/GeneratorScreen.kt

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import androidx.compose.foundation.layout.fillMaxWidth
1010
import androidx.compose.foundation.layout.height
1111
import androidx.compose.foundation.layout.imePadding
1212
import androidx.compose.foundation.layout.navigationBarsPadding
13+
import androidx.compose.foundation.layout.width
1314
import androidx.compose.foundation.lazy.LazyColumn
1415
import androidx.compose.foundation.lazy.LazyListState
1516
import androidx.compose.foundation.lazy.rememberLazyListState
@@ -639,7 +640,7 @@ private fun CoachMarkScope<ExploreGeneratorCoachMark>.MainStateOptionsItem(
639640
modifier = modifier
640641
.fillMaxWidth()
641642
.testTag(tag = "GeneratorTypePicker"),
642-
) { index, option ->
643+
) { index, weightedWidth, option ->
643644
when (index) {
644645
0 -> {
645646
CoachMarkHighlight(
@@ -658,7 +659,10 @@ private fun CoachMarkScope<ExploreGeneratorCoachMark>.MainStateOptionsItem(
658659
shape = CoachMarkHighlightShape.RoundedRectangle(radius = 50f),
659660
leftAction = null,
660661
) {
661-
SegmentedButtonOptionContent(option = option)
662+
SegmentedButtonOptionContent(
663+
option = option,
664+
modifier = Modifier.width(weightedWidth),
665+
)
662666
}
663667
}
664668

@@ -684,7 +688,10 @@ private fun CoachMarkScope<ExploreGeneratorCoachMark>.MainStateOptionsItem(
684688
},
685689
shape = CoachMarkHighlightShape.RoundedRectangle(radius = 50f),
686690
) {
687-
SegmentedButtonOptionContent(option = option)
691+
SegmentedButtonOptionContent(
692+
option = option,
693+
modifier = Modifier.width(weightedWidth),
694+
)
688695
}
689696
}
690697

@@ -710,12 +717,18 @@ private fun CoachMarkScope<ExploreGeneratorCoachMark>.MainStateOptionsItem(
710717
},
711718
shape = CoachMarkHighlightShape.RoundedRectangle(radius = 50f),
712719
) {
713-
SegmentedButtonOptionContent(option = option)
720+
SegmentedButtonOptionContent(
721+
option = option,
722+
modifier = Modifier.width(weightedWidth),
723+
)
714724
}
715725
}
716726

717727
else -> {
718-
SegmentedButtonOptionContent(option = option)
728+
SegmentedButtonOptionContent(
729+
option = option,
730+
modifier = Modifier.width(weightedWidth),
731+
)
719732
}
720733
}
721734
}

0 commit comments

Comments
 (0)