Skip to content

Commit b7f939b

Browse files
committed
library: some improvements
1 parent 45fe52e commit b7f939b

File tree

14 files changed

+270
-216
lines changed

14 files changed

+270
-216
lines changed

iosApp/iosApp/Info.plist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
<key>CFBundleShortVersionString</key>
1818
<string>1.0.5</string>
1919
<key>CFBundleVersion</key>
20-
<string>592</string>
20+
<string>593</string>
2121
<key>LSRequiresIPhoneOS</key>
2222
<true/>
2323
<key>CADisableMinimumFrameDurationOnPhone</key>

miuix/src/androidMain/kotlin/top/yukonga/miuix/kmp/utils/Utils.android.kt

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import androidx.compose.runtime.derivedStateOf
1616
import androidx.compose.runtime.getValue
1717
import androidx.compose.runtime.remember
1818
import androidx.compose.runtime.rememberUpdatedState
19+
import androidx.compose.runtime.SideEffect
1920
import androidx.compose.ui.ExperimentalComposeUiApi
2021
import androidx.compose.ui.platform.LocalConfiguration
2122
import androidx.compose.ui.platform.LocalContext
@@ -100,15 +101,14 @@ actual fun PredictiveBackHandler(
100101
onBack: () -> Unit
101102
) {
102103
val backDispatcher = LocalOnBackPressedDispatcherOwner.current?.onBackPressedDispatcher
103-
104+
104105
val currentOnBackStarted by rememberUpdatedState(onBackStarted)
105106
val currentOnBackProgressed by rememberUpdatedState(onBackProgressed)
106107
val currentOnBackCancelled by rememberUpdatedState(onBackCancelled)
107108
val currentOnBack by rememberUpdatedState(onBack)
108-
val currentEnabled by rememberUpdatedState(enabled)
109-
110-
DisposableEffect(backDispatcher) {
111-
val callback = object : OnBackPressedCallback(currentEnabled) {
109+
110+
val callback = remember {
111+
object : OnBackPressedCallback(false) {
112112
override fun handleOnBackStarted(backEvent: AndroidBackEventCompat) {
113113
currentOnBackStarted?.invoke(
114114
BackEventCompat(
@@ -119,7 +119,7 @@ actual fun PredictiveBackHandler(
119119
)
120120
)
121121
}
122-
122+
123123
override fun handleOnBackProgressed(backEvent: AndroidBackEventCompat) {
124124
currentOnBackProgressed?.invoke(
125125
BackEventCompat(
@@ -130,25 +130,25 @@ actual fun PredictiveBackHandler(
130130
)
131131
)
132132
}
133-
133+
134134
override fun handleOnBackCancelled() {
135135
currentOnBackCancelled?.invoke()
136136
}
137-
137+
138138
override fun handleOnBackPressed() {
139139
currentOnBack()
140140
}
141141
}
142-
142+
}
143+
144+
SideEffect {
145+
callback.isEnabled = enabled
146+
}
147+
148+
DisposableEffect(backDispatcher) {
143149
backDispatcher?.addCallback(callback)
144-
145150
onDispose {
146151
callback.remove()
147152
}
148153
}
149-
150-
// Update enabled state when it changes
151-
DisposableEffect(enabled) {
152-
onDispose { }
153-
}
154154
}

miuix/src/commonMain/kotlin/top/yukonga/miuix/kmp/basic/ColorPalette.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ private fun PaletteCanvas(
182182
val totalColumns = hueColumns + if (includeGrayColumn) 1 else 0
183183
val rowSV = remember(rows) { buildRowSV(rows) }
184184
val grayV = remember(rows) { buildGrayV(rows) }
185-
val shape = ContinuousRoundedRectangle(cornerRadius)
185+
val shape = remember(cornerRadius) { ContinuousRoundedRectangle(cornerRadius) }
186186

187187
var sizePx by remember { mutableStateOf(IntSize.Zero) }
188188

miuix/src/commonMain/kotlin/top/yukonga/miuix/kmp/basic/Component.kt

Lines changed: 25 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import androidx.compose.foundation.LocalIndication
77
import androidx.compose.foundation.clickable
88
import androidx.compose.foundation.interaction.MutableInteractionSource
99
import androidx.compose.foundation.layout.Arrangement
10+
import androidx.compose.foundation.layout.Column
1011
import androidx.compose.foundation.layout.PaddingValues
1112
import androidx.compose.foundation.layout.Row
1213
import androidx.compose.foundation.layout.RowScope
@@ -22,7 +23,6 @@ import androidx.compose.runtime.remember
2223
import androidx.compose.ui.Alignment
2324
import androidx.compose.ui.Modifier
2425
import androidx.compose.ui.graphics.Color
25-
import androidx.compose.ui.layout.SubcomposeLayout
2626
import androidx.compose.ui.text.font.FontWeight
2727
import androidx.compose.ui.unit.dp
2828
import top.yukonga.miuix.kmp.interfaces.HoldDownInteraction
@@ -50,7 +50,7 @@ fun BasicComponent(
5050
titleColor: BasicComponentColors = BasicComponentDefaults.titleColor(),
5151
summary: String? = null,
5252
summaryColor: BasicComponentColors = BasicComponentDefaults.summaryColor(),
53-
leftAction: @Composable (() -> Unit?)? = null,
53+
leftAction: @Composable (() -> Unit)? = null,
5454
rightActions: @Composable RowScope.() -> Unit = {},
5555
modifier: Modifier = Modifier,
5656
insideMargin: PaddingValues = BasicComponentDefaults.InsideMargin,
@@ -77,7 +77,7 @@ fun BasicComponent(
7777
}
7878
}
7979

80-
val clickableModifier = remember(onClick, enabled, interactionSource) {
80+
val clickableModifier = remember(onClick, enabled, interactionSource, indication) {
8181
if (onClick != null && enabled) {
8282
Modifier.clickable(
8383
indication = indication,
@@ -87,85 +87,41 @@ fun BasicComponent(
8787
} else Modifier
8888
}
8989

90-
val titleContent: (@Composable () -> Unit)? = remember(title, enabled, titleColor) {
91-
title?.let { text ->
92-
{
90+
Row(
91+
modifier = modifier
92+
.heightIn(min = 56.dp)
93+
.fillMaxWidth()
94+
.then(clickableModifier)
95+
.padding(insideMargin),
96+
verticalAlignment = Alignment.CenterVertically
97+
) {
98+
// Left action (optional)
99+
leftAction?.let { it() }
100+
101+
// Content
102+
Column(
103+
modifier = Modifier.weight(1f),
104+
verticalArrangement = Arrangement.Center
105+
) {
106+
if (title != null) {
93107
Text(
94-
text = text,
108+
text = title,
95109
fontSize = MiuixTheme.textStyles.headline1.fontSize,
96110
fontWeight = FontWeight.Medium,
97111
color = titleColor.color(enabled)
98112
)
99113
}
100-
}
101-
}
102-
103-
val summaryContent: (@Composable () -> Unit)? = remember(summary, enabled, summaryColor) {
104-
summary?.let { text ->
105-
{
114+
if (summary != null) {
106115
Text(
107-
text = text,
116+
text = summary,
108117
fontSize = MiuixTheme.textStyles.body2.fontSize,
109118
color = summaryColor.color(enabled)
110119
)
111120
}
112121
}
113-
}
114122

115-
SubcomposeLayout(
116-
modifier = modifier
117-
.heightIn(min = 56.dp)
118-
.fillMaxWidth()
119-
.then(clickableModifier)
120-
.padding(insideMargin)
121-
) { constraints ->
122-
val looseConstraints = constraints.copy(minWidth = 0, minHeight = 0)
123-
// 1. leftAction
124-
val leftPlaceables = leftAction?.let {
125-
subcompose("leftAction") { it() }.map { it -> it.measure(looseConstraints) }
126-
} ?: emptyList()
127-
val leftWidth = leftPlaceables.maxOfOrNull { it.width } ?: 0
128-
val leftHeight = leftPlaceables.maxOfOrNull { it.height } ?: 0
129-
// 2. rightActions
130-
val rightPlaceables = subcompose("rightActions") {
131-
Row(
132-
horizontalArrangement = Arrangement.End,
133-
verticalAlignment = Alignment.CenterVertically,
134-
content = rightActions
135-
)
136-
}.map { it.measure(looseConstraints) }
137-
val rightWidth = rightPlaceables.maxOfOrNull { it.width } ?: 0
138-
val rightHeight = rightPlaceables.maxOfOrNull { it.height } ?: 0
139-
// 3. content
140-
val contentMaxWidth = maxOf(0, constraints.maxWidth - leftWidth - rightWidth - 16.dp.roundToPx())
141-
val titlePlaceable = titleContent
142-
?.let { subcompose("title", it).first() }
143-
?.measure(looseConstraints.copy(maxWidth = contentMaxWidth))
144-
val summaryPlaceable = summaryContent
145-
?.let { subcompose("summary", it).first() }
146-
?.measure(looseConstraints.copy(maxWidth = contentMaxWidth))
147-
val contentHeight = (titlePlaceable?.height ?: 0) + (summaryPlaceable?.height ?: 0)
148-
val layoutHeight = maxOf(leftHeight, rightHeight, contentHeight)
149-
layout(constraints.maxWidth, layoutHeight) {
150-
var x = 0
151-
// leftAction
152-
leftPlaceables.forEach {
153-
it.placeRelative(x, (layoutHeight - it.height) / 2)
154-
x += it.width
155-
}
156-
// content
157-
var contentY = (layoutHeight - contentHeight) / 2
158-
titlePlaceable?.let {
159-
it.placeRelative(x, contentY)
160-
contentY += it.height
161-
}
162-
summaryPlaceable?.placeRelative(x, contentY)
163-
// rightActions
164-
val rightX = constraints.maxWidth - rightWidth
165-
rightPlaceables.forEach {
166-
it.placeRelative(rightX, (layoutHeight - it.height) / 2)
167-
}
168-
}
123+
// Right actions (optional)
124+
rightActions()
169125
}
170126
}
171127

0 commit comments

Comments
 (0)