Skip to content

Commit 8ab80ea

Browse files
committed
library: Rewrite TextStyles
1 parent c1aa461 commit 8ab80ea

File tree

2 files changed

+130
-50
lines changed

2 files changed

+130
-50
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ fun MiuixTheme(
2323
val miuixColors = remember { colors.copy() }.apply {
2424
updateColorsFrom(colors)
2525
}
26-
val miuixTextStyles = remember { textStyles }
26+
val miuixTextStyles = remember { textStyles.copy() }.apply {
27+
updateColorsFrom(colors)
28+
}
2729
val miuixIndication = remember(colors.onBackground) {
2830
MiuixIndication(backgroundColor = colors.onBackground)
2931
}

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

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

33
import androidx.compose.runtime.Immutable
4+
import androidx.compose.runtime.getValue
5+
import androidx.compose.runtime.mutableStateOf
6+
import androidx.compose.runtime.setValue
47
import androidx.compose.runtime.staticCompositionLocalOf
8+
import androidx.compose.runtime.structuralEqualityPolicy
59
import androidx.compose.ui.text.TextStyle
610
import androidx.compose.ui.unit.em
711
import androidx.compose.ui.unit.sp
@@ -11,39 +15,102 @@ import androidx.compose.ui.unit.sp
1115
* The default text styles for the Miuix components.
1216
*
1317
* @param main The main text style.
14-
* @param title The title text style.
1518
* @param paragraph The paragraph text style.
16-
* @param body1 dialog_message_text_size
17-
* @param body2 preference_secondary_text_size, preference_right_text_size, spinner_text_size_integrated
18-
* @param headline1 preference_normal_text_size, edit_text_font_size
19-
* @param subtitle preference_category_text_size
20-
* @param title1 action_bar_tab_expand_text_size
21-
* @param title3 action_bar_tab_text_size
22-
* @param title4 dialog_title_text_size
19+
* @param body1 The body1 text style.
20+
* @param body2 The body2 text style.
21+
* @param button The button text style.
22+
* @param footnote1 The footnote1 text style.
23+
* @param footnote2 The footnote2 text style.
24+
* @param headline1 The headline1 text style.
25+
* @param headline2 The headline2 text style.
26+
* @param subtitle The subtitle text style.
27+
* @param title1 The title1 text style.
28+
* @param title2 The title2 text style.
29+
* @param title3 The title3 text style.
30+
* @param title4 The title4 text style.
2331
*/
2432
@Immutable
2533
class TextStyles(
26-
val main: TextStyle,
27-
val title: TextStyle,
28-
val paragraph: TextStyle,
29-
val body1: TextStyle,
30-
val body2: TextStyle,
31-
val button: TextStyle,
32-
val footnote1: TextStyle,
33-
val footnote2: TextStyle,
34-
val headline1: TextStyle,
35-
val headline2: TextStyle,
36-
val subtitle: TextStyle,
37-
val title1: TextStyle,
38-
val title2: TextStyle,
39-
val title3: TextStyle,
40-
val title4: TextStyle,
41-
)
34+
main: TextStyle,
35+
paragraph: TextStyle,
36+
body1: TextStyle,
37+
body2: TextStyle,
38+
button: TextStyle,
39+
footnote1: TextStyle,
40+
footnote2: TextStyle,
41+
headline1: TextStyle,
42+
headline2: TextStyle,
43+
subtitle: TextStyle,
44+
title1: TextStyle,
45+
title2: TextStyle,
46+
title3: TextStyle,
47+
title4: TextStyle,
48+
) {
49+
var main by mutableStateOf(main, structuralEqualityPolicy())
50+
internal set
51+
var paragraph by mutableStateOf(paragraph, structuralEqualityPolicy())
52+
internal set
53+
var body1 by mutableStateOf(body1, structuralEqualityPolicy())
54+
internal set
55+
var body2 by mutableStateOf(body2, structuralEqualityPolicy())
56+
internal set
57+
var button by mutableStateOf(button, structuralEqualityPolicy())
58+
internal set
59+
var footnote1 by mutableStateOf(footnote1, structuralEqualityPolicy())
60+
internal set
61+
var footnote2 by mutableStateOf(footnote2, structuralEqualityPolicy())
62+
internal set
63+
var headline1 by mutableStateOf(headline1, structuralEqualityPolicy())
64+
internal set
65+
var headline2 by mutableStateOf(headline2, structuralEqualityPolicy())
66+
internal set
67+
var subtitle by mutableStateOf(subtitle, structuralEqualityPolicy())
68+
internal set
69+
var title1 by mutableStateOf(title1, structuralEqualityPolicy())
70+
internal set
71+
var title2 by mutableStateOf(title2, structuralEqualityPolicy())
72+
internal set
73+
var title3 by mutableStateOf(title3, structuralEqualityPolicy())
74+
internal set
75+
var title4 by mutableStateOf(title4, structuralEqualityPolicy())
76+
internal set
77+
78+
fun copy(
79+
main: TextStyle = Main,
80+
paragraph: TextStyle = Paragraph,
81+
body1: TextStyle = Body1,
82+
body2: TextStyle = Body2,
83+
button: TextStyle = Button,
84+
footnote1: TextStyle = Footnote1,
85+
footnote2: TextStyle = Footnote2,
86+
headline1: TextStyle = Headline1,
87+
headline2: TextStyle = Headline2,
88+
subtitle: TextStyle = Subtitle,
89+
title1: TextStyle = Title1,
90+
title2: TextStyle = Title2,
91+
title3: TextStyle = Title3,
92+
title4: TextStyle = Title4
93+
): TextStyles = TextStyles(
94+
main,
95+
paragraph,
96+
body1,
97+
body2,
98+
button,
99+
footnote1,
100+
footnote2,
101+
headline1,
102+
headline2,
103+
subtitle,
104+
title1,
105+
title2,
106+
title3,
107+
title4,
108+
)
109+
}
42110

43111
fun defaultTextStyles(
44-
main: TextStyle = DefaultTextStyle,
45-
title: TextStyle = TitleTextStyle,
46-
paragraph: TextStyle = ParagraphTextStyle,
112+
main: TextStyle = Main,
113+
paragraph: TextStyle = Paragraph,
47114
body1: TextStyle = Body1,
48115
body2: TextStyle = Body2,
49116
button: TextStyle = Button,
@@ -57,34 +124,28 @@ fun defaultTextStyles(
57124
title3: TextStyle = Title3,
58125
title4: TextStyle = Title4
59126
): TextStyles = TextStyles(
60-
main = main,
61-
title = title,
62-
paragraph = paragraph,
63-
body1 = body1,
64-
body2 = body2,
65-
button = button,
66-
footnote1 = footnote1,
67-
footnote2 = footnote2,
68-
headline1 = headline1,
69-
headline2 = headline2,
70-
subtitle = subtitle,
71-
title1 = title1,
72-
title2 = title2,
73-
title3 = title3,
74-
title4 = title4,
127+
main,
128+
paragraph,
129+
body1,
130+
body2,
131+
button,
132+
footnote1,
133+
footnote2,
134+
headline1,
135+
headline2,
136+
subtitle,
137+
title1,
138+
title2,
139+
title3,
140+
title4,
75141
)
76142

77-
private val DefaultTextStyle: TextStyle
143+
private val Main: TextStyle
78144
get() = TextStyle(
79145
fontSize = 17.sp,
80146
)
81147

82-
private val TitleTextStyle: TextStyle
83-
get() = TextStyle(
84-
fontSize = 12.sp
85-
)
86-
87-
private val ParagraphTextStyle: TextStyle
148+
private val Paragraph: TextStyle
88149
get() = TextStyle(
89150
fontSize = 17.sp,
90151
lineHeight = 1.2f.em
@@ -150,4 +211,21 @@ private val Title4: TextStyle
150211
fontSize = 18.sp
151212
)
152213

214+
internal fun TextStyles.updateColorsFrom(other: Colors) {
215+
main = main.copy(color = other.onBackground)
216+
paragraph = paragraph.copy(color = other.onBackground)
217+
body1 = body1.copy(color = other.onBackground)
218+
body2 = body2.copy(color = other.onBackground)
219+
button = button.copy(color = other.onBackground)
220+
footnote1 = footnote1.copy(color = other.onBackground)
221+
footnote2 = footnote2.copy(color = other.onBackground)
222+
headline1 = headline1.copy(color = other.onBackground)
223+
headline2 = headline2.copy(color = other.onBackground)
224+
subtitle = subtitle.copy(color = other.onBackground)
225+
title1 = title1.copy(color = other.onBackground)
226+
title2 = title2.copy(color = other.onBackground)
227+
title3 = title3.copy(color = other.onBackground)
228+
title4 = title4.copy(color = other.onBackground)
229+
}
230+
153231
val LocalTextStyles = staticCompositionLocalOf { defaultTextStyles() }

0 commit comments

Comments
 (0)