Skip to content

Commit 286ec70

Browse files
committed
library: Change click effect
1 parent 52130a6 commit 286ec70

File tree

7 files changed

+65
-34
lines changed

7 files changed

+65
-34
lines changed

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import androidx.compose.animation.core.animateDpAsState
55
import androidx.compose.animation.core.animateFloatAsState
66
import androidx.compose.animation.core.tween
77
import androidx.compose.foundation.Canvas
8-
import androidx.compose.foundation.background
98
import androidx.compose.foundation.gestures.detectTapGestures
109
import androidx.compose.foundation.layout.requiredSize
1110
import androidx.compose.foundation.layout.size
@@ -21,6 +20,7 @@ import androidx.compose.runtime.setValue
2120
import androidx.compose.ui.Alignment
2221
import androidx.compose.ui.Modifier
2322
import androidx.compose.ui.draw.clip
23+
import androidx.compose.ui.draw.drawBehind
2424
import androidx.compose.ui.geometry.Offset
2525
import androidx.compose.ui.graphics.Matrix
2626
import androidx.compose.ui.graphics.Path
@@ -33,7 +33,6 @@ import androidx.compose.ui.platform.LocalHapticFeedback
3333
import androidx.compose.ui.semantics.Role
3434
import androidx.compose.ui.unit.dp
3535
import top.yukonga.miuix.kmp.theme.MiuixTheme
36-
import top.yukonga.miuix.kmp.utils.SmoothRoundedCornerShape
3736

3837
/**
3938
* A checkbox component with Miuix style.
@@ -108,7 +107,7 @@ fun Checkbox(
108107
Canvas(
109108
modifier = Modifier
110109
.requiredSize(checkboxSize)
111-
.background(if (enabled) backgroundColor else disabledBackgroundColor)
110+
.drawBehind { drawRect(if (enabled) backgroundColor else disabledBackgroundColor) }
112111
) {
113112
val svgPath =
114113
"m400-416 236-236q11-11 28-11t28 11q11 11 11 28t-11 28L428-332q-12 12-28 12t-28-12L268-436q-11-11-11-28t11-28q11-11 28-11t28 11l76 76Z"

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

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,27 @@
11
package top.yukonga.miuix.kmp.basic
22

3+
import androidx.compose.animation.animateColorAsState
4+
import androidx.compose.animation.core.spring
35
import androidx.compose.foundation.clickable
6+
import androidx.compose.foundation.interaction.MutableInteractionSource
7+
import androidx.compose.foundation.interaction.collectIsPressedAsState
48
import androidx.compose.foundation.layout.Arrangement
59
import androidx.compose.foundation.layout.Column
610
import androidx.compose.foundation.layout.Row
711
import androidx.compose.foundation.layout.RowScope
812
import androidx.compose.foundation.layout.fillMaxWidth
913
import androidx.compose.foundation.layout.padding
1014
import androidx.compose.runtime.Composable
15+
import androidx.compose.runtime.getValue
16+
import androidx.compose.runtime.mutableStateOf
1117
import androidx.compose.runtime.remember
18+
import androidx.compose.runtime.setValue
1219
import androidx.compose.ui.Alignment
1320
import androidx.compose.ui.Modifier
21+
import androidx.compose.ui.draw.drawBehind
1422
import androidx.compose.ui.graphics.Color
23+
import androidx.compose.ui.input.pointer.PointerEventType
24+
import androidx.compose.ui.input.pointer.pointerInput
1525
import androidx.compose.ui.text.font.FontWeight
1626
import androidx.compose.ui.unit.DpSize
1727
import androidx.compose.ui.unit.dp
@@ -43,22 +53,44 @@ fun BasicComponent(
4353
leftAction: @Composable (() -> Unit?)? = null,
4454
rightActions: @Composable RowScope.() -> Unit = {},
4555
onClick: (() -> Unit)? = null,
46-
enabled: Boolean = true
56+
enabled: Boolean = true,
57+
extPressed: Boolean = false
4758
) {
59+
val interactionSource = remember { MutableInteractionSource() }
60+
val isPressed = interactionSource.collectIsPressedAsState().value
61+
var pointerPressed by remember { mutableStateOf(false) }
4862
val insideMargin = remember { insideMargin } ?: remember { DpSize(16.dp, 16.dp) }
4963
val paddingModifier = remember(insideMargin) {
5064
Modifier.padding(horizontal = insideMargin.width, vertical = insideMargin.height)
5165
}
66+
val backgroundColor = animateColorAsState(
67+
targetValue = if (isPressed || extPressed || pointerPressed) MiuixTheme.colorScheme.selectedTint else Color.Unspecified,
68+
animationSpec = spring(1f, 2000f)
69+
).value
5270
val titleColor = if (enabled) titleColor else MiuixTheme.colorScheme.disabledOnSecondaryVariant
5371
val summaryColor = if (enabled) summaryColor else MiuixTheme.colorScheme.disabledOnSecondaryVariant
5472
Row(
5573
modifier = if (onClick != null && enabled) {
5674
modifier
57-
.clickable { onClick.invoke() }
75+
.clickable(
76+
indication = null,
77+
interactionSource = interactionSource
78+
) {
79+
onClick.invoke()
80+
}
5881
} else {
5982
modifier
6083
}
84+
.pointerInput(Unit) {
85+
awaitPointerEventScope {
86+
while (enabled) {
87+
val event = awaitPointerEvent()
88+
pointerPressed = event.type == PointerEventType.Press
89+
}
90+
}
91+
}
6192
.fillMaxWidth()
93+
.drawBehind { drawRect(backgroundColor) }
6294
.then(paddingModifier),
6395
verticalAlignment = Alignment.CenterVertically,
6496
horizontalArrangement = Arrangement.SpaceBetween

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import androidx.compose.runtime.setValue
2222
import androidx.compose.ui.Alignment
2323
import androidx.compose.ui.Modifier
2424
import androidx.compose.ui.draw.clip
25+
import androidx.compose.ui.draw.drawBehind
2526
import androidx.compose.ui.hapticfeedback.HapticFeedbackType
2627
import androidx.compose.ui.input.pointer.pointerInput
2728
import androidx.compose.ui.platform.LocalHapticFeedback
@@ -107,7 +108,7 @@ fun Switch(
107108
.size(52.dp, 28.5.dp)
108109
.requiredSize(52.dp, 28.5.dp)
109110
.clip(SmoothRoundedCornerShape(52.dp))
110-
.background(if (enabled) backgroundColor else disabledBackgroundColor)
111+
.drawBehind { drawRect(if (enabled) backgroundColor else disabledBackgroundColor) }
111112
.pointerInput(Unit) {
112113
detectHorizontalDragGestures(
113114
onDragStart = {

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

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package top.yukonga.miuix.kmp.extra
22

3-
import androidx.compose.animation.animateColorAsState
4-
import androidx.compose.animation.core.spring
53
import androidx.compose.foundation.Image
64
import androidx.compose.foundation.background
75
import androidx.compose.foundation.clickable
@@ -112,8 +110,6 @@ fun SuperDropdown(
112110
val isDropdownExpanded = remember { mutableStateOf(false) }
113111
val hapticFeedback = LocalHapticFeedback.current
114112
val actionColor = if (enabled) MiuixTheme.colorScheme.onSurfaceVariantActions else MiuixTheme.colorScheme.disabledOnSecondaryVariant
115-
val targetColor = if (isDropdownExpanded.value) MiuixTheme.colorScheme.selectedTint else Color.Transparent
116-
val selectedTint by animateColorAsState(targetValue = targetColor, animationSpec = spring(stiffness = 2000f))
117113
var alignLeft by rememberSaveable { mutableStateOf(true) }
118114
var dropdownOffsetXPx by remember { mutableStateOf(0) }
119115
var dropdownOffsetYPx by remember { mutableStateOf(0) }
@@ -147,8 +143,8 @@ fun SuperDropdown(
147143
componentHeightPx = coordinates.size.height
148144
componentWidthPx = coordinates.size.width
149145
}
150-
}
151-
.background(selectedTint),
146+
},
147+
extPressed = isDropdownExpanded.value,
152148
insideMargin = insideMargin,
153149
title = title,
154150
titleColor = titleColor,
@@ -205,7 +201,7 @@ fun SuperDropdown(
205201
with(density) { WindowInsets.captionBar.asPaddingValues().calculateBottomPadding().toPx() }.roundToInt()
206202
)
207203
val dropdownMaxHeight by rememberUpdatedState(with(density) {
208-
(windowHeightPx - statusBarPx - navigationBarPx - captionBarPx ).toDp()
204+
(windowHeightPx - statusBarPx - navigationBarPx - captionBarPx).toDp()
209205
})
210206
val dropdownElevation by rememberUpdatedState(with(density) {
211207
11.dp.toPx()

miuix/src/commonMain/kotlin/top/yukonga/miuix/kmp/theme/MiuixColor.kt renamed to miuix/src/commonMain/kotlin/top/yukonga/miuix/kmp/theme/Colors.kt

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package top.yukonga.miuix.kmp.theme
33
import androidx.compose.runtime.Stable
44
import androidx.compose.runtime.getValue
55
import androidx.compose.runtime.mutableStateOf
6+
import androidx.compose.runtime.staticCompositionLocalOf
67
import androidx.compose.runtime.structuralEqualityPolicy
78
import androidx.compose.ui.graphics.Color
89

@@ -57,7 +58,7 @@ import androidx.compose.ui.graphics.Color
5758
* @param windowDimming The color of the window dimming. Cases: Dialog, Dropdown.
5859
*/
5960
@Stable
60-
class MiuixColor(
61+
class Colors(
6162
primary: Color,
6263
onPrimary: Color,
6364
primaryVariant: Color,
@@ -155,7 +156,7 @@ class MiuixColor(
155156
val selectedTint by mutableStateOf(selectedTint, structuralEqualityPolicy())
156157
}
157158

158-
fun lightColorScheme() = MiuixColor(
159+
fun lightColorScheme() = Colors(
159160
primary = Color(0xFF3482FF),
160161
onPrimary = Color.White,
161162
primaryVariant = Color(0xFF3482FF),
@@ -205,7 +206,7 @@ fun lightColorScheme() = MiuixColor(
205206
selectedTint = Color(0x14000000)
206207
)
207208

208-
fun darkColorScheme() = MiuixColor(
209+
fun darkColorScheme() = Colors(
209210
primary = Color(0xFF277Af7),
210211
onPrimary = Color.White,
211212
primaryVariant = Color(0xFF0073DD),
@@ -253,4 +254,6 @@ fun darkColorScheme() = MiuixColor(
253254
onSurfaceContainerHighest = Color(0xFFE9E9E9),
254255
windowDimming = Color.Black.copy(alpha = 0.6f),
255256
selectedTint = Color(0xCC393939)
256-
)
257+
)
258+
259+
val LocalColors = staticCompositionLocalOf { lightColorScheme() }

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

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,6 @@ import androidx.compose.runtime.Composable
66
import androidx.compose.runtime.CompositionLocalProvider
77
import androidx.compose.runtime.ReadOnlyComposable
88
import androidx.compose.runtime.remember
9-
import androidx.compose.runtime.staticCompositionLocalOf
10-
11-
private val LocalMiuixColor = staticCompositionLocalOf { lightColorScheme() }
12-
private val LocalMiuixTextStyles = staticCompositionLocalOf { miuixTextStyles() }
139

1410
/**
1511
* The default theme that provides color and text styles for the Miuix components.
@@ -20,12 +16,13 @@ private val LocalMiuixTextStyles = staticCompositionLocalOf { miuixTextStyles()
2016
*/
2117
@Composable
2218
fun MiuixTheme(
23-
colorScheme: MiuixColor = MiuixTheme.colorScheme,
24-
textStyles: MiuixTextStyles = MiuixTheme.textStyles,
19+
colorScheme: Colors = MiuixTheme.colorScheme,
20+
textStyles: TextStyles = MiuixTheme.textStyles,
2521
content: @Composable () -> Unit
2622
) {
23+
val miuixColors = remember(colorScheme) { colorScheme }
2724
val miuixTextStyles = remember(colorScheme.onBackground) {
28-
miuixTextStyles(
25+
defaultTextStyles(
2926
main = textStyles.main.copy(color = colorScheme.onBackground),
3027
title = textStyles.title.copy(color = colorScheme.onBackground),
3128
paragraph = textStyles.paragraph.copy(color = colorScheme.onBackground)
@@ -35,22 +32,22 @@ fun MiuixTheme(
3532
ripple(color = colorScheme.onBackground)
3633
}
3734
CompositionLocalProvider(
38-
LocalMiuixColor provides colorScheme,
39-
LocalMiuixTextStyles provides miuixTextStyles,
35+
LocalColors provides miuixColors,
36+
LocalTextStyles provides miuixTextStyles,
4037
LocalIndication provides miuixRipple,
4138
) {
4239
content()
4340
}
4441
}
4542

4643
object MiuixTheme {
47-
val colorScheme: MiuixColor
44+
val colorScheme: Colors
4845
@Composable
4946
@ReadOnlyComposable
50-
get() = LocalMiuixColor.current
47+
get() = LocalColors.current
5148

52-
val textStyles: MiuixTextStyles
49+
val textStyles: TextStyles
5350
@Composable
5451
@ReadOnlyComposable
55-
get() = LocalMiuixTextStyles.current
52+
get() = LocalTextStyles.current
5653
}

miuix/src/commonMain/kotlin/top/yukonga/miuix/kmp/theme/MiuixTextStyle.kt renamed to miuix/src/commonMain/kotlin/top/yukonga/miuix/kmp/theme/TextStyles.kt

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package top.yukonga.miuix.kmp.theme
22

33
import androidx.compose.runtime.Immutable
4+
import androidx.compose.runtime.staticCompositionLocalOf
45
import androidx.compose.ui.text.TextStyle
56
import androidx.compose.ui.unit.em
67
import androidx.compose.ui.unit.sp
@@ -14,17 +15,17 @@ import androidx.compose.ui.unit.sp
1415
* @param paragraph The paragraph text style.
1516
*/
1617
@Immutable
17-
class MiuixTextStyles(
18+
class TextStyles(
1819
val main: TextStyle,
1920
val title: TextStyle,
2021
val paragraph: TextStyle
2122
)
2223

23-
fun miuixTextStyles(
24+
fun defaultTextStyles(
2425
main: TextStyle = DefaultTextStyle,
2526
title: TextStyle = TitleTextStyle,
2627
paragraph: TextStyle = ParagraphTextStyle
27-
): MiuixTextStyles = MiuixTextStyles(
28+
): TextStyles = TextStyles(
2829
main = main,
2930
title = title,
3031
paragraph = paragraph
@@ -44,4 +45,6 @@ private val ParagraphTextStyle: TextStyle
4445
get() = TextStyle(
4546
fontSize = 17.sp,
4647
lineHeight = 1.2f.em
47-
)
48+
)
49+
50+
val LocalTextStyles = staticCompositionLocalOf { defaultTextStyles() }

0 commit comments

Comments
 (0)