Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve StandardToggleChip to handle largest font scale #1286

Merged
merged 1 commit into from
May 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion base-ui/api/current.api
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ package com.google.android.horologist.base.ui.components {
}

public final class StandardToggleChipKt {
method @androidx.compose.runtime.Composable @com.google.android.horologist.annotations.ExperimentalHorologistApi public static void StandardToggleChip(boolean checked, kotlin.jvm.functions.Function1<? super java.lang.Boolean,kotlin.Unit> onCheckedChange, String label, com.google.android.horologist.base.ui.components.StandardToggleChipToggleControl toggleControl, optional androidx.compose.ui.Modifier modifier, optional androidx.compose.ui.graphics.vector.ImageVector? icon, optional String? secondaryLabel, optional androidx.wear.compose.material.ToggleChipColors colors, optional boolean enabled, optional androidx.compose.foundation.interaction.MutableInteractionSource interactionSource);
method @androidx.compose.runtime.Composable @com.google.android.horologist.annotations.ExperimentalHorologistApi public static void StandardToggleChip(boolean checked, kotlin.jvm.functions.Function1<? super java.lang.Boolean,kotlin.Unit> onCheckedChanged, String label, com.google.android.horologist.base.ui.components.StandardToggleChipToggleControl toggleControl, optional androidx.compose.ui.Modifier modifier, optional androidx.compose.ui.graphics.vector.ImageVector? icon, optional String? secondaryLabel, optional androidx.wear.compose.material.ToggleChipColors colors, optional boolean enabled, optional androidx.compose.foundation.interaction.MutableInteractionSource interactionSource);
}

public enum StandardToggleChipToggleControl {
Expand Down Expand Up @@ -108,6 +108,10 @@ package com.google.android.horologist.base.ui.util {
property public static final String? DECORATIVE_ELEMENT_CONTENT_DESCRIPTION;
}

public final class AdjustChipHeightToFontScaleKt {
method public static androidx.compose.ui.Modifier adjustChipHeightToFontScale(androidx.compose.ui.Modifier, float fontScale, optional float padding);
}

public final class RememberVectorPainterKt {
method @androidx.compose.runtime.Composable @com.google.android.horologist.annotations.ExperimentalHorologistApi public static androidx.compose.ui.graphics.vector.VectorPainter rememberVectorPainter(androidx.compose.ui.graphics.vector.ImageVector image, long tintColor, optional int tintBlendMode);
}
Expand Down
1 change: 1 addition & 0 deletions base-ui/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ dependencies {

debugImplementation(libs.compose.ui.test.manifest)

testImplementation(libs.accompanist.testharness)
testImplementation(libs.junit)
testImplementation(libs.androidx.test.ext.ktx)
testImplementation(libs.kotlinx.coroutines.test)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,24 @@ import androidx.compose.ui.tooling.preview.Preview
fun StandardToggleChipOverflowPreviewWithLongText() {
StandardToggleChip(
checked = true,
onCheckedChange = { },
onCheckedChanged = { },
label = "Primary label very very very very very very very very very very very very very very very very very long text",
toggleControl = StandardToggleChipToggleControl.Switch
)
}

@Preview(
name = "With long text",
backgroundColor = 0xff000000,
showBackground = true,
fontScale = largestFontScale,
group = "Largest font scale"
)
@Composable
fun StandardToggleChipOverflowPreviewWithLongTextAndLargestFontScale() {
StandardToggleChip(
checked = true,
onCheckedChanged = { },
label = "Primary label very very very very very very very very very very very very very very very very very long text",
toggleControl = StandardToggleChipToggleControl.Switch
)
Expand All @@ -45,7 +62,25 @@ fun StandardToggleChipOverflowPreviewWithLongText() {
fun StandardToggleChipOverflowPreviewWithIconAndLongText() {
StandardToggleChip(
checked = true,
onCheckedChange = { },
onCheckedChanged = { },
label = "Primary label very very very very very very very very very very very very very very very very very long text",
toggleControl = StandardToggleChipToggleControl.Switch,
icon = Icons.Default.Image
)
}

@Preview(
name = "With icon and long text",
backgroundColor = 0xff000000,
showBackground = true,
fontScale = largestFontScale,
group = "Largest font scale"
)
@Composable
fun StandardToggleChipOverflowPreviewWithIconAndLongTextAndLargestFontScale() {
StandardToggleChip(
checked = true,
onCheckedChanged = { },
label = "Primary label very very very very very very very very very very very very very very very very very long text",
toggleControl = StandardToggleChipToggleControl.Switch,
icon = Icons.Default.Image
Expand All @@ -61,7 +96,25 @@ fun StandardToggleChipOverflowPreviewWithIconAndLongText() {
fun StandardToggleChipOverflowPreviewWithSecondaryLabelAndLongText() {
StandardToggleChip(
checked = true,
onCheckedChange = { },
onCheckedChanged = { },
label = "Primary label very very very very very very very very long text",
secondaryLabel = "Secondary label very very very very very very very very very long text",
toggleControl = StandardToggleChipToggleControl.Switch
)
}

@Preview(
name = "With secondary label and long text",
backgroundColor = 0xff000000,
showBackground = true,
fontScale = largestFontScale,
group = "Largest font scale"
)
@Composable
fun StandardToggleChipOverflowPreviewWithSecondaryLabelAndLongTextAndLargestFontScale() {
StandardToggleChip(
checked = true,
onCheckedChanged = { },
label = "Primary label very very very very very very very very long text",
secondaryLabel = "Secondary label very very very very very very very very very long text",
toggleControl = StandardToggleChipToggleControl.Switch
Expand All @@ -77,10 +130,31 @@ fun StandardToggleChipOverflowPreviewWithSecondaryLabelAndLongText() {
fun StandardToggleChipOverflowPreviewWithIconAndSecondaryLabelAndLongText() {
StandardToggleChip(
checked = true,
onCheckedChange = { },
onCheckedChanged = { },
label = "Primary label very very very very very very very very long text",
secondaryLabel = "Secondary label very very very very very very very very very long text",
toggleControl = StandardToggleChipToggleControl.Switch,
icon = Icons.Default.Image
)
}

@Preview(
name = "With icon, secondary label and long text",
backgroundColor = 0xff000000,
showBackground = true,
fontScale = largestFontScale,
group = "Largest font scale"
)
@Composable
fun StandardToggleChipOverflowPreviewWithIconAndSecondaryLabelAndLongTextAndLargestFontScale() {
StandardToggleChip(
checked = true,
onCheckedChanged = { },
label = "Primary label very very very very very very very very long text",
secondaryLabel = "Secondary label very very very very very very very very very long text",
toggleControl = StandardToggleChipToggleControl.Switch,
icon = Icons.Default.Image
)
}

private const val largestFontScale = 1.18f
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import androidx.compose.ui.tooling.preview.Preview
fun StandardToggleChipSwitchPreview() {
StandardToggleChip(
checked = true,
onCheckedChange = { },
onCheckedChanged = { },
label = "Primary label",
toggleControl = StandardToggleChipToggleControl.Switch
)
Expand All @@ -43,7 +43,7 @@ fun StandardToggleChipSwitchPreview() {
fun StandardToggleChipRadioPreview() {
StandardToggleChip(
checked = true,
onCheckedChange = { },
onCheckedChanged = { },
label = "Primary label",
toggleControl = StandardToggleChipToggleControl.Radio
)
Expand All @@ -57,12 +57,26 @@ fun StandardToggleChipRadioPreview() {
fun StandardToggleChipCheckboxPreview() {
StandardToggleChip(
checked = true,
onCheckedChange = { },
onCheckedChanged = { },
label = "Primary label",
toggleControl = StandardToggleChipToggleControl.Checkbox
)
}

@Preview(
backgroundColor = 0xff000000,
showBackground = true
)
@Composable
fun StandardToggleChipUncheckedPreview() {
StandardToggleChip(
checked = false,
onCheckedChanged = { },
label = "Primary label",
toggleControl = StandardToggleChipToggleControl.Switch
)
}

@Preview(
name = "With secondary label",
backgroundColor = 0xff000000,
Expand All @@ -72,7 +86,7 @@ fun StandardToggleChipCheckboxPreview() {
fun StandardToggleChipWithSecondaryLabel() {
StandardToggleChip(
checked = true,
onCheckedChange = { },
onCheckedChanged = { },
label = "Primary label",
toggleControl = StandardToggleChipToggleControl.Switch,
secondaryLabel = "Secondary label"
Expand All @@ -88,7 +102,7 @@ fun StandardToggleChipWithSecondaryLabel() {
fun StandardToggleChipPreviewWithIcon() {
StandardToggleChip(
checked = true,
onCheckedChange = { },
onCheckedChanged = { },
label = "Primary label",
toggleControl = StandardToggleChipToggleControl.Switch,
icon = Icons.Default.Image
Expand All @@ -104,7 +118,7 @@ fun StandardToggleChipPreviewWithIcon() {
fun StandardToggleChipPreviewWithSecondaryLabelAndIcon() {
StandardToggleChip(
checked = true,
onCheckedChange = { },
onCheckedChanged = { },
label = "Primary label",
toggleControl = StandardToggleChipToggleControl.Switch,
secondaryLabel = "Secondary label"
Expand All @@ -120,7 +134,22 @@ fun StandardToggleChipPreviewWithSecondaryLabelAndIcon() {
fun StandardToggleChipPreviewDisabled() {
StandardToggleChip(
checked = true,
onCheckedChange = { },
onCheckedChanged = { },
label = "Primary label",
toggleControl = StandardToggleChipToggleControl.Switch,
enabled = false
)
}

@Preview(
backgroundColor = 0xff000000,
showBackground = true
)
@Composable
fun StandardToggleChipUncheckedAndDisabledPreview() {
StandardToggleChip(
checked = false,
onCheckedChanged = { },
label = "Primary label",
toggleControl = StandardToggleChipToggleControl.Switch,
enabled = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.platform.LocalConfiguration
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.text.style.TextOverflow
Expand All @@ -40,6 +41,7 @@ import androidx.wear.compose.material.ToggleChipDefaults
import com.google.android.horologist.annotations.ExperimentalHorologistApi
import com.google.android.horologist.base.ui.R
import com.google.android.horologist.base.ui.util.DECORATIVE_ELEMENT_CONTENT_DESCRIPTION
import com.google.android.horologist.base.ui.util.adjustChipHeightToFontScale

/**
* This composable fulfils the redlines of the following components:
Expand All @@ -49,7 +51,7 @@ import com.google.android.horologist.base.ui.util.DECORATIVE_ELEMENT_CONTENT_DES
@Composable
public fun StandardToggleChip(
checked: Boolean,
onCheckedChange: (Boolean) -> Unit,
onCheckedChanged: (Boolean) -> Unit,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why past tense? Shouldn't we match the ToggleChip?

Copy link
Member Author

@luizgrp luizgrp May 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My interpretation is that the callback is called after the event has performed. Can revert in the future once figure out that is not the case!

label: String,
toggleControl: StandardToggleChipToggleControl,
modifier: Modifier = Modifier,
Expand All @@ -60,14 +62,13 @@ public fun StandardToggleChip(
interactionSource: MutableInteractionSource = remember { MutableInteractionSource() }
) {
val hasSecondaryLabel = secondaryLabel != null
val hasIcon = icon != null

val labelParam: (@Composable RowScope.() -> Unit) =
{
Text(
text = label,
modifier = Modifier.fillMaxWidth(),
textAlign = if (hasSecondaryLabel || hasIcon) TextAlign.Left else TextAlign.Center,
textAlign = TextAlign.Left,
overflow = TextOverflow.Ellipsis,
maxLines = if (hasSecondaryLabel) 1 else 2
)
Expand Down Expand Up @@ -118,10 +119,12 @@ public fun StandardToggleChip(

ToggleChip(
checked = checked,
onCheckedChange = onCheckedChange,
onCheckedChange = onCheckedChanged,
label = labelParam,
toggleControl = toggleControlParam,
modifier = modifier,
modifier = modifier
.adjustChipHeightToFontScale(LocalConfiguration.current.fontScale)
.fillMaxWidth(),
appIcon = iconParam,
secondaryLabel = secondaryLabelParam,
colors = colors,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright 2023 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
*
* https://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.google.android.horologist.base.ui.util

import androidx.compose.foundation.layout.height
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp

/** Adjusts height of the chip as per the font scale. */
public fun Modifier.adjustChipHeightToFontScale(fontScale: Float, padding: Dp = 0.dp): Modifier =
if (fontScale > 1.06) {
this.then(Modifier.height(60.dp + padding))
} else {
this
}
Loading