Hi, is it possible to implement edge-to-edge by co...
# compose
h
Hi, is it possible to implement edge-to-edge by compose atm?
r
What's edge to edge?
u
Drawing behind system bars
z
Don't see why not. Composables are hosted in Android views, so anywhere a view can go, a composable can go.
👆 2
u
he probably also means observing window insets to add padding bottom to recycling lists (with the clipPadding = false effect)
h
Exactly. How can I get Android view that hosts Composable?
t
I do it currently on my self but i think there will be also an easier Compose way soon.
Copy code
private var insets by mutableStateOf(WindowInsetsCompat.Builder().build())

    private val systemWindowInsetListener = { view: View, insets: WindowInsetsCompat ->
        this.insets = insets
        insets
    }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        ViewCompat.setOnApplyWindowInsetsListener(window.decorView.rootView, systemWindowInsetListener)
        setContent {
                AppThemeTransparent {
                    val insetPadding = with(DensityAmbient.current) {
                        val ltr = ConfigurationAmbient.current.localeLayoutDirection == LayoutDirection.Ltr
                        with(insets) {
                            InnerPadding(
                                    start = if (ltr) systemWindowInsetLeft.toDp() else systemWindowInsetRight.toDp(),
                                    top = systemWindowInsetTop.toDp(),
                                    end = if (ltr) systemWindowInsetRight.toDp() else systemWindowInsetLeft.toDp(),
                                    bottom = systemWindowInsetBottom.toDp()
                            )
                        }
                    }
👍 1
z
ViewGroup.setContent
h
I created an ambient for it.
z
Neat! There are a few issues with that code though: Don't do things like register listeners directly during composition. This function will set a new listener on every composition pass, which is unnecessary. It's can also be incorrect - it will set the listener even if the composition fails. It also leaks - this code doesn't remove the listener when the function is removed from the composition (parent stops calling it). To fix all of these, set the listener inside an
onActive
block, and use
onDisposed
to clean up. Also FYI you can share larger code snippets in slack, with syntax highlighting, by posting a Snippet.
👍 1
I also don't think you need to pass a View in, you can get the hosting view from
ViewAmbient
.
h
I can not get window inset with
onActive
, but I can by wrapping it in
remember
. Is it a bug? Also
ViewAmbient
can only be called inside Composable context.
c
Just going to leave this here: https://goo.gle/compose-insets. Still early days for it, but it should do everything you need
👍 4