TeaSpeak-Client/modules/renderer/UnloadHandler.ts

60 lines
2.0 KiB
TypeScript
Raw Normal View History

2020-08-21 07:37:10 -04:00
import {Settings, settings} from "tc-shared/settings";
import {tr} from "tc-shared/i18n/localize";
import {Arguments, process_args} from "../shared/process-arguments";
import {remote} from "electron";
2020-09-24 16:06:52 -04:00
import {server_connections} from "tc-shared/ConnectionManager";
2020-08-21 07:37:10 -04:00
2020-08-23 15:26:27 -04:00
window.onbeforeunload = event => {
2020-08-21 07:37:10 -04:00
if(settings.static(Settings.KEY_DISABLE_UNLOAD_DIALOG))
return;
const active_connections = server_connections.all_connections().filter(e => e.connected);
if(active_connections.length == 0) return;
const do_exit = (closeWindow: boolean) => {
const dp = server_connections.all_connections().map(e => {
if(e.serverConnection.connected())
return e.serverConnection.disconnect(tr("client closed"))
2020-08-23 15:26:27 -04:00
.catch(error => {
console.warn(tr("Failed to disconnect from server %s on client close: %o"),
e.serverConnection.remote_address().host + ":" + e.serverConnection.remote_address().port,
error
);
});
2020-08-21 07:37:10 -04:00
return Promise.resolve();
});
if(closeWindow) {
const exit = () => {
const {remote} = window.require('electron');
remote.getCurrentWindow().close();
};
Promise.all(dp).then(exit);
/* force exit after 2500ms */
setTimeout(exit, 2500);
}
};
if(process_args.has_flag(Arguments.DEBUG)) {
do_exit(false);
return;
}
remote.dialog.showMessageBox(remote.getCurrentWindow(), {
type: 'question',
buttons: ['Yes', 'No'],
title: 'Confirm',
message: 'Are you really sure?\nYou\'re still connected!'
}).then(result => {
if(result.response === 0) {
/* prevent quitting because we try to disconnect */
2020-08-23 15:26:27 -04:00
window.onbeforeunload = e => e.preventDefault();
2020-08-21 07:37:10 -04:00
do_exit(true);
}
});
2020-08-23 15:26:27 -04:00
event.preventDefault();
event.returnValue = "question";
}