diff options
| author | 2025-01-27 20:57:30 +0000 | |
|---|---|---|
| committer | 2025-01-27 13:01:48 -0800 | |
| commit | 1a325acbc1ee765ec4426f6faed9acfb0f9fe0d8 (patch) | |
| tree | 0069402b0e341fc6b46a813d388b08e53a5ef3d1 | |
| parent | 16357e24e53a241e179049e787feb137d809c157 (diff) | |
Create ChipContent Composable
Moves ChipContent to its own file. This makes it easier to move
the NeverDecreaseWidth modifier into a private function.
Test: N/A
Bug: b/390530157
Flag: com.android.systemui.status_bar_chips_modernization
Change-Id: I934b0a32c625f2b15bee29e095e1685ae4b82e29
3 files changed, 132 insertions, 127 deletions
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/chips/ui/compose/ChipContent.kt b/packages/SystemUI/src/com/android/systemui/statusbar/chips/ui/compose/ChipContent.kt new file mode 100644 index 000000000000..13f4e51f2ba2 --- /dev/null +++ b/packages/SystemUI/src/com/android/systemui/statusbar/chips/ui/compose/ChipContent.kt @@ -0,0 +1,131 @@ +/* + * Copyright (C) 2025 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.systemui.statusbar.chips.ui.compose + +import androidx.compose.foundation.layout.padding +import androidx.compose.material3.MaterialTheme +import androidx.compose.material3.Text +import androidx.compose.runtime.Composable +import androidx.compose.ui.Modifier +import androidx.compose.ui.graphics.Color +import androidx.compose.ui.layout.Measurable +import androidx.compose.ui.layout.MeasureResult +import androidx.compose.ui.layout.MeasureScope +import androidx.compose.ui.node.LayoutModifierNode +import androidx.compose.ui.node.ModifierNodeElement +import androidx.compose.ui.platform.LocalContext +import androidx.compose.ui.res.dimensionResource +import androidx.compose.ui.unit.Constraints +import androidx.compose.ui.unit.constrain +import androidx.compose.ui.unit.dp +import com.android.systemui.res.R +import com.android.systemui.statusbar.chips.ui.model.OngoingActivityChipModel +import com.android.systemui.statusbar.chips.ui.viewmodel.rememberChronometerState + +@Composable +fun ChipContent(viewModel: OngoingActivityChipModel.Shown, modifier: Modifier = Modifier) { + val context = LocalContext.current + val isTextOnly = viewModel.icon == null + val hasEmbeddedIcon = + viewModel.icon is OngoingActivityChipModel.ChipIcon.StatusBarView || + viewModel.icon is OngoingActivityChipModel.ChipIcon.StatusBarNotificationIcon + val startPadding = + if (isTextOnly || hasEmbeddedIcon) { + 0.dp + } else { + dimensionResource(id = R.dimen.ongoing_activity_chip_icon_text_padding) + } + val endPadding = + if (hasEmbeddedIcon) { + dimensionResource( + id = R.dimen.ongoing_activity_chip_text_end_padding_for_embedded_padding_icon + ) + } else { + 0.dp + } + val textStyle = MaterialTheme.typography.labelLarge + val textColor = Color(viewModel.colors.text(context)) + when (viewModel) { + is OngoingActivityChipModel.Shown.Timer -> { + val timerState = rememberChronometerState(startTimeMillis = viewModel.startTimeMs) + Text( + text = timerState.currentTimeText, + style = textStyle, + color = textColor, + modifier = + modifier.padding(start = startPadding, end = endPadding).neverDecreaseWidth(), + ) + } + + is OngoingActivityChipModel.Shown.Countdown -> { + ChipText( + text = viewModel.secondsUntilStarted.toString(), + style = textStyle, + color = textColor, + modifier = + modifier.padding(start = startPadding, end = endPadding).neverDecreaseWidth(), + backgroundColor = Color(viewModel.colors.background(context).defaultColor), + ) + } + + is OngoingActivityChipModel.Shown.Text -> { + ChipText( + text = viewModel.text, + style = textStyle, + color = textColor, + modifier = modifier.padding(start = startPadding, end = endPadding), + backgroundColor = Color(viewModel.colors.background(context).defaultColor), + ) + } + + is OngoingActivityChipModel.Shown.ShortTimeDelta -> { + // TODO(b/372657935): Implement ShortTimeDelta content in compose. + } + } +} + +/** A modifier that ensures the width of the content only increases and never decreases. */ +private fun Modifier.neverDecreaseWidth(): Modifier { + return this.then(neverDecreaseWidthElement) +} + +private data object neverDecreaseWidthElement : ModifierNodeElement<NeverDecreaseWidthNode>() { + override fun create(): NeverDecreaseWidthNode { + return NeverDecreaseWidthNode() + } + + override fun update(node: NeverDecreaseWidthNode) { + error("This should never be called") + } +} + +private class NeverDecreaseWidthNode : Modifier.Node(), LayoutModifierNode { + private var minWidth = 0 + + override fun MeasureScope.measure( + measurable: Measurable, + constraints: Constraints, + ): MeasureResult { + val placeable = measurable.measure(Constraints(minWidth = minWidth).constrain(constraints)) + val width = placeable.width + val height = placeable.height + + minWidth = maxOf(minWidth, width) + + return layout(width, height) { placeable.place(0, 0) } + } +} diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/chips/ui/compose/OngoingActivityChip.kt b/packages/SystemUI/src/com/android/systemui/statusbar/chips/ui/compose/OngoingActivityChip.kt index 279792ef7536..647f3bd469f1 100644 --- a/packages/SystemUI/src/com/android/systemui/statusbar/chips/ui/compose/OngoingActivityChip.kt +++ b/packages/SystemUI/src/com/android/systemui/statusbar/chips/ui/compose/OngoingActivityChip.kt @@ -29,8 +29,6 @@ import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.size import androidx.compose.foundation.layout.widthIn import androidx.compose.foundation.shape.RoundedCornerShape -import androidx.compose.material3.MaterialTheme -import androidx.compose.material3.Text import androidx.compose.runtime.Composable import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier @@ -45,10 +43,8 @@ import com.android.compose.animation.Expandable import com.android.systemui.animation.Expandable import com.android.systemui.common.ui.compose.Icon import com.android.systemui.res.R -import com.android.systemui.statusbar.chips.ui.compose.modifiers.neverDecreaseWidth import com.android.systemui.statusbar.chips.ui.model.ColorsModel import com.android.systemui.statusbar.chips.ui.model.OngoingActivityChipModel -import com.android.systemui.statusbar.chips.ui.viewmodel.rememberChronometerState @Composable fun OngoingActivityChip(model: OngoingActivityChipModel.Shown, modifier: Modifier = Modifier) { @@ -123,32 +119,8 @@ private fun ChipBody( model.icon?.let { ChipIcon(viewModel = it, colors = model.colors) } val isIconOnly = model is OngoingActivityChipModel.Shown.IconOnly - val isTextOnly = model.icon == null if (!isIconOnly) { - ChipContent( - viewModel = model, - modifier = - Modifier.padding( - start = - if (isTextOnly || hasEmbeddedIcon) { - 0.dp - } else { - dimensionResource( - id = R.dimen.ongoing_activity_chip_icon_text_padding - ) - }, - end = - if (hasEmbeddedIcon) { - dimensionResource( - id = - R.dimen - .ongoing_activity_chip_text_end_padding_for_embedded_padding_icon - ) - } else { - 0.dp - }, - ), - ) + ChipContent(viewModel = model) } } } @@ -196,46 +168,6 @@ private fun ChipIcon( } @Composable -private fun ChipContent(viewModel: OngoingActivityChipModel.Shown, modifier: Modifier = Modifier) { - val context = LocalContext.current - when (viewModel) { - is OngoingActivityChipModel.Shown.Timer -> { - val timerState = rememberChronometerState(startTimeMillis = viewModel.startTimeMs) - Text( - text = timerState.currentTimeText, - style = MaterialTheme.typography.labelLarge, - color = Color(viewModel.colors.text(context)), - modifier = modifier.neverDecreaseWidth(), - ) - } - - is OngoingActivityChipModel.Shown.Countdown -> { - ChipText( - text = viewModel.secondsUntilStarted.toString(), - color = Color(viewModel.colors.text(context)), - style = MaterialTheme.typography.labelLarge, - modifier = modifier.neverDecreaseWidth(), - backgroundColor = Color(viewModel.colors.background(context).defaultColor), - ) - } - - is OngoingActivityChipModel.Shown.Text -> { - ChipText( - text = viewModel.text, - color = Color(viewModel.colors.text(context)), - style = MaterialTheme.typography.labelLarge, - modifier = modifier, - backgroundColor = Color(viewModel.colors.background(context).defaultColor), - ) - } - - is OngoingActivityChipModel.Shown.ShortTimeDelta -> { - // TODO(b/372657935): Implement ShortTimeDelta content in compose. - } - } -} - -@Composable private fun ExpandableChip( color: () -> Color, shape: Shape, diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/chips/ui/compose/modifiers/NeverDecreaseWidth.kt b/packages/SystemUI/src/com/android/systemui/statusbar/chips/ui/compose/modifiers/NeverDecreaseWidth.kt deleted file mode 100644 index 505a5fcb18b4..000000000000 --- a/packages/SystemUI/src/com/android/systemui/statusbar/chips/ui/compose/modifiers/NeverDecreaseWidth.kt +++ /dev/null @@ -1,58 +0,0 @@ -/* - * Copyright (C) 2025 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.android.systemui.statusbar.chips.ui.compose.modifiers - -import androidx.compose.ui.Modifier -import androidx.compose.ui.layout.Measurable -import androidx.compose.ui.layout.MeasureResult -import androidx.compose.ui.layout.MeasureScope -import androidx.compose.ui.node.LayoutModifierNode -import androidx.compose.ui.node.ModifierNodeElement -import androidx.compose.ui.unit.Constraints -import androidx.compose.ui.unit.constrain - -/** A modifier that ensures the width of the content only increases and never decreases. */ -fun Modifier.neverDecreaseWidth(): Modifier { - return this.then(neverDecreaseWidthElement) -} - -private data object neverDecreaseWidthElement : ModifierNodeElement<NeverDecreaseWidthNode>() { - override fun create(): NeverDecreaseWidthNode { - return NeverDecreaseWidthNode() - } - - override fun update(node: NeverDecreaseWidthNode) { - error("This should never be called") - } -} - -private class NeverDecreaseWidthNode : Modifier.Node(), LayoutModifierNode { - private var minWidth = 0 - - override fun MeasureScope.measure( - measurable: Measurable, - constraints: Constraints, - ): MeasureResult { - val placeable = measurable.measure(Constraints(minWidth = minWidth).constrain(constraints)) - val width = placeable.width - val height = placeable.height - - minWidth = maxOf(minWidth, width) - - return layout(width, height) { placeable.place(0, 0) } - } -} |