Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose TextComponent functions which accept androidx.compose.ui.text.TextStyle #650

Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
* limitations under the License.
*/

@file:Suppress("DeprecatedCallableAddReplaceWith")

package com.patrykandpatrick.vico.compose.cartesian.axis

import android.graphics.Typeface
Expand All @@ -23,6 +25,8 @@ import androidx.compose.runtime.Composable
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.RectangleShape
import androidx.compose.ui.graphics.isSpecified
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.TextUnit
import androidx.compose.ui.unit.dp
Expand All @@ -33,6 +37,7 @@ import com.patrykandpatrick.vico.compose.common.of
import com.patrykandpatrick.vico.compose.common.shader.BrushShader
import com.patrykandpatrick.vico.compose.common.shape.dashed
import com.patrykandpatrick.vico.compose.common.shape.toVicoShape
import com.patrykandpatrick.vico.compose.common.VicoTheme
import com.patrykandpatrick.vico.compose.common.vicoTheme
import com.patrykandpatrick.vico.core.common.Defaults
import com.patrykandpatrick.vico.core.common.Dimensions
Expand All @@ -56,6 +61,9 @@ import com.patrykandpatrick.vico.core.common.shape.Shape
* @param typeface the [Typeface] for the text.
* @param textAlignment the text alignment.
*/
@Deprecated(
message = "Use the Composable function that accepts a Compose UI TextStyle instead of an android graphics Typeface.",
)
@Composable
public fun rememberAxisLabelComponent(
color: Color = vicoTheme.textColor,
Expand All @@ -82,6 +90,44 @@ public fun rememberAxisLabelComponent(
textAlignment,
)

/**
* Creates and remembers a [TextComponent] to be used for axis labels.
*
* @param textStyle the [TextStyle] for the text. This parameter controls the text's font, the text's color, and the
* size of the text. A [Color.Unspecified] text color will default this text's color to [VicoTheme.textColor].
* @param background an optional [ShapeComponent] to be displayed behind the text.
* @param ellipsize the text truncation behavior.
* @param lineCount the line count.
* @param padding the padding between the text and the background.
* @param margins the margins around the background.
* @param textAlignment the text alignment.
*/
@Composable
public fun rememberAxisLabelComponent(
textStyle: TextStyle = TextStyle.Default,
background: ShapeComponent? = null,
ellipsize: TextUtils.TruncateAt = TextUtils.TruncateAt.END,
lineCount: Int = Defaults.AXIS_LABEL_MAX_LINES,
padding: MutableDimensions = MutableDimensions.of(
Defaults.AXIS_LABEL_HORIZONTAL_PADDING.dp,
Defaults.AXIS_LABEL_VERTICAL_PADDING.dp
),
margins: MutableDimensions = MutableDimensions.of(
Defaults.AXIS_LABEL_HORIZONTAL_MARGIN.dp,
Defaults.AXIS_LABEL_VERTICAL_MARGIN.dp
),
textAlignment: Layout.Alignment = Layout.Alignment.ALIGN_NORMAL,
): TextComponent = rememberTextComponent(
if (textStyle.color.isSpecified) textStyle else textStyle.copy(color = vicoTheme.textColor),
background,
ellipsize,
lineCount,
padding,
margins,
textAlignment,
)


/**
* Creates and remembers a [LineComponent] styled as an axis line.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,16 @@ import androidx.compose.runtime.Stable
import androidx.compose.runtime.remember
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.isSpecified
import androidx.compose.ui.graphics.toArgb
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.TextUnit
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.patrykandpatrick.vico.compose.common.pixelSize
import com.patrykandpatrick.vico.compose.common.shader.toDynamicShader
import com.patrykandpatrick.vico.compose.common.text.toGraphicsTypeFace
import com.patrykandpatrick.vico.core.common.Defaults
import com.patrykandpatrick.vico.core.common.Dimensions
import com.patrykandpatrick.vico.core.common.LayeredComponent
Expand Down Expand Up @@ -154,6 +157,9 @@ public fun rememberLayeredComponent(
* @param textAlignment the text alignment.
* @param minWidth defines the minimum width.
*/
@Deprecated(
message = "Use the Composable function that accepts a Compose UI TextStyle instead of an android graphics Typeface.",
)
kevincianfarini marked this conversation as resolved.
Show resolved Hide resolved
@Composable
public fun rememberTextComponent(
color: Color = Color.Black,
Expand Down Expand Up @@ -182,6 +188,47 @@ public fun rememberTextComponent(
}
}

/**
* Create and remember [TextComponent].
*
* @param textStyle the [TextStyle] for the text. This parameter controls the text's font, the text's color, and the
* size of the text. A [Color.Unspecified] text color will default this text's color to [Color.Black].
* @param background an optional [ShapeComponent] to be displayed behind the text.
* @param ellipsize the text truncation behavior.
* @param lineCount the line count.
* @param padding the padding between the text and the background.
* @param margins the margins around the background.
* @param textAlignment the text alignment.
* @param minWidth defines the minimum width.
*/
@Composable
public fun rememberTextComponent(
textStyle: TextStyle = TextStyle.Default,
background: ShapeComponent? = null,
ellipsize: TextUtils.TruncateAt = TextUtils.TruncateAt.END,
lineCount: Int = Defaults.LABEL_LINE_COUNT,
padding: MutableDimensions = MutableDimensions.empty(),
margins: MutableDimensions = MutableDimensions.empty(),
textAlignment: Layout.Alignment = Layout.Alignment.ALIGN_NORMAL,
minWidth: TextComponent.MinWidth = TextComponent.MinWidth.fixed(),
): TextComponent {
val typeface = textStyle.toGraphicsTypeFace()
return remember(textStyle, typeface, background, ellipsize, lineCount, padding, margins, typeface, textAlignment, minWidth) {
TextComponent.build {
this.color = if (textStyle.color.isSpecified) textStyle.color.toArgb() else Color.Black.toArgb()
kevincianfarini marked this conversation as resolved.
Show resolved Hide resolved
this.textSizeSp = textStyle.fontSize.pixelSize()
this.ellipsize = ellipsize
this.lineCount = lineCount
this.background = background
this.padding = padding
this.margins = margins
this.typeface = typeface
this.textAlignment = textAlignment
this.minWidth = minWidth
}
}
}

/** A [Dp] version of [TextComponent.MinWidth.fixed]. */
@Stable
public fun TextComponent.MinWidth.Companion.fixed(value: Dp = 0.dp): TextComponent.MinWidth = fixed(value.value)
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright 2024 by Patryk Goworowski and Patrick Michalik.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.patrykandpatrick.vico.compose.common.text

import android.graphics.Typeface
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.platform.LocalFontFamilyResolver
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontStyle
import androidx.compose.ui.text.font.FontSynthesis
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.font.resolveAsTypeface

/**
* Coerce a Jetpack Compose UI [TextStyle] to a corresponding [Typeface].
*/
@Composable
internal fun TextStyle.toGraphicsTypeFace(): Typeface {
val resolver = LocalFontFamilyResolver.current
return remember(resolver, this) {
resolver.resolveAsTypeface(
fontFamily = this.fontFamily,
fontWeight = this.fontWeight ?: FontWeight.Normal,
fontStyle = this.fontStyle ?: FontStyle.Normal,
fontSynthesis = this.fontSynthesis ?: FontSynthesis.All,
)
}.value
}
Loading