@@ -2,93 +2,211 @@ package top.yukonga.miuix.kmp.basic
22
33import androidx.compose.foundation.layout.Arrangement
44import androidx.compose.foundation.layout.Row
5+ import androidx.compose.foundation.layout.RowScope
56import androidx.compose.foundation.layout.defaultMinSize
67import androidx.compose.foundation.layout.padding
78import androidx.compose.runtime.Composable
8- import androidx.compose.runtime.getValue
9- import androidx.compose.runtime.rememberUpdatedState
9+ import androidx.compose.runtime.Immutable
10+ import androidx.compose.runtime.Stable
1011import androidx.compose.ui.Alignment
1112import androidx.compose.ui.Modifier
1213import androidx.compose.ui.graphics.Color
13- import androidx.compose.ui.hapticfeedback.HapticFeedbackType
14- import androidx.compose.ui.platform.LocalHapticFeedback
1514import androidx.compose.ui.semantics.Role
1615import androidx.compose.ui.semantics.role
1716import androidx.compose.ui.semantics.semantics
18- import androidx.compose.ui.text.font.FontWeight
1917import androidx.compose.ui.unit.Dp
18+ import androidx.compose.ui.unit.DpSize
2019import androidx.compose.ui.unit.dp
2120import top.yukonga.miuix.kmp.theme.MiuixTheme
2221import top.yukonga.miuix.kmp.utils.SmoothRoundedCornerShape
2322
2423/* *
2524 * A [Button] component with Miuix style.
2625 *
27- * @param text The text of the [Button].
2826 * @param onClick The callback when the [Button] is clicked.
2927 * @param modifier The modifier to be applied to the [Button].
3028 * @param enabled Whether the [Button] is enabled.
31- * @param submit Whether the [Button] is a submit button .
29+ * @param colors The [ButtonColors] of the [Button] .
3230 * @param cornerRadius The corner radius of the [Button].
3331 * @param minWidth The minimum width of the [Button].
3432 * @param minHeight The minimum height of the [Button].
33+ * @param insideMargin The margin inside the [Button].
34+ * @param content The [Composable] content of the [Button].
3535 */
3636@Composable
3737fun Button (
38- text : String ,
3938 onClick : () -> Unit ,
4039 modifier : Modifier = Modifier ,
4140 enabled : Boolean = true,
42- submit : Boolean = false,
43- cornerRadius : Dp = 16.dp,
44- minWidth : Dp = 58.dp,
45- minHeight : Dp = 40.dp
41+ colors : ButtonColors = ButtonDefaults .buttonColors(),
42+ cornerRadius : Dp = ButtonDefaults .ConorRadius ,
43+ minWidth : Dp = ButtonDefaults .MinWidth ,
44+ minHeight : Dp = ButtonDefaults .MinHeight ,
45+ insideMargin : DpSize = DpSize (16.dp, 16.dp),
46+ content : @Composable RowScope .() -> Unit
4647) {
47- val hapticFeedback = LocalHapticFeedback .current
48- val color by rememberUpdatedState(getButtonColor(enabled, submit))
49- val textColor by rememberUpdatedState(getTextColor(enabled, submit))
50-
5148 Surface (
5249 onClick = {
5350 onClick()
54- hapticFeedback.performHapticFeedback(HapticFeedbackType .LongPress )
5551 },
5652 enabled = enabled,
5753 modifier = modifier.semantics { role = Role .Button },
5854 shape = SmoothRoundedCornerShape (cornerRadius),
59- color = color
55+ color = colors. color(enabled)
6056 ) {
6157 Row (
6258 Modifier
6359 .defaultMinSize(minWidth = minWidth, minHeight = minHeight)
64- .padding(16 .dp, 16 .dp ),
60+ .padding(insideMargin.width, insideMargin.height ),
6561 horizontalArrangement = Arrangement .Center ,
6662 verticalAlignment = Alignment .CenterVertically ,
67- ) {
68- Text (
69- text = text,
70- color = textColor,
71- fontSize = MiuixTheme .textStyles.button.fontSize,
72- fontWeight = FontWeight .Medium
73- )
74- }
63+ content = content
64+ )
7565 }
7666}
7767
68+ /* *
69+ * A [TextButton] component with Miuix style.
70+ *
71+ * @param text The text of the [TextButton].
72+ * @param onClick The callback when the [TextButton] is clicked.
73+ * @param modifier The modifier to be applied to the [TextButton].
74+ * @param enabled Whether the [TextButton] is enabled.
75+ * @param colors The [TextButtonColors] of the [TextButton].
76+ * @param cornerRadius The corner radius of the [TextButton].
77+ * @param minWidth The minimum width of the [TextButton].
78+ * @param minHeight The minimum height of the [TextButton].
79+ * @param insideMargin The margin inside the [TextButton].
80+ */
7881@Composable
79- fun getButtonColor (enabled : Boolean , submit : Boolean ): Color {
80- return if (enabled) {
81- if (submit) MiuixTheme .colorScheme.primary else MiuixTheme .colorScheme.secondaryVariant
82- } else {
83- if (submit) MiuixTheme .colorScheme.disabledPrimaryButton else MiuixTheme .colorScheme.disabledSecondaryVariant
82+ fun TextButton (
83+ text : String ,
84+ onClick : () -> Unit ,
85+ modifier : Modifier = Modifier ,
86+ enabled : Boolean = true,
87+ colors : TextButtonColors = ButtonDefaults .textButtonColors(),
88+ cornerRadius : Dp = ButtonDefaults .ConorRadius ,
89+ minWidth : Dp = ButtonDefaults .MinWidth ,
90+ minHeight : Dp = ButtonDefaults .MinHeight ,
91+ insideMargin : DpSize = DpSize (16.dp, 16.dp),
92+ ) {
93+ Surface (
94+ onClick = {
95+ onClick()
96+ },
97+ enabled = enabled,
98+ modifier = modifier.semantics { role = Role .Button },
99+ shape = SmoothRoundedCornerShape (cornerRadius),
100+ color = colors.color(enabled)
101+ ) {
102+ Row (
103+ Modifier
104+ .defaultMinSize(minWidth = minWidth, minHeight = minHeight)
105+ .padding(insideMargin.width, insideMargin.height),
106+ horizontalArrangement = Arrangement .Center ,
107+ verticalAlignment = Alignment .CenterVertically ,
108+ content = {
109+ Text (
110+ text = text,
111+ color = colors.textColor(enabled),
112+ style = MiuixTheme .textStyles.button
113+ )
114+ }
115+ )
84116 }
85117}
86118
87- @Composable
88- fun getTextColor (enabled : Boolean , submit : Boolean ): Color {
89- return if (enabled) {
90- if (submit) MiuixTheme .colorScheme.onPrimary else MiuixTheme .colorScheme.onSecondaryVariant
91- } else {
92- if (submit) MiuixTheme .colorScheme.disabledOnPrimaryButton else MiuixTheme .colorScheme.disabledOnSecondaryVariant
119+ object ButtonDefaults {
120+
121+ /* *
122+ * The default min width applied for all buttons. Note that you can override it by applying
123+ * Modifier.widthIn directly on the button composable.
124+ */
125+ val MinWidth = 58 .dp
126+
127+ /* *
128+ * The default min height applied for all buttons. Note that you can override it by applying
129+ * Modifier.heightIn directly on the button composable.
130+ */
131+ val MinHeight = 40 .dp
132+
133+ /* *
134+ * The default corner radius applied for all buttons.
135+ */
136+ val ConorRadius = 16 .dp
137+
138+ /* *
139+ * The default [ButtonColors] for all buttons.
140+ */
141+ @Composable
142+ fun buttonColors (
143+ color : Color = MiuixTheme .colorScheme.secondaryVariant,
144+ disabledColor : Color = MiuixTheme .colorScheme.disabledSecondaryVariant
145+ ): ButtonColors {
146+ return ButtonColors (
147+ color = color,
148+ disabledColor = disabledColor
149+ )
93150 }
151+
152+ /* *
153+ * The [ButtonColors] for primary buttons.
154+ */
155+ @Composable
156+ fun buttonColorsPrimary () = ButtonColors (
157+ color = MiuixTheme .colorScheme.primary,
158+ disabledColor = MiuixTheme .colorScheme.disabledPrimaryButton
159+ )
160+
161+ /* *
162+ * The default [TextButtonColors] for all text buttons.
163+ */
164+ @Composable
165+ fun textButtonColors (
166+ color : Color = MiuixTheme .colorScheme.secondaryVariant,
167+ disabledColor : Color = MiuixTheme .colorScheme.disabledSecondaryVariant,
168+ textColor : Color = MiuixTheme .colorScheme.onSecondaryVariant,
169+ disabledTextColor : Color = MiuixTheme .colorScheme.disabledOnSecondaryVariant
170+ ): TextButtonColors {
171+ return TextButtonColors (
172+ color = color,
173+ disabledColor = disabledColor,
174+ textColor = textColor,
175+ disabledTextColor = disabledTextColor
176+ )
177+ }
178+
179+ /* *
180+ * The [TextButtonColors] for primary text buttons.
181+ */
182+ @Composable
183+ fun textButtonColorsPrimary () = TextButtonColors (
184+ color = MiuixTheme .colorScheme.primary,
185+ disabledColor = MiuixTheme .colorScheme.disabledPrimaryButton,
186+ textColor = MiuixTheme .colorScheme.onPrimary,
187+ disabledTextColor = MiuixTheme .colorScheme.disabledOnPrimaryButton
188+ )
189+ }
190+
191+ @Immutable
192+ class ButtonColors (
193+ private val color : Color ,
194+ private val disabledColor : Color
195+ ) {
196+ @Stable
197+ internal fun color (enabled : Boolean ): Color = if (enabled) color else disabledColor
198+ }
199+
200+ @Immutable
201+ class TextButtonColors (
202+ private val color : Color ,
203+ private val disabledColor : Color ,
204+ private val textColor : Color ,
205+ private val disabledTextColor : Color
206+ ) {
207+ @Stable
208+ internal fun color (enabled : Boolean ): Color = if (enabled) color else disabledColor
209+
210+ @Stable
211+ internal fun textColor (enabled : Boolean ): Color = if (enabled) textColor else disabledTextColor
94212}
0 commit comments