diff --git a/flexiui-core/src/commonMain/kotlin/com/highcapable/flexiui/component/Button.kt b/flexiui-core/src/commonMain/kotlin/com/highcapable/flexiui/component/Button.kt index 0a67df7..e073d3c 100644 --- a/flexiui-core/src/commonMain/kotlin/com/highcapable/flexiui/component/Button.kt +++ b/flexiui-core/src/commonMain/kotlin/com/highcapable/flexiui/component/Button.kt @@ -37,7 +37,6 @@ import androidx.compose.runtime.ReadOnlyComposable import androidx.compose.runtime.remember import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier -import androidx.compose.ui.draw.alpha import androidx.compose.ui.draw.clip import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.Shape @@ -50,6 +49,7 @@ import com.highcapable.flexiui.interaction.rippleClickable import com.highcapable.flexiui.interaction.rippleToggleable import com.highcapable.flexiui.utils.borderOrNot import com.highcapable.flexiui.utils.orElse +import com.highcapable.flexiui.utils.status @Immutable data class ButtonColors( @@ -81,22 +81,24 @@ fun Button( footer: @Composable () -> Unit = {}, content: @Composable RowScope.() -> Unit ) { - var sModifier = modifier.clip(style.shape) - sModifier = if (enabled) sModifier.rippleClickable( - enabled = enabled, - role = Role.Button, - rippleColor = colors.rippleColor, - interactionSource = interactionSource, - onClick = onClick - ) else sModifier.alpha(0.5f) - sModifier = sModifier.background(colors.backgroundColor, style.shape) - sModifier = sModifier.borderOrNot(style.border, style.shape) val localTextStyle = LocalTextStyle.current.copy(color = colors.contentColor) val localProgressIndicatorColors = LocalProgressIndicatorColors.current.copy( foregroundColor = colors.contentColor, backgroundColor = Color.Transparent ) - Box(modifier = sModifier) { + Box( + modifier = modifier.status(enabled) + .clip(style.shape) + .background(colors.backgroundColor, style.shape) + .borderOrNot(style.border, style.shape) + .rippleClickable( + enabled = enabled, + role = Role.Button, + rippleColor = colors.rippleColor, + interactionSource = interactionSource, + onClick = onClick + ) + ) { CompositionLocalProvider( LocalTextStyle provides localTextStyle, LocalProgressIndicatorColors provides localProgressIndicatorColors 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 346d128..b57d090 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 @@ -56,6 +56,7 @@ import com.highcapable.flexiui.interaction.clickable import com.highcapable.flexiui.resources.Icons import com.highcapable.flexiui.resources.icon.CheckMark import com.highcapable.flexiui.utils.borderOrNot +import com.highcapable.flexiui.utils.status @Immutable data class CheckBoxColors( @@ -93,8 +94,7 @@ fun CheckBox( val animatedContentScale by animateFloatAsState(if (hovered) style.hoveredGain else 1f) val animatedContentAlpha by animateFloatAsState(if (checked) 1f else 0f) val animatedContentLayer by animateFloatAsState(if (checked) 1f else 0f) - val sModifier = if (enabled) modifier else modifier.alpha(0.5f) - Row(modifier = sModifier, verticalAlignment = Alignment.CenterVertically) { + Row(modifier = modifier.status(enabled), verticalAlignment = Alignment.CenterVertically) { Box( modifier = Modifier.clickable( interactionSource = interactionSource, 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 c8803ab..e5e705f 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 @@ -54,6 +54,7 @@ import androidx.compose.ui.unit.dp import com.highcapable.flexiui.LocalColors import com.highcapable.flexiui.LocalSizes import com.highcapable.flexiui.interaction.clickable +import com.highcapable.flexiui.utils.status @Immutable data class RadioButtonColors( @@ -92,8 +93,7 @@ fun RadioButton( val animatedContentScale by animateFloatAsState(if (hovered) style.hoveredGain else 1f) val animatedContentShadow by animateDpAsState(if (selected) style.contentShadowSize else 0.dp) val animatedContentAlpha by animateFloatAsState(if (selected) 1f else 0f) - val sModifier = if (enabled) modifier else modifier.alpha(0.5f) - Row(modifier = sModifier, verticalAlignment = Alignment.CenterVertically) { + Row(modifier = modifier.status(enabled), verticalAlignment = Alignment.CenterVertically) { Box( modifier = Modifier.clickable( interactionSource = interactionSource, 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 6fbe9c6..2f75d1e 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 @@ -48,7 +48,6 @@ import androidx.compose.runtime.remember import androidx.compose.runtime.setValue import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier -import androidx.compose.ui.draw.alpha import androidx.compose.ui.draw.scale import androidx.compose.ui.draw.shadow import androidx.compose.ui.graphics.Color @@ -64,6 +63,7 @@ import com.highcapable.flexiui.LocalShapes import com.highcapable.flexiui.LocalSizes import com.highcapable.flexiui.interaction.clickable import com.highcapable.flexiui.utils.borderOrNot +import com.highcapable.flexiui.utils.status import kotlin.math.roundToInt @Immutable @@ -117,7 +117,6 @@ fun Switch( updateTrackColor() val animatedTrackColor by animateColorAsState(trackColor) val efficientDragging = dragging && distance > 5 - val sModifier = if (enabled) modifier else modifier.alpha(0.5f) @Composable fun Track(content: @Composable RowScope.() -> Unit) { @@ -181,7 +180,7 @@ fun Switch( ) ) } - Row(modifier = sModifier) { + Row(modifier = modifier.status(enabled)) { Box( modifier = Modifier.padding(end = contentSpacing) .clickable(enabled = enabled) { onCheckedChange(!checked) } diff --git a/flexiui-core/src/commonMain/kotlin/com/highcapable/flexiui/component/TextField.kt b/flexiui-core/src/commonMain/kotlin/com/highcapable/flexiui/component/TextField.kt index 78e568e..580575f 100644 --- a/flexiui-core/src/commonMain/kotlin/com/highcapable/flexiui/component/TextField.kt +++ b/flexiui-core/src/commonMain/kotlin/com/highcapable/flexiui/component/TextField.kt @@ -62,6 +62,7 @@ import com.highcapable.flexiui.LocalShapes import com.highcapable.flexiui.LocalSizes import com.highcapable.flexiui.utils.borderOrNot import com.highcapable.flexiui.utils.orElse +import com.highcapable.flexiui.utils.status // TODO: Preset text boxes (password text box, text box with delete button, etc.) @@ -265,20 +266,17 @@ private fun Modifier.textField( border: BorderStroke, enabled: Boolean, interactionSource: MutableInteractionSource -): Modifier { - var sModifier = hoverable(interactionSource) - .clip(style.shape) - .background(colors.backgroundColor, style.shape) - .borderOrNot(border, style.shape) - .padding( - top = style.topPadding.orElse() ?: style.padding, - start = style.startPadding.orElse() ?: style.padding, - bottom = style.bottomPadding.orElse() ?: style.padding, - end = style.endPadding.orElse() ?: style.padding - ) - if (!enabled) sModifier = sModifier.alpha(0.5f) - return sModifier -} +) = status(enabled) + .hoverable(interactionSource) + .clip(style.shape) + .background(colors.backgroundColor, style.shape) + .borderOrNot(border, style.shape) + .padding( + top = style.topPadding.orElse() ?: style.padding, + start = style.startPadding.orElse() ?: style.padding, + bottom = style.bottomPadding.orElse() ?: style.padding, + end = style.endPadding.orElse() ?: style.padding + ) object TextField { val colors: TextFieldColors diff --git a/flexiui-core/src/commonMain/kotlin/com/highcapable/flexiui/utils/Modifier.kt b/flexiui-core/src/commonMain/kotlin/com/highcapable/flexiui/utils/Modifier.kt index ef21b19..2116659 100644 --- a/flexiui-core/src/commonMain/kotlin/com/highcapable/flexiui/utils/Modifier.kt +++ b/flexiui-core/src/commonMain/kotlin/com/highcapable/flexiui/utils/Modifier.kt @@ -1,12 +1,39 @@ +/* + * Flexi UI - A flexible and useful UI component library. + * Copyright (C) 2019-2023 HighCapable + * https://github.com/BetterAndroid/FlexiUI + * + * Apache License Version 2.0 + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * This file is created by fankes on 2023/11/14. + */ +@file:Suppress("unused") + package com.highcapable.flexiui.utils import androidx.compose.foundation.BorderStroke import androidx.compose.foundation.border import androidx.compose.runtime.Stable import androidx.compose.ui.Modifier +import androidx.compose.ui.draw.alpha import androidx.compose.ui.graphics.RectangleShape import androidx.compose.ui.graphics.Shape import androidx.compose.ui.unit.dp +@Stable +fun Modifier.status(enabled: Boolean) = if (enabled) this else alpha(0.5f) + @Stable fun Modifier.borderOrNot(border: BorderStroke, shape: Shape = RectangleShape) = border.takeIf { it.width > 0.dp }?.let { border(it, shape) } ?: this \ No newline at end of file