mirror of
https://github.com/BetterAndroid/FlexiUI.git
synced 2025-09-08 11:34:18 +08:00
style: optimize code
This commit is contained in:
@@ -26,8 +26,6 @@ package com.highcapable.flexiui.component
|
|||||||
import android.graphics.Rect
|
import android.graphics.Rect
|
||||||
import android.view.View
|
import android.view.View
|
||||||
import android.view.ViewTreeObserver
|
import android.view.ViewTreeObserver
|
||||||
import androidx.compose.foundation.BorderStroke
|
|
||||||
import androidx.compose.foundation.interaction.MutableInteractionSource
|
|
||||||
import androidx.compose.foundation.layout.BoxWithConstraints
|
import androidx.compose.foundation.layout.BoxWithConstraints
|
||||||
import androidx.compose.foundation.layout.BoxWithConstraintsScope
|
import androidx.compose.foundation.layout.BoxWithConstraintsScope
|
||||||
import androidx.compose.runtime.Composable
|
import androidx.compose.runtime.Composable
|
||||||
@@ -40,41 +38,21 @@ import androidx.compose.ui.layout.boundsInWindow
|
|||||||
import androidx.compose.ui.layout.onGloballyPositioned
|
import androidx.compose.ui.layout.onGloballyPositioned
|
||||||
import androidx.compose.ui.node.Ref
|
import androidx.compose.ui.node.Ref
|
||||||
import androidx.compose.ui.platform.LocalView
|
import androidx.compose.ui.platform.LocalView
|
||||||
import androidx.compose.ui.semantics.Role
|
|
||||||
import com.highcapable.flexiui.interaction.rippleClickable
|
|
||||||
import kotlin.math.max
|
import kotlin.math.max
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
internal actual fun DropdownListBox(
|
internal actual fun DropdownListBox(
|
||||||
expanded: Boolean,
|
|
||||||
onExpandedChange: (Boolean) -> Unit,
|
|
||||||
modifier: Modifier,
|
modifier: Modifier,
|
||||||
colors: DropdownListColors,
|
|
||||||
style: DropdownListStyle,
|
|
||||||
border: BorderStroke,
|
|
||||||
enabled: Boolean,
|
|
||||||
interactionSource: MutableInteractionSource,
|
|
||||||
menuHeightPx: (Int) -> Unit,
|
menuHeightPx: (Int) -> Unit,
|
||||||
content: @Composable @UiComposable BoxWithConstraintsScope.() -> Unit
|
content: @Composable @UiComposable BoxWithConstraintsScope.() -> Unit
|
||||||
) {
|
) {
|
||||||
val view = LocalView.current
|
val view = LocalView.current
|
||||||
val coordinates = remember { Ref<LayoutCoordinates>() }
|
val coordinates = remember { Ref<LayoutCoordinates>() }
|
||||||
BoxWithConstraints(
|
BoxWithConstraints(
|
||||||
modifier = modifier.dropdownList(
|
modifier = modifier.onGloballyPositioned {
|
||||||
colors = colors,
|
coordinates.value = it
|
||||||
style = style,
|
updateHeight(view.rootView, coordinates.value) { newHeight -> menuHeightPx(newHeight) }
|
||||||
border = border,
|
},
|
||||||
enabled = enabled,
|
|
||||||
interactionSource = interactionSource,
|
|
||||||
modifier = modifier.rippleClickable(
|
|
||||||
enabled = enabled,
|
|
||||||
role = Role.DropdownList,
|
|
||||||
interactionSource = interactionSource
|
|
||||||
) { onExpandedChange(!expanded) }.onGloballyPositioned {
|
|
||||||
coordinates.value = it
|
|
||||||
updateHeight(view.rootView, coordinates.value) { newHeight -> menuHeightPx(newHeight) }
|
|
||||||
}
|
|
||||||
),
|
|
||||||
content = content
|
content = content
|
||||||
)
|
)
|
||||||
DisposableEffect(view) {
|
DisposableEffect(view) {
|
||||||
|
@@ -180,14 +180,18 @@ fun DropdownList(
|
|||||||
else -> style.borderInactive
|
else -> style.borderInactive
|
||||||
}.copy(animatedBorderWidth, SolidColor(animatedBorderColor))
|
}.copy(animatedBorderWidth, SolidColor(animatedBorderColor))
|
||||||
DropdownListBox(
|
DropdownListBox(
|
||||||
expanded = expanded,
|
modifier = modifier.dropdownList(
|
||||||
onExpandedChange = onExpandedChange,
|
colors = colors,
|
||||||
modifier = modifier,
|
style = style,
|
||||||
colors = colors,
|
border = border,
|
||||||
style = style,
|
enabled = enabled,
|
||||||
border = border,
|
interactionSource = interactionSource,
|
||||||
enabled = enabled,
|
modifier = modifier.rippleClickable(
|
||||||
interactionSource = interactionSource,
|
enabled = enabled,
|
||||||
|
role = Role.DropdownList,
|
||||||
|
interactionSource = interactionSource
|
||||||
|
) { onExpandedChange(!expanded) }
|
||||||
|
),
|
||||||
menuHeightPx = { menuHeightPx = it }
|
menuHeightPx = { menuHeightPx = it }
|
||||||
) {
|
) {
|
||||||
val menuWidth = maxWidth + startPadding + endPadding
|
val menuWidth = maxWidth + startPadding + endPadding
|
||||||
@@ -352,19 +356,12 @@ private fun DropdownMenuContent(
|
|||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
internal expect fun DropdownListBox(
|
internal expect fun DropdownListBox(
|
||||||
expanded: Boolean,
|
|
||||||
onExpandedChange: (Boolean) -> Unit,
|
|
||||||
modifier: Modifier,
|
modifier: Modifier,
|
||||||
colors: DropdownListColors,
|
|
||||||
style: DropdownListStyle,
|
|
||||||
border: BorderStroke,
|
|
||||||
enabled: Boolean,
|
|
||||||
interactionSource: MutableInteractionSource,
|
|
||||||
menuHeightPx: (Int) -> Unit,
|
menuHeightPx: (Int) -> Unit,
|
||||||
content: @Composable @UiComposable BoxWithConstraintsScope.() -> Unit
|
content: @Composable @UiComposable BoxWithConstraintsScope.() -> Unit
|
||||||
)
|
)
|
||||||
|
|
||||||
internal fun Modifier.dropdownList(
|
private fun Modifier.dropdownList(
|
||||||
colors: DropdownListColors,
|
colors: DropdownListColors,
|
||||||
style: DropdownListStyle,
|
style: DropdownListStyle,
|
||||||
border: BorderStroke,
|
border: BorderStroke,
|
||||||
|
@@ -23,8 +23,6 @@
|
|||||||
|
|
||||||
package com.highcapable.flexiui.component
|
package com.highcapable.flexiui.component
|
||||||
|
|
||||||
import androidx.compose.foundation.BorderStroke
|
|
||||||
import androidx.compose.foundation.interaction.MutableInteractionSource
|
|
||||||
import androidx.compose.foundation.layout.BoxWithConstraints
|
import androidx.compose.foundation.layout.BoxWithConstraints
|
||||||
import androidx.compose.foundation.layout.BoxWithConstraintsScope
|
import androidx.compose.foundation.layout.BoxWithConstraintsScope
|
||||||
import androidx.compose.runtime.Composable
|
import androidx.compose.runtime.Composable
|
||||||
@@ -33,44 +31,24 @@ import androidx.compose.ui.UiComposable
|
|||||||
import androidx.compose.ui.layout.boundsInWindow
|
import androidx.compose.ui.layout.boundsInWindow
|
||||||
import androidx.compose.ui.layout.onGloballyPositioned
|
import androidx.compose.ui.layout.onGloballyPositioned
|
||||||
import androidx.compose.ui.platform.LocalWindowInfo
|
import androidx.compose.ui.platform.LocalWindowInfo
|
||||||
import androidx.compose.ui.semantics.Role
|
|
||||||
import androidx.compose.ui.unit.toIntRect
|
import androidx.compose.ui.unit.toIntRect
|
||||||
import com.highcapable.flexiui.interaction.rippleClickable
|
|
||||||
import kotlin.math.max
|
import kotlin.math.max
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
internal actual fun DropdownListBox(
|
internal actual fun DropdownListBox(
|
||||||
expanded: Boolean,
|
|
||||||
onExpandedChange: (Boolean) -> Unit,
|
|
||||||
modifier: Modifier,
|
modifier: Modifier,
|
||||||
colors: DropdownListColors,
|
|
||||||
style: DropdownListStyle,
|
|
||||||
border: BorderStroke,
|
|
||||||
enabled: Boolean,
|
|
||||||
interactionSource: MutableInteractionSource,
|
|
||||||
menuHeightPx: (Int) -> Unit,
|
menuHeightPx: (Int) -> Unit,
|
||||||
content: @Composable @UiComposable BoxWithConstraintsScope.() -> Unit
|
content: @Composable @UiComposable BoxWithConstraintsScope.() -> Unit
|
||||||
) {
|
) {
|
||||||
val windowInfo = LocalWindowInfo.current
|
val windowInfo = LocalWindowInfo.current
|
||||||
BoxWithConstraints(
|
BoxWithConstraints(
|
||||||
modifier = modifier.dropdownList(
|
modifier = modifier.onGloballyPositioned {
|
||||||
colors = colors,
|
val boundsInWindow = it.boundsInWindow()
|
||||||
style = style,
|
val visibleWindowBounds = windowInfo.containerSize.toIntRect()
|
||||||
border = border,
|
val heightAbove = boundsInWindow.top - visibleWindowBounds.top
|
||||||
enabled = enabled,
|
val heightBelow = visibleWindowBounds.height - boundsInWindow.bottom
|
||||||
interactionSource = interactionSource,
|
menuHeightPx(max(heightAbove, heightBelow).toInt())
|
||||||
modifier = modifier.rippleClickable(
|
},
|
||||||
enabled = enabled,
|
|
||||||
role = Role.DropdownList,
|
|
||||||
interactionSource = interactionSource
|
|
||||||
) { onExpandedChange(!expanded) }.onGloballyPositioned {
|
|
||||||
val boundsInWindow = it.boundsInWindow()
|
|
||||||
val visibleWindowBounds = windowInfo.containerSize.toIntRect()
|
|
||||||
val heightAbove = boundsInWindow.top - visibleWindowBounds.top
|
|
||||||
val heightBelow = visibleWindowBounds.height - boundsInWindow.bottom
|
|
||||||
menuHeightPx(max(heightAbove, heightBelow).toInt())
|
|
||||||
}
|
|
||||||
),
|
|
||||||
content = content
|
content = content
|
||||||
)
|
)
|
||||||
}
|
}
|
@@ -23,8 +23,6 @@
|
|||||||
|
|
||||||
package com.highcapable.flexiui.component
|
package com.highcapable.flexiui.component
|
||||||
|
|
||||||
import androidx.compose.foundation.BorderStroke
|
|
||||||
import androidx.compose.foundation.interaction.MutableInteractionSource
|
|
||||||
import androidx.compose.foundation.layout.BoxWithConstraints
|
import androidx.compose.foundation.layout.BoxWithConstraints
|
||||||
import androidx.compose.foundation.layout.BoxWithConstraintsScope
|
import androidx.compose.foundation.layout.BoxWithConstraintsScope
|
||||||
import androidx.compose.runtime.Composable
|
import androidx.compose.runtime.Composable
|
||||||
@@ -33,44 +31,24 @@ import androidx.compose.ui.UiComposable
|
|||||||
import androidx.compose.ui.layout.boundsInWindow
|
import androidx.compose.ui.layout.boundsInWindow
|
||||||
import androidx.compose.ui.layout.onGloballyPositioned
|
import androidx.compose.ui.layout.onGloballyPositioned
|
||||||
import androidx.compose.ui.platform.LocalWindowInfo
|
import androidx.compose.ui.platform.LocalWindowInfo
|
||||||
import androidx.compose.ui.semantics.Role
|
|
||||||
import androidx.compose.ui.unit.toIntRect
|
import androidx.compose.ui.unit.toIntRect
|
||||||
import com.highcapable.flexiui.interaction.rippleClickable
|
|
||||||
import kotlin.math.max
|
import kotlin.math.max
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
internal actual fun DropdownListBox(
|
internal actual fun DropdownListBox(
|
||||||
expanded: Boolean,
|
|
||||||
onExpandedChange: (Boolean) -> Unit,
|
|
||||||
modifier: Modifier,
|
modifier: Modifier,
|
||||||
colors: DropdownListColors,
|
|
||||||
style: DropdownListStyle,
|
|
||||||
border: BorderStroke,
|
|
||||||
enabled: Boolean,
|
|
||||||
interactionSource: MutableInteractionSource,
|
|
||||||
menuHeightPx: (Int) -> Unit,
|
menuHeightPx: (Int) -> Unit,
|
||||||
content: @Composable @UiComposable BoxWithConstraintsScope.() -> Unit
|
content: @Composable @UiComposable BoxWithConstraintsScope.() -> Unit
|
||||||
) {
|
) {
|
||||||
val windowInfo = LocalWindowInfo.current
|
val windowInfo = LocalWindowInfo.current
|
||||||
BoxWithConstraints(
|
BoxWithConstraints(
|
||||||
modifier = modifier.dropdownList(
|
modifier = modifier.onGloballyPositioned {
|
||||||
colors = colors,
|
val boundsInWindow = it.boundsInWindow()
|
||||||
style = style,
|
val visibleWindowBounds = windowInfo.containerSize.toIntRect()
|
||||||
border = border,
|
val heightAbove = boundsInWindow.top - visibleWindowBounds.top
|
||||||
enabled = enabled,
|
val heightBelow = visibleWindowBounds.height - boundsInWindow.bottom
|
||||||
interactionSource = interactionSource,
|
menuHeightPx(max(heightAbove, heightBelow).toInt())
|
||||||
modifier = modifier.rippleClickable(
|
},
|
||||||
enabled = enabled,
|
|
||||||
role = Role.DropdownList,
|
|
||||||
interactionSource = interactionSource
|
|
||||||
) { onExpandedChange(!expanded) }.onGloballyPositioned {
|
|
||||||
val boundsInWindow = it.boundsInWindow()
|
|
||||||
val visibleWindowBounds = windowInfo.containerSize.toIntRect()
|
|
||||||
val heightAbove = boundsInWindow.top - visibleWindowBounds.top
|
|
||||||
val heightBelow = visibleWindowBounds.height - boundsInWindow.bottom
|
|
||||||
menuHeightPx(max(heightAbove, heightBelow).toInt())
|
|
||||||
}
|
|
||||||
),
|
|
||||||
content = content
|
content = content
|
||||||
)
|
)
|
||||||
}
|
}
|
Reference in New Issue
Block a user