From dae497d8398b5edfb1b3cdac4c322ddc1da89c1b Mon Sep 17 00:00:00 2001 From: fscarponi Date: Tue, 10 Oct 2023 19:14:03 +0200 Subject: [PATCH] Refactor button state logic & tidy method declarations Revised the button state logic of the IconButton component in IconButtonMetrics.kt with a more intuitive 'when' statement, improving readability. Likewise, method declarations in the IntUiBridge.kt file were reformatted for brevity. This maintenance will enhance the code's readability and thus its maintainability. Changes in IconButton.kt reflect the updated logic and background colour application. The @Stable annotation was attached to the IconButtonMetrics interface to indicate thread-safe and side-effect-free operations, a useful indicator for composing efficient composable functions in Jetpack Compose. --- .../kotlin/org/jetbrains/jewel/IconButton.kt | 5 +- .../jewel/styling/IconButtonMetrics.kt | 16 +++--- .../org/jetbrains/jewel/bridge/IntUiBridge.kt | 53 +++++++++---------- 3 files changed, 36 insertions(+), 38 deletions(-) diff --git a/core/src/main/kotlin/org/jetbrains/jewel/IconButton.kt b/core/src/main/kotlin/org/jetbrains/jewel/IconButton.kt index b682cde80d..2ad0109fed 100644 --- a/core/src/main/kotlin/org/jetbrains/jewel/IconButton.kt +++ b/core/src/main/kotlin/org/jetbrains/jewel/IconButton.kt @@ -50,13 +50,14 @@ fun IconButton( is HoverInteraction.Enter -> buttonState = buttonState.copy(hovered = true) is HoverInteraction.Exit -> buttonState = buttonState.copy(hovered = false) + is FocusInteraction.Focus -> buttonState = buttonState.copy(focused = true) is FocusInteraction.Unfocus -> buttonState = buttonState.copy(focused = false) } } } val shape = RoundedCornerShape(style.metrics.cornerSize) - val background by style.colors.backgroundFor(buttonState) + val background = style.colors.backgroundFor(buttonState) Box( modifier = modifier .defaultMinSize(style.metrics.minSize.width, style.metrics.minSize.height) @@ -69,7 +70,7 @@ fun IconButton( ) .clip(shape) .padding(style.metrics.padding) - .background(background), + .background(background.value), propagateMinConstraints = true, contentAlignment = Alignment.Center, content = { diff --git a/core/src/main/kotlin/org/jetbrains/jewel/styling/IconButtonMetrics.kt b/core/src/main/kotlin/org/jetbrains/jewel/styling/IconButtonMetrics.kt index c09b674ca8..7e07c48d83 100644 --- a/core/src/main/kotlin/org/jetbrains/jewel/styling/IconButtonMetrics.kt +++ b/core/src/main/kotlin/org/jetbrains/jewel/styling/IconButtonMetrics.kt @@ -29,17 +29,17 @@ interface IconButtonColors { @Composable fun backgroundFor(state: ButtonState) = rememberUpdatedState( - state.chooseValue( - normal = background, - disabled = backgroundDisabled, - focused = backgroundFocused, - pressed = backgroundPressed, - hovered = backgroundHovered, - active = background, - ) + when { + !state.isEnabled -> backgroundDisabled + state.isPressed -> backgroundPressed + state.isFocused -> backgroundFocused + state.isHovered -> backgroundHovered + else -> background + }, ) } +@Stable interface IconButtonMetrics { val cornerSize: CornerSize diff --git a/ide-laf-bridge/src/main/kotlin/org/jetbrains/jewel/bridge/IntUiBridge.kt b/ide-laf-bridge/src/main/kotlin/org/jetbrains/jewel/bridge/IntUiBridge.kt index 4192ed3798..0e03b322df 100644 --- a/ide-laf-bridge/src/main/kotlin/org/jetbrains/jewel/bridge/IntUiBridge.kt +++ b/ide-laf-bridge/src/main/kotlin/org/jetbrains/jewel/bridge/IntUiBridge.kt @@ -904,33 +904,30 @@ private fun readEditorTabStyle(iconData: IntelliJThemeIconData, svgLoader: SvgLo private fun readCircularProgressStyle( isDark: Boolean, -): IntUiCircularProgressStyle = - IntUiCircularProgressStyle( - frameTime = 125.milliseconds, - color = retrieveColorOrUnspecified("ProgressIcon.color") - .takeIf { it.isSpecified } - ?: if (isDark) Color(0xFF6F737A) else Color(0xFFA8ADBD), - ) - -private fun readTooltipStyle(): IntUiTooltipStyle = - IntUiTooltipStyle( - metrics = IntUiTooltipMetrics(), - colors = IntUiTooltipColors( - content = retrieveColorOrUnspecified("ToolTip.foreground"), - background = retrieveColorOrUnspecified("ToolTip.background"), - border = retrieveColorOrUnspecified("ToolTip.borderColor"), - shadow = Color.Black.copy(alpha = .6f), - ), - ) +): IntUiCircularProgressStyle = IntUiCircularProgressStyle( + frameTime = 125.milliseconds, + color = retrieveColorOrUnspecified("ProgressIcon.color") + .takeIf { it.isSpecified } + ?: if (isDark) Color(0xFF6F737A) else Color(0xFFA8ADBD), +) + +private fun readTooltipStyle(): IntUiTooltipStyle = IntUiTooltipStyle( + metrics = IntUiTooltipMetrics(), + colors = IntUiTooltipColors( + content = retrieveColorOrUnspecified("ToolTip.foreground"), + background = retrieveColorOrUnspecified("ToolTip.background"), + border = retrieveColorOrUnspecified("ToolTip.borderColor"), + shadow = Color.Black.copy(alpha = .6f), + ), +) -private fun readIconButtonStyle(): IntUiIconButtonStyle = - IntUiIconButtonStyle( - metrics = IntUiIconButtonMetrics(), - colors = IntUiIconButtonColors( - Color.Unspecified, - Color.Unspecified, - Color.Unspecified, - retrieveColorOrUnspecified("ActionButton.pressedBackground"), - retrieveColorOrUnspecified("ActionButton.hoverBackground"), - ) +private fun readIconButtonStyle(): IntUiIconButtonStyle = IntUiIconButtonStyle( + metrics = IntUiIconButtonMetrics(), + colors = IntUiIconButtonColors( + background = Color.Unspecified, + backgroundDisabled = Color.Unspecified, + backgroundFocused = Color.Unspecified, + backgroundPressed = retrieveColorOrUnspecified("ActionButton.pressedBackground"), + backgroundHovered = retrieveColorOrUnspecified("ActionButton.hoverBackground"), ) +)