Skip to content

Commit

Permalink
Forward BackPresses on Android Modal
Browse files Browse the repository at this point in the history
  • Loading branch information
alexstyl committed Oct 16, 2024
1 parent 9ed8f15 commit 1698f74
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions core/src/androidMain/kotlin/Modal.android.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import android.os.Build
import android.view.Window
import android.view.WindowManager
import androidx.activity.ComponentDialog
import androidx.activity.compose.BackHandler
import androidx.compose.foundation.layout.Box
import androidx.compose.runtime.*
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.toArgb
import androidx.compose.ui.input.key.KeyEvent
import androidx.compose.ui.input.key.NativeKeyEvent
import androidx.compose.ui.input.key.onKeyEvent
import androidx.compose.ui.platform.ComposeView
import androidx.compose.ui.platform.LocalContext
Expand Down Expand Up @@ -48,6 +50,11 @@ internal actual fun Modal(
?: error("Attempted to get the dialog's window without content. This should never happen and it's a bug in the library. Kindly open an issue with the steps to reproduce so that we fix it ASAP: https://github.com/composablehorizons/composables-core/issues/new")
CompositionLocalProvider(LocalModalWindow provides localWindow) {
Box(Modifier.onKeyEvent(onKeyEvent)) {
BackHandler {
val backKeyDown = NativeKeyEvent(NativeKeyEvent.ACTION_DOWN, NativeKeyEvent.KEYCODE_BACK)
val backPress = KeyEvent(backKeyDown)
onKeyEvent(backPress)
}
content()
}
}
Expand Down

0 comments on commit 1698f74

Please sign in to comment.