Skip to content

Commit dd000d0

Browse files
committed
library: Use staticCompositionLocalOf to reduce read overhead
1 parent fc230ae commit dd000d0

File tree

10 files changed

+244
-204
lines changed

10 files changed

+244
-204
lines changed

example/src/commonMain/kotlin/UITest.kt

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ import androidx.compose.ui.Modifier
5353
import androidx.compose.ui.graphics.Color
5454
import androidx.compose.ui.hapticfeedback.HapticFeedbackType
5555
import androidx.compose.ui.input.nestedscroll.nestedScroll
56-
import androidx.compose.ui.platform.LocalDensity
5756
import androidx.compose.ui.platform.LocalHapticFeedback
5857
import androidx.compose.ui.platform.LocalLayoutDirection
5958
import androidx.compose.ui.platform.LocalUriHandler
@@ -237,17 +236,10 @@ private fun WideScreenLayout(
237236
windowSize: WindowSize,
238237
colorMode: MutableState<Int>
239238
) {
240-
val density = LocalDensity.current
241239
val layoutDirection = LocalLayoutDirection.current
242240

243241
val windowWidth = getWindowSize().width
244-
val defaultWeight = remember(windowWidth, density) {
245-
val minPx = with(density) { UIConstants.WIDE_SCREEN_THRESHOLD.toPx() }
246-
val maxPx = with(density) { 1920.dp.toPx() }
247-
val t = (windowWidth - minPx) / 2500
248-
0.3f - 0.2f * t
249-
}
250-
var weight by remember(windowWidth) { mutableStateOf(defaultWeight) }
242+
var weight by remember(windowWidth) { mutableStateOf(0.4f) }
251243
var potentialWeight by remember { mutableFloatStateOf(weight) }
252244
val dragState = rememberDraggableState { delta ->
253245
val nextPotentialWeight = potentialWeight + delta / windowWidth

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>533</string>
20+
<string>534</string>
2121
<key>LSRequiresIPhoneOS</key>
2222
<true/>
2323
<key>CADisableMinimumFrameDurationOnPhone</key>

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

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,31 @@ fun BasicComponent(
8787
} else Modifier
8888
}
8989

90+
val titleContent: (@Composable () -> Unit)? = remember(title, enabled, titleColor) {
91+
title?.let { text ->
92+
{
93+
Text(
94+
text = text,
95+
fontSize = MiuixTheme.textStyles.headline1.fontSize,
96+
fontWeight = FontWeight.Medium,
97+
color = titleColor.color(enabled)
98+
)
99+
}
100+
}
101+
}
102+
103+
val summaryContent: (@Composable () -> Unit)? = remember(summary, enabled, summaryColor) {
104+
summary?.let { text ->
105+
{
106+
Text(
107+
text = text,
108+
fontSize = MiuixTheme.textStyles.body2.fontSize,
109+
color = summaryColor.color(enabled)
110+
)
111+
}
112+
}
113+
}
114+
90115
SubcomposeLayout(
91116
modifier = modifier
92117
.heightIn(min = 56.dp)
@@ -113,25 +138,12 @@ fun BasicComponent(
113138
val rightHeight = rightPlaceables.maxOfOrNull { it.height } ?: 0
114139
// 3. content
115140
val contentMaxWidth = maxOf(0, constraints.maxWidth - leftWidth - rightWidth - 16.dp.roundToPx())
116-
val titlePlaceable = title?.let {
117-
subcompose("title") {
118-
Text(
119-
text = it,
120-
fontSize = MiuixTheme.textStyles.headline1.fontSize,
121-
fontWeight = FontWeight.Medium,
122-
color = titleColor.color(enabled)
123-
)
124-
}.first().measure(looseConstraints.copy(maxWidth = contentMaxWidth))
125-
}
126-
val summaryPlaceable = summary?.let {
127-
subcompose("summary") {
128-
Text(
129-
text = it,
130-
fontSize = MiuixTheme.textStyles.body2.fontSize,
131-
color = summaryColor.color(enabled)
132-
)
133-
}.first().measure(looseConstraints.copy(maxWidth = contentMaxWidth))
134-
}
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))
135147
val contentHeight = (titlePlaceable?.height ?: 0) + (summaryPlaceable?.height ?: 0)
136148
val layoutHeight = maxOf(leftHeight, rightHeight, contentHeight)
137149
layout(constraints.maxWidth, layoutHeight) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ import androidx.compose.ui.unit.LayoutDirection
4141
import androidx.compose.ui.unit.dp
4242
import top.yukonga.miuix.kmp.theme.MiuixTheme
4343
import top.yukonga.miuix.kmp.utils.BackHandler
44-
import top.yukonga.miuix.kmp.utils.MiuixPopupUtils.Companion.PopupLayout
4544
import top.yukonga.miuix.kmp.utils.G2RoundedCornerShape
45+
import top.yukonga.miuix.kmp.utils.MiuixPopupUtils.Companion.PopupLayout
4646
import top.yukonga.miuix.kmp.utils.getWindowSize
4747
import kotlin.math.min
4848

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ import androidx.compose.ui.unit.Dp
4848
import androidx.compose.ui.unit.dp
4949
import androidx.compose.ui.unit.sp
5050
import top.yukonga.miuix.kmp.theme.MiuixTheme
51-
import top.yukonga.miuix.kmp.utils.Platform
5251
import top.yukonga.miuix.kmp.utils.G2RoundedCornerShape
52+
import top.yukonga.miuix.kmp.utils.Platform
5353
import top.yukonga.miuix.kmp.utils.platform
5454

5555
/**

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import androidx.compose.foundation.interaction.collectIsDraggedAsState
2121
import androidx.compose.foundation.interaction.collectIsHoveredAsState
2222
import androidx.compose.foundation.interaction.collectIsPressedAsState
2323
import androidx.compose.foundation.layout.Box
24-
import androidx.compose.foundation.layout.padding
24+
import androidx.compose.foundation.layout.offset
2525
import androidx.compose.foundation.layout.requiredSize
2626
import androidx.compose.foundation.layout.size
2727
import androidx.compose.foundation.layout.wrapContentSize
@@ -123,7 +123,6 @@ fun Switch(
123123
Modifier
124124
}
125125

126-
127126
Box(
128127
modifier = modifier
129128
.wrapContentSize(Alignment.Center)
@@ -141,13 +140,13 @@ fun Switch(
141140
) {
142141
Box(
143142
modifier = Modifier
144-
.padding(start = thumbOffset)
145143
.align(Alignment.CenterStart)
144+
.offset(x = thumbOffset)
146145
.size(thumbSize)
147146
.drawBehind {
148147
drawCircle(color = thumbColor)
149148
}
150-
.pointerInput(checked) {
149+
.pointerInput(checked, enabled) {
151150
if (!enabled) return@pointerInput
152151
awaitEachGesture {
153152
val pressInteraction: PressInteraction.Press
@@ -163,7 +162,7 @@ fun Switch(
163162
}
164163
}
165164
}
166-
.pointerInput(checked) {
165+
.pointerInput(checked, enabled) {
167166
if (!enabled) return@pointerInput
168167
val dragInteraction: DragInteraction.Start = DragInteraction.Start()
169168
detectHorizontalDragGestures(

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

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@ package top.yukonga.miuix.kmp.extra
55

66
import androidx.compose.foundation.Image
77
import androidx.compose.foundation.layout.PaddingValues
8+
import androidx.compose.foundation.layout.RowScope
89
import androidx.compose.foundation.layout.padding
910
import androidx.compose.foundation.layout.size
1011
import androidx.compose.foundation.layout.widthIn
1112
import androidx.compose.runtime.Composable
1213
import androidx.compose.runtime.Immutable
1314
import androidx.compose.runtime.Stable
15+
import androidx.compose.runtime.remember
1416
import androidx.compose.ui.Modifier
1517
import androidx.compose.ui.graphics.Color
1618
import androidx.compose.ui.graphics.ColorFilter
@@ -56,6 +58,16 @@ fun SuperArrow(
5658
holdDownState: Boolean = false,
5759
enabled: Boolean = true
5860
) {
61+
val rightActionsContent: @Composable (RowScope.() -> Unit) =
62+
remember(rightText, rightActionColor, enabled) {
63+
{
64+
SuperArrowRightActions(
65+
rightText = rightText,
66+
tintColor = rightActionColor.color(enabled)
67+
)
68+
}
69+
}
70+
5971
BasicComponent(
6072
modifier = modifier,
6173
insideMargin = insideMargin,
@@ -64,13 +76,7 @@ fun SuperArrow(
6476
summary = summary,
6577
summaryColor = summaryColor,
6678
leftAction = leftAction,
67-
rightActions = {
68-
SuperArrowRightActions(
69-
rightText = rightText,
70-
rightActionColor = rightActionColor,
71-
enabled = enabled
72-
)
73-
},
79+
rightActions = rightActionsContent,
7480
onClick = onClick?.takeIf { enabled },
7581
holdDownState = holdDownState,
7682
enabled = enabled
@@ -80,17 +86,16 @@ fun SuperArrow(
8086
@Composable
8187
private fun SuperArrowRightActions(
8288
rightText: String?,
83-
rightActionColor: RightActionColors,
84-
enabled: Boolean
89+
tintColor: Color
8590
) {
86-
val currentRightActionColor = rightActionColor.color(enabled)
91+
val tintFilter = remember(tintColor) { ColorFilter.tint(tintColor) }
8792

8893
if (rightText != null) {
8994
Text(
9095
modifier = Modifier.widthIn(max = 130.dp),
9196
text = rightText,
9297
fontSize = MiuixTheme.textStyles.body2.fontSize,
93-
color = currentRightActionColor,
98+
color = tintColor,
9499
textAlign = TextAlign.End,
95100
overflow = TextOverflow.Ellipsis,
96101
maxLines = 2
@@ -102,7 +107,7 @@ private fun SuperArrowRightActions(
102107
.size(width = 10.dp, height = 16.dp),
103108
imageVector = MiuixIcons.Basic.ArrowRight,
104109
contentDescription = null,
105-
colorFilter = ColorFilter.tint(currentRightActionColor),
110+
colorFilter = tintFilter,
106111
)
107112
}
108113

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ import androidx.compose.ui.unit.dp
3232
import top.yukonga.miuix.kmp.basic.Text
3333
import top.yukonga.miuix.kmp.theme.MiuixTheme
3434
import top.yukonga.miuix.kmp.utils.BackHandler
35-
import top.yukonga.miuix.kmp.utils.MiuixPopupUtils.Companion.DialogLayout
3635
import top.yukonga.miuix.kmp.utils.G2RoundedCornerShape
36+
import top.yukonga.miuix.kmp.utils.MiuixPopupUtils.Companion.DialogLayout
3737
import top.yukonga.miuix.kmp.utils.getRoundedCorner
3838
import top.yukonga.miuix.kmp.utils.getWindowSize
3939

miuix/src/commonMain/kotlin/top/yukonga/miuix/kmp/theme/ContentColor.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import androidx.compose.ui.graphics.Color
88

99
/**
1010
* CompositionLocal containing the preferred content color for a given position in the hierarchy.
11-
* This typically represents the `on` color for a color in [ColorScheme]. For example, if the
11+
* This typically represents the `on` color for a color in [Colors]. For example, if the
1212
* background color is [Colors.surface], this color is typically set to
1313
* [Colors.onSurface].
1414
*

0 commit comments

Comments
 (0)