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 = {},
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

View File

@@ -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()
}

View File

@@ -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)

View File

@@ -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

View File

@@ -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<String, InlineTextContent> = 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
private val DefaultTextStyle = DefaultTypography.primary

View File

@@ -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(),