xref: /aosp_15_r20/frameworks/base/packages/SystemUI/compose/core/src/com/android/compose/ui/graphics/DrawInOverlay.kt (revision d57664e9bc4670b3ecf6748a746a57c557b6bc9e)
1 /*
2  * Copyright (C) 2024 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.compose.ui.graphics
18 
19 import android.view.View
20 import android.view.ViewGroupOverlay
21 import androidx.compose.foundation.layout.Box
22 import androidx.compose.foundation.layout.fillMaxSize
23 import androidx.compose.runtime.Composable
24 import androidx.compose.runtime.DisposableEffect
25 import androidx.compose.runtime.remember
26 import androidx.compose.runtime.rememberCompositionContext
27 import androidx.compose.ui.Modifier
28 import androidx.compose.ui.platform.ComposeView
29 import androidx.compose.ui.platform.LocalContext
30 import androidx.compose.ui.platform.LocalView
31 import androidx.compose.ui.unit.IntSize
32 import androidx.lifecycle.findViewTreeLifecycleOwner
33 import androidx.lifecycle.findViewTreeViewModelStoreOwner
34 import androidx.lifecycle.setViewTreeLifecycleOwner
35 import androidx.lifecycle.setViewTreeViewModelStoreOwner
36 import androidx.savedstate.findViewTreeSavedStateRegistryOwner
37 import androidx.savedstate.setViewTreeSavedStateRegistryOwner
38 
39 /**
40  * Draw this composable in the [overlay][ViewGroupOverlay] of the [current ComposeView][LocalView].
41  */
42 @Composable
drawInOverlaynull43 fun Modifier.drawInOverlay(): Modifier {
44     val containerState = remember { ContainerState() }
45     val context = LocalContext.current
46     val localView = LocalView.current
47     val compositionContext = rememberCompositionContext()
48     val displayMetrics = context.resources.displayMetrics
49     val displaySize = IntSize(displayMetrics.widthPixels, displayMetrics.heightPixels)
50 
51     DisposableEffect(containerState, context, localView, compositionContext, displaySize) {
52         val overlay = localView.rootView.overlay as ViewGroupOverlay
53         val view =
54             ComposeView(context).apply {
55                 setParentCompositionContext(compositionContext)
56 
57                 // Set the owners.
58                 setViewTreeLifecycleOwner(localView.findViewTreeLifecycleOwner())
59                 setViewTreeViewModelStoreOwner(localView.findViewTreeViewModelStoreOwner())
60                 setViewTreeSavedStateRegistryOwner(localView.findViewTreeSavedStateRegistryOwner())
61 
62                 setContent { Box(Modifier.fillMaxSize().container(containerState)) }
63             }
64 
65         overlay.add(view)
66 
67         // Make the ComposeView as big as the display. We have to manually measure and layout the
68         // View given that there is no layout pass in Android overlays.
69         view.measure(
70             View.MeasureSpec.makeSafeMeasureSpec(displaySize.width, View.MeasureSpec.EXACTLY),
71             View.MeasureSpec.makeSafeMeasureSpec(displaySize.height, View.MeasureSpec.EXACTLY),
72         )
73         view.layout(0, 0, displaySize.width, displaySize.height)
74 
75         onDispose { overlay.remove(view) }
76     }
77 
78     return this.drawInContainer(containerState, enabled = { true })
79 }
80