-
Notifications
You must be signed in to change notification settings - Fork 657
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make the Adaptive Layout Codelab edge-to-edge #485
base: main
Are you sure you want to change the base?
Conversation
contentPadding = WindowInsets.safeDrawing.asPaddingValues() | ||
.copy(layoutDirection, bottom = 0.dp) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this can be done with built-in APIs as
contentPadding = WindowInsets.safeDrawing.asPaddingValues() | |
.copy(layoutDirection, bottom = 0.dp) | |
contentPadding = WindowInsets.safeDrawing.only(WindowInsetsSides.Horizontal + WindowInsetsSides.Top).asPaddingValues() |
contentPadding = WindowInsets.safeDrawing.asPaddingValues() | ||
.copy(layoutDirection = layoutDirection, bottom = 0.dp) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ditto here
contentPadding = WindowInsets.safeDrawing.asPaddingValues() | |
.copy(layoutDirection = layoutDirection, bottom = 0.dp) | |
contentPadding = WindowInsets.safeDrawing.only(WindowInsetsSides.Horizontal + WindowInsetsSides.Top).asPaddingValues() |
end: Dp? = null, | ||
bottom: Dp? = null, | ||
) = PaddingValues( | ||
start = start ?: calculateStartPadding(layoutDirection), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this can be removed entirely with the alternate APIs above, but one subtle behavior issue that can arise with this code:
This causes calculateStartPadding
and other calculate*
methods to be invoked during composition. The correct inset values (as exposed in the PaddingValues
) won't necessarily be correct during composition, so anything based on the values that are computed during composition can end up resulting in being off by one frame.
A solution to this here would be to setup a PaddingValues
that defers calling the calculate*
values on the source PaddingValues
, so that they are read later when the inset values will be correct:
fun PaddingValues.copy(
start: Dp? = null,
top: Dp? = null,
end: Dp? = null,
bottom: Dp? = null,
) = object : PaddingValues {
override fun calculateBottomPadding(): Dp =
bottom ?: this@copy.calculateBottomPadding()
override fun calculateLeftPadding(layoutDirection: LayoutDirection): Dp =
when (layoutDirection) {
LayoutDirection.Ltr -> start
LayoutDirection.Rtl -> end
} ?: this@copy.calculateLeftPadding(layoutDirection)
override fun calculateRightPadding(layoutDirection: LayoutDirection): Dp =
when (layoutDirection) {
LayoutDirection.Ltr -> end
LayoutDirection.Rtl -> start
} ?: this@copy.calculateRightPadding(layoutDirection)
override fun calculateTopPadding(): Dp =
top ?: this@copy.calculateTopPadding()
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think Alex's comment should be addressed and then we good to merge!
@@ -34,6 +36,11 @@ class MainActivity : ComponentActivity() { | |||
override fun onCreate(savedInstanceState: Bundle?) { | |||
super.onCreate(savedInstanceState) | |||
|
|||
enableEdgeToEdge() | |||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Haven't seen this done before, should we add it to the compose docs for edge to edge?
MainActivity.kt
enableEdgeToEdge()
isNavigationBarContrastEnforced=false
Theme.kt
ReplyListContent.kt
ReplyListPane
andReplyDetailPane
. To do this, I setcontentPadding
=WindowInsets.safeDrawing.asPaddingValues().copy(layoutDirection, bottom = 0.dp)
.padding(16.dp)
withpadding(horizontal = 16.dp)
.These changes are made to the
main
branch.Portrait:
Landscape (looks a little weird, not yet sure what to do about this):
Unfolded, showing the last list item:
When this commit is cherry picked to the
end
branch the app will look like this: