forked from google/horologist
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement CompactChip Component google#1396
- Loading branch information
1 parent
87fc4cc
commit 3029cdd
Showing
4 changed files
with
468 additions
and
0 deletions.
There are no files selected for viewing
50 changes: 50 additions & 0 deletions
50
...erial/src/debug/java/com/google/android/horologist/compose/material/CompactChipPreview.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* 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.compose.material | ||
|
||
import androidx.compose.material.icons.Icons | ||
import androidx.compose.material.icons.filled.Add | ||
import androidx.compose.runtime.Composable | ||
import com.google.android.horologist.compose.tools.WearPreview | ||
|
||
@WearPreview | ||
@Composable | ||
fun CompactChipPreview() { | ||
CompactChip( | ||
onClick = { }, | ||
label = "Primary label" | ||
) | ||
} | ||
|
||
@WearPreview | ||
@Composable | ||
fun CompactChipPreviewWithIcon() { | ||
CompactChip( | ||
onClick = { }, | ||
label = "Primary label", | ||
icon = Icons.Filled.Add | ||
) | ||
} | ||
|
||
@WearPreview | ||
@Composable | ||
fun CompactChipPreviewIconOnly() { | ||
CompactChip( | ||
onClick = { }, | ||
icon = Icons.Filled.Add | ||
) | ||
} |
156 changes: 156 additions & 0 deletions
156
compose-material/src/main/java/com/google/android/horologist/compose/material/CompactChip.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
/* | ||
* 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.compose.material | ||
|
||
import androidx.annotation.StringRes | ||
import androidx.compose.foundation.Image | ||
import androidx.compose.foundation.interaction.MutableInteractionSource | ||
import androidx.compose.foundation.layout.BoxScope | ||
import androidx.compose.foundation.layout.Row | ||
import androidx.compose.foundation.layout.RowScope | ||
import androidx.compose.foundation.layout.fillMaxWidth | ||
import androidx.compose.foundation.layout.size | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.graphics.painter.Painter | ||
import androidx.compose.ui.graphics.vector.ImageVector | ||
import androidx.compose.ui.layout.ContentScale | ||
import androidx.compose.ui.res.stringResource | ||
import androidx.compose.ui.text.style.TextAlign | ||
import androidx.compose.ui.text.style.TextOverflow | ||
import androidx.wear.compose.material.ChipBorder | ||
import androidx.wear.compose.material.ChipColors | ||
import androidx.wear.compose.material.ChipDefaults | ||
import androidx.wear.compose.material.CompactChip | ||
import androidx.wear.compose.material.LocalContentAlpha | ||
import androidx.wear.compose.material.Text | ||
import coil.compose.rememberAsyncImagePainter | ||
import com.google.android.horologist.annotations.ExperimentalHorologistApi | ||
import com.google.android.horologist.compose.material.util.DECORATIVE_ELEMENT_CONTENT_DESCRIPTION | ||
|
||
@ExperimentalHorologistApi | ||
@Composable | ||
public fun CompactChip( | ||
onClick: () -> Unit, | ||
modifier: Modifier = Modifier, | ||
label: String? = null, | ||
icon: Any? = null, | ||
iconRtlMode: IconRtlMode = IconRtlMode.Default, | ||
placeholder: Painter? = null, | ||
colors: ChipColors = ChipDefaults.primaryChipColors(), | ||
enabled: Boolean = true, | ||
interactionSource: MutableInteractionSource = remember { MutableInteractionSource() }, | ||
border: ChipBorder = ChipDefaults.chipBorder() | ||
) { | ||
val iconParam: (@Composable BoxScope.() -> Unit)? = | ||
icon?.let { | ||
{ | ||
Row { | ||
val iconModifier = Modifier | ||
.size(ChipDefaults.SmallIconSize) | ||
when (icon) { | ||
is ImageVector -> | ||
Icon( | ||
imageVector = icon, | ||
contentDescription = DECORATIVE_ELEMENT_CONTENT_DESCRIPTION, | ||
modifier = iconModifier, | ||
rtlMode = iconRtlMode | ||
) | ||
|
||
is Int -> | ||
Icon( | ||
id = icon, | ||
contentDescription = DECORATIVE_ELEMENT_CONTENT_DESCRIPTION, | ||
modifier = iconModifier | ||
) | ||
|
||
else -> | ||
Image( | ||
painter = rememberAsyncImagePainter( | ||
model = icon, | ||
placeholder = placeholder | ||
), | ||
contentDescription = DECORATIVE_ELEMENT_CONTENT_DESCRIPTION, | ||
modifier = iconModifier, | ||
contentScale = ContentScale.Crop, | ||
alpha = LocalContentAlpha.current | ||
) | ||
} | ||
} | ||
} | ||
} | ||
val hasIcon = icon != null | ||
|
||
val labelParam: (@Composable RowScope.() -> Unit)? = | ||
label?.let { | ||
{ | ||
Text( | ||
modifier = Modifier.fillMaxWidth(), | ||
text = label, | ||
textAlign = if (hasIcon) TextAlign.Start else TextAlign.Center, | ||
overflow = TextOverflow.Ellipsis, | ||
maxLines = 1 | ||
) | ||
} | ||
} | ||
|
||
CompactChip( | ||
modifier = modifier, | ||
onClick = onClick, | ||
label = labelParam, | ||
icon = iconParam, | ||
colors = colors, | ||
enabled = enabled, | ||
interactionSource = interactionSource, | ||
border = border | ||
) | ||
} | ||
|
||
/** | ||
* This component is an alternative to [CompactChip], providing the following: | ||
* - a convenient way of providing a string resource label; | ||
* - a convenient way of providing an icon and a placeholder, and choosing their size based on the | ||
* sizes recommended by the Wear guidelines; | ||
*/ | ||
@ExperimentalHorologistApi | ||
@Composable | ||
public fun CompactChip( | ||
@StringRes labelId: Int, | ||
onClick: () -> Unit, | ||
modifier: Modifier = Modifier, | ||
icon: Any? = null, | ||
iconRtlMode: IconRtlMode = IconRtlMode.Default, | ||
placeholder: Painter? = null, | ||
colors: ChipColors = ChipDefaults.primaryChipColors(), | ||
enabled: Boolean = true, | ||
interactionSource: MutableInteractionSource = remember { MutableInteractionSource() }, | ||
border: ChipBorder = ChipDefaults.chipBorder() | ||
) { | ||
CompactChip( | ||
onClick = onClick, | ||
modifier = modifier, | ||
label = stringResource(id = labelId), | ||
icon = icon, | ||
iconRtlMode = iconRtlMode, | ||
placeholder = placeholder, | ||
colors = colors, | ||
enabled = enabled, | ||
interactionSource = interactionSource, | ||
border = border | ||
) | ||
} |
67 changes: 67 additions & 0 deletions
67
...erial/src/test/java/com/google/android/horologist/compose/material/CompactChipA11yTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* 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.compose.material | ||
|
||
import androidx.compose.foundation.layout.Box | ||
import androidx.compose.foundation.layout.fillMaxSize | ||
import androidx.compose.material.icons.Icons | ||
import androidx.compose.material.icons.filled.Add | ||
import androidx.compose.ui.Alignment | ||
import androidx.compose.ui.Modifier | ||
import com.google.android.horologist.screenshots.ScreenshotBaseTest | ||
import com.google.android.horologist.screenshots.ScreenshotTestRule | ||
import org.junit.Test | ||
|
||
class CompactChipA11yTest : ScreenshotBaseTest( | ||
ScreenshotTestRule.screenshotTestRuleParams { | ||
enableA11y = true | ||
screenTimeText = {} | ||
} | ||
) { | ||
@Test | ||
fun withIcon() { | ||
screenshotTestRule.setContent(takeScreenshot = true) { | ||
Box( | ||
modifier = Modifier.fillMaxSize(), | ||
contentAlignment = Alignment.Center | ||
) { | ||
CompactChip( | ||
onClick = { }, | ||
label = "Primary label", | ||
icon = Icons.Filled.Add | ||
) | ||
} | ||
} | ||
} | ||
|
||
@Test | ||
fun disabled() { | ||
screenshotTestRule.setContent(takeScreenshot = true) { | ||
Box( | ||
modifier = Modifier.fillMaxSize(), | ||
contentAlignment = Alignment.Center | ||
) { | ||
CompactChip( | ||
onClick = { }, | ||
label = "Primary label", | ||
icon = Icons.Filled.Add, | ||
enabled = false | ||
) | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.