diff --git a/component/compose-stylesheet-button/src/commonMain/kotlin/com.moriatsushi.compose.stylesheet.button/Button.kt b/component/compose-stylesheet-button/src/commonMain/kotlin/com.moriatsushi.compose.stylesheet.button/Button.kt index 546d133..2ebfd64 100644 --- a/component/compose-stylesheet-button/src/commonMain/kotlin/com.moriatsushi.compose.stylesheet.button/Button.kt +++ b/component/compose-stylesheet-button/src/commonMain/kotlin/com.moriatsushi.compose.stylesheet.button/Button.kt @@ -2,6 +2,8 @@ package com.moriatsushi.compose.stylesheet.button import androidx.compose.foundation.clickable import androidx.compose.foundation.interaction.MutableInteractionSource +import androidx.compose.foundation.interaction.collectIsFocusedAsState +import androidx.compose.foundation.interaction.collectIsHoveredAsState import androidx.compose.foundation.interaction.collectIsPressedAsState import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Row @@ -28,6 +30,7 @@ import com.moriatsushi.compose.stylesheet.tag.TagModifier fun Button( onClick: () -> Unit, modifier: Modifier = Modifier, + enabled: Boolean = true, tags: TagModifier = TagModifier(), interactionSource: MutableInteractionSource = remember { MutableInteractionSource() }, buttonStyle: ButtonStyle = ButtonStyle.Default, @@ -39,7 +42,14 @@ fun Button( this += buttonStyle } val isPressed by interactionSource.collectIsPressedAsState() - val stateStyle = mergedStyle.getStyleForState(isPressed = isPressed) + val isHovered by interactionSource.collectIsHoveredAsState() + val isFocused by interactionSource.collectIsFocusedAsState() + val stateStyle = mergedStyle.getStyleForState( + isEnabled = enabled, + isPressed = isPressed, + isHovered = isHovered, + isFocused = isFocused, + ) Row( modifier = modifier @@ -49,6 +59,7 @@ fun Button( interactionSource = interactionSource, indication = mergedStyle.indication?.value, onClick = onClick, + enabled = enabled, ) .componentPadding(stateStyle.commonStyle.padding), horizontalArrangement = Arrangement.Center, diff --git a/component/compose-stylesheet-button/src/commonMain/kotlin/com.moriatsushi.compose.stylesheet.button/ButtonStyle.kt b/component/compose-stylesheet-button/src/commonMain/kotlin/com.moriatsushi.compose.stylesheet.button/ButtonStyle.kt index a4e2c6d..2d8fe07 100644 --- a/component/compose-stylesheet-button/src/commonMain/kotlin/com.moriatsushi.compose.stylesheet.button/ButtonStyle.kt +++ b/component/compose-stylesheet-button/src/commonMain/kotlin/com.moriatsushi.compose.stylesheet.button/ButtonStyle.kt @@ -14,6 +14,9 @@ sealed interface ButtonStyle : ComponentStyle { val indication: IndicationStyle? val contentStyle: ContentStyle val pressedStyle: ButtonStateStyle + val hoveredStyle: ButtonStateStyle + val focusedStyle: ButtonStateStyle + val disabledStyle: ButtonStateStyle companion object { /** @@ -34,24 +37,45 @@ internal fun ButtonStyle( commonStyle: ComponentCommonStyle = ComponentCommonStyle.Default, contentStyle: ContentStyle = ContentStyle.Default, pressedStyle: ButtonStateStyle = ButtonStateStyle.Default, + hoveredStyle: ButtonStateStyle = ButtonStateStyle.Default, + focusedStyle: ButtonStateStyle = ButtonStateStyle.Default, + disabledStyle: ButtonStateStyle = ButtonStateStyle.Default, ): ButtonStyle = ButtonStyleImpl( indication = indication, commonStyle = commonStyle, contentStyle = contentStyle, pressedStyle = pressedStyle, + hoveredStyle = hoveredStyle, + focusedStyle = focusedStyle, + disabledStyle = disabledStyle, ) internal fun ButtonStyle.getStyleForState( + isEnabled: Boolean, isPressed: Boolean, + isHovered: Boolean, + isFocused: Boolean, ): ButtonStateStyle { val buttonStyle = this return ButtonStateStyle { this += buttonStyle.commonStyle content += buttonStyle.contentStyle + if (isFocused) { + this += buttonStyle.focusedStyle + } + + if (isHovered) { + this += buttonStyle.hoveredStyle + } + if (isPressed) { this += buttonStyle.pressedStyle } + + if (!isEnabled) { + this += buttonStyle.disabledStyle + } } } @@ -61,4 +85,7 @@ private data class ButtonStyleImpl( override val commonStyle: ComponentCommonStyle, override val contentStyle: ContentStyle, override val pressedStyle: ButtonStateStyle, + override val hoveredStyle: ButtonStateStyle, + override val focusedStyle: ButtonStateStyle, + override val disabledStyle: ButtonStateStyle, ) : ButtonStyle diff --git a/component/compose-stylesheet-button/src/commonMain/kotlin/com.moriatsushi.compose.stylesheet.button/ButtonStyleBuilder.kt b/component/compose-stylesheet-button/src/commonMain/kotlin/com.moriatsushi.compose.stylesheet.button/ButtonStyleBuilder.kt index adb624b..218dcdb 100644 --- a/component/compose-stylesheet-button/src/commonMain/kotlin/com.moriatsushi.compose.stylesheet.button/ButtonStyleBuilder.kt +++ b/component/compose-stylesheet-button/src/commonMain/kotlin/com.moriatsushi.compose.stylesheet.button/ButtonStyleBuilder.kt @@ -11,6 +11,9 @@ import com.moriatsushi.compose.stylesheet.indication.IndicationSetter @StyleSheetBuilderMarker class ButtonStyleBuilder internal constructor() : ComponentStyleBuilder() { private val pressedStyleBuilder = ButtonStateStyleBuilder() + private val hoveredStyleBuilder = ButtonStateStyleBuilder() + private val focusedStyleBuilder = ButtonStateStyleBuilder() + private val disabledStyleBuilder = ButtonStateStyleBuilder() /** * An indication representing visual effects that occur when certain interactions happen, such @@ -43,11 +46,35 @@ class ButtonStyleBuilder internal constructor() : ComponentStyleBuilder Unit) { + hoveredStyleBuilder.builder() + } + + /** + * Defines focused styles. + */ + fun focused(builder: ButtonStateStyleBuilder.() -> Unit) { + focusedStyleBuilder.builder() + } + + /** + * Defines disabled styles. + */ + fun disabled(builder: ButtonStateStyleBuilder.() -> Unit) { + disabledStyleBuilder.builder() + } + override fun plusAssign(other: ButtonStyle) { indication += other.indication this += other.commonStyle content += other.contentStyle pressedStyleBuilder += other.pressedStyle + hoveredStyleBuilder += other.hoveredStyle + focusedStyleBuilder += other.focusedStyle + disabledStyleBuilder += other.disabledStyle } override fun build(): ButtonStyle = ButtonStyle( @@ -55,5 +82,8 @@ class ButtonStyleBuilder internal constructor() : ComponentStyleBuilder