-
Notifications
You must be signed in to change notification settings - Fork 585
/
MaterialThemeFromMdcTheme.kt
640 lines (608 loc) · 27 KB
/
MaterialThemeFromMdcTheme.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
/*
* Copyright 2020 The Android Open Source Project
*
* 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
*
* https://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 dev.chrisbanes.accompanist.mdctheme
import android.content.Context
import android.content.res.Configuration
import android.content.res.Resources
import android.content.res.TypedArray
import android.graphics.Typeface
import android.os.Build
import android.util.TypedValue
import androidx.annotation.StyleRes
import androidx.compose.Composable
import androidx.compose.remember
import androidx.core.content.res.getColorOrThrow
import androidx.core.content.res.getResourceIdOrThrow
import androidx.core.content.res.use
import androidx.ui.core.ContextAmbient
import androidx.ui.foundation.shape.corner.CornerBasedShape
import androidx.ui.foundation.shape.corner.CornerSize
import androidx.ui.foundation.shape.corner.CutCornerShape
import androidx.ui.foundation.shape.corner.RoundedCornerShape
import androidx.ui.geometry.Offset
import androidx.ui.graphics.Color
import androidx.ui.graphics.Shadow
import androidx.ui.material.ColorPalette
import androidx.ui.material.MaterialTheme
import androidx.ui.material.Shapes
import androidx.ui.material.Typography
import androidx.ui.material.darkColorPalette
import androidx.ui.material.lightColorPalette
import androidx.ui.text.TextStyle
import androidx.ui.text.font.FontFamily
import androidx.ui.text.font.FontStyle
import androidx.ui.text.font.FontWeight
import androidx.ui.text.font.asFontFamily
import androidx.ui.text.font.font
import androidx.ui.unit.Density
import androidx.ui.unit.TextUnit
import androidx.ui.unit.dp
import androidx.ui.unit.em
import androidx.ui.unit.sp
import java.lang.reflect.Method
import kotlin.concurrent.getOrSet
/**
* A [MaterialTheme] which reads the corresponding values from an
* Material Design Components Android theme in the given [context].
*
* By default the text colors from any associated `TextAppearance`s from the theme are *not* read.
* This is because setting a fixed color in the resulting [TextStyle] breaks the usage of
* [androidx.ui.material.Emphasis] through [androidx.ui.material.ProvideEmphasis].
* You can customize this through the [useTextColors] parameter.
*
* @param context The context to read the theme from
* @param readColors whether the read the MDC color palette from the context's theme
* @param readTypography whether the read the MDC typography text appearances from the context's theme
* @param readShapes whether the read the MDC shape appearances from the context's theme
* @param useTextColors whether to read the colors from the `TextAppearance`s associated from the
* theme. Defaults to `false`
*/
@Composable
fun MaterialThemeFromMdcTheme(
context: Context = ContextAmbient.current,
readColors: Boolean = true,
readTypography: Boolean = true,
readShapes: Boolean = true,
useTextColors: Boolean = false,
children: @Composable () -> Unit
) {
// We try and use the theme key value if available, which should be a perfect key for caching
// and avoid the expensive theme lookups in re-compositions.
//
// If the key is not available, we use the Theme itself as a rough approximation. Using the
// Theme instance as the key is not perfect, but it should work for 90% of cases.
// It falls down when the theme is manually mutated after a composition has happened
// (via `applyStyle()`, `rebase()`, `setTo()`), but the majority of apps do not use those.
val key = context.theme.key ?: context.theme
val (colors, type, shapes) = remember(key) {
generateMaterialThemeFromMdcTheme(
context = context,
readColors = readColors,
readTypography = readTypography,
readShapes = readShapes,
useTextColors = useTextColors
)
}
MaterialTheme(
typography = type,
colors = colors,
shapes = shapes,
content = children
)
}
/**
* This effect generates the components of a [androidx.ui.material.MaterialTheme], reading the
* values from an Material Design Components Android theme.
*
* By default the text colors from any associated `TextAppearance`s from the theme are *not* read.
* This is because setting a fixed color in the resulting [TextStyle] breaks the usage of
* [androidx.ui.material.Emphasis] through [androidx.ui.material.ProvideEmphasis].
* You can customize this through the [useTextColors] parameter.
*
* @param context The context to read the theme from
* @param density The current density
* @param readColors whether the read the MDC color palette from the context's theme
* @param readTypography whether the read the MDC typography text appearances from the context's theme
* @param readShapes whether the read the MDC shape appearances from the context's theme
* @param useTextColors whether to read the colors from the `TextAppearance`s associated from the
* theme. Defaults to `false`
*/
fun generateMaterialThemeFromMdcTheme(
context: Context,
density: Density = Density(context),
readColors: Boolean = true,
readTypography: Boolean = true,
readShapes: Boolean = true,
useTextColors: Boolean = false
): Triple<ColorPalette, Typography, Shapes> {
return context.obtainStyledAttributes(R.styleable.AccompanistMdcTheme).use { ta ->
require(ta.hasValue(R.styleable.AccompanistMdcTheme_isMaterialTheme)) {
"MaterialThemeUsingMdcTheme requires the host context's theme" +
" to extend Theme.MaterialComponents"
}
val colors: ColorPalette = if (readColors) {
/* First we'll read the Material color palette */
val primary = ta.getComposeColor(R.styleable.AccompanistMdcTheme_colorPrimary)
val primaryVariant = ta.getComposeColor(R.styleable.AccompanistMdcTheme_colorPrimaryVariant)
val onPrimary = ta.getComposeColor(R.styleable.AccompanistMdcTheme_colorOnPrimary)
val secondary = ta.getComposeColor(R.styleable.AccompanistMdcTheme_colorSecondary)
val secondaryVariant = ta.getComposeColor(R.styleable.AccompanistMdcTheme_colorSecondaryVariant)
val onSecondary = ta.getComposeColor(R.styleable.AccompanistMdcTheme_colorOnSecondary)
val background = ta.getComposeColor(R.styleable.AccompanistMdcTheme_android_colorBackground)
val onBackground = ta.getComposeColor(R.styleable.AccompanistMdcTheme_colorOnBackground)
val surface = ta.getComposeColor(R.styleable.AccompanistMdcTheme_colorSurface)
val onSurface = ta.getComposeColor(R.styleable.AccompanistMdcTheme_colorOnSurface)
val error = ta.getComposeColor(R.styleable.AccompanistMdcTheme_colorError)
val onError = ta.getComposeColor(R.styleable.AccompanistMdcTheme_colorOnError)
val isLightTheme = ta.getBoolean(R.styleable.AccompanistMdcTheme_isLightTheme, true)
if (isLightTheme) {
lightColorPalette(
primary = primary,
primaryVariant = primaryVariant,
onPrimary = onPrimary,
secondary = secondary,
secondaryVariant = secondaryVariant,
onSecondary = onSecondary,
background = background,
onBackground = onBackground,
surface = surface,
onSurface = onSurface,
error = error,
onError = onError
)
} else {
darkColorPalette(
primary = primary,
primaryVariant = primaryVariant,
onPrimary = onPrimary,
secondary = secondary,
onSecondary = onSecondary,
background = background,
onBackground = onBackground,
surface = surface,
onSurface = onSurface,
error = error,
onError = onError
)
}
} else {
// Else we create an empty color palette based on the configuration's uiMode
val uiMode = context.resources.configuration.uiMode
if ((uiMode and Configuration.UI_MODE_NIGHT_MASK) == Configuration.UI_MODE_NIGHT_YES) {
darkColorPalette()
} else {
lightColorPalette()
}
}
/**
* Next we'll generate a typography instance, using the Material Theme text appearances
* for TextStyles.
*
* We create a normal 'empty' instance first to start from the defaults, then merge in our
* generated text styles from the Android theme.
*/
var typography = Typography()
if (readTypography) {
typography = typography.merge(
h1 = textStyleFromTextAppearance(
context,
density,
ta.getResourceIdOrThrow(R.styleable.AccompanistMdcTheme_textAppearanceHeadline1),
useTextColors
),
h2 = textStyleFromTextAppearance(
context,
density,
ta.getResourceIdOrThrow(R.styleable.AccompanistMdcTheme_textAppearanceHeadline2),
useTextColors
),
h3 = textStyleFromTextAppearance(
context,
density,
ta.getResourceIdOrThrow(R.styleable.AccompanistMdcTheme_textAppearanceHeadline3),
useTextColors
),
h4 = textStyleFromTextAppearance(
context,
density,
ta.getResourceIdOrThrow(R.styleable.AccompanistMdcTheme_textAppearanceHeadline4),
useTextColors
),
h5 = textStyleFromTextAppearance(
context,
density,
ta.getResourceIdOrThrow(R.styleable.AccompanistMdcTheme_textAppearanceHeadline5),
useTextColors
),
h6 = textStyleFromTextAppearance(
context,
density,
ta.getResourceIdOrThrow(R.styleable.AccompanistMdcTheme_textAppearanceHeadline6),
useTextColors
),
subtitle1 = textStyleFromTextAppearance(
context,
density,
ta.getResourceIdOrThrow(R.styleable.AccompanistMdcTheme_textAppearanceSubtitle1),
useTextColors
),
subtitle2 = textStyleFromTextAppearance(
context,
density,
ta.getResourceIdOrThrow(R.styleable.AccompanistMdcTheme_textAppearanceSubtitle2),
useTextColors
),
body1 = textStyleFromTextAppearance(
context,
density,
ta.getResourceIdOrThrow(R.styleable.AccompanistMdcTheme_textAppearanceBody1),
useTextColors
),
body2 = textStyleFromTextAppearance(
context,
density,
ta.getResourceIdOrThrow(R.styleable.AccompanistMdcTheme_textAppearanceBody2),
useTextColors
),
button = textStyleFromTextAppearance(
context,
density,
ta.getResourceIdOrThrow(R.styleable.AccompanistMdcTheme_textAppearanceButton),
useTextColors
),
caption = textStyleFromTextAppearance(
context,
density,
ta.getResourceIdOrThrow(R.styleable.AccompanistMdcTheme_textAppearanceCaption),
useTextColors
),
overline = textStyleFromTextAppearance(
context,
density,
ta.getResourceIdOrThrow(R.styleable.AccompanistMdcTheme_textAppearanceOverline),
useTextColors
)
)
}
/**
* Now read the shape appearances
*/
val shapes = if (readShapes) {
Shapes(
small = readShapeAppearance(
context = context,
id = ta.getResourceIdOrThrow(R.styleable.AccompanistMdcTheme_shapeAppearanceSmallComponent),
fallbackSize = CornerSize(4.dp)
),
medium = readShapeAppearance(
context = context,
id = ta.getResourceIdOrThrow(R.styleable.AccompanistMdcTheme_shapeAppearanceMediumComponent),
fallbackSize = CornerSize(4.dp)
),
large = readShapeAppearance(
context = context,
id = ta.getResourceIdOrThrow(R.styleable.AccompanistMdcTheme_shapeAppearanceLargeComponent),
fallbackSize = CornerSize(0.dp)
)
)
} else {
Shapes()
}
Triple(colors, typography, shapes)
}
}
private fun textStyleFromTextAppearance(
context: Context,
density: Density,
@StyleRes id: Int,
useTextColor: Boolean
): TextStyle {
return context.obtainStyledAttributes(id, R.styleable.AccompanistMdcTextAppearance).use { a ->
val textStyle = a.getInt(R.styleable.AccompanistMdcTextAppearance_android_textStyle, -1)
val textFontWeight = a.getInt(R.styleable.AccompanistMdcTextAppearance_android_textFontWeight, -1)
val typeface = a.getInt(R.styleable.AccompanistMdcTextAppearance_android_typeface, -1)
// TODO read and expand android:fontVariationSettings.
// Variable fonts are not supported in Compose yet
// FYI, this only works with static font files in assets
val fontFamilyWeight = when {
a.hasValue(R.styleable.AccompanistMdcTextAppearance_android_fontFamily) -> {
a.getFontFamilyOrNull(R.styleable.AccompanistMdcTextAppearance_android_fontFamily)
}
a.hasValue(R.styleable.AccompanistMdcTextAppearance_fontFamily) -> {
a.getFontFamilyOrNull(R.styleable.AccompanistMdcTextAppearance_fontFamily)
}
else -> null
}
TextStyle(
color = if (useTextColor) {
a.getComposeColor(R.styleable.AccompanistMdcTextAppearance_android_textColor)
} else Color.Unset,
fontSize = a.getTextUnit(R.styleable.AccompanistMdcTextAppearance_android_textSize, density),
lineHeight = a.getTextUnit(R.styleable.AccompanistMdcTextAppearance_android_lineHeight, density),
fontFamily = when {
fontFamilyWeight != null -> fontFamilyWeight.fontFamily
// Values below are from frameworks/base attrs.xml
typeface == 1 -> FontFamily.SansSerif
typeface == 2 -> FontFamily.Serif
typeface == 3 -> FontFamily.Monospace
else -> null
},
fontStyle = if ((textStyle and Typeface.ITALIC) != 0) FontStyle.Italic else FontStyle.Normal,
fontWeight = when {
textFontWeight in 0..149 -> FontWeight.W100
textFontWeight in 150..249 -> FontWeight.W200
textFontWeight in 250..349 -> FontWeight.W300
textFontWeight in 350..449 -> FontWeight.W400
textFontWeight in 450..549 -> FontWeight.W500
textFontWeight in 550..649 -> FontWeight.W600
textFontWeight in 650..749 -> FontWeight.W700
textFontWeight in 750..849 -> FontWeight.W800
textFontWeight in 850..999 -> FontWeight.W900
// Else, check the text style for bold
(textStyle and Typeface.BOLD) != 0 -> FontWeight.Bold
// Else, the font family might have an implicit weight (san-serif-light, etc)
fontFamilyWeight != null -> fontFamilyWeight.weight
else -> null
},
fontFeatureSettings = a.getString(R.styleable.AccompanistMdcTextAppearance_android_fontFeatureSettings),
shadow = run {
val shadowColor = a.getComposeColor(R.styleable.AccompanistMdcTextAppearance_android_shadowColor)
if (shadowColor != Color.Unset) {
val dx = a.getFloat(R.styleable.AccompanistMdcTextAppearance_android_shadowDx, 0f)
val dy = a.getFloat(R.styleable.AccompanistMdcTextAppearance_android_shadowDy, 0f)
val rad = a.getFloat(R.styleable.AccompanistMdcTextAppearance_android_shadowRadius, 0f)
Shadow(color = shadowColor, offset = Offset(dx, dy), blurRadius = rad)
} else null
},
letterSpacing = when {
a.hasValue(R.styleable.AccompanistMdcTextAppearance_android_letterSpacing) -> {
a.getFloat(R.styleable.AccompanistMdcTextAppearance_android_letterSpacing, 0f).em
}
else -> TextUnit.Inherit
}
)
}
}
private fun readShapeAppearance(
context: Context,
@StyleRes id: Int,
fallbackSize: CornerSize
): CornerBasedShape {
return context.obtainStyledAttributes(id, R.styleable.AccompanistMdcShapeAppearance).use { a ->
val defaultCornerSize = a.getCornerSize(
R.styleable.AccompanistMdcShapeAppearance_cornerSize,
fallbackSize
)
val cornerSizeTL = a.getCornerSizeOrNull(
R.styleable.AccompanistMdcShapeAppearance_cornerSizeTopLeft
)
val cornerSizeTR = a.getCornerSizeOrNull(
R.styleable.AccompanistMdcShapeAppearance_cornerSizeTopRight
)
val cornerSizeBL = a.getCornerSizeOrNull(
R.styleable.AccompanistMdcShapeAppearance_cornerSizeBottomLeft
)
val cornerSizeBR = a.getCornerSizeOrNull(
R.styleable.AccompanistMdcShapeAppearance_cornerSizeBottomRight
)
/**
* We do not support the individual `cornerFamilyTopLeft`, etc, since Compose only supports
* one corner type per shape. Therefore we only read the `cornerFamily` attribute.
*/
when (a.getInt(R.styleable.AccompanistMdcShapeAppearance_cornerFamily, 0)) {
0 -> {
RoundedCornerShape(
topLeft = cornerSizeTL ?: defaultCornerSize,
topRight = cornerSizeTR ?: defaultCornerSize,
bottomRight = cornerSizeBR ?: defaultCornerSize,
bottomLeft = cornerSizeBL ?: defaultCornerSize
)
}
1 -> {
CutCornerShape(
topLeft = cornerSizeTL ?: defaultCornerSize,
topRight = cornerSizeTR ?: defaultCornerSize,
bottomRight = cornerSizeBR ?: defaultCornerSize,
bottomLeft = cornerSizeBL ?: defaultCornerSize
)
}
else -> throw IllegalArgumentException("Unknown cornerFamily set in ShapeAppearance")
}
}
}
private fun Typography.merge(
h1: TextStyle = TextStyle(),
h2: TextStyle = TextStyle(),
h3: TextStyle = TextStyle(),
h4: TextStyle = TextStyle(),
h5: TextStyle = TextStyle(),
h6: TextStyle = TextStyle(),
subtitle1: TextStyle = TextStyle(),
subtitle2: TextStyle = TextStyle(),
body1: TextStyle = TextStyle(),
body2: TextStyle = TextStyle(),
button: TextStyle = TextStyle(),
caption: TextStyle = TextStyle(),
overline: TextStyle = TextStyle()
) = copy(
h1 = h1.merge(h1),
h2 = h2.merge(h2),
h3 = h3.merge(h3),
h4 = h4.merge(h4),
h5 = h5.merge(h5),
h6 = h6.merge(h6),
subtitle1 = subtitle1.merge(subtitle1),
subtitle2 = subtitle2.merge(subtitle2),
body1 = body1.merge(body1),
body2 = body2.merge(body2),
button = button.merge(button),
caption = caption.merge(caption),
overline = overline.merge(overline)
)
private val tempTypedValue = ThreadLocal<TypedValue>()
private fun TypedArray.getComposeColor(
index: Int,
fallbackColor: Color = Color.Unset
): Color = if (hasValue(index)) Color(getColorOrThrow(index)) else fallbackColor
/**
* Returns the given index as a [FontFamily] and [FontWeight],
* or [fallback] if the value can not be coerced to a [FontFamily].
*
* @param index index of attribute to retrieve.
* @param fallback Value to return if the attribute is not defined or cannot be coerced to an [FontFamily].
*/
private fun TypedArray.getFontFamily(index: Int, fallback: FontFamily): FontFamilyWeight {
return getFontFamilyOrNull(index) ?: FontFamilyWeight(fallback)
}
/**
* Returns the given index as a [FontFamily] and [FontWeight],
* or `null` if the value can not be coerced to a [FontFamily].
*
* @param index index of attribute to retrieve.
*/
private fun TypedArray.getFontFamilyOrNull(index: Int): FontFamilyWeight? {
val tv = tempTypedValue.getOrSet { TypedValue() }
if (getValue(index, tv) && tv.type == TypedValue.TYPE_STRING) {
if (tv.resourceId != 0) {
// If there's a resource ID, it's probably a @font resource
return FontFamilyWeight(font(tv.resourceId).asFontFamily())
}
return when (tv.string) {
"san-serif" -> FontFamilyWeight(FontFamily.SansSerif)
"sans-serif-thin" -> FontFamilyWeight(FontFamily.SansSerif, FontWeight.Thin)
"san-serif-light" -> FontFamilyWeight(FontFamily.SansSerif, FontWeight.Light)
"sans-serif-medium" -> FontFamilyWeight(FontFamily.SansSerif, FontWeight.Medium)
"sans-serif-black" -> FontFamilyWeight(FontFamily.SansSerif, FontWeight.Black)
"serif" -> FontFamilyWeight(FontFamily.Serif)
"cursive" -> FontFamilyWeight(FontFamily.Cursive)
"monospace" -> FontFamilyWeight(FontFamily.Monospace)
// TODO: Compose does not expose a FontFamily for "sans-serif-condensed" yet
else -> null
}
}
return null
}
private data class FontFamilyWeight(
val fontFamily: FontFamily,
val weight: FontWeight = FontWeight.Normal
)
/**
* Returns the given index as a [TextUnit], or [fallback] if the value can not be coerced to a [TextUnit].
*
* @param index index of attribute to retrieve.
* @param density the current display density.
* @param fallback Value to return if the attribute is not defined or cannot be coerced to an [TextUnit].
*/
private fun TypedArray.getTextUnit(
index: Int,
density: Density,
fallback: TextUnit = TextUnit.Inherit
): TextUnit = getTextUnitOrNull(index, density) ?: fallback
/**
* Returns the given index as a [TextUnit], or `null` if the value can not be coerced to a [TextUnit].
*
* @param index index of attribute to retrieve.
* @param density the current display density.
*/
private fun TypedArray.getTextUnitOrNull(
index: Int,
density: Density
): TextUnit? {
val tv = tempTypedValue.getOrSet { TypedValue() }
if (getValue(index, tv) && tv.type == TypedValue.TYPE_DIMENSION) {
return when (tv.complexUnitCompat) {
// For SP values, we convert the value directly to an TextUnit.Sp
TypedValue.COMPLEX_UNIT_SP -> TypedValue.complexToFloat(tv.data).sp
// For DIP values, we convert the value to an TextUnit.Em (roughly equivalent)
TypedValue.COMPLEX_UNIT_DIP -> TypedValue.complexToFloat(tv.data).em
// For another other types, we let the TypedArray flatten to a px value, and
// we convert it to an Sp based on the current density
else -> with(density) { getDimension(index, 0f).toSp() }
}
}
return null
}
/**
* Returns the given index as a [CornerSize], or `null` if the value can not be coerced to a [CornerSize].
*
* @param index index of attribute to retrieve.
*/
private fun TypedArray.getCornerSizeOrNull(index: Int): CornerSize? {
val tv = tempTypedValue.getOrSet { TypedValue() }
if (getValue(index, tv)) {
return when (tv.type) {
TypedValue.TYPE_DIMENSION -> {
when (tv.complexUnitCompat) {
// For DIP and PX values, we convert the value to the equivalent
TypedValue.COMPLEX_UNIT_DIP -> CornerSize(TypedValue.complexToFloat(tv.data).dp)
TypedValue.COMPLEX_UNIT_PX -> CornerSize(TypedValue.complexToFloat(tv.data))
// For another other dim types, we let the TypedArray flatten to a px value
else -> CornerSize(getDimensionPixelSize(index, 0))
}
}
TypedValue.TYPE_FRACTION -> CornerSize(tv.getFraction(1f, 1f))
else -> null
}
}
return null
}
/**
* Returns the given index as a [CornerSize], or [fallback] if the value can not be coerced to a [CornerSize].
*
* @param index index of attribute to retrieve.
* @param fallback Value to return if the attribute is not defined or cannot be coerced to an [CornerSize].
*/
private fun TypedArray.getCornerSize(index: Int, fallback: CornerSize): CornerSize {
return getCornerSizeOrNull(index) ?: fallback
}
/**
* A workaround since [TypedValue.getComplexUnit] is API 22+
*/
private inline val TypedValue.complexUnitCompat
get() = when {
Build.VERSION.SDK_INT > 22 -> complexUnit
else -> TypedValue.COMPLEX_UNIT_MASK and (data shr TypedValue.COMPLEX_UNIT_SHIFT)
}
/**
* This is gross, but we need a way to check for theme equality. Theme does not implement
* `equals()` or `hashCode()`, but it does have a hidden method called `getKey()`.
*
* The cost of this reflective invoke is a lot cheaper than the full theme read which currently
* happens on every re-composition.
*/
private inline val Resources.Theme.key: Any?
get() {
if (!sThemeGetKeyMethodFetched) {
try {
@Suppress("PrivateApi")
sThemeGetKeyMethod = Resources.Theme::class.java.getDeclaredMethod("getKey")
.apply { isAccessible = true }
} catch (e: ReflectiveOperationException) {
// Failed to retrieve Theme.getKey method
}
sThemeGetKeyMethodFetched = true
}
if (sThemeGetKeyMethod != null) {
return try {
sThemeGetKeyMethod?.invoke(this)
} catch (e: ReflectiveOperationException) {
// Failed to invoke Theme.getKey()
}
}
return null
}
private var sThemeGetKeyMethodFetched = false
private var sThemeGetKeyMethod: Method? = null