-
Notifications
You must be signed in to change notification settings - Fork 74
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
Host-side API for view-owned widgets #2458
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -98,7 +98,10 @@ class TreehouseLayoutTest { | |
@Test fun uiConfigurationEmitsSystemBarsSafeAreaInsetsChanges() = runTest { | ||
val layout = TreehouseLayout(activity, throwingWidgetSystem, activity.onBackPressedDispatcher) | ||
layout.uiConfiguration.test { | ||
assertThat(awaitItem().safeAreaInsets).isEqualTo(Margin.Zero) | ||
val value1 = awaitItem() | ||
assertThat(value1.safeAreaInsets).isEqualTo(Margin.Zero) | ||
assertThat(value1.viewInsets).isEqualTo(Margin.Zero) | ||
|
||
val insets = Insets.of(10, 20, 30, 40) | ||
val windowInsets = WindowInsetsCompat.Builder() | ||
.setInsets(WindowInsetsCompat.Type.systemBars(), insets) | ||
|
@@ -112,7 +115,9 @@ class TreehouseLayoutTest { | |
bottom = insets.bottom.toDp(), | ||
) | ||
} | ||
assertThat(awaitItem().safeAreaInsets).isEqualTo(expectedInsets) | ||
val value2 = awaitItem() | ||
assertThat(value2.safeAreaInsets).isEqualTo(Margin.Zero) | ||
assertThat(value2.viewInsets).isEqualTo(expectedInsets) | ||
Comment on lines
+118
to
+120
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Change log item for this behavior change? Probably want to give a heads-up to internal users as well, if they still exist, that is. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep, lemme do. |
||
} | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,12 +22,12 @@ import android.view.View | |
import android.view.ViewGroup | ||
import androidx.activity.OnBackPressedCallback as AndroidOnBackPressedCallback | ||
import androidx.activity.OnBackPressedDispatcher as AndroidOnBackPressedDispatcher | ||
import androidx.core.graphics.Insets | ||
import androidx.core.view.children as viewGroupChildren | ||
import androidx.savedstate.findViewTreeSavedStateRegistryOwner | ||
import app.cash.redwood.ui.Cancellable | ||
import app.cash.redwood.ui.Density | ||
import app.cash.redwood.ui.LayoutDirection | ||
import app.cash.redwood.ui.Margin | ||
import app.cash.redwood.ui.OnBackPressedCallback as RedwoodOnBackPressedCallback | ||
import app.cash.redwood.ui.OnBackPressedDispatcher as RedwoodOnBackPressedDispatcher | ||
import app.cash.redwood.ui.Size | ||
|
@@ -51,7 +51,11 @@ public open class RedwoodLayout( | |
id = R.id.redwood_layout | ||
} | ||
|
||
private val mutableUiConfiguration = MutableStateFlow(computeUiConfiguration()) | ||
private val mutableUiConfiguration = MutableStateFlow( | ||
computeUiConfiguration( | ||
viewInsets = Margin.Zero, | ||
), | ||
) | ||
|
||
override val onBackPressedDispatcher: RedwoodOnBackPressedDispatcher = | ||
object : RedwoodOnBackPressedDispatcher { | ||
|
@@ -82,7 +86,9 @@ public open class RedwoodLayout( | |
|
||
init { | ||
setOnWindowInsetsChangeListener { insets -> | ||
mutableUiConfiguration.value = computeUiConfiguration(insets = insets.safeDrawing) | ||
mutableUiConfiguration.value = computeUiConfiguration( | ||
viewInsets = insets.safeDrawing.toMargin(Density(resources)), | ||
) | ||
} | ||
} | ||
|
||
|
@@ -112,7 +118,7 @@ public open class RedwoodLayout( | |
|
||
private fun computeUiConfiguration( | ||
config: Configuration = context.resources.configuration, | ||
insets: Insets = rootWindowInsetsCompat.safeDrawing, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ☠️This is the core problem of the old API. If we end up calling this function for a configuration change, it forgets what the view-local insets were and reverts to the window insets. |
||
viewInsets: Margin = uiConfiguration.value.viewInsets, | ||
): UiConfiguration { | ||
val viewportSize: Size | ||
val density: Double | ||
|
@@ -122,7 +128,8 @@ public open class RedwoodLayout( | |
} | ||
return UiConfiguration( | ||
darkMode = (config.uiMode and Configuration.UI_MODE_NIGHT_MASK) == Configuration.UI_MODE_NIGHT_YES, | ||
safeAreaInsets = insets.toMargin(Density(resources)), | ||
safeAreaInsets = Margin.Zero, | ||
viewInsets = viewInsets, | ||
viewportSize = viewportSize, | ||
density = density, | ||
layoutDirection = when (config.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.
Unfortunately
safeAreaInsets
are potentially non-zero on already-shipping clients, but they don’t necessarily apply to the view we want them to.Instead I’m creating another property that’s my attempt at fixing the window-local vs. view-local problem.