From a6f92bf1fcfd205e3002d12ec105de0474f53af1 Mon Sep 17 00:00:00 2001 From: fankesyooni Date: Fri, 1 Dec 2023 10:55:25 +0800 Subject: [PATCH] refactor: make content optional --- .../com/highcapable/flexiui/component/CheckBox.kt | 15 +++++++++------ .../highcapable/flexiui/component/RadioButton.kt | 15 +++++++++------ .../com/highcapable/flexiui/component/Switch.kt | 13 ++++++++----- 3 files changed, 26 insertions(+), 17 deletions(-) diff --git a/flexiui-core/src/commonMain/kotlin/com/highcapable/flexiui/component/CheckBox.kt b/flexiui-core/src/commonMain/kotlin/com/highcapable/flexiui/component/CheckBox.kt index afc6a25..e19d40f 100644 --- a/flexiui-core/src/commonMain/kotlin/com/highcapable/flexiui/component/CheckBox.kt +++ b/flexiui-core/src/commonMain/kotlin/com/highcapable/flexiui/component/CheckBox.kt @@ -32,8 +32,9 @@ import androidx.compose.foundation.interaction.collectIsHoveredAsState import androidx.compose.foundation.interaction.collectIsPressedAsState import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.Row -import androidx.compose.foundation.layout.padding +import androidx.compose.foundation.layout.RowScope import androidx.compose.foundation.layout.size +import androidx.compose.foundation.layout.width import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.runtime.Composable import androidx.compose.runtime.Immutable @@ -85,7 +86,7 @@ fun CheckBox( style: CheckBoxStyle = CheckBox.style, enabled: Boolean = true, interactionSource: MutableInteractionSource = remember { MutableInteractionSource() }, - content: @Composable () -> Unit = {} + content: @Composable (RowScope.() -> Unit)? = null ) { val hovered by interactionSource.collectIsHoveredAsState() val pressed by interactionSource.collectIsPressedAsState() @@ -120,10 +121,12 @@ fun CheckBox( tint = colors.contentColor ) } - Box( - modifier = Modifier.padding(start = style.contentSpacing) - .clickable(enabled = enabled) { onCheckedChange(!checked) } - ) { content() } + content?.also { content -> + Row(modifier = Modifier.clickable(enabled = enabled) { onCheckedChange(!checked) }) { + Box(modifier = Modifier.width(style.contentSpacing)) + content() + } + } } } diff --git a/flexiui-core/src/commonMain/kotlin/com/highcapable/flexiui/component/RadioButton.kt b/flexiui-core/src/commonMain/kotlin/com/highcapable/flexiui/component/RadioButton.kt index 98c4db6..90adf97 100644 --- a/flexiui-core/src/commonMain/kotlin/com/highcapable/flexiui/component/RadioButton.kt +++ b/flexiui-core/src/commonMain/kotlin/com/highcapable/flexiui/component/RadioButton.kt @@ -33,8 +33,9 @@ import androidx.compose.foundation.interaction.collectIsHoveredAsState import androidx.compose.foundation.interaction.collectIsPressedAsState import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.Row -import androidx.compose.foundation.layout.padding +import androidx.compose.foundation.layout.RowScope import androidx.compose.foundation.layout.size +import androidx.compose.foundation.layout.width import androidx.compose.runtime.Composable import androidx.compose.runtime.Immutable import androidx.compose.runtime.ReadOnlyComposable @@ -84,7 +85,7 @@ fun RadioButton( style: RadioButtonStyle = RadioButton.style, enabled: Boolean = true, interactionSource: MutableInteractionSource = remember { MutableInteractionSource() }, - content: @Composable () -> Unit = {} + content: @Composable (RowScope.() -> Unit)? = null ) { val contentDiameter = style.contentRadius * 2 val strokeDiameter = style.strokeRadius * 2 @@ -115,10 +116,12 @@ fun RadioButton( .background(colors.contentColor, style.shape) ) } - Box( - modifier = Modifier.padding(start = style.contentSpacing) - .clickable(enabled = enabled, onClick = onClick) - ) { content() } + content?.also { content -> + Row(modifier = Modifier.clickable(enabled = enabled, onClick = onClick)) { + Box(modifier = Modifier.width(style.contentSpacing)) + content() + } + } } } diff --git a/flexiui-core/src/commonMain/kotlin/com/highcapable/flexiui/component/Switch.kt b/flexiui-core/src/commonMain/kotlin/com/highcapable/flexiui/component/Switch.kt index ecfbcbd..f129283 100644 --- a/flexiui-core/src/commonMain/kotlin/com/highcapable/flexiui/component/Switch.kt +++ b/flexiui-core/src/commonMain/kotlin/com/highcapable/flexiui/component/Switch.kt @@ -39,6 +39,7 @@ import androidx.compose.foundation.layout.RowScope import androidx.compose.foundation.layout.offset import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.size +import androidx.compose.foundation.layout.width import androidx.compose.runtime.Composable import androidx.compose.runtime.Immutable import androidx.compose.runtime.ReadOnlyComposable @@ -98,7 +99,7 @@ fun Switch( style: SwitchStyle = Switch.style, enabled: Boolean = true, interactionSource: MutableInteractionSource = remember { MutableInteractionSource() }, - content: @Composable () -> Unit = {} + content: @Composable (RowScope.() -> Unit)? = null ) { val thumbDiameter = style.thumbRadius * 2 val maxOffsetX = with(LocalDensity.current) { (style.trackWidth - thumbDiameter - style.padding.horizontal).toPx() } @@ -183,10 +184,12 @@ fun Switch( ) } Row(modifier = Modifier.status(enabled).then(modifier)) { - Box( - modifier = Modifier.padding(end = style.contentSpacing) - .clickable(enabled = enabled) { onCheckedChange(!checked) } - ) { content() } + content?.also { content -> + Row(modifier = Modifier.clickable(enabled = enabled) { onCheckedChange(!checked) }) { + content() + Box(modifier = Modifier.width(style.contentSpacing)) + } + } Track { Thumb() } } }