Prevent multiple instances and don't quit when all windows are closed

This commit is contained in:
Samuel Elliott 2022-06-18 12:20:42 +01:00
parent d54ba360cf
commit 7e5988685d
No known key found for this signature in database
GPG Key ID: 8420C7CDE43DC4D6
2 changed files with 18 additions and 7 deletions

View File

@ -2,4 +2,4 @@ const electron = require('electron');
// Do anything that must be run before the app is ready...
electron.app.whenReady().then(() => import('./index.js'));
electron.app.whenReady().then(() => import('./index.js')).then(m => m.init.call(null));

View File

@ -57,7 +57,14 @@ export class App {
}
}
app.whenReady().then(async () => {
export async function init() {
if (!app.requestSingleInstanceLock()) {
debug('Failed to acquire single instance lock');
console.warn('Failed to acquire single instance lock. Another instance of the app is running and will be focused.');
setTimeout(() => app.quit(), 1000);
return;
}
dotenvExpand.expand(dotenv.config({
path: path.join(paths.data, '.env'),
}));
@ -80,7 +87,15 @@ app.whenReady().then(async () => {
const menu = new MenuApp(appinstance);
appinstance.menu = menu;
app.on('second-instance', (event, command_line, working_directory, additional_data) => {
debug('Second instance', command_line, working_directory, additional_data);
appinstance.showMainWindow();
});
app.on('open-url', (event, url) => {
debug('Open URL', url);
if (url.match(/^com\.nintendo\.znca:\/\/(znca\/)game\/(\d+)\/?($|\?|\#)/i)) {
handleOpenWebServiceUri(appinstance.store, url);
event.preventDefault();
@ -95,11 +110,7 @@ app.whenReady().then(async () => {
debug('App started');
appinstance.showMainWindow();
});
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') app.quit();
});
}
class Updater {
private _cache: UpdateCacheData | null = null;