Skip to content
This repository has been archived by the owner on Mar 26, 2024. It is now read-only.

Commit

Permalink
Block closing of browser window
Browse files Browse the repository at this point in the history
  • Loading branch information
SerVB committed Jul 21, 2020
1 parent 8226d35 commit bdb1c0d
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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()
}
Expand Down Expand Up @@ -458,6 +463,8 @@ sealed class ClientState {

mobileKeyboardHelper.dispose()

closeBlocker.removeListener()

showDisconnectedMessage(action.url, action.closeCode)

Disconnected
Expand Down

0 comments on commit bdb1c0d

Please sign in to comment.