Skip to content

Commit

Permalink
Merge pull request #46 from mori-atsushi/indication
Browse files Browse the repository at this point in the history
Support Indication
  • Loading branch information
mori-atsushi authored Nov 24, 2023
2 parents d5874ec + 48b4499 commit 3d004d1
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import androidx.compose.ui.semantics.semantics
import com.moriatsushi.compose.stylesheet.StyleSheet
import com.moriatsushi.compose.stylesheet.component.Component
import com.moriatsushi.compose.stylesheet.component.componentCommonStyle
import com.moriatsushi.compose.stylesheet.component.padding.componentPadding
import com.moriatsushi.compose.stylesheet.content.ProvideContentStyle
import com.moriatsushi.compose.stylesheet.tag.TagModifier

Expand All @@ -43,12 +44,13 @@ fun Button(
Row(
modifier = modifier
.semantics { role = Role.Button }
.componentCommonStyle(stateStyle.commonStyle, includePadding = false)
.clickable(
interactionSource = interactionSource,
indication = null,
indication = mergedStyle.indication?.value,
onClick = onClick,
)
.componentCommonStyle(stateStyle.commonStyle),
.componentPadding(stateStyle.commonStyle.padding),
horizontalArrangement = Arrangement.Center,
verticalAlignment = Alignment.CenterVertically,
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import androidx.compose.runtime.Immutable
import com.moriatsushi.compose.stylesheet.component.ComponentCommonStyle
import com.moriatsushi.compose.stylesheet.component.ComponentStyle
import com.moriatsushi.compose.stylesheet.content.ContentStyle
import com.moriatsushi.compose.stylesheet.indication.IndicationStyle

/**
* A style for [Button].
*/
@Immutable
sealed interface ButtonStyle : ComponentStyle {
val indication: IndicationStyle?
val contentStyle: ContentStyle
val pressedStyle: ButtonStateStyle

Expand All @@ -28,10 +30,12 @@ fun ButtonStyle(builder: ButtonStyleBuilder.() -> Unit): ButtonStyle =
ButtonStyleBuilder().apply(builder).build()

internal fun ButtonStyle(
indication: IndicationStyle? = null,
commonStyle: ComponentCommonStyle = ComponentCommonStyle.Default,
contentStyle: ContentStyle = ContentStyle.Default,
pressedStyle: ButtonStateStyle = ButtonStateStyle.Default,
): ButtonStyle = ButtonStyleImpl(
indication = indication,
commonStyle = commonStyle,
contentStyle = contentStyle,
pressedStyle = pressedStyle,
Expand All @@ -53,6 +57,7 @@ internal fun ButtonStyle.getStyleForState(

@Immutable
private data class ButtonStyleImpl(
override val indication: IndicationStyle?,
override val commonStyle: ComponentCommonStyle,
override val contentStyle: ContentStyle,
override val pressedStyle: ButtonStateStyle,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.moriatsushi.compose.stylesheet.button
import com.moriatsushi.compose.stylesheet.StyleSheetBuilderMarker
import com.moriatsushi.compose.stylesheet.component.ComponentStyleBuilder
import com.moriatsushi.compose.stylesheet.content.ContentStyleBuilder
import com.moriatsushi.compose.stylesheet.indication.IndicationSetter

/**
* A builder for [ButtonStyle].
Expand All @@ -11,6 +12,18 @@ import com.moriatsushi.compose.stylesheet.content.ContentStyleBuilder
class ButtonStyleBuilder internal constructor() : ComponentStyleBuilder<ButtonStyle>() {
private val pressedStyleBuilder = ButtonStateStyleBuilder()

/**
* An indication representing visual effects that occur when certain interactions happen, such
* as pressing.
*
* Example:
* ```
* indication += SampleIndication()
* indication += { rememberRipple() }
* ```
*/
val indication: IndicationSetter = IndicationSetter()

/**
* A content style.
*/
Expand All @@ -31,12 +44,14 @@ class ButtonStyleBuilder internal constructor() : ComponentStyleBuilder<ButtonSt
}

override fun plusAssign(other: ButtonStyle) {
indication += other.indication
this += other.commonStyle
content += other.contentStyle
pressedStyleBuilder += other.pressedStyle
}

override fun build(): ButtonStyle = ButtonStyle(
indication = indication.value,
commonStyle = buildCommonStyle(),
contentStyle = content.build(),
pressedStyle = pressedStyleBuilder.build(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import com.moriatsushi.compose.stylesheet.token.value
@StyleSheetComponentApi
data class ComponentCommonStyle internal constructor(
internal val size: ComponentSize = ComponentSize.Default,
internal val padding: ComponentPadding? = null,
val padding: ComponentPadding? = null,
internal val background: Token<Color>? = null,
internal val shape: Token<Shape?>? = null,
internal val border: Token<BorderStroke?>? = null,
Expand All @@ -40,10 +40,16 @@ data class ComponentCommonStyle internal constructor(

/**
* Applies the component common [style] to [this].
*
* @param includePadding If false, the padding will not be applied. Please call
* [Modifier].[componentPadding] separately.
*/
@StyleSheetComponentApi
@Composable
fun Modifier.componentCommonStyle(style: ComponentCommonStyle): Modifier {
fun Modifier.componentCommonStyle(
style: ComponentCommonStyle,
includePadding: Boolean = true,
): Modifier {
val shape = style.shape?.value ?: RectangleShape

val border = style.border?.value
Expand All @@ -62,7 +68,7 @@ fun Modifier.componentCommonStyle(style: ComponentCommonStyle): Modifier {

val clipModifier = if (shape != RectangleShape) Modifier.clip(shape) else Modifier

val paddingModifier = if (style.padding != null) {
val paddingModifier = if (includePadding) {
Modifier.componentPadding(style.padding)
} else {
Modifier
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,5 +87,7 @@ private data class ComponentPaddingImpl(
}

@Composable
internal fun Modifier.componentPadding(padding: ComponentPadding): Modifier =
this.padding(padding.asPaddingValues())
@StyleSheetComponentApi
fun Modifier.componentPadding(padding: ComponentPadding?): Modifier = this.then(
if (padding != null) Modifier.padding(padding.asPaddingValues()) else Modifier,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.moriatsushi.compose.stylesheet.indication

import androidx.compose.foundation.Indication
import androidx.compose.runtime.Composable
import com.moriatsushi.compose.stylesheet.component.StyleSheetComponentApi

class IndicationSetter @StyleSheetComponentApi constructor() {
@StyleSheetComponentApi
var value: IndicationStyle? = null
private set

@StyleSheetComponentApi
operator fun plusAssign(style: IndicationStyle?) {
if (style != null) {
this.value = style
}
}

operator fun plusAssign(indication: Indication?) {
this.value = IndicationStyle { indication }
}

operator fun plusAssign(builder: @Composable () -> Indication?) {
this.value = IndicationStyle(builder)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.moriatsushi.compose.stylesheet.indication

import androidx.compose.foundation.Indication
import androidx.compose.runtime.Composable
import com.moriatsushi.compose.stylesheet.component.StyleSheetComponentApi
import kotlin.jvm.JvmInline

@StyleSheetComponentApi
sealed interface IndicationStyle {
val value: Indication?
@Composable
get
}

@StyleSheetComponentApi
internal fun IndicationStyle(builder: @Composable () -> Indication?): IndicationStyle =
IndicationStyleImpl(builder)

@JvmInline
private value class IndicationStyleImpl(
val builder: @Composable () -> Indication?,
) : IndicationStyle {
override val value: Indication?
@Composable
get() = builder()
}

0 comments on commit 3d004d1

Please sign in to comment.