mirror of
https://github.com/BetterAndroid/FlexiUI.git
synced 2025-09-08 11:34:18 +08:00
refactor: move disable alpha to status extension function
This commit is contained in:
@@ -37,7 +37,6 @@ import androidx.compose.runtime.ReadOnlyComposable
|
|||||||
import androidx.compose.runtime.remember
|
import androidx.compose.runtime.remember
|
||||||
import androidx.compose.ui.Alignment
|
import androidx.compose.ui.Alignment
|
||||||
import androidx.compose.ui.Modifier
|
import androidx.compose.ui.Modifier
|
||||||
import androidx.compose.ui.draw.alpha
|
|
||||||
import androidx.compose.ui.draw.clip
|
import androidx.compose.ui.draw.clip
|
||||||
import androidx.compose.ui.graphics.Color
|
import androidx.compose.ui.graphics.Color
|
||||||
import androidx.compose.ui.graphics.Shape
|
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.interaction.rippleToggleable
|
||||||
import com.highcapable.flexiui.utils.borderOrNot
|
import com.highcapable.flexiui.utils.borderOrNot
|
||||||
import com.highcapable.flexiui.utils.orElse
|
import com.highcapable.flexiui.utils.orElse
|
||||||
|
import com.highcapable.flexiui.utils.status
|
||||||
|
|
||||||
@Immutable
|
@Immutable
|
||||||
data class ButtonColors(
|
data class ButtonColors(
|
||||||
@@ -81,22 +81,24 @@ fun Button(
|
|||||||
footer: @Composable () -> Unit = {},
|
footer: @Composable () -> Unit = {},
|
||||||
content: @Composable RowScope.() -> 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 localTextStyle = LocalTextStyle.current.copy(color = colors.contentColor)
|
||||||
val localProgressIndicatorColors = LocalProgressIndicatorColors.current.copy(
|
val localProgressIndicatorColors = LocalProgressIndicatorColors.current.copy(
|
||||||
foregroundColor = colors.contentColor,
|
foregroundColor = colors.contentColor,
|
||||||
backgroundColor = Color.Transparent
|
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(
|
CompositionLocalProvider(
|
||||||
LocalTextStyle provides localTextStyle,
|
LocalTextStyle provides localTextStyle,
|
||||||
LocalProgressIndicatorColors provides localProgressIndicatorColors
|
LocalProgressIndicatorColors provides localProgressIndicatorColors
|
||||||
|
@@ -56,6 +56,7 @@ import com.highcapable.flexiui.interaction.clickable
|
|||||||
import com.highcapable.flexiui.resources.Icons
|
import com.highcapable.flexiui.resources.Icons
|
||||||
import com.highcapable.flexiui.resources.icon.CheckMark
|
import com.highcapable.flexiui.resources.icon.CheckMark
|
||||||
import com.highcapable.flexiui.utils.borderOrNot
|
import com.highcapable.flexiui.utils.borderOrNot
|
||||||
|
import com.highcapable.flexiui.utils.status
|
||||||
|
|
||||||
@Immutable
|
@Immutable
|
||||||
data class CheckBoxColors(
|
data class CheckBoxColors(
|
||||||
@@ -93,8 +94,7 @@ fun CheckBox(
|
|||||||
val animatedContentScale by animateFloatAsState(if (hovered) style.hoveredGain else 1f)
|
val animatedContentScale by animateFloatAsState(if (hovered) style.hoveredGain else 1f)
|
||||||
val animatedContentAlpha by animateFloatAsState(if (checked) 1f else 0f)
|
val animatedContentAlpha by animateFloatAsState(if (checked) 1f else 0f)
|
||||||
val animatedContentLayer 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 = modifier.status(enabled), verticalAlignment = Alignment.CenterVertically) {
|
||||||
Row(modifier = sModifier, verticalAlignment = Alignment.CenterVertically) {
|
|
||||||
Box(
|
Box(
|
||||||
modifier = Modifier.clickable(
|
modifier = Modifier.clickable(
|
||||||
interactionSource = interactionSource,
|
interactionSource = interactionSource,
|
||||||
|
@@ -54,6 +54,7 @@ import androidx.compose.ui.unit.dp
|
|||||||
import com.highcapable.flexiui.LocalColors
|
import com.highcapable.flexiui.LocalColors
|
||||||
import com.highcapable.flexiui.LocalSizes
|
import com.highcapable.flexiui.LocalSizes
|
||||||
import com.highcapable.flexiui.interaction.clickable
|
import com.highcapable.flexiui.interaction.clickable
|
||||||
|
import com.highcapable.flexiui.utils.status
|
||||||
|
|
||||||
@Immutable
|
@Immutable
|
||||||
data class RadioButtonColors(
|
data class RadioButtonColors(
|
||||||
@@ -92,8 +93,7 @@ fun RadioButton(
|
|||||||
val animatedContentScale by animateFloatAsState(if (hovered) style.hoveredGain else 1f)
|
val animatedContentScale by animateFloatAsState(if (hovered) style.hoveredGain else 1f)
|
||||||
val animatedContentShadow by animateDpAsState(if (selected) style.contentShadowSize else 0.dp)
|
val animatedContentShadow by animateDpAsState(if (selected) style.contentShadowSize else 0.dp)
|
||||||
val animatedContentAlpha by animateFloatAsState(if (selected) 1f else 0f)
|
val animatedContentAlpha by animateFloatAsState(if (selected) 1f else 0f)
|
||||||
val sModifier = if (enabled) modifier else modifier.alpha(0.5f)
|
Row(modifier = modifier.status(enabled), verticalAlignment = Alignment.CenterVertically) {
|
||||||
Row(modifier = sModifier, verticalAlignment = Alignment.CenterVertically) {
|
|
||||||
Box(
|
Box(
|
||||||
modifier = Modifier.clickable(
|
modifier = Modifier.clickable(
|
||||||
interactionSource = interactionSource,
|
interactionSource = interactionSource,
|
||||||
|
@@ -48,7 +48,6 @@ import androidx.compose.runtime.remember
|
|||||||
import androidx.compose.runtime.setValue
|
import androidx.compose.runtime.setValue
|
||||||
import androidx.compose.ui.Alignment
|
import androidx.compose.ui.Alignment
|
||||||
import androidx.compose.ui.Modifier
|
import androidx.compose.ui.Modifier
|
||||||
import androidx.compose.ui.draw.alpha
|
|
||||||
import androidx.compose.ui.draw.scale
|
import androidx.compose.ui.draw.scale
|
||||||
import androidx.compose.ui.draw.shadow
|
import androidx.compose.ui.draw.shadow
|
||||||
import androidx.compose.ui.graphics.Color
|
import androidx.compose.ui.graphics.Color
|
||||||
@@ -64,6 +63,7 @@ import com.highcapable.flexiui.LocalShapes
|
|||||||
import com.highcapable.flexiui.LocalSizes
|
import com.highcapable.flexiui.LocalSizes
|
||||||
import com.highcapable.flexiui.interaction.clickable
|
import com.highcapable.flexiui.interaction.clickable
|
||||||
import com.highcapable.flexiui.utils.borderOrNot
|
import com.highcapable.flexiui.utils.borderOrNot
|
||||||
|
import com.highcapable.flexiui.utils.status
|
||||||
import kotlin.math.roundToInt
|
import kotlin.math.roundToInt
|
||||||
|
|
||||||
@Immutable
|
@Immutable
|
||||||
@@ -117,7 +117,6 @@ fun Switch(
|
|||||||
updateTrackColor()
|
updateTrackColor()
|
||||||
val animatedTrackColor by animateColorAsState(trackColor)
|
val animatedTrackColor by animateColorAsState(trackColor)
|
||||||
val efficientDragging = dragging && distance > 5
|
val efficientDragging = dragging && distance > 5
|
||||||
val sModifier = if (enabled) modifier else modifier.alpha(0.5f)
|
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
fun Track(content: @Composable RowScope.() -> Unit) {
|
fun Track(content: @Composable RowScope.() -> Unit) {
|
||||||
@@ -181,7 +180,7 @@ fun Switch(
|
|||||||
)
|
)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
Row(modifier = sModifier) {
|
Row(modifier = modifier.status(enabled)) {
|
||||||
Box(
|
Box(
|
||||||
modifier = Modifier.padding(end = contentSpacing)
|
modifier = Modifier.padding(end = contentSpacing)
|
||||||
.clickable(enabled = enabled) { onCheckedChange(!checked) }
|
.clickable(enabled = enabled) { onCheckedChange(!checked) }
|
||||||
|
@@ -62,6 +62,7 @@ import com.highcapable.flexiui.LocalShapes
|
|||||||
import com.highcapable.flexiui.LocalSizes
|
import com.highcapable.flexiui.LocalSizes
|
||||||
import com.highcapable.flexiui.utils.borderOrNot
|
import com.highcapable.flexiui.utils.borderOrNot
|
||||||
import com.highcapable.flexiui.utils.orElse
|
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.)
|
// TODO: Preset text boxes (password text box, text box with delete button, etc.)
|
||||||
|
|
||||||
@@ -265,20 +266,17 @@ private fun Modifier.textField(
|
|||||||
border: BorderStroke,
|
border: BorderStroke,
|
||||||
enabled: Boolean,
|
enabled: Boolean,
|
||||||
interactionSource: MutableInteractionSource
|
interactionSource: MutableInteractionSource
|
||||||
): Modifier {
|
) = status(enabled)
|
||||||
var sModifier = hoverable(interactionSource)
|
.hoverable(interactionSource)
|
||||||
.clip(style.shape)
|
.clip(style.shape)
|
||||||
.background(colors.backgroundColor, style.shape)
|
.background(colors.backgroundColor, style.shape)
|
||||||
.borderOrNot(border, style.shape)
|
.borderOrNot(border, style.shape)
|
||||||
.padding(
|
.padding(
|
||||||
top = style.topPadding.orElse() ?: style.padding,
|
top = style.topPadding.orElse() ?: style.padding,
|
||||||
start = style.startPadding.orElse() ?: style.padding,
|
start = style.startPadding.orElse() ?: style.padding,
|
||||||
bottom = style.bottomPadding.orElse() ?: style.padding,
|
bottom = style.bottomPadding.orElse() ?: style.padding,
|
||||||
end = style.endPadding.orElse() ?: style.padding
|
end = style.endPadding.orElse() ?: style.padding
|
||||||
)
|
)
|
||||||
if (!enabled) sModifier = sModifier.alpha(0.5f)
|
|
||||||
return sModifier
|
|
||||||
}
|
|
||||||
|
|
||||||
object TextField {
|
object TextField {
|
||||||
val colors: TextFieldColors
|
val colors: TextFieldColors
|
||||||
|
@@ -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
|
package com.highcapable.flexiui.utils
|
||||||
|
|
||||||
import androidx.compose.foundation.BorderStroke
|
import androidx.compose.foundation.BorderStroke
|
||||||
import androidx.compose.foundation.border
|
import androidx.compose.foundation.border
|
||||||
import androidx.compose.runtime.Stable
|
import androidx.compose.runtime.Stable
|
||||||
import androidx.compose.ui.Modifier
|
import androidx.compose.ui.Modifier
|
||||||
|
import androidx.compose.ui.draw.alpha
|
||||||
import androidx.compose.ui.graphics.RectangleShape
|
import androidx.compose.ui.graphics.RectangleShape
|
||||||
import androidx.compose.ui.graphics.Shape
|
import androidx.compose.ui.graphics.Shape
|
||||||
import androidx.compose.ui.unit.dp
|
import androidx.compose.ui.unit.dp
|
||||||
|
|
||||||
|
@Stable
|
||||||
|
fun Modifier.status(enabled: Boolean) = if (enabled) this else alpha(0.5f)
|
||||||
|
|
||||||
@Stable
|
@Stable
|
||||||
fun Modifier.borderOrNot(border: BorderStroke, shape: Shape = RectangleShape) = border.takeIf { it.width > 0.dp }?.let { border(it, shape) } ?: this
|
fun Modifier.borderOrNot(border: BorderStroke, shape: Shape = RectangleShape) = border.takeIf { it.width > 0.dp }?.let { border(it, shape) } ?: this
|
Reference in New Issue
Block a user