diff --git a/projector-client-web/src/main/kotlin/org/jetbrains/projector/client/web/misc/CloseBlocker.kt b/projector-client-web/src/main/kotlin/org/jetbrains/projector/client/web/misc/CloseBlocker.kt new file mode 100644 index 000000000..32c89c093 --- /dev/null +++ b/projector-client-web/src/main/kotlin/org/jetbrains/projector/client/web/misc/CloseBlocker.kt @@ -0,0 +1,51 @@ +/* + * MIT License + * + * Copyright (c) 2019-2020 JetBrains s.r.o. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package org.jetbrains.projector.client.web.misc + +import org.w3c.dom.BeforeUnloadEvent +import org.w3c.dom.Window +import org.w3c.dom.events.Event + +class CloseBlocker(private val window: Window) { + + private fun onBeforeUnload(e: Event) { + require(e is BeforeUnloadEvent) + + e.preventDefault() + e.returnValue = "" + } + + fun setListener() { + window.addEventListener(beforeUnloadType, this::onBeforeUnload) + } + + fun removeListener() { + window.removeEventListener(beforeUnloadType, this::onBeforeUnload) + } + + companion object { + + private const val beforeUnloadType = "beforeunload" + } +} diff --git a/projector-client-web/src/main/kotlin/org/jetbrains/projector/client/web/state/ClientState.kt b/projector-client-web/src/main/kotlin/org/jetbrains/projector/client/web/state/ClientState.kt index fab378d55..a9f2248dc 100644 --- a/projector-client-web/src/main/kotlin/org/jetbrains/projector/client/web/state/ClientState.kt +++ b/projector-client-web/src/main/kotlin/org/jetbrains/projector/client/web/state/ClientState.kt @@ -57,6 +57,7 @@ import org.khronos.webgl.ArrayBuffer import org.w3c.dom.* import org.w3c.dom.events.Event import kotlin.browser.document +import kotlin.browser.window import kotlin.math.roundToInt sealed class ClientState { @@ -335,6 +336,10 @@ sealed class ClientState { else -> MobileKeyboardHelperImpl(openingTimeStamp, inputController.specialKeysState) { stateMachine.fire(ClientAction.AddEvent(it)) } } + private val closeBlocker = CloseBlocker(window).apply { + setListener() + } + init { windowSizeController.addListener() } @@ -458,6 +463,8 @@ sealed class ClientState { mobileKeyboardHelper.dispose() + closeBlocker.removeListener() + showDisconnectedMessage(action.url, action.closeCode) Disconnected