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

Upgrade to Haze 0.4.0 #1672

Merged
merged 1 commit into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ fun <E : Entry> EntryGrid(
}
}

TiviScaffold(
HazeScaffold(
topBar = {
EntryGridAppBar(
title = title,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Copyright 2023, Christopher Banes and the Tivi project contributors
// SPDX-License-Identifier: Apache-2.0

package app.tivi.common.compose

import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.WindowInsets
import androidx.compose.material3.FabPosition
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.ScaffoldDefaults
import androidx.compose.material3.contentColorFor
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import dev.chrisbanes.haze.HazeState
import dev.chrisbanes.haze.haze
import dev.chrisbanes.haze.hazeChild

@Composable
fun HazeScaffold(
modifier: Modifier = Modifier,
topBar: @Composable () -> Unit = {},
bottomBar: @Composable () -> Unit = {},
snackbarHost: @Composable () -> Unit = {},
floatingActionButton: @Composable () -> Unit = {},
floatingActionButtonPosition: FabPosition = FabPosition.End,
containerColor: Color = MaterialTheme.colorScheme.background,
contentColor: Color = contentColorFor(containerColor),
contentWindowInsets: WindowInsets = ScaffoldDefaults.contentWindowInsets,
blurTopBar: Boolean = false,
blurBottomBar: Boolean = false,
content: @Composable (PaddingValues) -> Unit,
) {
val hazeState = remember { HazeState() }

NestedScaffold(
modifier = modifier,
topBar = {
Box(
modifier = Modifier.thenIf(blurTopBar) { hazeChild(hazeState) },
content = { topBar() },
)
},
bottomBar = {
Box(
modifier = Modifier.thenIf(blurBottomBar) { hazeChild(hazeState) },
content = { bottomBar() },
)
},
snackbarHost = snackbarHost,
floatingActionButton = floatingActionButton,
floatingActionButtonPosition = floatingActionButtonPosition,
containerColor = containerColor,
contentColor = contentColor,
contentWindowInsets = contentWindowInsets,
) { contentPadding ->
Box(
modifier = Modifier.haze(
state = hazeState,
backgroundColor = containerColor,
),
content = { content(contentPadding) },
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

package app.tivi.common.compose

import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.WindowInsets
import androidx.compose.foundation.layout.asPaddingValues
Expand All @@ -24,26 +23,22 @@ import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.Immutable
import androidx.compose.runtime.staticCompositionLocalOf
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Rect
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.SubcomposeLayout
import androidx.compose.ui.unit.LayoutDirection
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.offset
import app.tivi.common.compose.ui.plus
import dev.chrisbanes.haze.haze

private val LocalScaffoldContentPadding = staticCompositionLocalOf { PaddingValues(0.dp) }

/**
* A copy of Material 3's [Scaffold] composable, but with a few tweaks:
*
* - Supports being used nested. The `contentPadding` is compounded on each level.
* - Supports automatic blurring of content over [topBar] and [bottomBar], via
* [blurTopBar] and [blurBottomBar].
*/
@Composable
fun TiviScaffold(
internal fun NestedScaffold(
modifier: Modifier = Modifier,
topBar: @Composable () -> Unit = {},
bottomBar: @Composable () -> Unit = {},
Expand All @@ -53,8 +48,6 @@ fun TiviScaffold(
containerColor: Color = MaterialTheme.colorScheme.background,
contentColor: Color = contentColorFor(containerColor),
contentWindowInsets: WindowInsets = ScaffoldDefaults.contentWindowInsets,
blurTopBar: Boolean = false,
blurBottomBar: Boolean = false,
content: @Composable (PaddingValues) -> Unit,
) {
Surface(modifier = modifier, color = containerColor, contentColor = contentColor) {
Expand All @@ -66,8 +59,6 @@ fun TiviScaffold(
snackbar = snackbarHost,
contentWindowInsets = contentWindowInsets,
fab = floatingActionButton,
blurTopBar = blurTopBar,
blurBottomBar = blurBottomBar,
incomingContentPadding = LocalScaffoldContentPadding.current,
)
}
Expand Down Expand Up @@ -95,8 +86,6 @@ private fun NestedScaffoldLayout(
contentWindowInsets: WindowInsets,
incomingContentPadding: PaddingValues,
bottomBar: @Composable () -> Unit,
blurTopBar: Boolean,
blurBottomBar: Boolean,
) {
SubcomposeLayout { constraints ->
val layoutWidth = constraints.maxWidth
Expand Down Expand Up @@ -193,40 +182,10 @@ private fun NestedScaffoldLayout(
end = contentInsets.calculateEndPadding((this@SubcomposeLayout).layoutDirection),
)

val blurAreas = listOfNotNull(
if (blurTopBar) {
Rect(
left = 0f,
top = 0f,
right = layoutWidth.toFloat(),
bottom = innerPadding.calculateTopPadding().toPx(),
).takeUnless { it.isEmpty }
} else {
null
},
if (blurBottomBar) {
Rect(
left = 0f,
top = layoutHeight.toFloat() - innerPadding.calculateBottomPadding().toPx(),
right = layoutWidth.toFloat(),
bottom = layoutHeight.toFloat(),
).takeUnless { it.isEmpty }
} else {
null
},
)

Box(
modifier = Modifier.haze(
*blurAreas.toTypedArray(),
backgroundColor = MaterialTheme.colorScheme.surface,
),
) {
// Scaffold always applies the insets, so we only want to pass down the content padding
// without the insets (i.e. padding from the bottom bar, etc)
CompositionLocalProvider(LocalScaffoldContentPadding provides innerPadding) {
content(innerPadding)
}
// Scaffold always applies the insets, so we only want to pass down the content padding
// without the insets (i.e. padding from the bottom bar, etc)
CompositionLocalProvider(LocalScaffoldContentPadding provides innerPadding) {
content(innerPadding)
}
}.map { it.measure(looseConstraints) }

Expand Down Expand Up @@ -276,7 +235,7 @@ internal class FabPlacement(
/**
* CompositionLocal containing a [FabPlacement] that is used to calculate the FAB bottom offset.
*/
internal val LocalFabPlacement = staticCompositionLocalOf<app.tivi.common.compose.FabPlacement?> { null }
internal val LocalFabPlacement = staticCompositionLocalOf<FabPlacement?> { null }

// FAB spacing above the bottom bar / bottom of the Scaffold
private val FabSpacing = 16.dp
Expand Down
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ compose-material3-windowsizeclass = "dev.chrisbanes.material3:material3-window-s

crashkios-crashlytics = "co.touchlab.crashkios:crashlytics:0.8.5"

haze = "dev.chrisbanes.haze:haze:0.3.1"
haze = "dev.chrisbanes.haze:haze:0.4.0"

tools-desugarjdklibs = "com.android.tools:desugar_jdk_libs:2.0.4"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.text.withStyle
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import app.tivi.common.compose.HazeScaffold
import app.tivi.common.compose.LocalStrings
import app.tivi.common.compose.TiviScaffold
import app.tivi.screens.DevLogScreen
import app.tivi.util.Severity
import com.slack.circuit.runtime.CircuitContext
Expand Down Expand Up @@ -56,7 +56,7 @@ internal fun DevLog(
) {
val eventSink = state.eventSink

TiviScaffold(
HazeScaffold(
topBar = {
TopAppBar(
title = { Text("Log") },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBar
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import app.tivi.common.compose.HazeScaffold
import app.tivi.common.compose.LocalStrings
import app.tivi.common.compose.TiviScaffold
import app.tivi.common.compose.ui.CheckboxPreference
import app.tivi.common.compose.ui.Preference
import app.tivi.screens.DevSettingsScreen
Expand Down Expand Up @@ -47,7 +47,7 @@ internal fun DevSettings(
) {
val eventSink = state.eventSink

TiviScaffold(
HazeScaffold(
topBar = {
TopAppBar(
title = { Text(LocalStrings.current.developerSettingsTitle) },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@ import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.layout.FirstBaseline
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.unit.dp
import app.tivi.common.compose.HazeScaffold
import app.tivi.common.compose.Layout
import app.tivi.common.compose.LocalStrings
import app.tivi.common.compose.LocalTiviTextCreator
import app.tivi.common.compose.LocalWindowSizeClass
import app.tivi.common.compose.ReportDrawnWhen
import app.tivi.common.compose.TiviScaffold
import app.tivi.common.compose.bodyWidth
import app.tivi.common.compose.rememberCoroutineScope
import app.tivi.common.compose.theme.TiviTheme
Expand Down Expand Up @@ -177,7 +177,7 @@ internal fun Discover(
state.trendingItems.isNotEmpty()
}

TiviScaffold(
HazeScaffold(
topBar = {
TiviRootScreenAppBar(
title = LocalStrings.current.discoverTitle,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,11 @@ import androidx.compose.ui.unit.dp
import app.cash.paging.LoadStateLoading
import app.cash.paging.compose.LazyPagingItems
import app.cash.paging.compose.itemKey
import app.tivi.common.compose.HazeScaffold
import app.tivi.common.compose.Layout
import app.tivi.common.compose.LocalStrings
import app.tivi.common.compose.LocalTiviDateFormatter
import app.tivi.common.compose.LocalTiviTextCreator
import app.tivi.common.compose.TiviScaffold
import app.tivi.common.compose.bodyWidth
import app.tivi.common.compose.fullSpanItem
import app.tivi.common.compose.rememberCoroutineScope
Expand Down Expand Up @@ -169,7 +169,7 @@ internal fun Library(

val scrollBehavior = TopAppBarDefaults.enterAlwaysScrollBehavior()

TiviScaffold(
HazeScaffold(
topBar = {
TiviRootScreenAppBar(
title = LocalStrings.current.libraryTitle,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ import androidx.compose.material3.TopAppBar
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import app.tivi.common.compose.HazeScaffold
import app.tivi.common.compose.LocalStrings
import app.tivi.common.compose.TiviScaffold
import app.tivi.common.compose.ui.Preference
import app.tivi.common.compose.ui.PreferenceHeader
import app.tivi.screens.LicensesScreen
Expand Down Expand Up @@ -53,7 +53,7 @@ internal fun Licenses(
) {
val eventSink = state.eventSink

TiviScaffold(
HazeScaffold(
topBar = {
TopAppBar(
title = { Text(LocalStrings.current.settingsOpenSource) },
Expand Down
4 changes: 2 additions & 2 deletions ui/root/src/commonMain/kotlin/app/tivi/home/Home.kt
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.unit.dp
import app.tivi.common.compose.HazeScaffold
import app.tivi.common.compose.LocalStrings
import app.tivi.common.compose.LocalWindowSizeClass
import app.tivi.common.compose.TiviScaffold
import app.tivi.common.ui.resources.TiviStrings
import app.tivi.screens.DiscoverScreen
import app.tivi.screens.LibraryScreen
Expand Down Expand Up @@ -77,7 +77,7 @@ internal fun Home(
val strings = LocalStrings.current
val navigationItems = remember(strings) { buildNavigationItems(strings) }

TiviScaffold(
HazeScaffold(
bottomBar = {
if (navigationType == NavigationType.BOTTOM_NAVIGATION) {
HomeNavigationBar(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.text.input.TextFieldValue
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import app.tivi.common.compose.HazeScaffold
import app.tivi.common.compose.Layout
import app.tivi.common.compose.LocalStrings
import app.tivi.common.compose.TiviScaffold
import app.tivi.common.compose.bodyWidth
import app.tivi.common.compose.ui.EmptyContent
import app.tivi.common.compose.ui.PosterCard
Expand Down Expand Up @@ -116,7 +116,7 @@ internal fun Search(
}
}

TiviScaffold(
HazeScaffold(
topBar = {
Box(
Modifier
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.unit.dp
import app.tivi.common.compose.HazeScaffold
import app.tivi.common.compose.LocalStrings
import app.tivi.common.compose.TiviScaffold
import app.tivi.common.compose.itemSpacer
import app.tivi.common.compose.ui.CheckboxPreference
import app.tivi.common.compose.ui.Preference
Expand Down Expand Up @@ -63,7 +63,7 @@ internal fun Settings(

val strings = LocalStrings.current

TiviScaffold(
HazeScaffold(
topBar = {
TopAppBar(
title = { Text(strings.settingsTitle) },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,10 @@ import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.max
import androidx.compose.ui.unit.sp
import app.tivi.common.compose.HazeScaffold
import app.tivi.common.compose.Layout
import app.tivi.common.compose.LocalStrings
import app.tivi.common.compose.LocalTiviTextCreator
import app.tivi.common.compose.TiviScaffold
import app.tivi.common.compose.bodyWidth
import app.tivi.common.compose.gutterSpacer
import app.tivi.common.compose.itemSpacer
Expand Down Expand Up @@ -203,7 +203,7 @@ internal fun ShowDetails(

val scrollBehavior = TopAppBarDefaults.pinnedScrollBehavior()

TiviScaffold(
HazeScaffold(
topBar = {
ShowDetailsAppBar(
title = null,
Expand Down
Loading