Skip to content

Commit aad6edb

Browse files
committed
chore: single window handling in electron
1 parent caeb1fe commit aad6edb

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

src-electron/main.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@ const { registerFsIpcHandlers, getAppDataDir } = require('./main-fs-ipc');
77
const { registerCredIpcHandlers, cleanupWindowTrust } = require('./main-cred-ipc');
88
const { registerWindowIpcHandlers, registerWindow } = require('./main-window-ipc');
99

10+
// Request single instance lock - only one instance of the app should run at a time
11+
const gotTheLock = app.requestSingleInstanceLock();
12+
13+
if (!gotTheLock) {
14+
// Another instance is running, quit this one immediately
15+
app.quit();
16+
}
17+
1018
// In-memory key-value store shared across all windows (mirrors Tauri's put_item/get_all_items)
1119
// Used for multi-window storage synchronization
1220
const sharedStorageMap = new Map();
@@ -99,6 +107,20 @@ app.on('quit-requested', (exitCode) => {
99107
gracefulShutdown(exitCode);
100108
});
101109

110+
// Handle second instance attempts - forward args to existing windows
111+
app.on('second-instance', (event, commandLine, workingDirectory) => {
112+
// Forward to all windows via IPC
113+
// Window focusing is handled by the renderer's singleInstanceHandler
114+
BrowserWindow.getAllWindows().forEach(win => {
115+
if (!win.isDestroyed()) {
116+
win.webContents.send('single-instance', {
117+
args: commandLine,
118+
cwd: workingDirectory
119+
});
120+
}
121+
});
122+
});
123+
102124
app.whenReady().then(async () => {
103125
// Remove default menu bar
104126
Menu.setApplicationMenu(null);
@@ -136,6 +158,10 @@ app.on('window-all-closed', () => {
136158
gracefulShutdown(0);
137159
});
138160

161+
// macOS: When dock icon is clicked and no windows are open, create a new window.
162+
// Currently this won't fire because window-all-closed quits the app. If macOS support
163+
// is added and we want apps to stay running with no windows, change window-all-closed
164+
// to not quit on macOS (process.platform !== 'darwin').
139165
app.on('activate', () => {
140166
if (BrowserWindow.getAllWindows().length === 0) {
141167
createWindow();

src-electron/preload.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,5 +133,8 @@ contextBridge.exposeInMainWorld('electronAPI', {
133133
// Close requested handler
134134
onCloseRequested: (callback) => ipcRenderer.on('close-requested', () => callback()),
135135
registerCloseHandler: () => ipcRenderer.invoke('register-close-handler'),
136-
allowClose: () => ipcRenderer.invoke('allow-close')
136+
allowClose: () => ipcRenderer.invoke('allow-close'),
137+
138+
// Single instance event listener (mirrors Tauri's single-instance event)
139+
onSingleInstance: (callback) => ipcRenderer.on('single-instance', (_event, payload) => callback(payload))
137140
});

0 commit comments

Comments
 (0)