Skip to content

Commit eec8dc0

Browse files
committed
library: Optimize SuperDropdown performance again
1 parent f49e7bb commit eec8dc0

File tree

6 files changed

+63
-68
lines changed

6 files changed

+63
-68
lines changed

composeApp/src/androidMain/AndroidManifest.xml

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
2+
<manifest xmlns:tools="http://schemas.android.com/tools"
3+
xmlns:android="http://schemas.android.com/apk/res/android">
34

45
<application
56
android:allowBackup="true"
7+
android:enableOnBackInvokedCallback="true"
68
android:icon="@drawable/ic_launcher"
79
android:label="@string/app_name"
810
android:supportsRtl="true"
9-
android:theme="@style/Theme.Miuix">
11+
android:theme="@style/Theme.Miuix"
12+
tools:ignore="UnusedAttribute">
1013
<activity
1114
android:name=".MainActivity"
1215
android:configChanges="orientation|screenSize|screenLayout|keyboardHidden|mnc|colorMode|density|fontScale|fontWeightAdjustment|keyboard|layoutDirection|locale|mcc|navigation|smallestScreenSize|touchscreen|uiMode"
13-
android:windowSoftInputMode="adjustResize"
14-
android:exported="true">
16+
android:exported="true"
17+
android:windowSoftInputMode="adjustResize">
1518
<intent-filter>
1619
<action android:name="android.intent.action.MAIN" />
1720

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

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -56,33 +56,29 @@ fun SuperArrow(
5656
summary = summary,
5757
summaryColor = summaryColor,
5858
leftAction = leftAction,
59-
rightActions = { createRightActions(rightText) },
60-
onClick = updatedOnClick,
59+
rightActions = {
60+
if (rightText != null) {
61+
Text(
62+
text = rightText,
63+
fontSize = 15.sp,
64+
color = MiuixTheme.colorScheme.onSurfaceVariantActions,
65+
textAlign = TextAlign.End,
66+
)
67+
}
68+
Image(
69+
modifier = Modifier
70+
.size(15.dp)
71+
.padding(start = 6.dp),
72+
imageVector = MiuixIcons.ArrowRight,
73+
contentDescription = null,
74+
colorFilter = BlendModeColorFilter(MiuixTheme.colorScheme.onSurfaceVariantActions, BlendMode.SrcIn),
75+
)
76+
},
77+
onClick = {
78+
if (enabled) {
79+
updatedOnClick?.invoke()
80+
}
81+
},
6182
enabled = enabled
6283
)
6384
}
64-
65-
/**
66-
* Create the right actions of the [SuperArrow].
67-
*
68-
* @param rightText The text on the right side of the [SuperArrow].
69-
*/
70-
@Composable
71-
private fun createRightActions(rightText: String?) {
72-
if (rightText != null) {
73-
Text(
74-
text = rightText,
75-
fontSize = 15.sp,
76-
color = MiuixTheme.colorScheme.onSurfaceVariantActions,
77-
textAlign = TextAlign.End,
78-
)
79-
}
80-
Image(
81-
modifier = Modifier
82-
.size(15.dp)
83-
.padding(start = 6.dp),
84-
imageVector = MiuixIcons.ArrowRight,
85-
contentDescription = null,
86-
colorFilter = BlendModeColorFilter(MiuixTheme.colorScheme.onSurfaceVariantActions, BlendMode.SrcIn),
87-
)
88-
}

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,7 @@ fun SuperCheckbox(
4646
var isChecked by remember { mutableStateOf(checked) }
4747
val updatedOnCheckedChange by rememberUpdatedState(onCheckedChange)
4848

49-
if (isChecked != checked) {
50-
isChecked = checked
51-
}
49+
if (isChecked != checked) isChecked = checked
5250

5351
BasicComponent(
5452
modifier = modifier,

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,11 @@ fun SuperDialog(
8686
}
8787
}
8888

89-
BackHandler(enabled = isDialogShowing()) {
90-
dismissDialog(show)
91-
onDismissRequest?.invoke()
89+
if (show.value) {
90+
BackHandler(enabled = isDialogShowing()) {
91+
dismissDialog(show)
92+
onDismissRequest?.invoke()
93+
}
9294
}
9395

9496
Box(

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

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import androidx.compose.ui.graphics.BlendModeColorFilter
4141
import androidx.compose.ui.graphics.Color
4242
import androidx.compose.ui.graphics.graphicsLayer
4343
import androidx.compose.ui.hapticfeedback.HapticFeedbackType
44+
import androidx.compose.ui.input.pointer.PointerEventType
4445
import androidx.compose.ui.input.pointer.pointerInput
4546
import androidx.compose.ui.layout.onGloballyPositioned
4647
import androidx.compose.ui.layout.positionInWindow
@@ -106,36 +107,23 @@ fun SuperDropdown(
106107
enabled: Boolean = true
107108
) {
108109
val isDropdownExpanded = remember { mutableStateOf(false) }
109-
110-
if (!dropdownStates.contains(isDropdownExpanded)) dropdownStates.add(isDropdownExpanded)
111-
LaunchedEffect(isDropdownExpanded.value) {
112-
if (isDropdownExpanded.value) {
113-
dropdownStates.forEach { state -> if (state != isDropdownExpanded) state.value = false }
114-
}
115-
}
116-
117110
val hapticFeedback = LocalHapticFeedback.current
111+
val actionColor = if (enabled) MiuixTheme.colorScheme.onSurfaceVariantActions else MiuixTheme.colorScheme.disabledOnSecondaryVariant
118112
var alignLeft by rememberSaveable { mutableStateOf(true) }
119113
var dropdownOffsetPx by remember { mutableStateOf(0) }
120114
var componentHeightPx by remember { mutableStateOf(0) }
121115
var componentWidthPx by remember { mutableStateOf(0) }
122116

123-
BackHandler(enabled = isDropdownExpanded.value) {
124-
dismissPopup(isDropdownExpanded)
125-
}
126-
127117
BasicComponent(
128-
onClick = {
129-
isDropdownExpanded.value = enabled
130-
hapticFeedback.performHapticFeedback(HapticFeedbackType.LongPress)
131-
},
132118
modifier = modifier
133119
.pointerInput(Unit) {
134120
awaitPointerEventScope {
135-
while (true) {
121+
while (enabled) {
136122
val event = awaitPointerEvent()
137-
val position = event.changes.first().position
138-
alignLeft = position.x < (size.width / 2)
123+
if (event.type != PointerEventType.Move) {
124+
val eventChange = event.changes.first()
125+
alignLeft = eventChange.position.x < (size.width / 2)
126+
}
139127
}
140128
}
141129
}
@@ -157,24 +145,33 @@ fun SuperDropdown(
157145
modifier = Modifier.padding(end = 6.dp),
158146
text = items[selectedIndex],
159147
fontSize = 15.sp,
160-
color = if (enabled) MiuixTheme.colorScheme.onSurfaceVariantActions else MiuixTheme.colorScheme.disabledOnSecondaryVariant,
148+
color = actionColor,
161149
textAlign = TextAlign.End,
162150
)
163151
Image(
164152
modifier = Modifier
165153
.size(15.dp)
166154
.align(Alignment.CenterVertically),
167155
imageVector = MiuixIcons.ArrowUpDown,
168-
colorFilter = BlendModeColorFilter(
169-
if (enabled) MiuixTheme.colorScheme.onSurfaceVariantActions else MiuixTheme.colorScheme.disabledOnSecondaryVariant,
170-
BlendMode.SrcIn
171-
),
156+
colorFilter = BlendModeColorFilter(actionColor, BlendMode.SrcIn),
172157
contentDescription = null
173158
)
174159
},
160+
onClick = {
161+
if (enabled) {
162+
isDropdownExpanded.value = enabled
163+
hapticFeedback.performHapticFeedback(HapticFeedbackType.LongPress)
164+
}
165+
},
175166
enabled = enabled
176167
)
177168
if (isDropdownExpanded.value) {
169+
if (!dropdownStates.contains(isDropdownExpanded)) dropdownStates.add(isDropdownExpanded)
170+
LaunchedEffect(isDropdownExpanded.value) {
171+
if (isDropdownExpanded.value) {
172+
dropdownStates.forEach { state -> if (state != isDropdownExpanded) state.value = false }
173+
}
174+
}
178175
val density = LocalDensity.current
179176
var offsetPx by remember { mutableStateOf(0) }
180177
val textMeasurer = rememberTextMeasurer()
@@ -194,16 +191,17 @@ fun SuperDropdown(
194191
)
195192
val insideHeightPx by rememberUpdatedState(with(density) { insideMargin.height.toPx() }.roundToInt())
196193
val displayCutoutSize =
197-
WindowInsets.displayCutout.asPaddingValues(density).calculateLeftPadding(LayoutDirection.Ltr) +
198-
WindowInsets.displayCutout.asPaddingValues(density).calculateRightPadding(LayoutDirection.Ltr)
194+
WindowInsets.displayCutout.asPaddingValues(density).calculateLeftPadding(LayoutDirection.Ltr) + WindowInsets.displayCutout.asPaddingValues(density)
195+
.calculateRightPadding(LayoutDirection.Ltr)
199196
val popupPadding by rememberUpdatedState {
200197
derivedStateOf {
201198
max(
202-
horizontalPadding + (windowWeightPx.dp - componentWidthPx.dp) / 2 / density.density -
203-
if (defaultWindowInsetsPadding) displayCutoutSize / 2 else 0.dp, 0.dp
199+
horizontalPadding + (windowWeightPx.dp - componentWidthPx.dp) / 2 / density.density
200+
- if (defaultWindowInsetsPadding) displayCutoutSize / 2 else 0.dp, 0.dp
204201
)
205202
}
206203
}
204+
BackHandler(enabled = isDropdownExpanded.value) { dismissPopup(isDropdownExpanded) }
207205
showPopup(
208206
content = {
209207
Box(

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,7 @@ fun SuperSwitch(
4747
var isChecked by remember { mutableStateOf(checked) }
4848
val updatedOnCheckedChange by rememberUpdatedState(onCheckedChange)
4949

50-
if (isChecked != checked) {
51-
isChecked = checked
52-
}
50+
if (isChecked != checked) isChecked = checked
5351

5452
BasicComponent(
5553
modifier = modifier,

0 commit comments

Comments
 (0)