Skip to content

Commit

Permalink
Refactor button state logic & tidy method declarations
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
fscarponi committed Oct 10, 2023
1 parent afaa7cd commit dae497d
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 38 deletions.
5 changes: 3 additions & 2 deletions core/src/main/kotlin/org/jetbrains/jewel/IconButton.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -69,7 +70,7 @@ fun IconButton(
)
.clip(shape)
.padding(style.metrics.padding)
.background(background),
.background(background.value),
propagateMinConstraints = true,
contentAlignment = Alignment.Center,
content = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
)
)

0 comments on commit dae497d

Please sign in to comment.