Skip to content
This repository has been archived by the owner on Aug 7, 2024. It is now read-only.

Commit

Permalink
Merge pull request #44 from ricknout/snapshot
Browse files Browse the repository at this point in the history
Handle RTL for shape corner sizes
  • Loading branch information
ricknout authored Feb 8, 2021
2 parents cb1a66b + 050d413 commit b9c179d
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 15 deletions.
4 changes: 2 additions & 2 deletions lib/api/lib.api
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
public final class com/google/android/material/composethemeadapter/MdcTheme {
public static final fun MdcTheme (Landroid/content/Context;ZZZZLkotlin/jvm/functions/Function2;Landroidx/compose/runtime/Composer;II)V
public static final fun createMdcTheme (Landroid/content/Context;Landroidx/compose/ui/unit/Density;ZZZZ)Lcom/google/android/material/composethemeadapter/ThemeParameters;
public static synthetic fun createMdcTheme$default (Landroid/content/Context;Landroidx/compose/ui/unit/Density;ZZZZILjava/lang/Object;)Lcom/google/android/material/composethemeadapter/ThemeParameters;
public static final fun createMdcTheme (Landroid/content/Context;Landroidx/compose/ui/unit/LayoutDirection;Landroidx/compose/ui/unit/Density;ZZZZ)Lcom/google/android/material/composethemeadapter/ThemeParameters;
public static synthetic fun createMdcTheme$default (Landroid/content/Context;Landroidx/compose/ui/unit/LayoutDirection;Landroidx/compose/ui/unit/Density;ZZZZILjava/lang/Object;)Lcom/google/android/material/composethemeadapter/ThemeParameters;
}

public final class com/google/android/material/composethemeadapter/ThemeParameters {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package com.google.android.material.composethemeadapter

import android.content.Context
import android.content.res.Resources
import android.view.View
import androidx.compose.material.Colors
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Shapes
Expand All @@ -27,9 +28,11 @@ import androidx.compose.material.darkColors
import androidx.compose.material.lightColors
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.platform.LocalConfiguration
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.unit.Density
import androidx.compose.ui.unit.LayoutDirection
import androidx.core.content.res.getResourceIdOrThrow
import androidx.core.content.res.use
import java.lang.reflect.Method
Expand All @@ -43,12 +46,17 @@ import java.lang.reflect.Method
* [androidx.compose.material.ContentAlpha] through [androidx.compose.material.AmbientContentAlpha].
* You can customize this through the [setTextColors] parameter.
*
* For [Shapes], the configuration layout direction is taken into account when reading corner sizes
* of `ShapeAppearance`s from the theme. For example, [Shapes.medium.topStart] will be read from
* `cornerSizeTopLeft` for [View.LAYOUT_DIRECTION_LTR] and `cornerSizeTopRight` for
* [View.LAYOUT_DIRECTION_RTL].
*
* @param context The context to read the theme from.
* @param readColors whether the read the MDC color palette from the context's theme.
* @param readColors whether the read the MDC color palette from the [context]'s theme.
* If `false`, the current value of [MaterialTheme.colors] is preserved.
* @param readTypography whether the read the MDC text appearances from [context]'s theme.
* If `false`, the current value of [MaterialTheme.typography] is preserved.
* @param readShapes whether the read the MDC shape appearances from the context's theme.
* @param readShapes whether the read the MDC shape appearances from the [context]'s theme.
* If `false`, the current value of [MaterialTheme.shapes] is preserved.
* @param setTextColors whether to read the colors from the `TextAppearance`s associated from the
* theme. Defaults to `false`.
Expand All @@ -71,9 +79,15 @@ fun MdcTheme(
// (via `applyStyle()`, `rebase()`, `setTo()`), but the majority of apps do not use those.
val key = context.theme.key ?: context.theme

val layoutDirection = when (LocalConfiguration.current.layoutDirection) {
View.LAYOUT_DIRECTION_RTL -> LayoutDirection.Rtl
else -> LayoutDirection.Ltr
}

val themeParams = remember(key) {
createMdcTheme(
context = context,
layoutDirection = layoutDirection,
readColors = readColors,
readTypography = readTypography,
readShapes = readShapes,
Expand Down Expand Up @@ -108,22 +122,28 @@ data class ThemeParameters(
* [androidx.compose.material.ContentAlpha] through [androidx.compose.material.AmbientContentAlpha].
* You can customize this through the [setTextColors] parameter.
*
* For [Shapes], the [layoutDirection] is taken into account when reading corner sizes of
* `ShapeAppearance`s from the theme. For example, [Shapes.medium.topStart] will be read from
* `cornerSizeTopLeft` for [LayoutDirection.Ltr] and `cornerSizeTopRight` for [LayoutDirection.Rtl].
*
* The individual components of the returned [ThemeParameters] may be `null`, depending on the
* matching 'read' parameter. For example, if you set [readColors] to `false`,
* [ThemeParameters.colors] will be null.
*
* @param context The context to read the theme from.
* @param layoutDirection The layout direction to be used when reading shapes.
* @param density The current density.
* @param readColors whether the read the MDC color palette from the context's theme.
* @param readColors whether the read the MDC color palette from the [context]'s theme.
* @param readTypography whether the read the MDC text appearances from [context]'s theme.
* @param readShapes whether the read the MDC shape appearances from the context's theme.
* @param readShapes whether the read the MDC shape appearances from the [context]'s theme.
* @param setTextColors whether to read the colors from the `TextAppearance`s associated from the
* theme. Defaults to `false`.
* @return [ThemeParameters] instance containing the resulting [Colors], [Typography]
* and [Shapes].
*/
fun createMdcTheme(
context: Context,
layoutDirection: LayoutDirection,
density: Density = Density(context),
readColors: Boolean = true,
readTypography: Boolean = true,
Expand Down Expand Up @@ -277,24 +297,27 @@ fun createMdcTheme(
} else null

/**
* Now read the shape appearances
* Now read the shape appearances, taking into account the layout direction.
*/
val shapes = if (readShapes) {
Shapes(
small = parseShapeAppearance(
context = context,
id = ta.getResourceIdOrThrow(R.styleable.ComposeThemeAdapterTheme_shapeAppearanceSmallComponent),
fallbackShape = emptyShapes.small
fallbackShape = emptyShapes.small,
layoutDirection = layoutDirection
),
medium = parseShapeAppearance(
context = context,
id = ta.getResourceIdOrThrow(R.styleable.ComposeThemeAdapterTheme_shapeAppearanceMediumComponent),
fallbackShape = emptyShapes.medium
fallbackShape = emptyShapes.medium,
layoutDirection = layoutDirection
),
large = parseShapeAppearance(
context = context,
id = ta.getResourceIdOrThrow(R.styleable.ComposeThemeAdapterTheme_shapeAppearanceLargeComponent),
fallbackShape = emptyShapes.large
fallbackShape = emptyShapes.large,
layoutDirection = layoutDirection
)
)
} else null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import androidx.compose.ui.text.font.FontStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.font.toFontFamily
import androidx.compose.ui.unit.Density
import androidx.compose.ui.unit.LayoutDirection
import androidx.compose.ui.unit.TextUnit
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.em
Expand Down Expand Up @@ -131,24 +132,30 @@ internal fun textStyleFromTextAppearance(
internal fun parseShapeAppearance(
context: Context,
@StyleRes id: Int,
fallbackShape: CornerBasedShape
fallbackShape: CornerBasedShape,
layoutDirection: LayoutDirection
): CornerBasedShape {
return context.obtainStyledAttributes(id, R.styleable.ComposeThemeAdapterShapeAppearance).use { a ->
val defaultCornerSize = a.getCornerSizeOrNull(
R.styleable.ComposeThemeAdapterShapeAppearance_cornerSize
)
val cornerSizeTS = a.getCornerSizeOrNull(
val cornerSizeTL = a.getCornerSizeOrNull(
R.styleable.ComposeThemeAdapterShapeAppearance_cornerSizeTopLeft
)
val cornerSizeTE = a.getCornerSizeOrNull(
val cornerSizeTR = a.getCornerSizeOrNull(
R.styleable.ComposeThemeAdapterShapeAppearance_cornerSizeTopRight
)
val cornerSizeBS = a.getCornerSizeOrNull(
val cornerSizeBL = a.getCornerSizeOrNull(
R.styleable.ComposeThemeAdapterShapeAppearance_cornerSizeBottomLeft
)
val cornerSizeBE = a.getCornerSizeOrNull(
val cornerSizeBR = a.getCornerSizeOrNull(
R.styleable.ComposeThemeAdapterShapeAppearance_cornerSizeBottomRight
)
val isRtl = layoutDirection == LayoutDirection.Rtl
val cornerSizeTS = if (isRtl) cornerSizeTR else cornerSizeTL
val cornerSizeTE = if (isRtl) cornerSizeTL else cornerSizeTR
val cornerSizeBS = if (isRtl) cornerSizeBR else cornerSizeBL
val cornerSizeBE = if (isRtl) cornerSizeBL else cornerSizeBR

/**
* We do not support the individual `cornerFamilyTopLeft`, etc, since Compose only supports
Expand Down

0 comments on commit b9c179d

Please sign in to comment.