Skip to content

Commit 5ed7f1c

Browse files
committed
Always uses actual consumed safe drawing inset when composing TopicScreen.
Closes #1780 Modifier.windowInsetsTopHeight was reset to 0 when navigating from child navigation to top level navigation, caused Spacer not render, and LazyListState updated top position to item 1 instead of 0 (Spacer). Test: ./gradlew connectedDemoDebugAndroidTest
1 parent af9631e commit 5ed7f1c

File tree

1 file changed

+18
-3
lines changed
  • feature/topic/src/main/kotlin/com/google/samples/apps/nowinandroid/feature/topic

1 file changed

+18
-3
lines changed

feature/topic/src/main/kotlin/com/google/samples/apps/nowinandroid/feature/topic/TopicScreen.kt

+18-3
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,25 @@ import androidx.compose.foundation.gestures.Orientation
2121
import androidx.compose.foundation.layout.Arrangement
2222
import androidx.compose.foundation.layout.Box
2323
import androidx.compose.foundation.layout.Column
24+
import androidx.compose.foundation.layout.ExperimentalLayoutApi
25+
import androidx.compose.foundation.layout.MutableWindowInsets
2426
import androidx.compose.foundation.layout.Row
2527
import androidx.compose.foundation.layout.Spacer
2628
import androidx.compose.foundation.layout.WindowInsets
29+
import androidx.compose.foundation.layout.asPaddingValues
30+
import androidx.compose.foundation.layout.exclude
2731
import androidx.compose.foundation.layout.fillMaxHeight
2832
import androidx.compose.foundation.layout.fillMaxWidth
33+
import androidx.compose.foundation.layout.height
34+
import androidx.compose.foundation.layout.onConsumedWindowInsetsChanged
2935
import androidx.compose.foundation.layout.padding
36+
import androidx.compose.foundation.layout.safeContent
3037
import androidx.compose.foundation.layout.safeDrawing
3138
import androidx.compose.foundation.layout.size
3239
import androidx.compose.foundation.layout.systemBars
3340
import androidx.compose.foundation.layout.width
3441
import androidx.compose.foundation.layout.windowInsetsBottomHeight
3542
import androidx.compose.foundation.layout.windowInsetsPadding
36-
import androidx.compose.foundation.layout.windowInsetsTopHeight
3743
import androidx.compose.foundation.lazy.LazyColumn
3844
import androidx.compose.foundation.lazy.LazyListScope
3945
import androidx.compose.foundation.lazy.rememberLazyListState
@@ -43,6 +49,7 @@ import androidx.compose.material3.MaterialTheme
4349
import androidx.compose.material3.Text
4450
import androidx.compose.runtime.Composable
4551
import androidx.compose.runtime.getValue
52+
import androidx.compose.runtime.remember
4653
import androidx.compose.ui.Alignment
4754
import androidx.compose.ui.Modifier
4855
import androidx.compose.ui.platform.testTag
@@ -95,6 +102,7 @@ fun TopicScreen(
95102
)
96103
}
97104

105+
@OptIn(ExperimentalLayoutApi::class)
98106
@VisibleForTesting
99107
@Composable
100108
internal fun TopicScreen(
@@ -109,16 +117,23 @@ internal fun TopicScreen(
109117
modifier: Modifier = Modifier,
110118
) {
111119
val state = rememberLazyListState()
120+
val remainingInsets = remember { MutableWindowInsets() }
121+
val safeContent = WindowInsets.safeContent
112122
TrackScrollJank(scrollableState = state, stateName = "topic:screen")
113123
Box(
114-
modifier = modifier,
124+
modifier = modifier.onConsumedWindowInsetsChanged { consumedWindowInsets ->
125+
remainingInsets.insets = safeContent.exclude(consumedWindowInsets)
126+
}
115127
) {
116128
LazyColumn(
117129
state = state,
118130
horizontalAlignment = Alignment.CenterHorizontally,
119131
) {
120132
item {
121-
Spacer(Modifier.windowInsetsTopHeight(WindowInsets.safeDrawing))
133+
// Use actual remaining insets that has been consumed.
134+
Spacer(
135+
Modifier.height(remainingInsets.asPaddingValues().calculateTopPadding())
136+
)
122137
}
123138
when (topicUiState) {
124139
TopicUiState.Loading -> item {

0 commit comments

Comments
 (0)