Skip to content

Commit

Permalink
Fix indeterminate state not working in Checkboxes (#705)
Browse files Browse the repository at this point in the history
The issue was that we weren't considering indeterminate as selected,
icon-wise, but the IDE resources do consider it as such.
  • Loading branch information
rock3r authored Nov 27, 2024
1 parent 9205770 commit f1900e1
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 12 deletions.
2 changes: 2 additions & 0 deletions foundation/api/foundation.api
Original file line number Diff line number Diff line change
Expand Up @@ -872,6 +872,7 @@ public abstract interface class org/jetbrains/jewel/foundation/state/ToggleableC
public static final field Companion Lorg/jetbrains/jewel/foundation/state/ToggleableComponentState$Companion;
public abstract fun getToggleableState ()Landroidx/compose/ui/state/ToggleableState;
public abstract fun isSelected ()Z
public abstract fun isSelectedOrIndeterminate ()Z
}

public final class org/jetbrains/jewel/foundation/state/ToggleableComponentState$Companion {
Expand All @@ -880,6 +881,7 @@ public final class org/jetbrains/jewel/foundation/state/ToggleableComponentState

public final class org/jetbrains/jewel/foundation/state/ToggleableComponentState$DefaultImpls {
public static fun isSelected (Lorg/jetbrains/jewel/foundation/state/ToggleableComponentState;)Z
public static fun isSelectedOrIndeterminate (Lorg/jetbrains/jewel/foundation/state/ToggleableComponentState;)Z
}

public abstract interface class org/jetbrains/jewel/foundation/theme/JewelTheme {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ public interface ToggleableComponentState {
public val isSelected: Boolean
get() = toggleableState == ToggleableState.On

public val isSelectedOrIndeterminate: Boolean
get() = toggleableState != ToggleableState.Off

public companion object {
public fun ULong.readToggleableState(): ToggleableState {
val selected = this and Selected != 0UL
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ fun Checkboxes() {
TriStateCheckboxRow(
"Checkbox",
checked,
{
onClick = {
checked =
when (checked) {
ToggleableState.On -> ToggleableState.Off
Expand All @@ -32,7 +32,7 @@ fun Checkboxes() {
TriStateCheckboxRow(
"Error",
checked,
{
onClick = {
checked =
when (checked) {
ToggleableState.On -> ToggleableState.Off
Expand All @@ -45,7 +45,7 @@ fun Checkboxes() {
TriStateCheckboxRow(
"Warning",
checked,
{
onClick = {
checked =
when (checked) {
ToggleableState.On -> ToggleableState.Off
Expand All @@ -55,6 +55,6 @@ fun Checkboxes() {
},
outline = Outline.Warning,
)
TriStateCheckboxRow("Disabled", checked, {}, enabled = false)
TriStateCheckboxRow("Disabled", checked, onClick = {}, enabled = false)
}
}
4 changes: 4 additions & 0 deletions ui/api/ui.api
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,8 @@ public final class org/jetbrains/jewel/ui/component/CheckboxState : org/jetbrain
public static fun isPressed-impl (J)Z
public fun isSelected ()Z
public static fun isSelected-impl (J)Z
public fun isSelectedOrIndeterminate ()Z
public static fun isSelectedOrIndeterminate-impl (J)Z
public fun toString ()Ljava/lang/String;
public static fun toString-impl (J)Ljava/lang/String;
public final synthetic fun unbox-impl ()J
Expand Down Expand Up @@ -1037,6 +1039,8 @@ public final class org/jetbrains/jewel/ui/component/ToggleableIconButtonState :
public static fun isPressed-impl (J)Z
public fun isSelected ()Z
public static fun isSelected-impl (J)Z
public fun isSelectedOrIndeterminate ()Z
public static fun isSelectedOrIndeterminate-impl (J)Z
public fun toString ()Ljava/lang/String;
public static fun toString-impl (J)Ljava/lang/String;
public final synthetic fun unbox-impl ()J
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,10 @@ private fun CheckboxImpl(
val wrapperModifier =
modifier.triStateToggleable(
state = state,
onClick = onClick,
onClick = {
onClick()
state
},
enabled = enabled,
role = Role.Checkbox,
interactionSource = interactionSource,
Expand All @@ -315,7 +318,7 @@ private fun CheckboxImpl(
} else {
PainterHint.None
},
Selected(checkboxState.toggleableState == ToggleableState.On),
Selected(checkboxState.toggleableState != ToggleableState.Off),
Stateful(checkboxState),
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ public class CheckboxMetrics(
public fun outlineCornerSizeFor(state: CheckboxState): State<CornerSize> =
rememberUpdatedState(
when {
state.isFocused && state.isSelected -> outlineSelectedFocusedCornerSize
!state.isFocused && state.isSelected -> outlineSelectedCornerSize
state.isFocused && !state.isSelected -> outlineFocusedCornerSize
state.isFocused && state.isSelectedOrIndeterminate -> outlineSelectedFocusedCornerSize
!state.isFocused && state.isSelectedOrIndeterminate -> outlineSelectedCornerSize
state.isFocused && !state.isSelectedOrIndeterminate -> outlineFocusedCornerSize
else -> outlineCornerSize
}
)
Expand All @@ -74,9 +74,9 @@ public class CheckboxMetrics(
public fun outlineSizeFor(state: CheckboxState): State<DpSize> =
rememberUpdatedState(
when {
state.isFocused && state.isSelected -> outlineSelectedFocusedSize
!state.isFocused && state.isSelected -> outlineSelectedSize
state.isFocused && !state.isSelected -> outlineFocusedSize
state.isFocused && state.isSelectedOrIndeterminate -> outlineSelectedFocusedSize
!state.isFocused && state.isSelectedOrIndeterminate -> outlineSelectedSize
state.isFocused && !state.isSelectedOrIndeterminate -> outlineFocusedSize
else -> outlineSize
}
)
Expand Down

0 comments on commit f1900e1

Please sign in to comment.