From a2cac44a81ebd03d494647c917e7b08ff1114c14 Mon Sep 17 00:00:00 2001 From: kPherox Date: Mon, 29 Apr 2019 23:49:56 +0900 Subject: [PATCH] Add url scheme handler No yet confirmed for Windows/Linux --- src/main/Application.ts | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/src/main/Application.ts b/src/main/Application.ts index 06d177ad..7a206c86 100644 --- a/src/main/Application.ts +++ b/src/main/Application.ts @@ -30,9 +30,17 @@ export default class Application { private constructor() { this.isDarkMode = systemPreferences.isDarkMode() + const gotTheLock = app.requestSingleInstanceLock() + if (!gotTheLock) { + app.quit() + return + } + + app.setAsDefaultProtocolClient('thedesk') app.on('window-all-closed', () => this.onWindowAllClosed()) - app.on('ready', () => this.onReady()) - app.on('activate', () => this.onActivated()) + app.on('will-finish-launching', () => this.onWillFinishLaunching()) + app.on('ready', (launchInfo: any) => this.onReady()) + app.on('activate', (event: Event, hasVisibleWindows: boolean) => this.onActivated()) systemPreferences.subscribeNotification('AppleInterfaceThemeChangedNotification', () => { this.isDarkMode = systemPreferences.isDarkMode() @@ -78,6 +86,23 @@ export default class Application { } } + private onWillFinishLaunching() { + app.on('open-url', (event: Event, url: string) => { + // On macOS: url = thedesk://login?code=xxx + Application.handleOpenURL(event, url) + }) + app.on('second-instance', (event: Event, argv: string[], workingDirectory: string) => { + // On Windows/Linux: argv = ここをmacOSに合わせるかこちらに合わせるか + Application.handleOpenURL(event, argv[1]) + }) + } + + private static handleOpenURL(event: Event, url: string) { + if (Window.windowMap.has('main')) { + Window.windowMap.get('main')!.webContents.send("open-url-scheme", url) + } + } + public static openUrl(event: Event, url: string) { shell.openExternal(url, { activate: false