TeaSpeak-Client/modules/core/AppInstance.ts

51 lines
1.3 KiB
TypeScript
Raw Normal View History

2020-12-02 12:08:49 -05:00
import {app, BrowserWindow} from "electron";
2020-10-01 04:56:22 -04:00
import * as crash_handler from "../crash_handler";
let appReferences = 0;
2020-12-02 12:08:49 -05:00
let windowOpen = false;
2020-10-01 04:56:22 -04:00
/**
* Normally the app closes when all windows have been closed.
* If you're holding an app reference, it will not terminate when all windows have been closed.
*/
export function referenceApp() {
appReferences++;
}
export function dereferenceApp() {
appReferences--;
testAppState();
}
function testAppState() {
if(appReferences > 0) { return; }
2020-12-02 12:08:49 -05:00
if(windowOpen) { return; }
2020-10-01 04:56:22 -04:00
console.log("All windows have been closed, closing app.");
app.quit();
}
function initializeAppListeners() {
app.on('quit', () => {
console.debug("Shutting down app.");
crash_handler.finalize_handler();
console.log("App has been finalized.");
});
app.on('window-all-closed', () => {
2020-12-02 12:08:49 -05:00
windowOpen = false;
2020-10-01 04:56:22 -04:00
console.log("All windows have been closed. Manual app reference count: %d", appReferences);
testAppState();
});
2020-12-02 12:08:49 -05:00
app.on("browser-window-created", () => {
windowOpen = true;
})
2020-10-01 04:56:22 -04:00
app.on('activate', () => {
// On macOS it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
});
}
initializeAppListeners();