Skip to content

Commit

Permalink
Add masking sample (#386)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisbanes authored Nov 3, 2024
1 parent 97cab07 commit 593a9c8
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import androidx.test.uiautomator.By
import dev.chrisbanes.haze.testutils.navigateToCreditCard
import dev.chrisbanes.haze.testutils.navigateToImagesList
import dev.chrisbanes.haze.testutils.navigateToScaffold
import dev.chrisbanes.haze.testutils.navigateToScaffoldWithMask
import dev.chrisbanes.haze.testutils.navigateToScaffoldWithProgressive
import dev.chrisbanes.haze.testutils.scroll
import dev.chrisbanes.haze.testutils.waitForObject
Expand Down Expand Up @@ -75,6 +76,22 @@ class BenchmarkTest {
}
}

@Test
fun scaffoldMask() {
benchmarkRule.measureRepeated(
packageName = APP_PACKAGE,
metrics = listOf(FrameTimingMetric()),
startupMode = StartupMode.WARM,
iterations = DEFAULT_ITERATIONS,
setupBlock = {
startActivityAndWait()
device.navigateToScaffoldWithMask()
},
) {
device.scroll("lazy_grid")
}
}

@Test
fun creditCard() {
benchmarkRule.measureRepeated(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ internal fun UiDevice.navigateToScaffoldWithProgressive() {
waitForIdle()
}

internal fun UiDevice.navigateToScaffoldWithMask() {
waitForObject(By.res("Scaffold (with mask)")).click()
waitForIdle()
}

internal fun UiDevice.navigateToCreditCard() {
waitForObject(By.res("Credit Card")).click()
waitForIdle()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,12 @@ fun Brush.Companion.easedGradient(

fun Brush.Companion.easedVerticalGradient(
easing: Easing,
numStops: Int = 16,
startY: Float = 0.0f,
endY: Float = Float.POSITIVE_INFINITY,
numStops: Int = 16,
): Brush = easedGradient(
easing = easing,
numStops = numStops,
start = Offset(x = 0f, y = startY),
end = Offset(x = 0f, y = endY),
)

fun Brush.Companion.cubicHorizontalGradient(
easing: Easing,
numStops: Int = 16,
startX: Float = 0.0f,
endX: Float = Float.POSITIVE_INFINITY,
): Brush = easedGradient(
easing = easing,
numStops = numStops,
start = Offset(x = startX, y = 0f),
end = Offset(x = endX, y = 0f),
)
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.testTag

val Samples = listOf(
Sample("Scaffold") { ScaffoldSample(it, false) },
Sample("Scaffold (with progressive blur)") { ScaffoldSample(it, true) },
Sample("Scaffold") { ScaffoldSample(it) },
Sample("Scaffold (with progressive blur)") { ScaffoldSample(it, ScaffoldSampleMode.Progressive) },
Sample("Scaffold (with mask)") { ScaffoldSample(it, ScaffoldSampleMode.Mask) },
Sample("Credit Card") { CreditCardSample(it) },
Sample("Images List") { ImagesList(it) },
Sample("List over Image") { ListOverImage(it) },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package dev.chrisbanes.haze.sample

import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.core.EaseIn
import androidx.compose.animation.slideInVertically
import androidx.compose.animation.slideOutVertically
import androidx.compose.foundation.layout.Arrangement
Expand All @@ -26,7 +27,6 @@ import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.NavigationBar
import androidx.compose.material3.NavigationBarItem
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBarDefaults
import androidx.compose.runtime.Composable
import androidx.compose.runtime.derivedStateOf
Expand All @@ -35,6 +35,7 @@ import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.unit.dp
Expand All @@ -45,9 +46,18 @@ import dev.chrisbanes.haze.hazeChild
import dev.chrisbanes.haze.materials.ExperimentalHazeMaterialsApi
import dev.chrisbanes.haze.materials.HazeMaterials

enum class ScaffoldSampleMode {
Default,
Progressive,
Mask,
}

@OptIn(ExperimentalMaterial3Api::class, ExperimentalHazeMaterialsApi::class)
@Composable
fun ScaffoldSample(navigator: Navigator, useProgressive: Boolean) {
fun ScaffoldSample(
navigator: Navigator,
mode: ScaffoldSampleMode = ScaffoldSampleMode.Default,
) {
val hazeState = remember { HazeState() }
val gridState = rememberLazyGridState()
val showNavigationBar by remember(gridState) {
Expand All @@ -59,7 +69,7 @@ fun ScaffoldSample(navigator: Navigator, useProgressive: Boolean) {
Scaffold(
topBar = {
LargeTopAppBar(
title = { Text(text = "Haze Scaffold sample") },
title = { },
navigationIcon = {
IconButton(
onClick = navigator::navigateUp,
Expand All @@ -75,8 +85,18 @@ fun ScaffoldSample(navigator: Navigator, useProgressive: Boolean) {
modifier = Modifier
.hazeChild(hazeState) {
this.style = style
if (useProgressive) {
progressive = HazeProgressive.verticalGradient(startIntensity = 1f, endIntensity = 0f)

when (mode) {
ScaffoldSampleMode.Default -> Unit
ScaffoldSampleMode.Progressive -> {
progressive = HazeProgressive.verticalGradient(
startIntensity = 1f,
endIntensity = 0f,
)
}
ScaffoldSampleMode.Mask -> {
mask = Brush.easedVerticalGradient(EaseIn)
}
}
}
.fillMaxWidth(),
Expand Down

0 comments on commit 593a9c8

Please sign in to comment.