Skip to content

Commit

Permalink
Refactor AppBars window insets default value
Browse files Browse the repository at this point in the history
  • Loading branch information
norihirosunada committed Sep 10, 2024
1 parent 52a3bde commit 173aaea
Show file tree
Hide file tree
Showing 16 changed files with 75 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@ import androidx.compose.animation.ExitTransition
import androidx.compose.animation.fadeIn
import androidx.compose.foundation.layout.RowScope
import androidx.compose.foundation.layout.WindowInsets
import androidx.compose.foundation.layout.WindowInsetsSides
import androidx.compose.foundation.layout.displayCutout
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.only
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.systemBars
import androidx.compose.foundation.layout.union
import androidx.compose.material.icons.Icons.AutoMirrored.Filled
import androidx.compose.material.icons.automirrored.filled.ArrowBack
import androidx.compose.material3.ExperimentalMaterial3Api
Expand Down Expand Up @@ -38,7 +43,7 @@ fun AnimatedMediumTopAppBar(
navIconContentDescription: String?,
modifier: Modifier = Modifier,
actions: @Composable RowScope.() -> Unit = {},
windowInsets: WindowInsets = TopAppBarDefaults.windowInsets,
windowInsets: WindowInsets = AnimatedMediumTopAppBarDefaults.windowInsets(),
colors: TopAppBarColors = TopAppBarDefaults.largeTopAppBarColors().copy(
scrolledContainerColor = MaterialTheme.colorScheme.surfaceContainer,
),
Expand Down Expand Up @@ -108,3 +113,10 @@ fun AnimatedMediumTopAppBar(
scrollBehavior = scrollBehavior,
)
}

object AnimatedMediumTopAppBarDefaults {
@Composable
fun windowInsets() = WindowInsets.displayCutout.union(WindowInsets.systemBars).only(
WindowInsetsSides.Horizontal + WindowInsetsSides.Top,
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ package io.github.droidkaigi.confsched.droidkaigiui.component
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.RowScope
import androidx.compose.foundation.layout.WindowInsets
import androidx.compose.foundation.layout.WindowInsetsSides
import androidx.compose.foundation.layout.displayCutout
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.only
import androidx.compose.foundation.layout.systemBars
import androidx.compose.foundation.layout.union
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.TopAppBar
Expand All @@ -25,7 +30,7 @@ fun AnimatedTextTopAppBar(
modifier: Modifier = Modifier,
navigationIcon: @Composable () -> Unit = {},
actions: @Composable RowScope.() -> Unit = {},
windowInsets: WindowInsets = TopAppBarDefaults.windowInsets,
windowInsets: WindowInsets = AnimatedTextTopAppBarDefaults.windowInsets(),
colors: TopAppBarColors = TopAppBarDefaults.topAppBarColors().copy(
scrolledContainerColor = MaterialTheme.colorScheme.surfaceContainer,
),
Expand Down Expand Up @@ -80,3 +85,10 @@ fun TopAppBarScrollBehavior.resetScroll() {
this.state.heightOffset = 0f
this.state.contentOffset = 0f
}

object AnimatedTextTopAppBarDefaults {
@Composable
fun windowInsets() = WindowInsets.displayCutout.union(WindowInsets.systemBars).only(
WindowInsetsSides.Horizontal + WindowInsetsSides.Top,
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,6 @@ fun AboutScreen(
AnimatedTextTopAppBar(
title = stringResource(AboutRes.string.about_droidkaigi),
scrollBehavior = scrollBehavior,
windowInsets = WindowInsets(
left = contentPadding.calculateLeftPadding(layoutDirection),
top = contentPadding.calculateTopPadding(),
right = contentPadding.calculateRightPadding(layoutDirection),
),
)
},
snackbarHost = { SnackbarHost(hostState = snackbarHostState) },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@ package io.github.droidkaigi.confsched.contributors
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.WindowInsets
import androidx.compose.foundation.layout.WindowInsetsSides
import androidx.compose.foundation.layout.calculateEndPadding
import androidx.compose.foundation.layout.calculateStartPadding
import androidx.compose.foundation.layout.displayCutout
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.only
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.systemBars
import androidx.compose.foundation.layout.union
Expand Down Expand Up @@ -128,9 +126,6 @@ fun ContributorsScreen(
onBackClick = onBackClick,
scrollBehavior = scrollBehavior,
navIconContentDescription = "Back",
windowInsets = WindowInsets.displayCutout.union(WindowInsets.systemBars).only(
WindowInsetsSides.Horizontal + WindowInsetsSides.Top,
),
)
}
},
Expand All @@ -144,7 +139,7 @@ fun ContributorsScreen(
contentPadding = PaddingValues(
start = padding.calculateStartPadding(layoutDirection),
end = padding.calculateEndPadding(layoutDirection),
bottom = 40.dp + padding.calculateBottomPadding()
bottom = 40.dp + padding.calculateBottomPadding(),
),
modifier = Modifier
.fillMaxSize()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,6 @@ fun EventMapScreen(
AnimatedTextTopAppBar(
title = stringResource(EventMapRes.string.eventmap),
scrollBehavior = scrollBehavior,
windowInsets = WindowInsets(
left = contentPadding.calculateLeftPadding(layoutDirection),
top = contentPadding.calculateTopPadding(),
right = contentPadding.calculateRightPadding(layoutDirection),
),
)
},
contentWindowInsets = WindowInsets(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import androidx.compose.animation.core.FastOutLinearInEasing
import androidx.compose.animation.core.Spring
import androidx.compose.animation.core.spring
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.WindowInsets
import androidx.compose.foundation.layout.padding
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.outlined.ArrowBack
Expand All @@ -24,7 +23,6 @@ import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.lerp
import androidx.compose.ui.input.nestedscroll.nestedScroll
import androidx.compose.ui.platform.LocalLayoutDirection
import androidx.compose.ui.platform.testTag
import androidx.navigation.NavController
import androidx.navigation.NavGraph.Companion.findStartDestination
Expand Down Expand Up @@ -140,7 +138,6 @@ private fun FavoritesScreen(
modifier: Modifier = Modifier,
contentPadding: PaddingValues = PaddingValues(),
) {
val layoutDirection = LocalLayoutDirection.current
val scrollBehavior = TopAppBarDefaults.pinnedScrollBehavior()

val fraction = if (scrollBehavior.state.overlappedFraction > 0.01f) 1f else 0f
Expand Down Expand Up @@ -175,11 +172,6 @@ private fun FavoritesScreen(
colors = TopAppBarDefaults.topAppBarColors().copy(
scrolledContainerColor = MaterialTheme.colorScheme.surfaceContainer,
),
windowInsets = WindowInsets(
left = contentPadding.calculateLeftPadding(layoutDirection),
top = contentPadding.calculateTopPadding(),
right = contentPadding.calculateRightPadding(layoutDirection),
),
)
},
) { padding ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -281,11 +281,6 @@ internal fun ProfileCardScreen(
AnimatedTextTopAppBar(
title = stringResource(ProfileCardRes.string.profile_card_title),
scrollBehavior = scrollBehavior,
windowInsets = WindowInsets(
left = contentPadding.calculateLeftPadding(layoutDirection),
top = contentPadding.calculateTopPadding(),
right = contentPadding.calculateRightPadding(layoutDirection),
),
)
}

Expand All @@ -300,11 +295,6 @@ internal fun ProfileCardScreen(
textColor = MaterialTheme.colorScheme.scrim,
title = stringResource(ProfileCardRes.string.profile_card_title),
scrollBehavior = scrollBehavior,
windowInsets = WindowInsets(
left = contentPadding.calculateLeftPadding(layoutDirection),
top = contentPadding.calculateTopPadding(),
right = contentPadding.calculateRightPadding(layoutDirection),
),
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@ import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.WindowInsets
import androidx.compose.foundation.layout.WindowInsetsSides
import androidx.compose.foundation.layout.calculateEndPadding
import androidx.compose.foundation.layout.calculateStartPadding
import androidx.compose.foundation.layout.displayCutout
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.imePadding
import androidx.compose.foundation.layout.only
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.systemBars
import androidx.compose.foundation.layout.union
Expand Down Expand Up @@ -153,9 +151,6 @@ fun SearchScreen(
onChangeSearchWord = onSearchWordChanged,
onClickClear = onClearSearchWordClick,
onClickBack = onBackClick,
windowInsets = WindowInsets.displayCutout.union(WindowInsets.systemBars).only(
WindowInsetsSides.Horizontal + WindowInsetsSides.Top,
),
)
},
modifier = modifier,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@ import androidx.compose.animation.fadeIn
import androidx.compose.animation.fadeOut
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.WindowInsets
import androidx.compose.foundation.layout.WindowInsetsSides
import androidx.compose.foundation.layout.calculateEndPadding
import androidx.compose.foundation.layout.calculateStartPadding
import androidx.compose.foundation.layout.displayCutout
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.only
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.systemBars
import androidx.compose.foundation.layout.union
Expand Down Expand Up @@ -189,9 +187,6 @@ private fun TimetableItemDetailScreen(
TimetableItemDetailTopAppBar(
onNavigationIconClick = onNavigationIconClick,
scrollBehavior = scrollBehavior,
windowInsets = WindowInsets.displayCutout.union(WindowInsets.systemBars).only(
WindowInsetsSides.Horizontal + WindowInsetsSides.Top,
),
)
}
}
Expand All @@ -204,9 +199,6 @@ private fun TimetableItemDetailScreen(
onBookmarkClick = onBookmarkClick,
onCalendarRegistrationClick = onCalendarRegistrationClick,
onShareClick = onShareClick,
windowInsets = WindowInsets.displayCutout.union(WindowInsets.systemBars).only(
WindowInsetsSides.Horizontal + WindowInsetsSides.Bottom,
),
)
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,17 @@ import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.WindowInsets
import androidx.compose.foundation.layout.WindowInsetsSides
import androidx.compose.foundation.layout.calculateEndPadding
import androidx.compose.foundation.layout.calculateStartPadding
import androidx.compose.foundation.layout.displayCutout
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.only
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.statusBarsPadding
import androidx.compose.foundation.layout.systemBars
import androidx.compose.foundation.layout.union
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Search
import androidx.compose.material3.ExperimentalMaterial3Api
Expand Down Expand Up @@ -192,10 +197,8 @@ private fun TimetableScreen(
}
}
},
windowInsets = WindowInsets(
left = contentPadding.calculateLeftPadding(layoutDirection),
top = contentPadding.calculateTopPadding(),
right = contentPadding.calculateRightPadding(layoutDirection),
windowInsets = WindowInsets.displayCutout.union(WindowInsets.systemBars).only(
WindowInsetsSides.Horizontal + WindowInsetsSides.Top,
),
)
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@ package io.github.droidkaigi.confsched.sessions.component
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.IntrinsicSize
import androidx.compose.foundation.layout.WindowInsets
import androidx.compose.foundation.layout.WindowInsetsSides
import androidx.compose.foundation.layout.displayCutout
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.only
import androidx.compose.foundation.layout.systemBars
import androidx.compose.foundation.layout.union
import androidx.compose.foundation.text.BasicTextField
import androidx.compose.foundation.text.KeyboardActions
import androidx.compose.foundation.text.KeyboardOptions
Expand All @@ -19,7 +24,6 @@ import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.material3.TextFieldDefaults
import androidx.compose.material3.TopAppBar
import androidx.compose.material3.TopAppBarDefaults
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.remember
Expand Down Expand Up @@ -50,7 +54,7 @@ fun SearchTextFieldAppBar(
onClickClear: () -> Unit,
onClickBack: () -> Unit,
modifier: Modifier = Modifier,
windowInsets: WindowInsets = TopAppBarDefaults.windowInsets,
windowInsets: WindowInsets = SearchTextFieldAppBarDefaults.windowInsets(),
) {
val focusManager = LocalFocusManager.current
val focusRequester = remember { FocusRequester() }
Expand Down Expand Up @@ -132,6 +136,13 @@ fun SearchTextFieldAppBar(
)
}

object SearchTextFieldAppBarDefaults {
@Composable
fun windowInsets() = WindowInsets.displayCutout.union(WindowInsets.systemBars).only(
WindowInsetsSides.Horizontal + WindowInsetsSides.Top,
)
}

@Preview
@Composable
fun SearchTextFieldAppBarPreview_FilledSearchWord() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package io.github.droidkaigi.confsched.sessions.component

import androidx.compose.foundation.layout.WindowInsets
import androidx.compose.foundation.layout.WindowInsetsSides
import androidx.compose.foundation.layout.displayCutout
import androidx.compose.foundation.layout.only
import androidx.compose.foundation.layout.systemBars
import androidx.compose.foundation.layout.union
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Favorite
import androidx.compose.material.icons.outlined.FavoriteBorder
import androidx.compose.material.icons.outlined.Share
import androidx.compose.material3.BottomAppBar
import androidx.compose.material3.BottomAppBarDefaults
import androidx.compose.material3.FloatingActionButton
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
Expand Down Expand Up @@ -36,7 +40,7 @@ fun TimetableItemDetailBottomAppBar(
onBookmarkClick: (TimetableItem) -> Unit,
onCalendarRegistrationClick: (TimetableItem) -> Unit,
modifier: Modifier = Modifier,
windowInsets: WindowInsets = BottomAppBarDefaults.windowInsets,
windowInsets: WindowInsets = TimetableItemDetailBottomAppBarDefaults.windowInsets(),
onShareClick: (TimetableItem) -> Unit,
) {
val haptic = LocalHapticFeedback.current
Expand Down Expand Up @@ -85,6 +89,13 @@ fun TimetableItemDetailBottomAppBar(
)
}

object TimetableItemDetailBottomAppBarDefaults {
@Composable
fun windowInsets() = WindowInsets.displayCutout.union(WindowInsets.systemBars).only(
WindowInsetsSides.Horizontal + WindowInsetsSides.Bottom,
)
}

@Composable
@Preview
fun TimetableItemDetailBottomAppBarPreview() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@ package io.github.droidkaigi.confsched.sessions.component

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.WindowInsets
import androidx.compose.foundation.layout.WindowInsetsSides
import androidx.compose.foundation.layout.asPaddingValues
import androidx.compose.foundation.layout.consumeWindowInsets
import androidx.compose.foundation.layout.displayCutout
import androidx.compose.foundation.layout.only
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.statusBars
import androidx.compose.foundation.layout.systemBars
import androidx.compose.foundation.layout.union
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.outlined.ArrowBack
import androidx.compose.material3.ExperimentalMaterial3Api
Expand All @@ -30,7 +35,7 @@ fun TimetableItemDetailTopAppBar(
onNavigationIconClick: () -> Unit,
scrollBehavior: TopAppBarScrollBehavior,
modifier: Modifier = Modifier,
windowInsets: WindowInsets = TopAppBarDefaults.windowInsets,
windowInsets: WindowInsets = TimetableItemDetailTopAppBarDefaults.windowInsets(),
) {
// Allow content to be displayed at the statusBar when scrolling
val statusBarHeight = WindowInsets.statusBars.asPaddingValues().calculateTopPadding()
Expand Down Expand Up @@ -61,6 +66,13 @@ fun TimetableItemDetailTopAppBar(
)
}

object TimetableItemDetailTopAppBarDefaults {
@Composable
fun windowInsets() = WindowInsets.displayCutout.union(WindowInsets.systemBars).only(
WindowInsetsSides.Horizontal + WindowInsetsSides.Top,
)
}

@OptIn(ExperimentalMaterial3Api::class)
@Composable
@Preview
Expand Down
Loading

0 comments on commit 173aaea

Please sign in to comment.