Skip to content

Commit

Permalink
Only allow one instance of app + quit app on Windows when window is c…
Browse files Browse the repository at this point in the history
…losed. (#73)

* Only allow one instance of electron app to be created.

* Hide on mac.
  • Loading branch information
robinjhuang authored Oct 9, 2024
1 parent c5176c8 commit 98b13d5
Showing 1 changed file with 45 additions and 18 deletions.
63 changes: 45 additions & 18 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import * as Sentry from '@sentry/electron/main';

import { updateElectronApp, UpdateSourceType } from 'update-electron-app';
import * as net from 'net';
import { ProgressUpdate } from './renderer/screens/ProgressOverlay';

updateElectronApp({
updateSource: {
Expand All @@ -33,6 +32,22 @@ import('electron-squirrel-startup').then((ess) => {
}
});

const gotTheLock = app.requestSingleInstanceLock();

if (!gotTheLock) {
app.quit();
} else {
app.on('second-instance', (event, commandLine, workingDirectory, additionalData) => {
log.info('Received second instance message!');
log.info(additionalData);

if (mainWindow) {
if (mainWindow.isMinimized()) mainWindow.restore();
mainWindow.focus();
}
});
}

app.isPackaged &&
Sentry.init({
dsn: SENTRY_URL_ENDPOINT,
Expand Down Expand Up @@ -126,9 +141,18 @@ function buildMenu(): Menu {
return menu;
}

/**
* Creates the main window. If the window already exists, it will return the existing window.
* @param userResourcesPath The path to the user's resources.
* @returns The main window.
*/
export const createWindow = async (userResourcesPath: string): Promise<BrowserWindow> => {
const primaryDisplay = screen.getPrimaryDisplay();
const { width, height } = primaryDisplay.workAreaSize;
if (mainWindow) {
log.info('Main window already exists');
return mainWindow;
}
mainWindow = new BrowserWindow({
title: 'ComfyUI',
width: width,
Expand All @@ -143,27 +167,15 @@ export const createWindow = async (userResourcesPath: string): Promise<BrowserWi

await loadRendererIntoMainWindow();

ipcMain.on(IPC_CHANNELS.RENDERER_READY, () => {
log.info('Received renderer-ready message!');
// Send all queued messages
while (messageQueue.length > 0) {
const message = messageQueue.shift();
log.info('Sending queued message ', message.channel);
mainWindow.webContents.send(message.channel, message.data);
}
});

// Set up the System Tray Icon for all platforms
// Returns a tray so you can set a global var to access.
SetupTray(mainWindow, userResourcesPath);

// Overrides the behavior of closing the window to allow for
// the python server to continue to run in the background
mainWindow.on('close', (e: Electron.Event) => {
e.preventDefault();
mainWindow.hide();
// Mac Only Behavior
if (process.platform === 'darwin') {
e.preventDefault();
mainWindow.hide();
app.dock.hide();
}
});
Expand Down Expand Up @@ -316,6 +328,19 @@ app.on('ready', async () => {

try {
await createWindow(userResourcesPath);
mainWindow.on('close', () => {
mainWindow = null;
app.quit();
});
ipcMain.on(IPC_CHANNELS.RENDERER_READY, () => {
log.info('Received renderer-ready message!');
// Send all queued messages
while (messageQueue.length > 0) {
const message = messageQueue.shift();
log.info('Sending queued message ', message.channel);
mainWindow.webContents.send(message.channel, message.data);
}
});
port = await findAvailablePort(8000, 9999).catch((err) => {
log.error(`ERROR: Failed to find available port: ${err}`);
throw err;
Expand Down Expand Up @@ -452,9 +477,11 @@ app.on('window-all-closed', () => {
app.on('activate', () => {
// On OS X it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (BrowserWindow.getAllWindows().length === 0) {
const { userResourcesPath } = getResourcesPaths();
createWindow(userResourcesPath);
if (process.platform === 'darwin') {
if (BrowserWindow.getAllWindows().length === 0) {
const { userResourcesPath } = getResourcesPaths();
createWindow(userResourcesPath);
}
}
});

Expand Down

0 comments on commit 98b13d5

Please sign in to comment.