Skip to content

Commit b7f1297

Browse files
authored
fix: only allow a single instance of the app (#109)
1 parent a7f7835 commit b7f1297

File tree

2 files changed

+27
-5
lines changed

2 files changed

+27
-5
lines changed

src/main/Entrypoint.ts

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,32 @@ export class Entrypoint {
2222
}
2323

2424
public readonly start = (): void => {
25+
// Check we're the first instance of this application.
26+
if (!this.application.requestSingleInstanceLock()) {
27+
this.application.quit();
28+
}
29+
2530
const privSchemes = this.customProtocols.map((p) => p.privilegedSchemes).flat();
2631
log.verbose(
2732
'Registering schemes as privileged:',
2833
privSchemes.map((s) => s.scheme),
2934
);
3035
protocol.registerSchemesAsPrivileged(privSchemes);
3136

37+
this.application.on('second-instance', this.onSecondInstance);
3238
this.application.on('window-all-closed', this.onWindowAllClosed);
3339

34-
// Signal to Electron that we're ready to go when it is.
40+
// The ready handler should be the last registered as it may be fired immediately.
3541
this.application.on('ready', this.onReady);
3642
};
3743

38-
private readonly onWindowAllClosed = (): void => {
39-
if (process.platform !== 'darwin') {
40-
this.application.quit();
41-
}
44+
private readonly onSecondInstance = (
45+
_event: Electron.Event,
46+
_argv: string[],
47+
_workingDirectory: string,
48+
_additionalData: unknown,
49+
): void => {
50+
this.mainWindow.bringWindowToTop();
4251
};
4352

4453
private readonly onReady = (): void => {
@@ -49,6 +58,10 @@ export class Entrypoint {
4958
.catch(this.onFatalError);
5059
};
5160

61+
private readonly onWindowAllClosed = (): void => {
62+
this.application.quit();
63+
};
64+
5265
private readonly onFatalError = (error: unknown): never => {
5366
log.error(error);
5467
// eslint-disable-next-line no-process-exit

src/main/MainWindow.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,15 @@ export class MainWindow implements OnReadyHandler {
7676
);
7777
};
7878

79+
public readonly bringWindowToTop = (): void => {
80+
if (this.window != null) {
81+
if (this.window.isMinimized()) {
82+
this.window.restore();
83+
}
84+
this.window.focus();
85+
}
86+
};
87+
7988
private readonly isAllowedInternalNavigationUrl = (url: string): boolean => {
8089
const parsed = new URL(url);
8190

0 commit comments

Comments
 (0)