Nipun Rajput
12/03/2020, 10:30 AMTimo Drick
12/03/2020, 4:46 PM@Composable
fun FullScreenTransparentDialog(onCloseRequest: () -> Unit, children: @Composable () -> Unit) {
val view = ViewAmbient.current
val dialog = remember(view) { DialogWrapperTransparent(view, onCloseRequest) }
dialog.onCloseRequest = onCloseRequest
onActive {
dialog.show()
onDispose {
dialog.dismiss()
dialog.disposeComposition()
}
}
val composition = compositionReference()
onCommit {
dialog.setContent(composition) {
Box(Modifier.semantics {
this.dialog()
}) {
children()
}
}
}
}
private class DialogWrapperTransparent(composeView: View, var onCloseRequest: () -> Unit) : Dialog(composeView.context) {
val frameLayout = FrameLayout(context)
private var composition: Composition? = null
init {
window!!.apply {
requestFeature(Window.FEATURE_NO_TITLE)
setBackgroundDrawableResource(android.R.color.transparent)
setFlags(0, WindowManager.LayoutParams.FLAG_DIM_BEHIND)
addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
val displayRect = Rect()
decorView.getWindowVisibleDisplayFrame(displayRect) // This call is necessary to get the full screen. I do not know why :-(
setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT)
}
setContentView(frameLayout)
ViewTreeLifecycleOwner.set(frameLayout, ViewTreeLifecycleOwner.get(composeView))
ViewTreeViewModelStoreOwner.set(frameLayout, ViewTreeViewModelStoreOwner.get(composeView))
ViewTreeSavedStateRegistryOwner.set(
frameLayout,
ViewTreeSavedStateRegistryOwner.get(composeView)
)
}
fun setContent(parentComposition: CompositionReference, children: @Composable () -> Unit) {
// TODO: This should probably create a child composition of the original
composition = frameLayout.setContent(parentComposition, children)
}
fun disposeComposition() {
composition?.dispose()
}
override fun onTouchEvent(event: MotionEvent): Boolean {
val result = super.onTouchEvent(event)
if (result) {
onCloseRequest()
}
return result
}
override fun cancel() {
// Prevents the dialog from dismissing itself
return
}
override fun onBackPressed() {
onCloseRequest()
}
}
Nipun Rajput
01/08/2021, 9:18 AM