From 70025a7a9f57a82176b2582a062aac1e084b9930 Mon Sep 17 00:00:00 2001 From: fankesyooni Date: Wed, 29 Nov 2023 00:10:43 +0800 Subject: [PATCH] refactor: merge to new FontColor in TextStyle inheritance relationship --- .../highcapable/flexiui/component/Button.kt | 2 +- .../highcapable/flexiui/component/Dropdown.kt | 2 - .../flexiui/component/Navigation.kt | 2 +- .../com/highcapable/flexiui/component/Tab.kt | 2 +- .../com/highcapable/flexiui/component/Text.kt | 79 +++---------------- .../flexiui/component/TextField.kt | 46 +++++------ 6 files changed, 34 insertions(+), 99 deletions(-) diff --git a/flexiui-core/src/commonMain/kotlin/com/highcapable/flexiui/component/Button.kt b/flexiui-core/src/commonMain/kotlin/com/highcapable/flexiui/component/Button.kt index 93c17de..923c38a 100644 --- a/flexiui-core/src/commonMain/kotlin/com/highcapable/flexiui/component/Button.kt +++ b/flexiui-core/src/commonMain/kotlin/com/highcapable/flexiui/component/Button.kt @@ -79,7 +79,7 @@ fun Button( footer: @Composable () -> Unit = {}, content: @Composable RowScope.() -> Unit ) { - val localTextStyle = LocalTextStyle.current.default(color = colors.contentColor) + val localTextStyle = LocalTextStyle.current.copy(color = colors.contentColor) val localProgressIndicatorColors = LocalProgressIndicatorColors.current.copy( foregroundColor = colors.contentColor, backgroundColor = Color.Transparent diff --git a/flexiui-core/src/commonMain/kotlin/com/highcapable/flexiui/component/Dropdown.kt b/flexiui-core/src/commonMain/kotlin/com/highcapable/flexiui/component/Dropdown.kt index ca72887..6bf6a70 100644 --- a/flexiui-core/src/commonMain/kotlin/com/highcapable/flexiui/component/Dropdown.kt +++ b/flexiui-core/src/commonMain/kotlin/com/highcapable/flexiui/component/Dropdown.kt @@ -352,8 +352,6 @@ fun DropdownMenuItem( style = currentStyle, verticalAlignment = Alignment.CenterVertically ) { - // Note: Since this is a popup menu, we don't want to propagate the content color. - // So here we use copy NOT default. CompositionLocalProvider(LocalTextStyle provides LocalTextStyle.current.copy(color = currentColor)) { content() } diff --git a/flexiui-core/src/commonMain/kotlin/com/highcapable/flexiui/component/Navigation.kt b/flexiui-core/src/commonMain/kotlin/com/highcapable/flexiui/component/Navigation.kt index 95a68f6..fb205c9 100644 --- a/flexiui-core/src/commonMain/kotlin/com/highcapable/flexiui/component/Navigation.kt +++ b/flexiui-core/src/commonMain/kotlin/com/highcapable/flexiui/component/Navigation.kt @@ -135,7 +135,7 @@ fun NavigationItem( val currentContentShape = contentShape ?: LocalNavigationContentShape.current ?: Navigation.style.contentShape val animatedIndicatorColor by animateColorAsState(if (selected) currentColors.indicatorColor else Color.Transparent) val animatedContentColor by animateColorAsState(if (selected) currentColors.selectedContentColor else currentColors.unselectedContentColor) - val currentContentStyle = LocalTextStyle.current.default(animatedContentColor) + val currentContentStyle = LocalTextStyle.current.copy(animatedContentColor) Box( modifier = Modifier.status(enabled) .clip(currentContentShape) diff --git a/flexiui-core/src/commonMain/kotlin/com/highcapable/flexiui/component/Tab.kt b/flexiui-core/src/commonMain/kotlin/com/highcapable/flexiui/component/Tab.kt index 9c5deb7..bd7042f 100644 --- a/flexiui-core/src/commonMain/kotlin/com/highcapable/flexiui/component/Tab.kt +++ b/flexiui-core/src/commonMain/kotlin/com/highcapable/flexiui/component/Tab.kt @@ -210,7 +210,7 @@ fun Tab( val currentContentPadding = contentPadding ?: LocalTabContentPadding.current ?: Tab.style.contentPadding val currentContentShape = contentShape ?: LocalTabContentShape.current ?: Tab.style.contentShape val contentColor by animateColorAsState(if (selected) currentSelectedContentColor else currentUnselectedContentColor) - val contentStyle = LocalTextStyle.current.default(contentColor) + val contentStyle = LocalTextStyle.current.copy(contentColor) CompositionLocalProvider( LocalIconTint provides contentColor, LocalTextStyle provides contentStyle diff --git a/flexiui-core/src/commonMain/kotlin/com/highcapable/flexiui/component/Text.kt b/flexiui-core/src/commonMain/kotlin/com/highcapable/flexiui/component/Text.kt index 2d6354c..dfb5ff6 100644 --- a/flexiui-core/src/commonMain/kotlin/com/highcapable/flexiui/component/Text.kt +++ b/flexiui-core/src/commonMain/kotlin/com/highcapable/flexiui/component/Text.kt @@ -33,13 +33,7 @@ import androidx.compose.ui.graphics.Color import androidx.compose.ui.text.AnnotatedString import androidx.compose.ui.text.TextLayoutResult import androidx.compose.ui.text.TextStyle -import androidx.compose.ui.text.font.FontFamily -import androidx.compose.ui.text.font.FontStyle -import androidx.compose.ui.text.font.FontWeight -import androidx.compose.ui.text.style.TextAlign -import androidx.compose.ui.text.style.TextDecoration import androidx.compose.ui.text.style.TextOverflow -import androidx.compose.ui.unit.TextUnit import com.highcapable.flexiui.DefaultTypography import com.highcapable.flexiui.LocalColors import com.highcapable.flexiui.extension.orElse @@ -49,41 +43,25 @@ fun Text( text: String, modifier: Modifier = Modifier, color: Color = Color.Unspecified, - fontSize: TextUnit = TextUnit.Unspecified, - fontStyle: FontStyle? = null, - fontWeight: FontWeight? = null, - fontFamily: FontFamily? = null, - letterSpacing: TextUnit = TextUnit.Unspecified, - textDecoration: TextDecoration? = null, - textAlign: TextAlign? = null, - lineHeight: TextUnit = TextUnit.Unspecified, + style: TextStyle = Text.style, overflow: TextOverflow = TextOverflow.Clip, softWrap: Boolean = true, singleLine: Boolean = false, maxLines: Int = if (singleLine) 1 else Int.MAX_VALUE, minLines: Int = 1, - onTextLayout: (TextLayoutResult) -> Unit = {}, - style: TextStyle = Text.style + onTextLayout: (TextLayoutResult) -> Unit = {} ) { Text( text = AnnotatedString(text), modifier = modifier, color = color, - fontSize = fontSize, - fontStyle = fontStyle, - fontWeight = fontWeight, - fontFamily = fontFamily, - letterSpacing = letterSpacing, - textDecoration = textDecoration, - textAlign = textAlign, - lineHeight = lineHeight, + style = style, overflow = overflow, softWrap = softWrap, maxLines = maxLines, minLines = minLines, inlineContent = emptyMap(), - onTextLayout = onTextLayout, - style = style + onTextLayout = onTextLayout ) } @@ -92,39 +70,20 @@ fun Text( text: AnnotatedString, modifier: Modifier = Modifier, color: Color = Color.Unspecified, - fontSize: TextUnit = TextUnit.Unspecified, - fontStyle: FontStyle? = null, - fontWeight: FontWeight? = null, - fontFamily: FontFamily? = null, - letterSpacing: TextUnit = TextUnit.Unspecified, - textDecoration: TextDecoration? = null, - textAlign: TextAlign? = null, - lineHeight: TextUnit = TextUnit.Unspecified, + style: TextStyle = Text.style, overflow: TextOverflow = TextOverflow.Clip, softWrap: Boolean = true, singleLine: Boolean = false, maxLines: Int = if (singleLine) 1 else Int.MAX_VALUE, minLines: Int = 1, inlineContent: Map = mapOf(), - onTextLayout: (TextLayoutResult) -> Unit = {}, - style: TextStyle = Text.style + onTextLayout: (TextLayoutResult) -> Unit = {} ) { + val currentColor = color.orElse() ?: style.color.orElse() ?: Text.color BasicText( text = text, modifier = modifier, - style = style.merge( - TextStyle( - color = color.orElse() ?: style.color.orElse() ?: Text.color, - fontSize = fontSize.orElse() ?: style.fontSize.orElse() ?: Text.fontSize, - fontWeight = fontWeight, - textAlign = textAlign, - lineHeight = lineHeight.orElse() ?: style.lineHeight.orElse() ?: Text.lineHeight, - fontFamily = fontFamily, - textDecoration = textDecoration, - fontStyle = fontStyle, - letterSpacing = letterSpacing - ) - ), + style = style.copy(color = currentColor), onTextLayout = onTextLayout, overflow = overflow, softWrap = softWrap, @@ -139,28 +98,16 @@ object Text { @Composable @ReadOnlyComposable get() = defaultTextColor() - val fontSize: TextUnit - @Composable - @ReadOnlyComposable - get() = style.fontSize - val lineHeight: TextUnit - @Composable - @ReadOnlyComposable - get() = style.lineHeight val style: TextStyle @Composable @ReadOnlyComposable get() = LocalTextStyle.current } +@Composable +@ReadOnlyComposable +internal fun defaultTextColor() = LocalColors.current.textPrimary + internal val LocalTextStyle = compositionLocalOf { DefaultTextStyle } -internal val DefaultTextStyle = DefaultTypography.primary - -@Composable -@ReadOnlyComposable -internal fun TextStyle.default(color: Color) = copy(color = LocalTextStyle.current.color.orElse() ?: color) - -@Composable -@ReadOnlyComposable -private fun defaultTextColor() = LocalTextStyle.current.default(LocalColors.current.textPrimary).color \ No newline at end of file +private val DefaultTextStyle = DefaultTypography.primary \ No newline at end of file diff --git a/flexiui-core/src/commonMain/kotlin/com/highcapable/flexiui/component/TextField.kt b/flexiui-core/src/commonMain/kotlin/com/highcapable/flexiui/component/TextField.kt index 188d709..2fae1f2 100644 --- a/flexiui-core/src/commonMain/kotlin/com/highcapable/flexiui/component/TextField.kt +++ b/flexiui-core/src/commonMain/kotlin/com/highcapable/flexiui/component/TextField.kt @@ -101,6 +101,7 @@ import com.highcapable.flexiui.resources.icon.ViewerOpen @Immutable data class TextFieldColors( + val textColor: Color, val cursorColor: Color, val selectionColors: TextSelectionColors, val completionColors: AutoCompleteBoxColors, @@ -120,6 +121,7 @@ data class AutoCompleteBoxColors( @Immutable data class TextFieldStyle( + val textStyle: TextStyle, val padding: PaddingValues, val shape: Shape, val borderInactive: BorderStroke, @@ -157,8 +159,7 @@ fun TextField( interactionSource: MutableInteractionSource = remember { MutableInteractionSource() }, header: @Composable (() -> Unit)? = null, placeholder: @Composable () -> Unit = {}, - footer: @Composable (() -> Unit)? = null, - textStyle: TextStyle = TextField.textStyle + footer: @Composable (() -> Unit)? = null ) { val focused by interactionSource.collectIsFocusedAsState() val hovered by interactionSource.collectIsHoveredAsState() @@ -179,6 +180,7 @@ fun TextField( focused || hovered -> style.borderInactive else -> style.borderInactive }.copy(animatedBorderWidth, SolidColor(animatedBorderColor)) + val textColor = style.textStyle.color.orElse() ?: colors.textColor BoxWithConstraints( modifier = Modifier.textField( enabled = enabled, @@ -212,7 +214,7 @@ fun TextField( .onKeyEvent { keyEventFactory.onKeyEvent?.invoke(it) ?: false }, enabled = enabled, readOnly = readOnly, - textStyle = textStyle, + textStyle = style.textStyle.copy(color = textColor), keyboardOptions = keyboardOptions, keyboardActions = keyboardActions, singleLine = singleLine, @@ -279,8 +281,7 @@ fun TextField( interactionSource: MutableInteractionSource = remember { MutableInteractionSource() }, header: @Composable (() -> Unit)? = null, placeholder: @Composable () -> Unit = {}, - footer: @Composable (() -> Unit)? = null, - textStyle: TextStyle = TextField.textStyle + footer: @Composable (() -> Unit)? = null ) { var textFieldValue by remember { mutableStateOf(TextFieldValue(value)) } TextField( @@ -307,8 +308,7 @@ fun TextField( interactionSource = interactionSource, header = header, placeholder = placeholder, - footer = footer, - textStyle = textStyle + footer = footer ) } @@ -330,8 +330,7 @@ fun PasswordTextField( focusRequester: FocusRequester = remember { FocusRequester() }, interactionSource: MutableInteractionSource = remember { MutableInteractionSource() }, header: @Composable (() -> Unit)? = null, - placeholder: @Composable () -> Unit = {}, - textStyle: TextStyle = TextField.textStyle + placeholder: @Composable () -> Unit = {} ) { var passwordVisible by remember { mutableStateOf(defaultPasswordVisible) } TextField( @@ -377,8 +376,7 @@ fun PasswordTextField( interactionSource = cInteractionSource ) { Icon(imageVector = if (passwordVisible) Icons.ViewerOpen else Icons.ViewerClose) } } - }, - textStyle = textStyle + } ) } @@ -400,8 +398,7 @@ fun PasswordTextField( focusRequester: FocusRequester = remember { FocusRequester() }, interactionSource: MutableInteractionSource = remember { MutableInteractionSource() }, header: @Composable (() -> Unit)? = null, - placeholder: @Composable () -> Unit = {}, - textStyle: TextStyle = TextField.textStyle + placeholder: @Composable () -> Unit = {} ) { var textFieldValue by remember { mutableStateOf(TextFieldValue(value)) } PasswordTextField( @@ -424,8 +421,7 @@ fun PasswordTextField( focusRequester = focusRequester, interactionSource = interactionSource, header = header, - placeholder = placeholder, - textStyle = textStyle + placeholder = placeholder ) } @@ -450,8 +446,7 @@ fun BackspaceTextField( focusRequester: FocusRequester = remember { FocusRequester() }, interactionSource: MutableInteractionSource = remember { MutableInteractionSource() }, header: @Composable (() -> Unit)? = null, - placeholder: @Composable () -> Unit = {}, - textStyle: TextStyle = TextField.textStyle + placeholder: @Composable () -> Unit = {} ) { TextField( value = value, @@ -499,8 +494,7 @@ fun BackspaceTextField( interactionSource = cInteractionSource ) { Icon(imageVector = Icons.Backspace) } } - }, - textStyle = textStyle + } ) } @@ -525,8 +519,7 @@ fun BackspaceTextField( focusRequester: FocusRequester = remember { FocusRequester() }, interactionSource: MutableInteractionSource = remember { MutableInteractionSource() }, header: @Composable (() -> Unit)? = null, - placeholder: @Composable () -> Unit = {}, - textStyle: TextStyle = TextField.textStyle + placeholder: @Composable () -> Unit = {} ) { var textFieldValue by remember { mutableStateOf(TextFieldValue(value)) } BackspaceTextField( @@ -552,8 +545,7 @@ fun BackspaceTextField( focusRequester = focusRequester, interactionSource = interactionSource, header = header, - placeholder = placeholder, - textStyle = textStyle + placeholder = placeholder ) } @@ -691,7 +683,7 @@ private fun TextFieldDecorationBox( val animatedAlpha by animateFloatAsState(if (value.isNotEmpty()) 0f else 1f) Box(modifier = Modifier.alpha(animatedAlpha)) { CompositionLocalProvider( - LocalTextStyle provides LocalTextStyle.current.default(placeholderContentColor) + LocalTextStyle provides LocalTextStyle.current.copy(placeholderContentColor) ) { placeholder() } } innerTextField() @@ -761,15 +753,12 @@ object TextField { @Composable @ReadOnlyComposable get() = defaultTextFieldStyle() - val textStyle: TextStyle - @Composable - @ReadOnlyComposable - get() = LocalTextStyle.current.default(LocalColors.current.textPrimary) } @Composable @ReadOnlyComposable private fun defaultTextFieldColors() = TextFieldColors( + textColor = defaultTextColor(), cursorColor = LocalColors.current.themePrimary, selectionColors = TextSelectionColors( handleColor = LocalColors.current.themePrimary, @@ -790,6 +779,7 @@ private fun defaultTextFieldColors() = TextFieldColors( @Composable @ReadOnlyComposable private fun defaultTextFieldStyle() = TextFieldStyle( + textStyle = LocalTextStyle.current, padding = PaddingValues(LocalSizes.current.spacingSecondary), shape = withAreaBoxShape(), borderInactive = defaultTextFieldInactiveBorder(),