refactor: merge to new FontColor in TextStyle inheritance relationship

This commit is contained in:
2023-11-29 00:10:43 +08:00
parent 9435ec65a0
commit 70025a7a9f
6 changed files with 34 additions and 99 deletions

View File

@@ -79,7 +79,7 @@ fun Button(
footer: @Composable () -> Unit = {}, footer: @Composable () -> Unit = {},
content: @Composable RowScope.() -> 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( val localProgressIndicatorColors = LocalProgressIndicatorColors.current.copy(
foregroundColor = colors.contentColor, foregroundColor = colors.contentColor,
backgroundColor = Color.Transparent backgroundColor = Color.Transparent

View File

@@ -352,8 +352,6 @@ fun DropdownMenuItem(
style = currentStyle, style = currentStyle,
verticalAlignment = Alignment.CenterVertically 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)) { CompositionLocalProvider(LocalTextStyle provides LocalTextStyle.current.copy(color = currentColor)) {
content() content()
} }

View File

@@ -135,7 +135,7 @@ fun NavigationItem(
val currentContentShape = contentShape ?: LocalNavigationContentShape.current ?: Navigation.style.contentShape val currentContentShape = contentShape ?: LocalNavigationContentShape.current ?: Navigation.style.contentShape
val animatedIndicatorColor by animateColorAsState(if (selected) currentColors.indicatorColor else Color.Transparent) val animatedIndicatorColor by animateColorAsState(if (selected) currentColors.indicatorColor else Color.Transparent)
val animatedContentColor by animateColorAsState(if (selected) currentColors.selectedContentColor else currentColors.unselectedContentColor) val animatedContentColor by animateColorAsState(if (selected) currentColors.selectedContentColor else currentColors.unselectedContentColor)
val currentContentStyle = LocalTextStyle.current.default(animatedContentColor) val currentContentStyle = LocalTextStyle.current.copy(animatedContentColor)
Box( Box(
modifier = Modifier.status(enabled) modifier = Modifier.status(enabled)
.clip(currentContentShape) .clip(currentContentShape)

View File

@@ -210,7 +210,7 @@ fun Tab(
val currentContentPadding = contentPadding ?: LocalTabContentPadding.current ?: Tab.style.contentPadding val currentContentPadding = contentPadding ?: LocalTabContentPadding.current ?: Tab.style.contentPadding
val currentContentShape = contentShape ?: LocalTabContentShape.current ?: Tab.style.contentShape val currentContentShape = contentShape ?: LocalTabContentShape.current ?: Tab.style.contentShape
val contentColor by animateColorAsState(if (selected) currentSelectedContentColor else currentUnselectedContentColor) val contentColor by animateColorAsState(if (selected) currentSelectedContentColor else currentUnselectedContentColor)
val contentStyle = LocalTextStyle.current.default(contentColor) val contentStyle = LocalTextStyle.current.copy(contentColor)
CompositionLocalProvider( CompositionLocalProvider(
LocalIconTint provides contentColor, LocalIconTint provides contentColor,
LocalTextStyle provides contentStyle LocalTextStyle provides contentStyle

View File

@@ -33,13 +33,7 @@ import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.AnnotatedString import androidx.compose.ui.text.AnnotatedString
import androidx.compose.ui.text.TextLayoutResult import androidx.compose.ui.text.TextLayoutResult
import androidx.compose.ui.text.TextStyle 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.text.style.TextOverflow
import androidx.compose.ui.unit.TextUnit
import com.highcapable.flexiui.DefaultTypography import com.highcapable.flexiui.DefaultTypography
import com.highcapable.flexiui.LocalColors import com.highcapable.flexiui.LocalColors
import com.highcapable.flexiui.extension.orElse import com.highcapable.flexiui.extension.orElse
@@ -49,41 +43,25 @@ fun Text(
text: String, text: String,
modifier: Modifier = Modifier, modifier: Modifier = Modifier,
color: Color = Color.Unspecified, color: Color = Color.Unspecified,
fontSize: TextUnit = TextUnit.Unspecified, style: TextStyle = Text.style,
fontStyle: FontStyle? = null,
fontWeight: FontWeight? = null,
fontFamily: FontFamily? = null,
letterSpacing: TextUnit = TextUnit.Unspecified,
textDecoration: TextDecoration? = null,
textAlign: TextAlign? = null,
lineHeight: TextUnit = TextUnit.Unspecified,
overflow: TextOverflow = TextOverflow.Clip, overflow: TextOverflow = TextOverflow.Clip,
softWrap: Boolean = true, softWrap: Boolean = true,
singleLine: Boolean = false, singleLine: Boolean = false,
maxLines: Int = if (singleLine) 1 else Int.MAX_VALUE, maxLines: Int = if (singleLine) 1 else Int.MAX_VALUE,
minLines: Int = 1, minLines: Int = 1,
onTextLayout: (TextLayoutResult) -> Unit = {}, onTextLayout: (TextLayoutResult) -> Unit = {}
style: TextStyle = Text.style
) { ) {
Text( Text(
text = AnnotatedString(text), text = AnnotatedString(text),
modifier = modifier, modifier = modifier,
color = color, color = color,
fontSize = fontSize, style = style,
fontStyle = fontStyle,
fontWeight = fontWeight,
fontFamily = fontFamily,
letterSpacing = letterSpacing,
textDecoration = textDecoration,
textAlign = textAlign,
lineHeight = lineHeight,
overflow = overflow, overflow = overflow,
softWrap = softWrap, softWrap = softWrap,
maxLines = maxLines, maxLines = maxLines,
minLines = minLines, minLines = minLines,
inlineContent = emptyMap(), inlineContent = emptyMap(),
onTextLayout = onTextLayout, onTextLayout = onTextLayout
style = style
) )
} }
@@ -92,39 +70,20 @@ fun Text(
text: AnnotatedString, text: AnnotatedString,
modifier: Modifier = Modifier, modifier: Modifier = Modifier,
color: Color = Color.Unspecified, color: Color = Color.Unspecified,
fontSize: TextUnit = TextUnit.Unspecified, style: TextStyle = Text.style,
fontStyle: FontStyle? = null,
fontWeight: FontWeight? = null,
fontFamily: FontFamily? = null,
letterSpacing: TextUnit = TextUnit.Unspecified,
textDecoration: TextDecoration? = null,
textAlign: TextAlign? = null,
lineHeight: TextUnit = TextUnit.Unspecified,
overflow: TextOverflow = TextOverflow.Clip, overflow: TextOverflow = TextOverflow.Clip,
softWrap: Boolean = true, softWrap: Boolean = true,
singleLine: Boolean = false, singleLine: Boolean = false,
maxLines: Int = if (singleLine) 1 else Int.MAX_VALUE, maxLines: Int = if (singleLine) 1 else Int.MAX_VALUE,
minLines: Int = 1, minLines: Int = 1,
inlineContent: Map<String, InlineTextContent> = mapOf(), inlineContent: Map<String, InlineTextContent> = mapOf(),
onTextLayout: (TextLayoutResult) -> Unit = {}, onTextLayout: (TextLayoutResult) -> Unit = {}
style: TextStyle = Text.style
) { ) {
val currentColor = color.orElse() ?: style.color.orElse() ?: Text.color
BasicText( BasicText(
text = text, text = text,
modifier = modifier, modifier = modifier,
style = style.merge( style = style.copy(color = currentColor),
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
)
),
onTextLayout = onTextLayout, onTextLayout = onTextLayout,
overflow = overflow, overflow = overflow,
softWrap = softWrap, softWrap = softWrap,
@@ -139,28 +98,16 @@ object Text {
@Composable @Composable
@ReadOnlyComposable @ReadOnlyComposable
get() = defaultTextColor() get() = defaultTextColor()
val fontSize: TextUnit
@Composable
@ReadOnlyComposable
get() = style.fontSize
val lineHeight: TextUnit
@Composable
@ReadOnlyComposable
get() = style.lineHeight
val style: TextStyle val style: TextStyle
@Composable @Composable
@ReadOnlyComposable @ReadOnlyComposable
get() = LocalTextStyle.current get() = LocalTextStyle.current
} }
@Composable
@ReadOnlyComposable
internal fun defaultTextColor() = LocalColors.current.textPrimary
internal val LocalTextStyle = compositionLocalOf { DefaultTextStyle } internal val LocalTextStyle = compositionLocalOf { DefaultTextStyle }
internal val DefaultTextStyle = DefaultTypography.primary private 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

View File

@@ -101,6 +101,7 @@ import com.highcapable.flexiui.resources.icon.ViewerOpen
@Immutable @Immutable
data class TextFieldColors( data class TextFieldColors(
val textColor: Color,
val cursorColor: Color, val cursorColor: Color,
val selectionColors: TextSelectionColors, val selectionColors: TextSelectionColors,
val completionColors: AutoCompleteBoxColors, val completionColors: AutoCompleteBoxColors,
@@ -120,6 +121,7 @@ data class AutoCompleteBoxColors(
@Immutable @Immutable
data class TextFieldStyle( data class TextFieldStyle(
val textStyle: TextStyle,
val padding: PaddingValues, val padding: PaddingValues,
val shape: Shape, val shape: Shape,
val borderInactive: BorderStroke, val borderInactive: BorderStroke,
@@ -157,8 +159,7 @@ fun TextField(
interactionSource: MutableInteractionSource = remember { MutableInteractionSource() }, interactionSource: MutableInteractionSource = remember { MutableInteractionSource() },
header: @Composable (() -> Unit)? = null, header: @Composable (() -> Unit)? = null,
placeholder: @Composable () -> Unit = {}, placeholder: @Composable () -> Unit = {},
footer: @Composable (() -> Unit)? = null, footer: @Composable (() -> Unit)? = null
textStyle: TextStyle = TextField.textStyle
) { ) {
val focused by interactionSource.collectIsFocusedAsState() val focused by interactionSource.collectIsFocusedAsState()
val hovered by interactionSource.collectIsHoveredAsState() val hovered by interactionSource.collectIsHoveredAsState()
@@ -179,6 +180,7 @@ fun TextField(
focused || hovered -> style.borderInactive focused || hovered -> style.borderInactive
else -> style.borderInactive else -> style.borderInactive
}.copy(animatedBorderWidth, SolidColor(animatedBorderColor)) }.copy(animatedBorderWidth, SolidColor(animatedBorderColor))
val textColor = style.textStyle.color.orElse() ?: colors.textColor
BoxWithConstraints( BoxWithConstraints(
modifier = Modifier.textField( modifier = Modifier.textField(
enabled = enabled, enabled = enabled,
@@ -212,7 +214,7 @@ fun TextField(
.onKeyEvent { keyEventFactory.onKeyEvent?.invoke(it) ?: false }, .onKeyEvent { keyEventFactory.onKeyEvent?.invoke(it) ?: false },
enabled = enabled, enabled = enabled,
readOnly = readOnly, readOnly = readOnly,
textStyle = textStyle, textStyle = style.textStyle.copy(color = textColor),
keyboardOptions = keyboardOptions, keyboardOptions = keyboardOptions,
keyboardActions = keyboardActions, keyboardActions = keyboardActions,
singleLine = singleLine, singleLine = singleLine,
@@ -279,8 +281,7 @@ fun TextField(
interactionSource: MutableInteractionSource = remember { MutableInteractionSource() }, interactionSource: MutableInteractionSource = remember { MutableInteractionSource() },
header: @Composable (() -> Unit)? = null, header: @Composable (() -> Unit)? = null,
placeholder: @Composable () -> Unit = {}, placeholder: @Composable () -> Unit = {},
footer: @Composable (() -> Unit)? = null, footer: @Composable (() -> Unit)? = null
textStyle: TextStyle = TextField.textStyle
) { ) {
var textFieldValue by remember { mutableStateOf(TextFieldValue(value)) } var textFieldValue by remember { mutableStateOf(TextFieldValue(value)) }
TextField( TextField(
@@ -307,8 +308,7 @@ fun TextField(
interactionSource = interactionSource, interactionSource = interactionSource,
header = header, header = header,
placeholder = placeholder, placeholder = placeholder,
footer = footer, footer = footer
textStyle = textStyle
) )
} }
@@ -330,8 +330,7 @@ fun PasswordTextField(
focusRequester: FocusRequester = remember { FocusRequester() }, focusRequester: FocusRequester = remember { FocusRequester() },
interactionSource: MutableInteractionSource = remember { MutableInteractionSource() }, interactionSource: MutableInteractionSource = remember { MutableInteractionSource() },
header: @Composable (() -> Unit)? = null, header: @Composable (() -> Unit)? = null,
placeholder: @Composable () -> Unit = {}, placeholder: @Composable () -> Unit = {}
textStyle: TextStyle = TextField.textStyle
) { ) {
var passwordVisible by remember { mutableStateOf(defaultPasswordVisible) } var passwordVisible by remember { mutableStateOf(defaultPasswordVisible) }
TextField( TextField(
@@ -377,8 +376,7 @@ fun PasswordTextField(
interactionSource = cInteractionSource interactionSource = cInteractionSource
) { Icon(imageVector = if (passwordVisible) Icons.ViewerOpen else Icons.ViewerClose) } ) { Icon(imageVector = if (passwordVisible) Icons.ViewerOpen else Icons.ViewerClose) }
} }
}, }
textStyle = textStyle
) )
} }
@@ -400,8 +398,7 @@ fun PasswordTextField(
focusRequester: FocusRequester = remember { FocusRequester() }, focusRequester: FocusRequester = remember { FocusRequester() },
interactionSource: MutableInteractionSource = remember { MutableInteractionSource() }, interactionSource: MutableInteractionSource = remember { MutableInteractionSource() },
header: @Composable (() -> Unit)? = null, header: @Composable (() -> Unit)? = null,
placeholder: @Composable () -> Unit = {}, placeholder: @Composable () -> Unit = {}
textStyle: TextStyle = TextField.textStyle
) { ) {
var textFieldValue by remember { mutableStateOf(TextFieldValue(value)) } var textFieldValue by remember { mutableStateOf(TextFieldValue(value)) }
PasswordTextField( PasswordTextField(
@@ -424,8 +421,7 @@ fun PasswordTextField(
focusRequester = focusRequester, focusRequester = focusRequester,
interactionSource = interactionSource, interactionSource = interactionSource,
header = header, header = header,
placeholder = placeholder, placeholder = placeholder
textStyle = textStyle
) )
} }
@@ -450,8 +446,7 @@ fun BackspaceTextField(
focusRequester: FocusRequester = remember { FocusRequester() }, focusRequester: FocusRequester = remember { FocusRequester() },
interactionSource: MutableInteractionSource = remember { MutableInteractionSource() }, interactionSource: MutableInteractionSource = remember { MutableInteractionSource() },
header: @Composable (() -> Unit)? = null, header: @Composable (() -> Unit)? = null,
placeholder: @Composable () -> Unit = {}, placeholder: @Composable () -> Unit = {}
textStyle: TextStyle = TextField.textStyle
) { ) {
TextField( TextField(
value = value, value = value,
@@ -499,8 +494,7 @@ fun BackspaceTextField(
interactionSource = cInteractionSource interactionSource = cInteractionSource
) { Icon(imageVector = Icons.Backspace) } ) { Icon(imageVector = Icons.Backspace) }
} }
}, }
textStyle = textStyle
) )
} }
@@ -525,8 +519,7 @@ fun BackspaceTextField(
focusRequester: FocusRequester = remember { FocusRequester() }, focusRequester: FocusRequester = remember { FocusRequester() },
interactionSource: MutableInteractionSource = remember { MutableInteractionSource() }, interactionSource: MutableInteractionSource = remember { MutableInteractionSource() },
header: @Composable (() -> Unit)? = null, header: @Composable (() -> Unit)? = null,
placeholder: @Composable () -> Unit = {}, placeholder: @Composable () -> Unit = {}
textStyle: TextStyle = TextField.textStyle
) { ) {
var textFieldValue by remember { mutableStateOf(TextFieldValue(value)) } var textFieldValue by remember { mutableStateOf(TextFieldValue(value)) }
BackspaceTextField( BackspaceTextField(
@@ -552,8 +545,7 @@ fun BackspaceTextField(
focusRequester = focusRequester, focusRequester = focusRequester,
interactionSource = interactionSource, interactionSource = interactionSource,
header = header, header = header,
placeholder = placeholder, placeholder = placeholder
textStyle = textStyle
) )
} }
@@ -691,7 +683,7 @@ private fun TextFieldDecorationBox(
val animatedAlpha by animateFloatAsState(if (value.isNotEmpty()) 0f else 1f) val animatedAlpha by animateFloatAsState(if (value.isNotEmpty()) 0f else 1f)
Box(modifier = Modifier.alpha(animatedAlpha)) { Box(modifier = Modifier.alpha(animatedAlpha)) {
CompositionLocalProvider( CompositionLocalProvider(
LocalTextStyle provides LocalTextStyle.current.default(placeholderContentColor) LocalTextStyle provides LocalTextStyle.current.copy(placeholderContentColor)
) { placeholder() } ) { placeholder() }
} }
innerTextField() innerTextField()
@@ -761,15 +753,12 @@ object TextField {
@Composable @Composable
@ReadOnlyComposable @ReadOnlyComposable
get() = defaultTextFieldStyle() get() = defaultTextFieldStyle()
val textStyle: TextStyle
@Composable
@ReadOnlyComposable
get() = LocalTextStyle.current.default(LocalColors.current.textPrimary)
} }
@Composable @Composable
@ReadOnlyComposable @ReadOnlyComposable
private fun defaultTextFieldColors() = TextFieldColors( private fun defaultTextFieldColors() = TextFieldColors(
textColor = defaultTextColor(),
cursorColor = LocalColors.current.themePrimary, cursorColor = LocalColors.current.themePrimary,
selectionColors = TextSelectionColors( selectionColors = TextSelectionColors(
handleColor = LocalColors.current.themePrimary, handleColor = LocalColors.current.themePrimary,
@@ -790,6 +779,7 @@ private fun defaultTextFieldColors() = TextFieldColors(
@Composable @Composable
@ReadOnlyComposable @ReadOnlyComposable
private fun defaultTextFieldStyle() = TextFieldStyle( private fun defaultTextFieldStyle() = TextFieldStyle(
textStyle = LocalTextStyle.current,
padding = PaddingValues(LocalSizes.current.spacingSecondary), padding = PaddingValues(LocalSizes.current.spacingSecondary),
shape = withAreaBoxShape(), shape = withAreaBoxShape(),
borderInactive = defaultTextFieldInactiveBorder(), borderInactive = defaultTextFieldInactiveBorder(),