From 3d22d550a521699ba5a764a250f306e730f14eb8 Mon Sep 17 00:00:00 2001 From: fankesyooni Date: Sun, 26 Nov 2023 10:26:53 +0800 Subject: [PATCH] refactor: make Modifier function composed --- .../highcapable/flexiui/component/AreaBox.kt | 39 +++++++++-------- .../highcapable/flexiui/component/Button.kt | 21 +++++---- .../highcapable/flexiui/component/Dropdown.kt | 35 ++++++++------- .../com/highcapable/flexiui/component/Icon.kt | 14 +++--- .../highcapable/flexiui/component/Surface.kt | 23 +++++----- .../flexiui/component/TextField.kt | 43 +++++++++++-------- .../flexiui/component/TextField.desktop.kt | 6 ++- 7 files changed, 101 insertions(+), 80 deletions(-) diff --git a/flexiui-core/src/commonMain/kotlin/com/highcapable/flexiui/component/AreaBox.kt b/flexiui-core/src/commonMain/kotlin/com/highcapable/flexiui/component/AreaBox.kt index ee7e7c7..a53a064 100644 --- a/flexiui-core/src/commonMain/kotlin/com/highcapable/flexiui/component/AreaBox.kt +++ b/flexiui-core/src/commonMain/kotlin/com/highcapable/flexiui/component/AreaBox.kt @@ -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 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 ff0fb00..83c622d 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 @@ -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, diff --git a/flexiui-core/src/commonMain/kotlin/com/highcapable/flexiui/component/Dropdown.kt b/flexiui-core/src/commonMain/kotlin/com/highcapable/flexiui/component/Dropdown.kt index c32ad4f..89ded40 100644 --- a/flexiui-core/src/commonMain/kotlin/com/highcapable/flexiui/component/Dropdown.kt +++ b/flexiui-core/src/commonMain/kotlin/com/highcapable/flexiui/component/Dropdown.kt @@ -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 { diff --git a/flexiui-core/src/commonMain/kotlin/com/highcapable/flexiui/component/Icon.kt b/flexiui-core/src/commonMain/kotlin/com/highcapable/flexiui/component/Icon.kt index 247d76d..31e59f2 100644 --- a/flexiui-core/src/commonMain/kotlin/com/highcapable/flexiui/component/Icon.kt +++ b/flexiui-core/src/commonMain/kotlin/com/highcapable/flexiui/component/Icon.kt @@ -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 diff --git a/flexiui-core/src/commonMain/kotlin/com/highcapable/flexiui/component/Surface.kt b/flexiui-core/src/commonMain/kotlin/com/highcapable/flexiui/component/Surface.kt index cff42b7..e6529bb 100644 --- a/flexiui-core/src/commonMain/kotlin/com/highcapable/flexiui/component/Surface.kt +++ b/flexiui-core/src/commonMain/kotlin/com/highcapable/flexiui/component/Surface.kt @@ -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 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 d6e59bc..37cfef2 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 @@ -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 { diff --git a/flexiui-core/src/desktopMain/kotlin/com/highcapable/flexiui/component/TextField.desktop.kt b/flexiui-core/src/desktopMain/kotlin/com/highcapable/flexiui/component/TextField.desktop.kt index d35702e..c660a4b 100644 --- a/flexiui-core/src/desktopMain/kotlin/com/highcapable/flexiui/component/TextField.desktop.kt +++ b/flexiui-core/src/desktopMain/kotlin/com/highcapable/flexiui/component/TextField.desktop.kt @@ -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 }) ) - ) \ No newline at end of file + ) +} \ No newline at end of file