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 FullScreenComposeViewInOverlay { Modifier.container(containerState) }
46 return this.drawInContainer(containerState, enabled = { true })
47 }
48
49 @Composable
FullScreenComposeViewInOverlaynull50 internal fun FullScreenComposeViewInOverlay(
51 overlay: ViewGroupOverlay? = null,
52 modifier: (ComposeView) -> Modifier = { Modifier },
53 ) {
54 val context = LocalContext.current
55 val localView = LocalView.current
56 val compositionContext = rememberCompositionContext()
57 val displayMetrics = context.resources.displayMetrics
58 val displaySize = IntSize(displayMetrics.widthPixels, displayMetrics.heightPixels)
59 val overlay = overlay ?: localView.rootView.overlay as ViewGroupOverlay
60
<lambda>null61 DisposableEffect(context, localView, overlay, compositionContext, displaySize) {
62 val view =
63 ComposeView(context).apply {
64 setParentCompositionContext(compositionContext)
65
66 // Set the owners.
67 setViewTreeLifecycleOwner(localView.findViewTreeLifecycleOwner())
68 setViewTreeViewModelStoreOwner(localView.findViewTreeViewModelStoreOwner())
69 setViewTreeSavedStateRegistryOwner(localView.findViewTreeSavedStateRegistryOwner())
70
71 val view = this
72 setContent { Box(modifier(view).fillMaxSize()) }
73 }
74
75 overlay.add(view)
76
77 // Make the ComposeView as big as the display. We have to manually measure and layout the
78 // View given that there is no layout pass in Android overlays.
79 view.measure(
80 View.MeasureSpec.makeSafeMeasureSpec(displaySize.width, View.MeasureSpec.EXACTLY),
81 View.MeasureSpec.makeSafeMeasureSpec(displaySize.height, View.MeasureSpec.EXACTLY),
82 )
83 view.layout(0, 0, displaySize.width, displaySize.height)
84
85 onDispose { overlay.remove(view) }
86 }
87 }
88