Skip to content

Commit

Permalink
Rename ChipIndicator to ChipDecoration
Browse files Browse the repository at this point in the history
  • Loading branch information
ab-gnm committed Dec 10, 2024
1 parent 75d603a commit 697f2d5
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp
import com.gu.source.R
import com.gu.source.Source
import com.gu.source.components.chips.ChipIndicator
import com.gu.source.components.chips.ChipDecoration
import com.gu.source.components.chips.SourceChip
import com.gu.source.components.chips.SourceChipSupportingButton
import com.gu.source.components.chips.SourceMultiSelectChip
Expand Down Expand Up @@ -63,7 +63,7 @@ internal fun ChipsPreview(modifier: Modifier = Modifier) {
size = size,
onClick = {},
showBadge = false,
iconOrImage = ChipIndicator.Icon.Component {
iconOrImage = ChipDecoration.Icon.Component {
Icon(
imageVector = Source.Icons.Base.Plus,
contentDescription = null,
Expand All @@ -78,7 +78,7 @@ internal fun ChipsPreview(modifier: Modifier = Modifier) {
size = size,
onClick = {},
showBadge = false,
iconOrImage = ChipIndicator.Image.Painter(
iconOrImage = ChipDecoration.Image.Painter(
painter = painterResource(R.drawable.marina_hyde),
contentDescription = null,
),
Expand All @@ -90,7 +90,7 @@ internal fun ChipsPreview(modifier: Modifier = Modifier) {
size = size,
onClick = {},
showBadge = false,
iconOrImage = ChipIndicator.Image.Painter(
iconOrImage = ChipDecoration.Image.Painter(
painter = painterResource(R.drawable.marina_hyde),
contentDescription = null,
),
Expand All @@ -105,7 +105,7 @@ internal fun ChipsPreview(modifier: Modifier = Modifier) {
style = SourceChip.Style.Default.copy(
badgeColour = AppColour.Unspecified,
),
iconOrImage = ChipIndicator.Icon.Vector(
iconOrImage = ChipDecoration.Icon.Vector(
imageVector = Source.Icons.Base.Plus,
),
)
Expand Down Expand Up @@ -147,7 +147,7 @@ internal fun ChipsPreview(modifier: Modifier = Modifier) {
size = SourceChip.Size.Medium,
onClick = {},
style = SourceChip.Style.Default,
iconOrImage = ChipIndicator.Icon.Painter(
iconOrImage = ChipDecoration.Icon.Painter(
painter = painterResource(R.drawable.ic_list),
),
)
Expand All @@ -166,7 +166,7 @@ internal fun ChipsPreview(modifier: Modifier = Modifier) {
text = "Follow",
size = SourceChip.Size.Medium,
onClick = {},
iconOrImage = ChipIndicator.Icon.Vector(
iconOrImage = ChipDecoration.Icon.Vector(
imageVector = Source.Icons.Base.Plus,
),
)
Expand Down
2 changes: 1 addition & 1 deletion android/source/detekt-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
<SmellBaseline>
<ManuallySuppressedIssues/>
<CurrentIssues>
<ID>CognitiveComplexMethod:SourceChip.kt$private fun getSpacing( size: SourceChip.Size, leadingIndicator: ChipIndicator, hasTrailIndicator: Boolean, hasText: Boolean, ): Spacing</ID>
<ID>CognitiveComplexMethod:SourceChip.kt$private fun getSpacing( size: SourceChip.Size, leadingDecoration: ChipDecoration, hasTrailDecoration: Boolean, hasText: Boolean, ): Spacing</ID>
</CurrentIssues>
</SmellBaseline>
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import com.gu.source.R
import com.gu.source.Source
import com.gu.source.components.chips.ChipIndicator.Icon
import com.gu.source.components.chips.ChipIndicator.Image
import com.gu.source.components.chips.ChipDecoration.Icon
import com.gu.source.components.chips.ChipDecoration.Image
import com.gu.source.icons.Check

/**
Expand All @@ -28,16 +28,16 @@ import com.gu.source.icons.Check
* [Icon]s are `18.dp` tall and the [Image]s are `24.dp` tall.
*
* The icon or image will be tinted with the [SourceChip.Style.contentColour]. To override the
* tinting, use [ChipIndicator.Icon.Component] or [ChipIndicator.Image.Component] and provide a
* tinting, use [ChipDecoration.Icon.Component] or [ChipDecoration.Image.Component] and provide a
* custom composable function with the desired colour.
*
* For icons, prefer to use [ChipIndicator.Icon.Vector] with a [Source.Icons] vector where possible.
* Alternatively use [ChipIndicator.Icon.Painter] with a drawable resource ID.
* For icons, prefer to use [ChipDecoration.Icon.Vector] with a [Source.Icons] vector where possible.
* Alternatively use [ChipDecoration.Icon.Painter] with a drawable resource ID.
*
* For images, prefer to use [ChipIndicator.Image.Painter] for static images. For images fetched
* from the network, use [ChipIndicator.Image.Component] with a custom composable function.
* For images, prefer to use [ChipDecoration.Image.Painter] for static images. For images fetched
* from the network, use [ChipDecoration.Image.Component] with a custom composable function.
*/
sealed class ChipIndicator {
sealed class ChipDecoration {
/**
* The content to display. The provided modifier _must_ be set on the content.
* The modifier is used to apply the correct size to the icon/image.
Expand All @@ -51,7 +51,7 @@ sealed class ChipIndicator {
* Represents an [Icon] displayed before/after the chip's text. Icons are `18.dp` tall and will
* be tinted with the [SourceChip.Style.contentColour].
*/
sealed class Icon : ChipIndicator() {
sealed class Icon : ChipDecoration() {
override val height: Dp = 18.dp

/**
Expand Down Expand Up @@ -111,7 +111,7 @@ sealed class ChipIndicator {
/**
* Represents an [Image] displayed before/after the chip's text. Images are `24.dp` tall.
*/
sealed class Image : ChipIndicator() {
sealed class Image : ChipDecoration() {
override val height: Dp = 24.dp

/**
Expand Down Expand Up @@ -168,7 +168,7 @@ sealed class ChipIndicator {
}

/** Represents no image or icon displayed before/after the chip's text. */
data object None : ChipIndicator() {
data object None : ChipDecoration() {
override val content: @Composable RowScope.(Modifier) -> Unit = {}
override val height: Dp = 0.dp
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ fun SourceChip(
modifier: Modifier = Modifier,
style: SourceChip.Style = SourceChip.Style.Default,
onClickLabel: String? = null,
iconOrImage: ChipIndicator = ChipIndicator.None,
iconOrImage: ChipDecoration = ChipDecoration.None,
) {
SourceChipInternal(
text = text,
Expand All @@ -215,8 +215,8 @@ fun SourceChip(
modifier = modifier,
allowsMultiSelection = false,
onClickLabel = onClickLabel,
indicatorBefore = iconOrImage,
indicatorAfter = ChipIndicator.None,
decorationBeforeText = iconOrImage,
decorationAfterText = ChipDecoration.None,
badge = if (showBadge) {
{ Badge(containerColor = it) }
} else {
Expand Down Expand Up @@ -252,7 +252,7 @@ fun SourceMultiSelectChip(
modifier: Modifier = Modifier,
style: SourceChip.Style = SourceChip.Style.Default,
onClickLabel: String? = null,
iconOrImage: ChipIndicator = ChipIndicator.None,
iconOrImage: ChipDecoration = ChipDecoration.None,
) {
SourceChipInternal(
text = text,
Expand All @@ -263,17 +263,17 @@ fun SourceMultiSelectChip(
modifier = modifier,
allowsMultiSelection = true,
onClickLabel = onClickLabel,
indicatorBefore = iconOrImage,
indicatorAfter = if (isSelected) {
ChipIndicator.Icon.Component {
decorationBeforeText = iconOrImage,
decorationAfterText = if (isSelected) {
ChipDecoration.Icon.Component {
Icon(
imageVector = Source.Icons.Base.Check,
contentDescription = null,
modifier = Modifier.height(CheckIconHeight),
)
}
} else {
ChipIndicator.None
ChipDecoration.None
},
badge = if (showBadge) {
{ Badge(containerColor = it) }
Expand All @@ -284,8 +284,8 @@ fun SourceMultiSelectChip(
}

private data class Spacing(
val beforeLeadingIndicator: Dp,
val betweenLeadingIndicatorAndText: Dp,
val beforeLeadingDecoration: Dp,
val betweenLeadingDecorationAndText: Dp,
val betweenTextAndCheckmark: Dp,
val afterCheckmark: Dp,
)
Expand Down Expand Up @@ -315,11 +315,11 @@ private data class Spacing(
*/
private fun getSpacing(
size: SourceChip.Size,
leadingIndicator: ChipIndicator,
hasTrailIndicator: Boolean,
leadingDecoration: ChipDecoration,
hasTrailDecoration: Boolean,
hasText: Boolean,
): Spacing {
val beforeLeadingIndicator = if (leadingIndicator is ChipIndicator.None) {
val beforeLeadingDecoration = if (leadingDecoration is ChipDecoration.None) {
// Label only
16.dp
} else if (!hasText) {
Expand All @@ -333,18 +333,18 @@ private fun getSpacing(
}
}

val betweenLeadingIndicatorAndText = when (leadingIndicator) {
is ChipIndicator.Icon -> {
val betweenLeadingDecorationAndText = when (leadingDecoration) {
is ChipDecoration.Icon -> {
if (hasText) 4.dp else 0.dp
}

is ChipIndicator.Image -> if (hasText) 8.dp else 0.dp
ChipIndicator.None -> 0.dp
is ChipDecoration.Image -> if (hasText) 8.dp else 0.dp
ChipDecoration.None -> 0.dp
}

val betweenTextAndCheckmark = if (hasTrailIndicator) 8.dp else 0.dp
val betweenTextAndCheckmark = if (hasTrailDecoration) 8.dp else 0.dp

val afterCheckmark = if (hasTrailIndicator) {
val afterCheckmark = if (hasTrailDecoration) {
8.dp
} else if (hasText) {
// Label only without check
Expand All @@ -355,8 +355,8 @@ private fun getSpacing(
}

return Spacing(
beforeLeadingIndicator = beforeLeadingIndicator,
betweenLeadingIndicatorAndText = betweenLeadingIndicatorAndText,
beforeLeadingDecoration = beforeLeadingDecoration,
betweenLeadingDecorationAndText = betweenLeadingDecorationAndText,
betweenTextAndCheckmark = betweenTextAndCheckmark,
afterCheckmark = afterCheckmark,
)
Expand All @@ -378,8 +378,8 @@ private fun getSpacing(
* @param allowsMultiSelection Optional - whether the chip allows multiple selections. This is used
* to set correct semantic role for the chip - checkbox if true, button if false.
* @param onClickLabel Optional label for the onClick action.
* @param indicatorBefore Optional content to display an icon/image before the title.
* @param indicatorAfter Optional content to display an icon/image after the title.
* @param decorationBeforeText Optional content to display an icon/image before the title.
* @param decorationAfterText Optional content to display an icon/image after the title.
* @param badge Optional content to display a badge over the chip. Usually a [Badge]. Badge colour
* is passed to the [badge] slot.
*/
Expand All @@ -395,8 +395,8 @@ internal fun SourceChipInternal(
style: SourceChip.Style = SourceChip.Style.Default,
allowsMultiSelection: Boolean = false,
onClickLabel: String? = null,
indicatorBefore: ChipIndicator = ChipIndicator.None,
indicatorAfter: ChipIndicator = ChipIndicator.None,
decorationBeforeText: ChipDecoration = ChipDecoration.None,
decorationAfterText: ChipDecoration = ChipDecoration.None,
badge: @Composable ((Color) -> Unit)? = null,
) {
SourceBaseChip(
Expand Down Expand Up @@ -425,17 +425,17 @@ internal fun SourceChipInternal(

val spacing = getSpacing(
size = size,
leadingIndicator = indicatorBefore,
hasTrailIndicator = indicatorAfter !is ChipIndicator.None,
leadingDecoration = decorationBeforeText,
hasTrailDecoration = decorationAfterText !is ChipDecoration.None,
hasText = text.isNotBlank(),
)

CompositionLocalProvider(LocalContentColor provides contentColour) {
Spacer(modifier = Modifier.width(spacing.beforeLeadingIndicator))
Spacer(modifier = Modifier.width(spacing.beforeLeadingDecoration))

indicatorBefore.content(this, Modifier.height(indicatorBefore.height))
decorationBeforeText.content(this, Modifier.height(decorationBeforeText.height))

Spacer(modifier = Modifier.width(spacing.betweenLeadingIndicatorAndText))
Spacer(modifier = Modifier.width(spacing.betweenLeadingDecorationAndText))

HorizontalExpandingText(
text = text,
Expand All @@ -447,7 +447,7 @@ internal fun SourceChipInternal(

Spacer(modifier = Modifier.width(spacing.betweenTextAndCheckmark))

indicatorAfter.content(this, Modifier)
decorationAfterText.content(this, Modifier)

Spacer(modifier = Modifier.width(spacing.afterCheckmark))
}
Expand Down Expand Up @@ -475,7 +475,7 @@ fun SourceChipSupportingButton(
onClick: () -> Unit,
modifier: Modifier = Modifier,
style: SourceChip.Style = SourceChip.Style.SupportingButton,
iconOrImage: ChipIndicator = ChipIndicator.None,
iconOrImage: ChipDecoration = ChipDecoration.None,
) {
SourceChipInternal(
text = text,
Expand All @@ -484,8 +484,8 @@ fun SourceChipSupportingButton(
style = style,
onClick = onClick,
modifier = modifier,
indicatorBefore = iconOrImage,
indicatorAfter = ChipIndicator.None,
decorationBeforeText = iconOrImage,
decorationAfterText = ChipDecoration.None,
)
}

Expand Down Expand Up @@ -534,7 +534,7 @@ internal fun SourceChipPreview(modifier: Modifier = Modifier) {
size = size,
onClick = {},
showBadge = false,
iconOrImage = ChipIndicator.Icon.Component {
iconOrImage = ChipDecoration.Icon.Component {
Icon(
imageVector = Source.Icons.Base.Plus,
contentDescription = null,
Expand All @@ -549,7 +549,7 @@ internal fun SourceChipPreview(modifier: Modifier = Modifier) {
size = size,
onClick = {},
showBadge = false,
iconOrImage = ChipIndicator.Image.Painter(
iconOrImage = ChipDecoration.Image.Painter(
painter = painterResource(R.drawable.marina_hyde),
contentDescription = null,
),
Expand All @@ -561,7 +561,7 @@ internal fun SourceChipPreview(modifier: Modifier = Modifier) {
size = size,
onClick = {},
showBadge = false,
iconOrImage = ChipIndicator.Image.Painter(
iconOrImage = ChipDecoration.Image.Painter(
painter = painterResource(R.drawable.marina_hyde),
contentDescription = null,
),
Expand All @@ -576,7 +576,7 @@ internal fun SourceChipPreview(modifier: Modifier = Modifier) {
style = SourceChip.Style.Default.copy(
badgeColour = AppColour.Unspecified,
),
iconOrImage = ChipIndicator.Icon.Vector(
iconOrImage = ChipDecoration.Icon.Vector(
imageVector = Source.Icons.Base.Plus,
),
)
Expand Down Expand Up @@ -618,7 +618,7 @@ internal fun SourceChipPreview(modifier: Modifier = Modifier) {
size = SourceChip.Size.Medium,
onClick = {},
style = SourceChip.Style.Default,
iconOrImage = ChipIndicator.Icon.Painter(
iconOrImage = ChipDecoration.Icon.Painter(
painter = painterResource(R.drawable.ic_list),
),
)
Expand All @@ -637,7 +637,7 @@ internal fun SourceChipPreview(modifier: Modifier = Modifier) {
text = "Follow",
size = SourceChip.Size.Medium,
onClick = {},
iconOrImage = ChipIndicator.Icon.Vector(
iconOrImage = ChipDecoration.Icon.Vector(
imageVector = Source.Icons.Base.Plus,
),
)
Expand Down

0 comments on commit 697f2d5

Please sign in to comment.