refactor: make Modifier function composed

This commit is contained in:
2023-11-26 10:26:53 +08:00
parent bf47b9b052
commit 3d22d550a5
7 changed files with 101 additions and 80 deletions

View File

@@ -40,6 +40,7 @@ import androidx.compose.runtime.ReadOnlyComposable
import androidx.compose.runtime.compositionLocalOf
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.composed
import androidx.compose.ui.draw.clip
import androidx.compose.ui.draw.shadow
import androidx.compose.ui.graphics.Color
@@ -78,7 +79,7 @@ fun AreaBox(
LocalAreaBoxShape provides style.shape
) {
Box(
modifier = Modifier.box(style, color, modifier, initializer),
modifier = Modifier.areaBox(color, style, modifier, initializer),
contentAlignment = contentAlignment,
propagateMinConstraints = propagateMinConstraints,
content = content
@@ -101,7 +102,7 @@ fun AreaRow(
LocalAreaBoxShape provides style.shape
) {
Row(
modifier = Modifier.box(style, color, modifier, initializer),
modifier = Modifier.areaBox(color, style, modifier, initializer),
horizontalArrangement = horizontalArrangement,
verticalAlignment = verticalAlignment,
content = content
@@ -124,7 +125,7 @@ fun AreaColumn(
LocalAreaBoxShape provides style.shape
) {
Column(
modifier = Modifier.box(style, color, modifier, initializer),
modifier = Modifier.areaBox(color, style, modifier, initializer),
verticalArrangement = verticalArrangement,
horizontalAlignment = horizontalAlignment,
content = content
@@ -132,23 +133,25 @@ fun AreaColumn(
}
}
private fun Modifier.box(
style: AreaBoxStyle,
private fun Modifier.areaBox(
color: Color,
modifier: Modifier,
style: AreaBoxStyle,
then: Modifier,
initializer: Modifier.() -> Modifier
) = initializer()
.shadow(style.shadowSize, style.shape)
.clip(style.shape)
.background(color, style.shape)
.borderOrNot(style.border, style.shape)
.then(modifier)
.padding(
start = style.paddings.start,
top = style.paddings.top,
end = style.paddings.end,
bottom = style.paddings.bottom
)
) = composed {
initializer()
.shadow(style.shadowSize, style.shape)
.clip(style.shape)
.background(color, style.shape)
.borderOrNot(style.border, style.shape)
.then(then)
.padding(
start = style.paddings.start,
top = style.paddings.top,
end = style.paddings.end,
bottom = style.paddings.bottom
)
}
object AreaBox {
val color: Color

View File

@@ -38,6 +38,7 @@ import androidx.compose.runtime.ReadOnlyComposable
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.composed
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.Shape
@@ -92,7 +93,7 @@ fun Button(
enabled = enabled,
colors = colors,
style = style,
modifier = modifier
then = modifier
).rippleClickable(
enabled = enabled,
role = Role.Button,
@@ -133,7 +134,7 @@ fun IconButton(
enabled = enabled,
colors = colors,
style = style,
modifier = modifier
then = modifier
).rippleClickable(
rippleColor = colors.rippleColor,
bounded = false,
@@ -162,7 +163,7 @@ fun IconToggleButton(
enabled = enabled,
colors = colors,
style = style,
modifier = modifier
then = modifier
).rippleToggleable(
value = checked,
rippleColor = colors.rippleColor,
@@ -180,12 +181,14 @@ private fun Modifier.button(
enabled: Boolean,
colors: ButtonColors,
style: ButtonStyle,
modifier: Modifier
) = status(enabled)
.clip(style.shape)
.background(colors.backgroundColor, style.shape)
.borderOrNot(style.border, style.shape)
.then(modifier)
then: Modifier
) = composed {
status(enabled)
.clip(style.shape)
.background(colors.backgroundColor, style.shape)
.borderOrNot(style.border, style.shape)
.then(then)
}
private fun Modifier.buttonPadding(style: ButtonStyle) = padding(
start = style.paddings.start,

View File

@@ -69,6 +69,7 @@ import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.composed
import androidx.compose.ui.draw.clip
import androidx.compose.ui.focus.FocusDirection
import androidx.compose.ui.focus.FocusManager
@@ -193,7 +194,7 @@ fun DropdownList(
enabled = enabled,
focusRequester = focusRequester,
interactionSource = interactionSource,
modifier = modifier.rippleClickable(
then = modifier.rippleClickable(
enabled = enabled,
role = Role.DropdownList,
interactionSource = interactionSource
@@ -416,21 +417,23 @@ private fun Modifier.dropdownList(
enabled: Boolean,
focusRequester: FocusRequester,
interactionSource: MutableInteractionSource,
modifier: Modifier
) = status(enabled)
.focusRequester(focusRequester)
.focusable(enabled, interactionSource)
.hoverable(interactionSource, enabled)
.clip(style.shape)
.background(colors.backgroundColor, style.shape)
.borderOrNot(border, style.shape)
.then(modifier)
.padding(
start = style.paddings.start,
top = style.paddings.top,
end = style.paddings.end,
bottom = style.paddings.bottom
)
then: Modifier
) = composed {
status(enabled)
.focusRequester(focusRequester)
.focusable(enabled, interactionSource)
.hoverable(interactionSource, enabled)
.clip(style.shape)
.background(colors.backgroundColor, style.shape)
.borderOrNot(border, style.shape)
.then(then)
.padding(
start = style.paddings.start,
top = style.paddings.top,
end = style.paddings.end,
bottom = style.paddings.bottom
)
}
private fun calculateTransformOrigin(parentBounds: IntRect, menuBounds: IntRect): TransformOrigin {
val pivotX = when {

View File

@@ -29,6 +29,7 @@ import androidx.compose.runtime.Composable
import androidx.compose.runtime.ReadOnlyComposable
import androidx.compose.runtime.compositionLocalOf
import androidx.compose.ui.Modifier
import androidx.compose.ui.composed
import androidx.compose.ui.draw.paint
import androidx.compose.ui.geometry.Size
import androidx.compose.ui.graphics.Color
@@ -84,12 +85,13 @@ fun Icon(
internal val LocalIconTint = compositionLocalOf { Color.Unspecified }
@Composable
private fun Modifier.defaultSizeFor(painter: Painter) = then(
if (painter.intrinsicSize == Size.Unspecified || painter.intrinsicSize.isInfinite())
Modifier.size(defaultIconSize())
else Modifier
)
private fun Modifier.defaultSizeFor(painter: Painter) = composed {
then(
if (painter.intrinsicSize == Size.Unspecified || painter.intrinsicSize.isInfinite())
Modifier.size(defaultIconSize())
else Modifier
)
}
@Composable
@ReadOnlyComposable

View File

@@ -32,6 +32,7 @@ import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.Immutable
import androidx.compose.runtime.ReadOnlyComposable
import androidx.compose.ui.Modifier
import androidx.compose.ui.composed
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.Dp
import com.highcapable.flexiui.LocalColors
@@ -72,17 +73,19 @@ fun Surface(
private fun Modifier.surface(
colors: SurfaceColors,
style: SurfaceStyle,
modifier: Modifier,
then: Modifier,
initializer: Modifier.() -> Modifier
) = initializer()
.background(colors.backgroundColor)
.then(modifier)
.padding(
start = style.paddings.start,
top = style.paddings.top,
end = style.paddings.end,
bottom = style.paddings.bottom
)
) = composed {
initializer()
.background(colors.backgroundColor)
.then(then)
.padding(
start = style.paddings.start,
top = style.paddings.top,
end = style.paddings.end,
bottom = style.paddings.bottom
)
}
object Surface {
val colors: SurfaceColors

View File

@@ -58,6 +58,7 @@ import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.composed
import androidx.compose.ui.draw.alpha
import androidx.compose.ui.draw.clip
import androidx.compose.ui.focus.FocusRequester
@@ -184,7 +185,7 @@ fun TextField(
border = border,
enabled = enabled,
interactionSource = interactionSource,
modifier = modifier
then = modifier
).pointerHoverState(TextFieldPointerState.TEXT)
) {
// Note: If minWidth is not 0, a constant width is currently set.
@@ -716,29 +717,33 @@ private fun Modifier.textField(
border: BorderStroke,
enabled: Boolean,
interactionSource: MutableInteractionSource,
modifier: Modifier
) = status(enabled)
.focusable(enabled, interactionSource)
.hoverable(interactionSource, enabled)
.clip(style.shape)
.background(colors.backgroundColor, style.shape)
.borderOrNot(border, style.shape)
.then(modifier)
then: Modifier
) = composed {
status(enabled)
.focusable(enabled, interactionSource)
.hoverable(interactionSource, enabled)
.clip(style.shape)
.background(colors.backgroundColor, style.shape)
.borderOrNot(border, style.shape)
.then(then)
}
private fun Modifier.textFieldPadding(
style: TextFieldStyle,
fitStart: Boolean = false,
fitEnd: Boolean = false
) = when {
!fitStart && !fitEnd -> padding(
start = style.paddings.start,
top = style.paddings.top,
end = style.paddings.end,
bottom = style.paddings.bottom
)
fitStart -> padding(start = style.paddings.start)
fitEnd -> padding(end = style.paddings.end)
else -> this
) = composed {
when {
!fitStart && !fitEnd -> padding(
start = style.paddings.start,
top = style.paddings.top,
end = style.paddings.end,
bottom = style.paddings.bottom
)
fitStart -> padding(start = style.paddings.start)
fitEnd -> padding(end = style.paddings.end)
else -> this
}
}
object TextField {

View File

@@ -24,11 +24,12 @@
package com.highcapable.flexiui.component
import androidx.compose.ui.Modifier
import androidx.compose.ui.composed
import androidx.compose.ui.input.pointer.PointerIcon
import androidx.compose.ui.input.pointer.pointerHoverIcon
import java.awt.Cursor
internal actual fun Modifier.pointerHoverState(state: TextFieldPointerState) =
internal actual fun Modifier.pointerHoverState(state: TextFieldPointerState) = composed {
pointerHoverIcon(
PointerIcon(
Cursor.getPredefinedCursor(when (state) {
@@ -36,4 +37,5 @@ internal actual fun Modifier.pointerHoverState(state: TextFieldPointerState) =
TextFieldPointerState.TEXT -> Cursor.TEXT_CURSOR
})
)
)
)
}