Skip to content

Commit afaab6c

Browse files
committed
library: Opt recomposition
1 parent 0df892c commit afaab6c

File tree

6 files changed

+197
-181
lines changed

6 files changed

+197
-181
lines changed

example/build.gradle.kts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,6 @@ android {
150150
(this as BaseVariantOutputImpl).outputFileName = "$appName-v$versionName($versionCode)-$name.apk"
151151
}
152152
}
153-
resources.excludes += "**"
154153
}
155154
buildTypes {
156155
release {
@@ -166,6 +165,12 @@ android {
166165
}
167166
}
168167

168+
androidComponents {
169+
onVariants(selector().withBuildType("release")) {
170+
it.packaging.resources.excludes.add("**")
171+
}
172+
}
173+
169174
compose.desktop {
170175
application {
171176
mainClass = "Main_desktopKt"

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.4</string>
1919
<key>CFBundleVersion</key>
20-
<string>484</string>
20+
<string>485</string>
2121
<key>LSRequiresIPhoneOS</key>
2222
<true/>
2323
<key>CADisableMinimumFrameDurationOnPhone</key>

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

Lines changed: 52 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -75,66 +75,16 @@ fun ListPopup(
7575
) {
7676
if (!show.value) return
7777

78-
val currentOnDismissRequest by rememberUpdatedState(onDismissRequest)
79-
val currentContent by rememberUpdatedState(content)
80-
81-
var offset by remember { mutableStateOf(IntOffset.Zero) }
8278
val density = LocalDensity.current
8379
val layoutDirection = LocalLayoutDirection.current
8480

8581
val getWindowSizeState = rememberUpdatedState(getWindowSize())
8682
var windowSize by remember { mutableStateOf(IntSize(getWindowSizeState.value.width, getWindowSizeState.value.height)) }
8783
var parentBounds by remember { mutableStateOf(IntRect.Zero) }
8884

89-
val windowBounds = with(density) {
90-
IntRect(
91-
left = WindowInsets.displayCutout.asPaddingValues(density).calculateLeftPadding(layoutDirection).roundToPx(),
92-
top = WindowInsets.statusBars.asPaddingValues().calculateTopPadding().roundToPx(),
93-
right = windowSize.width -
94-
WindowInsets.displayCutout.asPaddingValues(density).calculateRightPadding(layoutDirection).roundToPx(),
95-
bottom = windowSize.height -
96-
WindowInsets.navigationBars.asPaddingValues().calculateBottomPadding().roundToPx() -
97-
WindowInsets.captionBar.asPaddingValues().calculateBottomPadding().roundToPx()
98-
)
99-
}
100-
101-
var popupContentSize by remember { mutableStateOf(IntSize.Zero) }
102-
103-
val popupMargin = remember(popupPositionProvider, density, layoutDirection) {
104-
val popupMarginsPd = popupPositionProvider.getMargins()
105-
with(density) {
106-
IntRect(
107-
left = popupMarginsPd.calculateLeftPadding(layoutDirection).roundToPx(),
108-
top = popupMarginsPd.calculateTopPadding().roundToPx(),
109-
right = popupMarginsPd.calculateRightPadding(layoutDirection).roundToPx(),
110-
bottom = popupMarginsPd.calculateBottomPadding().roundToPx()
111-
)
112-
}
113-
}
114-
115-
val transformOrigin = remember(parentBounds, popupMargin, windowSize, density, alignment) {
116-
val xInWindow = if (alignment in listOf(
117-
PopupPositionProvider.Align.Right,
118-
PopupPositionProvider.Align.TopRight,
119-
PopupPositionProvider.Align.BottomRight,
120-
)
121-
) {
122-
parentBounds.right - popupMargin.right - with(density) { 64.dp.roundToPx() }
123-
} else {
124-
parentBounds.left + popupMargin.left + with(density) { 64.dp.roundToPx() }
125-
}
126-
val yInWindow = parentBounds.top + parentBounds.height / 2 - with(density) { 56.dp.roundToPx() }
127-
safeTransformOrigin(
128-
xInWindow / windowSize.width.toFloat(),
129-
yInWindow / windowSize.height.toFloat()
130-
)
131-
}
132-
133-
// Anchor point for the popup
13485
Layout(
13586
modifier = Modifier.onGloballyPositioned { childCoordinates ->
136-
val parentLayoutCoordinates = childCoordinates.parentLayoutCoordinates
137-
if (parentLayoutCoordinates != null) {
87+
childCoordinates.parentLayoutCoordinates?.let { parentLayoutCoordinates ->
13888
val positionInWindow = parentLayoutCoordinates.positionInWindow()
13989
parentBounds = IntRect(
14090
left = positionInWindow.x.toInt(),
@@ -149,10 +99,53 @@ fun ListPopup(
14999
windowSize = newIntSize
150100
}
151101
}
152-
) { _, _ ->
153-
layout(0, 0) {}
102+
) { _, _ -> layout(0, 0) {} }
103+
104+
val popupMargin = remember(popupPositionProvider, layoutDirection, density) {
105+
val pd = popupPositionProvider.getMargins()
106+
with(density) {
107+
IntRect(
108+
left = pd.calculateLeftPadding(layoutDirection).roundToPx(),
109+
top = pd.calculateTopPadding().roundToPx(),
110+
right = pd.calculateRightPadding(layoutDirection).roundToPx(),
111+
bottom = pd.calculateBottomPadding().roundToPx()
112+
)
113+
}
114+
}
115+
116+
val displayCutoutPadding = WindowInsets.displayCutout.asPaddingValues(density)
117+
val statusBarsPadding = WindowInsets.statusBars.asPaddingValues()
118+
val navigationBarsPadding = WindowInsets.navigationBars.asPaddingValues()
119+
val captionBarPadding = WindowInsets.captionBar.asPaddingValues()
120+
121+
val windowBounds = with(density) {
122+
IntRect(
123+
left = displayCutoutPadding.calculateLeftPadding(layoutDirection).roundToPx(),
124+
top = statusBarsPadding.calculateTopPadding().roundToPx(),
125+
right = windowSize.width - displayCutoutPadding.calculateRightPadding(layoutDirection).roundToPx(),
126+
bottom = windowSize.height -
127+
navigationBarsPadding.calculateBottomPadding().roundToPx() -
128+
captionBarPadding.calculateBottomPadding().roundToPx()
129+
)
130+
}
131+
132+
val transformOrigin = remember(parentBounds, popupMargin, windowSize, alignment, density) {
133+
val xInWindow = when (alignment) {
134+
PopupPositionProvider.Align.Right,
135+
PopupPositionProvider.Align.TopRight,
136+
PopupPositionProvider.Align.BottomRight -> parentBounds.right - popupMargin.right - with(density) { 64.dp.roundToPx() }
137+
else -> parentBounds.left + popupMargin.left + with(density) { 64.dp.roundToPx() }
138+
}
139+
val yInWindow = parentBounds.top + parentBounds.height / 2 - with(density) { 56.dp.roundToPx() }
140+
safeTransformOrigin(
141+
xInWindow / windowSize.width.toFloat(),
142+
yInWindow / windowSize.height.toFloat()
143+
)
154144
}
155145

146+
var offset by remember { mutableStateOf(IntOffset.Zero) }
147+
var popupContentSize by remember { mutableStateOf(IntSize.Zero) }
148+
156149
PopupLayout(
157150
visible = show,
158151
enableWindowDim = enableWindowDim,
@@ -163,10 +156,10 @@ fun ListPopup(
163156

164157
Box(
165158
modifier = popupModifier
166-
.pointerInput(currentOnDismissRequest) {
167-
detectTapGestures {
168-
currentOnDismissRequest?.invoke()
169-
}
159+
.pointerInput(onDismissRequest) {
160+
detectTapGestures(
161+
onTap = { onDismissRequest?.invoke() }
162+
)
170163
}
171164
.layout { measurable, constraints ->
172165
val placeable = measurable.measure(
@@ -213,13 +206,13 @@ fun ListPopup(
213206
)
214207
.background(MiuixTheme.colorScheme.surface)
215208
) {
216-
currentContent()
209+
content()
217210
}
218211
}
219212
}
220213

221214
BackHandler(enabled = show.value) {
222-
currentOnDismissRequest?.invoke()
215+
onDismissRequest?.invoke()
223216
}
224217
}
225218

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

Lines changed: 22 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
package top.yukonga.miuix.kmp.basic
55

6-
import androidx.compose.animation.core.Animatable
76
import androidx.compose.animation.core.LinearEasing
87
import androidx.compose.animation.core.RepeatMode
98
import androidx.compose.animation.core.animateFloat
@@ -17,10 +16,8 @@ import androidx.compose.foundation.layout.height
1716
import androidx.compose.foundation.layout.size
1817
import androidx.compose.runtime.Composable
1918
import androidx.compose.runtime.Immutable
20-
import androidx.compose.runtime.LaunchedEffect
2119
import androidx.compose.runtime.Stable
2220
import androidx.compose.runtime.getValue
23-
import androidx.compose.runtime.remember
2421
import androidx.compose.runtime.rememberUpdatedState
2522
import androidx.compose.ui.Modifier
2623
import androidx.compose.ui.geometry.CornerRadius
@@ -53,33 +50,31 @@ fun LinearProgressIndicator(
5350
height: Dp = ProgressIndicatorDefaults.DefaultLinearProgressIndicatorHeight
5451
) {
5552
if (progress == null) {
56-
val animatedValue = remember { Animatable(initialValue = 0f) }
57-
58-
LaunchedEffect(Unit) {
59-
animatedValue.animateTo(
60-
targetValue = 1f,
61-
animationSpec = infiniteRepeatable(
62-
animation = tween(durationMillis = 1250, easing = LinearEasing),
63-
repeatMode = RepeatMode.Restart
64-
)
53+
val transition = rememberInfiniteTransition()
54+
val animatedValue by transition.animateFloat(
55+
initialValue = 0f,
56+
targetValue = 1f,
57+
animationSpec = infiniteRepeatable(
58+
animation = tween(durationMillis = 1250, easing = LinearEasing),
59+
repeatMode = RepeatMode.Restart
6560
)
66-
}
61+
)
6762

6863
Canvas(
6964
modifier = modifier
7065
.fillMaxWidth()
7166
.height(height)
7267
) {
7368
val currentBackgroundColor = colors.backgroundColor()
74-
val currentForegroundColor = colors.foregroundColor(true) // Assuming enabled for indeterminate
69+
val currentForegroundColor = colors.foregroundColor(true)
7570

7671
drawRoundRect(
7772
color = currentBackgroundColor,
7873
size = Size(size.width, size.height),
7974
cornerRadius = CornerRadius(size.height / 2)
8075
)
8176

82-
val value = animatedValue.value
77+
val value = animatedValue
8378
val segmentWidth = 0.45f
8479
val gap = 0.55f
8580

@@ -128,7 +123,7 @@ fun LinearProgressIndicator(
128123
} else {
129124
val progressValue = progress.coerceIn(0f, 1f)
130125
val currentBackgroundColor = colors.backgroundColor()
131-
val currentForegroundColor = colors.foregroundColor(true) // Assuming enabled for determinate
126+
val currentForegroundColor = colors.foregroundColor(true)
132127

133128
Canvas(
134129
modifier = modifier
@@ -225,9 +220,9 @@ fun CircularProgressIndicator(
225220
)
226221
}
227222
} else {
228-
val progressValue = progress.coerceIn(0f, 1f)
223+
val progressValue by rememberUpdatedState(progress.coerceIn(0f, 1f))
229224
val currentBackgroundColor = colors.backgroundColor()
230-
val currentForegroundColor = colors.foregroundColor(true) // Assuming enabled
225+
val currentForegroundColor = colors.foregroundColor(true)
231226

232227
Canvas(
233228
modifier = modifier.size(size)
@@ -244,7 +239,6 @@ fun CircularProgressIndicator(
244239
)
245240

246241
val minSweepAngle = 0.1f
247-
248242
val sweepAngle = minSweepAngle + (360f - minSweepAngle) * progressValue
249243

250244
drawArc(
@@ -278,18 +272,17 @@ fun InfiniteProgressIndicator(
278272
strokeWidth: Dp = ProgressIndicatorDefaults.DefaultInfiniteProgressIndicatorStrokeWidth,
279273
orbitingDotSize: Dp = ProgressIndicatorDefaults.DefaultInfiniteProgressIndicatorOrbitingDotSize
280274
) {
281-
val rotationAnim = remember { Animatable(0f) }
282275
val currentColor by rememberUpdatedState(color)
283276

284-
LaunchedEffect(Unit) {
285-
rotationAnim.animateTo(
286-
targetValue = 360f,
287-
animationSpec = infiniteRepeatable(
288-
animation = tween(800, easing = LinearEasing),
289-
repeatMode = RepeatMode.Restart
290-
)
277+
val transition = rememberInfiniteTransition()
278+
val rotation by transition.animateFloat(
279+
initialValue = 0f,
280+
targetValue = 360f,
281+
animationSpec = infiniteRepeatable(
282+
animation = tween(800, easing = LinearEasing),
283+
repeatMode = RepeatMode.Restart
291284
)
292-
}
285+
)
293286

294287
Canvas(
295288
modifier = modifier.size(size)
@@ -305,7 +298,7 @@ fun InfiniteProgressIndicator(
305298
)
306299

307300
val orbitRadius = radius - 2 * orbitingDotSize.toPx()
308-
val angle = rotationAnim.value * PI.toFloat() / 180f
301+
val angle = rotation * PI.toFloat() / 180f
309302
val dotCenter = center + Offset(
310303
x = orbitRadius * cos(angle),
311304
y = orbitRadius * sin(angle)

miuix/src/commonMain/kotlin/top/yukonga/miuix/kmp/extra/SuperDialog.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ fun SuperDialog(
6868
defaultWindowInsetsPadding: Boolean = true,
6969
content: @Composable () -> Unit
7070
) {
71+
if (!show.value) return
7172
DialogLayout(
7273
visible = show,
7374
enableWindowDim = enableWindowDim,

0 commit comments

Comments
 (0)