Skip to content

Commit

Permalink
library: Optimize SuperDropdown performance again
Browse files Browse the repository at this point in the history
  • Loading branch information
YuKongA committed Oct 12, 2024
1 parent f49e7bb commit eec8dc0
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 68 deletions.
11 changes: 7 additions & 4 deletions composeApp/src/androidMain/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<manifest xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android">

<application
android:allowBackup="true"
android:enableOnBackInvokedCallback="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/Theme.Miuix">
android:theme="@style/Theme.Miuix"
tools:ignore="UnusedAttribute">
<activity
android:name=".MainActivity"
android:configChanges="orientation|screenSize|screenLayout|keyboardHidden|mnc|colorMode|density|fontScale|fontWeightAdjustment|keyboard|layoutDirection|locale|mcc|navigation|smallestScreenSize|touchscreen|uiMode"
android:windowSoftInputMode="adjustResize"
android:exported="true">
android:exported="true"
android:windowSoftInputMode="adjustResize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,33 +56,29 @@ fun SuperArrow(
summary = summary,
summaryColor = summaryColor,
leftAction = leftAction,
rightActions = { createRightActions(rightText) },
onClick = updatedOnClick,
rightActions = {
if (rightText != null) {
Text(
text = rightText,
fontSize = 15.sp,
color = MiuixTheme.colorScheme.onSurfaceVariantActions,
textAlign = TextAlign.End,
)
}
Image(
modifier = Modifier
.size(15.dp)
.padding(start = 6.dp),
imageVector = MiuixIcons.ArrowRight,
contentDescription = null,
colorFilter = BlendModeColorFilter(MiuixTheme.colorScheme.onSurfaceVariantActions, BlendMode.SrcIn),
)
},
onClick = {
if (enabled) {
updatedOnClick?.invoke()
}
},
enabled = enabled
)
}

/**
* Create the right actions of the [SuperArrow].
*
* @param rightText The text on the right side of the [SuperArrow].
*/
@Composable
private fun createRightActions(rightText: String?) {
if (rightText != null) {
Text(
text = rightText,
fontSize = 15.sp,
color = MiuixTheme.colorScheme.onSurfaceVariantActions,
textAlign = TextAlign.End,
)
}
Image(
modifier = Modifier
.size(15.dp)
.padding(start = 6.dp),
imageVector = MiuixIcons.ArrowRight,
contentDescription = null,
colorFilter = BlendModeColorFilter(MiuixTheme.colorScheme.onSurfaceVariantActions, BlendMode.SrcIn),
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,7 @@ fun SuperCheckbox(
var isChecked by remember { mutableStateOf(checked) }
val updatedOnCheckedChange by rememberUpdatedState(onCheckedChange)

if (isChecked != checked) {
isChecked = checked
}
if (isChecked != checked) isChecked = checked

BasicComponent(
modifier = modifier,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,11 @@ fun SuperDialog(
}
}

BackHandler(enabled = isDialogShowing()) {
dismissDialog(show)
onDismissRequest?.invoke()
if (show.value) {
BackHandler(enabled = isDialogShowing()) {
dismissDialog(show)
onDismissRequest?.invoke()
}
}

Box(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import androidx.compose.ui.graphics.BlendModeColorFilter
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.graphicsLayer
import androidx.compose.ui.hapticfeedback.HapticFeedbackType
import androidx.compose.ui.input.pointer.PointerEventType
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.layout.onGloballyPositioned
import androidx.compose.ui.layout.positionInWindow
Expand Down Expand Up @@ -106,36 +107,23 @@ fun SuperDropdown(
enabled: Boolean = true
) {
val isDropdownExpanded = remember { mutableStateOf(false) }

if (!dropdownStates.contains(isDropdownExpanded)) dropdownStates.add(isDropdownExpanded)
LaunchedEffect(isDropdownExpanded.value) {
if (isDropdownExpanded.value) {
dropdownStates.forEach { state -> if (state != isDropdownExpanded) state.value = false }
}
}

val hapticFeedback = LocalHapticFeedback.current
val actionColor = if (enabled) MiuixTheme.colorScheme.onSurfaceVariantActions else MiuixTheme.colorScheme.disabledOnSecondaryVariant
var alignLeft by rememberSaveable { mutableStateOf(true) }
var dropdownOffsetPx by remember { mutableStateOf(0) }
var componentHeightPx by remember { mutableStateOf(0) }
var componentWidthPx by remember { mutableStateOf(0) }

BackHandler(enabled = isDropdownExpanded.value) {
dismissPopup(isDropdownExpanded)
}

BasicComponent(
onClick = {
isDropdownExpanded.value = enabled
hapticFeedback.performHapticFeedback(HapticFeedbackType.LongPress)
},
modifier = modifier
.pointerInput(Unit) {
awaitPointerEventScope {
while (true) {
while (enabled) {
val event = awaitPointerEvent()
val position = event.changes.first().position
alignLeft = position.x < (size.width / 2)
if (event.type != PointerEventType.Move) {
val eventChange = event.changes.first()
alignLeft = eventChange.position.x < (size.width / 2)
}
}
}
}
Expand All @@ -157,24 +145,33 @@ fun SuperDropdown(
modifier = Modifier.padding(end = 6.dp),
text = items[selectedIndex],
fontSize = 15.sp,
color = if (enabled) MiuixTheme.colorScheme.onSurfaceVariantActions else MiuixTheme.colorScheme.disabledOnSecondaryVariant,
color = actionColor,
textAlign = TextAlign.End,
)
Image(
modifier = Modifier
.size(15.dp)
.align(Alignment.CenterVertically),
imageVector = MiuixIcons.ArrowUpDown,
colorFilter = BlendModeColorFilter(
if (enabled) MiuixTheme.colorScheme.onSurfaceVariantActions else MiuixTheme.colorScheme.disabledOnSecondaryVariant,
BlendMode.SrcIn
),
colorFilter = BlendModeColorFilter(actionColor, BlendMode.SrcIn),
contentDescription = null
)
},
onClick = {
if (enabled) {
isDropdownExpanded.value = enabled
hapticFeedback.performHapticFeedback(HapticFeedbackType.LongPress)
}
},
enabled = enabled
)
if (isDropdownExpanded.value) {
if (!dropdownStates.contains(isDropdownExpanded)) dropdownStates.add(isDropdownExpanded)
LaunchedEffect(isDropdownExpanded.value) {
if (isDropdownExpanded.value) {
dropdownStates.forEach { state -> if (state != isDropdownExpanded) state.value = false }
}
}
val density = LocalDensity.current
var offsetPx by remember { mutableStateOf(0) }
val textMeasurer = rememberTextMeasurer()
Expand All @@ -194,16 +191,17 @@ fun SuperDropdown(
)
val insideHeightPx by rememberUpdatedState(with(density) { insideMargin.height.toPx() }.roundToInt())
val displayCutoutSize =
WindowInsets.displayCutout.asPaddingValues(density).calculateLeftPadding(LayoutDirection.Ltr) +
WindowInsets.displayCutout.asPaddingValues(density).calculateRightPadding(LayoutDirection.Ltr)
WindowInsets.displayCutout.asPaddingValues(density).calculateLeftPadding(LayoutDirection.Ltr) + WindowInsets.displayCutout.asPaddingValues(density)
.calculateRightPadding(LayoutDirection.Ltr)
val popupPadding by rememberUpdatedState {
derivedStateOf {
max(
horizontalPadding + (windowWeightPx.dp - componentWidthPx.dp) / 2 / density.density -
if (defaultWindowInsetsPadding) displayCutoutSize / 2 else 0.dp, 0.dp
horizontalPadding + (windowWeightPx.dp - componentWidthPx.dp) / 2 / density.density
- if (defaultWindowInsetsPadding) displayCutoutSize / 2 else 0.dp, 0.dp
)
}
}
BackHandler(enabled = isDropdownExpanded.value) { dismissPopup(isDropdownExpanded) }
showPopup(
content = {
Box(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,7 @@ fun SuperSwitch(
var isChecked by remember { mutableStateOf(checked) }
val updatedOnCheckedChange by rememberUpdatedState(onCheckedChange)

if (isChecked != checked) {
isChecked = checked
}
if (isChecked != checked) isChecked = checked

BasicComponent(
modifier = modifier,
Expand Down

0 comments on commit eec8dc0

Please sign in to comment.