Skip to content

Commit

Permalink
Proof of concept of refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
rock3r committed Oct 20, 2023
1 parent 0101ed3 commit 2afed4a
Show file tree
Hide file tree
Showing 4 changed files with 260 additions and 276 deletions.
62 changes: 35 additions & 27 deletions core/src/main/kotlin/org/jetbrains/jewel/styling/ButtonStyling.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,37 @@ import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.DpSize
import org.jetbrains.jewel.ButtonState
import org.jetbrains.jewel.GenerateDataFunctions

@Stable
interface ButtonStyle {
@GenerateDataFunctions
class ButtonStyle(
val colors: ButtonColors,
val metrics: ButtonMetrics,
) {

val colors: ButtonColors
val metrics: ButtonMetrics
companion object
}

@Immutable
interface ButtonColors {

val background: Brush
val backgroundDisabled: Brush
val backgroundFocused: Brush
val backgroundPressed: Brush
val backgroundHovered: Brush
@GenerateDataFunctions
class ButtonColors(
val background: Brush,
val backgroundDisabled: Brush,
val backgroundFocused: Brush,
val backgroundPressed: Brush,
val backgroundHovered: Brush,
val content: Color,
val contentDisabled: Color,
val contentFocused: Color,
val contentPressed: Color,
val contentHovered: Color,
val border: Brush,
val borderDisabled: Brush,
val borderFocused: Brush,
val borderPressed: Brush,
val borderHovered: Brush,
) {

@Composable
fun backgroundFor(state: ButtonState) = rememberUpdatedState(
Expand All @@ -41,12 +56,6 @@ interface ButtonColors {
),
)

val content: Color
val contentDisabled: Color
val contentFocused: Color
val contentPressed: Color
val contentHovered: Color

@Composable
fun contentFor(state: ButtonState) = rememberUpdatedState(
state.chooseValue(
Expand All @@ -59,12 +68,6 @@ interface ButtonColors {
),
)

val border: Brush
val borderDisabled: Brush
val borderFocused: Brush
val borderPressed: Brush
val borderHovered: Brush

@Composable
fun borderFor(state: ButtonState) = rememberUpdatedState(
state.chooseValue(
Expand All @@ -76,15 +79,20 @@ interface ButtonColors {
active = border,
),
)

companion object
}

@Stable
interface ButtonMetrics {
@GenerateDataFunctions
class ButtonMetrics(
val cornerSize: CornerSize,
val padding: PaddingValues,
val minSize: DpSize,
val borderWidth: Dp,
) {

val cornerSize: CornerSize
val padding: PaddingValues
val minSize: DpSize
val borderWidth: Dp
companion object
}

val LocalDefaultButtonStyle = staticCompositionLocalOf<ButtonStyle> {
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.IntUiButtonColors
import org.jetbrains.jewel.intui.standalone.styling.IntUiButtonMetrics
import org.jetbrains.jewel.intui.standalone.styling.IntUiButtonStyle
import org.jetbrains.jewel.intui.standalone.styling.IntUiCheckboxColors
import org.jetbrains.jewel.intui.standalone.styling.IntUiCheckboxIcons
import org.jetbrains.jewel.intui.standalone.styling.IntUiCheckboxMetrics
Expand Down Expand Up @@ -90,6 +87,9 @@ import org.jetbrains.jewel.intui.standalone.styling.IntUiTextFieldStyle
import org.jetbrains.jewel.intui.standalone.styling.IntUiTooltipColors
import org.jetbrains.jewel.intui.standalone.styling.IntUiTooltipMetrics
import org.jetbrains.jewel.intui.standalone.styling.IntUiTooltipStyle
import org.jetbrains.jewel.styling.ButtonColors
import org.jetbrains.jewel.styling.ButtonMetrics
import org.jetbrains.jewel.styling.ButtonStyle
import org.jetbrains.jewel.styling.InputFieldStyle
import org.jetbrains.skiko.DependsOnJBR
import javax.swing.UIManager
Expand Down Expand Up @@ -172,7 +172,7 @@ internal fun createSwingIntUiComponentStyling(
)
}

private fun readDefaultButtonStyle(): IntUiButtonStyle {
private fun readDefaultButtonStyle(): ButtonStyle {
val normalBackground =
retrieveColorsOrUnspecified(
"Button.default.startBackground",
Expand All @@ -183,7 +183,7 @@ private fun readDefaultButtonStyle(): IntUiButtonStyle {
retrieveColorsOrUnspecified("Button.default.startBorderColor", "Button.default.endBorderColor")
.createVerticalBrush()

val colors = IntUiButtonColors(
val colors = ButtonColors(
background = normalBackground,
backgroundDisabled = SolidColor(Color.Transparent),
backgroundFocused = normalBackground,
Expand All @@ -201,9 +201,9 @@ private fun readDefaultButtonStyle(): IntUiButtonStyle {
borderHovered = normalBorder,
)

return IntUiButtonStyle(
return ButtonStyle(
colors = colors,
metrics = IntUiButtonMetrics(
metrics = ButtonMetrics(
cornerSize = retrieveArcAsCornerSizeWithFallbacks("Button.default.arc", "Button.arc"),
padding = PaddingValues(horizontal = 14.dp), // see DarculaButtonUI.HORIZONTAL_PADDING
minSize = DpSize(DarculaUIUtil.MINIMUM_WIDTH.dp, DarculaUIUtil.MINIMUM_HEIGHT.dp),
Expand All @@ -212,7 +212,7 @@ private fun readDefaultButtonStyle(): IntUiButtonStyle {
)
}

private fun readOutlinedButtonStyle(): IntUiButtonStyle {
private fun readOutlinedButtonStyle(): ButtonStyle {
val normalBackground =
retrieveColorsOrUnspecified("Button.startBackground", "Button.endBackground")
.createVerticalBrush()
Expand All @@ -221,7 +221,7 @@ private fun readOutlinedButtonStyle(): IntUiButtonStyle {
retrieveColorsOrUnspecified("Button.startBorderColor", "Button.endBorderColor")
.createVerticalBrush()

val colors = IntUiButtonColors(
val colors = ButtonColors(
background = normalBackground,
backgroundDisabled = SolidColor(Color.Transparent),
backgroundFocused = normalBackground,
Expand All @@ -239,9 +239,9 @@ private fun readOutlinedButtonStyle(): IntUiButtonStyle {
borderHovered = normalBorder,
)

return IntUiButtonStyle(
return ButtonStyle(
colors = colors,
metrics = IntUiButtonMetrics(
metrics = ButtonMetrics(
cornerSize = CornerSize(DarculaUIUtil.BUTTON_ARC.dp / 2),
padding = PaddingValues(horizontal = 14.dp), // see DarculaButtonUI.HORIZONTAL_PADDING
minSize = DpSize(DarculaUIUtil.MINIMUM_WIDTH.dp, DarculaUIUtil.MINIMUM_HEIGHT.dp),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package org.jetbrains.jewel.intui.standalone
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.ProvidedValue
import androidx.compose.runtime.getValue
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontFamily
Expand All @@ -20,7 +19,7 @@ import org.jetbrains.jewel.intui.core.IntUiThemeDefinition
import org.jetbrains.jewel.intui.core.theme.IntUiDarkTheme
import org.jetbrains.jewel.intui.core.theme.IntUiLightTheme
import org.jetbrains.jewel.intui.standalone.IntUiTheme.defaultComponentStyling
import org.jetbrains.jewel.intui.standalone.styling.IntUiButtonStyle
import org.jetbrains.jewel.intui.standalone.styling.Default
import org.jetbrains.jewel.intui.standalone.styling.IntUiCheckboxStyle
import org.jetbrains.jewel.intui.standalone.styling.IntUiChipStyle
import org.jetbrains.jewel.intui.standalone.styling.IntUiCircularProgressStyle
Expand All @@ -39,6 +38,7 @@ import org.jetbrains.jewel.intui.standalone.styling.IntUiTabStyle
import org.jetbrains.jewel.intui.standalone.styling.IntUiTextAreaStyle
import org.jetbrains.jewel.intui.standalone.styling.IntUiTextFieldStyle
import org.jetbrains.jewel.intui.standalone.styling.IntUiTooltipStyle
import org.jetbrains.jewel.intui.standalone.styling.Outlined
import org.jetbrains.jewel.painter.LocalPainterHintsProvider
import org.jetbrains.jewel.styling.ButtonStyle
import org.jetbrains.jewel.styling.CheckboxStyle
Expand Down Expand Up @@ -91,8 +91,8 @@ object IntUiTheme : BaseIntUiTheme {
if (theme.isDark) darkComponentStyling() else lightComponentStyling()

@Composable fun darkComponentStyling(
defaultButtonStyle: ButtonStyle = IntUiButtonStyle.Default.dark(),
outlinedButtonStyle: ButtonStyle = IntUiButtonStyle.Outlined.dark(),
defaultButtonStyle: ButtonStyle = ButtonStyle.Default.dark(),
outlinedButtonStyle: ButtonStyle = ButtonStyle.Outlined.dark(),
checkboxStyle: CheckboxStyle = IntUiCheckboxStyle.dark(),
chipStyle: ChipStyle = IntUiChipStyle.dark(),
dividerStyle: DividerStyle = IntUiDividerStyle.dark(),
Expand Down Expand Up @@ -137,8 +137,8 @@ object IntUiTheme : BaseIntUiTheme {
)

@Composable fun lightComponentStyling(
defaultButtonStyle: ButtonStyle = IntUiButtonStyle.Default.light(),
outlinedButtonStyle: ButtonStyle = IntUiButtonStyle.Outlined.light(),
defaultButtonStyle: ButtonStyle = ButtonStyle.Default.light(),
outlinedButtonStyle: ButtonStyle = ButtonStyle.Outlined.light(),
checkboxStyle: CheckboxStyle = IntUiCheckboxStyle.light(),
chipStyle: ChipStyle = IntUiChipStyle.light(),
dividerStyle: DividerStyle = IntUiDividerStyle.light(),
Expand Down
Loading

0 comments on commit 2afed4a

Please sign in to comment.