Skip to content

Commit

Permalink
ChipStyle refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
rock3r committed Oct 20, 2023
1 parent 0ccb2cf commit 1d57dca
Show file tree
Hide file tree
Showing 5 changed files with 216 additions and 246 deletions.
8 changes: 7 additions & 1 deletion core/src/main/kotlin/org/jetbrains/jewel/Chip.kt
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,16 @@ private fun ChipImpl(
val colors = style.colors
val borderColor by colors.borderFor(chipState)

val borderWidth = if (chipState.isSelected) {
style.metrics.borderWidthSelected
} else {
style.metrics.borderWidth
}

Row(
modifier = modifier
.background(colors.backgroundFor(chipState).value, shape)
.border(Stroke.Alignment.Center, style.metrics.borderWidth, borderColor, shape)
.border(Stroke.Alignment.Center, borderWidth, borderColor, shape)
.focusOutline(chipState, shape)
.padding(style.metrics.padding),
verticalAlignment = Alignment.CenterVertically,
Expand Down
92 changes: 50 additions & 42 deletions core/src/main/kotlin/org/jetbrains/jewel/styling/ChipStyling.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,53 @@ import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.Dp
import org.jetbrains.jewel.ChipState
import org.jetbrains.jewel.GenerateDataFunctions
import org.jetbrains.jewel.IntelliJTheme

@Stable
interface ChipStyle {
@GenerateDataFunctions
class ChipStyle(
val colors: ChipColors,
val metrics: ChipMetrics,
) {

val colors: ChipColors
val metrics: ChipMetrics
companion object
}

@Immutable
interface ChipColors {

val background: Brush
val backgroundDisabled: Brush
val backgroundFocused: Brush
val backgroundPressed: Brush
val backgroundHovered: Brush
val backgroundSelected: Brush
val backgroundSelectedDisabled: Brush
val backgroundSelectedPressed: Brush
val backgroundSelectedFocused: Brush
val backgroundSelectedHovered: Brush
@GenerateDataFunctions
class ChipColors(
val background: Brush,
val backgroundDisabled: Brush,
val backgroundFocused: Brush,
val backgroundPressed: Brush,
val backgroundHovered: Brush,
val backgroundSelected: Brush,
val backgroundSelectedDisabled: Brush,
val backgroundSelectedPressed: Brush,
val backgroundSelectedFocused: Brush,
val backgroundSelectedHovered: Brush,
val content: Color,
val contentDisabled: Color,
val contentFocused: Color,
val contentPressed: Color,
val contentHovered: Color,
val contentSelected: Color,
val contentSelectedDisabled: Color,
val contentSelectedPressed: Color,
val contentSelectedFocused: Color,
val contentSelectedHovered: Color,
val border: Color,
val borderDisabled: Color,
val borderFocused: Color,
val borderPressed: Color,
val borderHovered: Color,
val borderSelected: Color,
val borderSelectedDisabled: Color,
val borderSelectedPressed: Color,
val borderSelectedFocused: Color,
val borderSelectedHovered: Color,
) {

@Composable
fun backgroundFor(state: ChipState) = rememberUpdatedState(
Expand All @@ -55,17 +80,6 @@ interface ChipColors {
},
)

val content: Color
val contentDisabled: Color
val contentFocused: Color
val contentPressed: Color
val contentHovered: Color
val contentSelected: Color
val contentSelectedDisabled: Color
val contentSelectedPressed: Color
val contentSelectedFocused: Color
val contentSelectedHovered: Color

@Composable
fun contentFor(state: ChipState) = rememberUpdatedState(
if (state.isSelected) {
Expand All @@ -87,17 +101,6 @@ interface ChipColors {
},
)

val border: Color
val borderDisabled: Color
val borderFocused: Color
val borderPressed: Color
val borderHovered: Color
val borderSelected: Color
val borderSelectedDisabled: Color
val borderSelectedPressed: Color
val borderSelectedFocused: Color
val borderSelectedHovered: Color

@Composable
fun borderFor(state: ChipState) = rememberUpdatedState(
if (state.isSelected) {
Expand All @@ -118,15 +121,20 @@ interface ChipColors {
}
},
)

companion object
}

@Stable
interface ChipMetrics {
@GenerateDataFunctions
class ChipMetrics(
val cornerSize: CornerSize,
val padding: PaddingValues,
val borderWidth: Dp,
val borderWidthSelected: Dp,
) {

val cornerSize: CornerSize
val padding: PaddingValues
val borderWidth: Dp
val borderWidthSelected: Dp
companion object
}

val LocalChipStyle = staticCompositionLocalOf<ChipStyle> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@ import com.intellij.util.ui.NamedColorUtil
import com.intellij.util.ui.StatusText
import org.jetbrains.jewel.IntelliJComponentStyling
import org.jetbrains.jewel.intui.core.IntUiThemeDefinition
import org.jetbrains.jewel.intui.standalone.styling.IntUiChipColors
import org.jetbrains.jewel.intui.standalone.styling.IntUiChipMetrics
import org.jetbrains.jewel.intui.standalone.styling.IntUiChipStyle
import org.jetbrains.jewel.intui.standalone.styling.IntUiCircularProgressStyle
import org.jetbrains.jewel.intui.standalone.styling.IntUiDividerMetrics
import org.jetbrains.jewel.intui.standalone.styling.IntUiDividerStyle
Expand Down Expand Up @@ -90,6 +87,9 @@ import org.jetbrains.jewel.styling.CheckboxColors
import org.jetbrains.jewel.styling.CheckboxIcons
import org.jetbrains.jewel.styling.CheckboxMetrics
import org.jetbrains.jewel.styling.CheckboxStyle
import org.jetbrains.jewel.styling.ChipColors
import org.jetbrains.jewel.styling.ChipMetrics
import org.jetbrains.jewel.styling.ChipStyle
import org.jetbrains.jewel.styling.InputFieldStyle
import org.jetbrains.skiko.DependsOnJBR
import javax.swing.UIManager
Expand Down Expand Up @@ -282,7 +282,7 @@ private fun readCheckboxStyle(): CheckboxStyle {
// 1. There is no real disabled state, we're making it sort of up
// 2. Chips can be used as buttons (see run configs) or as radio buttons (see MeetNewUi)
// 3. We also have a toggleable version because why not
private fun readChipStyle(): IntUiChipStyle {
private fun readChipStyle(): ChipStyle {
val normalBackground =
retrieveColorsOrUnspecified("Button.startBackground", "Button.endBackground")
.createVerticalBrush()
Expand All @@ -291,7 +291,7 @@ private fun readChipStyle(): IntUiChipStyle {
val disabledBorder = retrieveColorOrUnspecified("Button.disabledBorderColor")
val selectedBorder = retrieveColorOrUnspecified("Component.focusColor")

val colors = IntUiChipColors(
val colors = ChipColors(
background = normalBackground,
backgroundDisabled = normalBackground,
backgroundFocused = normalBackground,
Expand Down Expand Up @@ -323,9 +323,9 @@ private fun readChipStyle(): IntUiChipStyle {
borderSelectedFocused = selectedBorder,
borderSelectedHovered = selectedBorder,
)
return IntUiChipStyle(
return ChipStyle(
colors = colors,
metrics = IntUiChipMetrics(
metrics = ChipMetrics(
cornerSize = CornerSize(6.dp),
padding = PaddingValues(horizontal = 12.dp, vertical = 8.dp),
borderWidth = 1.dp,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import org.jetbrains.jewel.intui.core.theme.IntUiLightTheme
import org.jetbrains.jewel.intui.standalone.IntUiTheme.defaultComponentStyling
import org.jetbrains.jewel.intui.standalone.styling.Default
import org.jetbrains.jewel.intui.standalone.styling.Defaults
import org.jetbrains.jewel.intui.standalone.styling.IntUiChipStyle
import org.jetbrains.jewel.intui.standalone.styling.IntUiCircularProgressStyle
import org.jetbrains.jewel.intui.standalone.styling.IntUiDividerStyle
import org.jetbrains.jewel.intui.standalone.styling.IntUiDropdownStyle
Expand Down Expand Up @@ -94,7 +93,7 @@ object IntUiTheme : BaseIntUiTheme {
defaultButtonStyle: ButtonStyle = ButtonStyle.Default.dark(),
outlinedButtonStyle: ButtonStyle = ButtonStyle.Outlined.dark(),
checkboxStyle: CheckboxStyle = CheckboxStyle.Defaults.dark(),
chipStyle: ChipStyle = IntUiChipStyle.dark(),
chipStyle: ChipStyle = ChipStyle.Defaults.dark(),
dividerStyle: DividerStyle = IntUiDividerStyle.dark(),
dropdownStyle: DropdownStyle = IntUiDropdownStyle.dark(),
groupHeaderStyle: GroupHeaderStyle = IntUiGroupHeaderStyle.dark(),
Expand Down Expand Up @@ -140,7 +139,7 @@ object IntUiTheme : BaseIntUiTheme {
defaultButtonStyle: ButtonStyle = ButtonStyle.Default.light(),
outlinedButtonStyle: ButtonStyle = ButtonStyle.Outlined.light(),
checkboxStyle: CheckboxStyle = CheckboxStyle.Defaults.light(),
chipStyle: ChipStyle = IntUiChipStyle.light(),
chipStyle: ChipStyle = ChipStyle.Defaults.light(),
dividerStyle: DividerStyle = IntUiDividerStyle.light(),
dropdownStyle: DropdownStyle = IntUiDropdownStyle.light(),
groupHeaderStyle: GroupHeaderStyle = IntUiGroupHeaderStyle.light(),
Expand Down
Loading

0 comments on commit 1d57dca

Please sign in to comment.