Skip to content

Commit 1c6809d

Browse files
committed
fix(ui): prevent Shader causing crashes on API < 33
<=API 32 doesn't have RuntimeShader so can't even initialize it without Build.Version checks Signed-off-by: Brandon McAnsh <git@bmcreations.dev>
1 parent fe2dc51 commit 1c6809d

1 file changed

Lines changed: 18 additions & 12 deletions

File tree

ui/components/src/main/kotlin/com/getcode/ui/theme/CodeButton.kt

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -350,10 +350,12 @@ fun CodeButton(
350350
propagateMinConstraints = true,
351351
) {
352352
Row(
353-
Modifier.defaultMinSize(
354-
minWidth = ButtonDefaults.MinWidth,
355-
minHeight = ButtonDefaults.MinHeight,
356-
).padding(cp),
353+
Modifier
354+
.defaultMinSize(
355+
minWidth = ButtonDefaults.MinWidth,
356+
minHeight = ButtonDefaults.MinHeight,
357+
)
358+
.padding(cp),
357359
horizontalArrangement = Arrangement.Center,
358360
verticalAlignment = Alignment.CenterVertically,
359361
) {
@@ -371,7 +373,13 @@ fun CodeButton(
371373
modifier = Modifier.fillMaxSize(),
372374
contentAlignment = Alignment.Center
373375
) {
374-
ProvideTextStyle(value = style.copy(color = colors.contentColor(enabled).value)) {
376+
ProvideTextStyle(
377+
value = style.copy(
378+
color = colors.contentColor(
379+
enabled
380+
).value
381+
)
382+
) {
375383
this@Row.content()
376384
}
377385
}
@@ -413,9 +421,7 @@ fun CodeButton(
413421
}
414422
}
415423

416-
@RequiresApi(Build.VERSION_CODES.TIRAMISU)
417-
private val FROSTED_GLASS_SHADER = RuntimeShader(
418-
"""
424+
private val FROSTED_GLASS_SHADER = """
419425
uniform shader content;
420426
uniform float2 resolution;
421427
uniform float blurRadius;
@@ -435,21 +441,21 @@ private val FROSTED_GLASS_SHADER = RuntimeShader(
435441
return color;
436442
}
437443
"""
438-
)
439444

440445
fun Modifier.frostedGlass(
441446
tint: Color = Color.White.copy(alpha = 0.1f),
442447
noiseStrength: Float = 0.03f,
443448
shape: Shape = RectangleShape
444449
): Modifier = this.then(
445450
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
451+
val shader = RuntimeShader(FROSTED_GLASS_SHADER)
446452
Modifier
447453
.drawWithContent {
448454
drawRect(tint)
449455

450-
FROSTED_GLASS_SHADER.setFloatUniform("resolution", size.width, size.height)
451-
FROSTED_GLASS_SHADER.setFloatUniform("noiseStrength", noiseStrength)
452-
drawRect(brush = ShaderBrush(FROSTED_GLASS_SHADER))
456+
shader.setFloatUniform("resolution", size.width, size.height)
457+
shader.setFloatUniform("noiseStrength", noiseStrength)
458+
drawRect(brush = ShaderBrush(shader))
453459

454460
drawContent()
455461
}

0 commit comments

Comments
 (0)