Skip to content
This repository has been archived by the owner on Jun 20, 2022. It is now read-only.

Commit

Permalink
feat: get url and focus window when receiving deep link
Browse files Browse the repository at this point in the history
  • Loading branch information
Antonella Sgarlatta committed Oct 7, 2021
1 parent dde9c1d commit 3956f5d
Showing 1 changed file with 31 additions and 18 deletions.
49 changes: 31 additions & 18 deletions app/application.ts
Expand Up @@ -20,15 +20,17 @@ import { UpdateState } from './javascripts/main/updateManager';
import { handleTestMessage } from './javascripts/main/testing';
import { MessageType } from '../test/TestIpcMessage';

const deepLinkScheme = 'standardnotes';

export class AppState {
readonly version: string;
readonly store: Store;
readonly startUrl = Urls.indexHtml;
readonly isPrimaryInstance: boolean;
deepLinkUrl?: string;
public willQuitApp = false;
public lastBackupDate: number | null = null;
public windowState?: WindowState;
public deepLinkUrl?: string;
public readonly updates: UpdateState;

constructor(app: Electron.App) {
Expand Down Expand Up @@ -77,22 +79,28 @@ export function initializeApplication(args: {
}
}

function registerSingleInstanceHandler(
app: Electron.App,
appState: Pick<AppState, 'windowState'>
) {
app.on('second-instance', () => {
/* Someone tried to run a second instance, we should focus our window. */
const window = appState.windowState?.window;
if (window) {
if (!window.isVisible()) {
window.show();
}
if (window.isMinimized()) {
window.restore();
}
window.focus();
function focusWindow(appState: AppState) {
const window = appState.windowState?.window;

if (window) {
if (!window.isVisible()) {
window.show();
}
if (window.isMinimized()) {
window.restore();
}
window.focus();
}
}

function registerSingleInstanceHandler(app: Electron.App, appState: AppState) {
app.on('second-instance', (_event: Event, argv: string[]) => {
if (isWindows()) {
appState.deepLinkUrl = argv.find((arg) => arg.startsWith(deepLinkScheme));
}

/* Someone tried to run a second instance, we should focus our window. */
focusWindow(appState);
});
}

Expand Down Expand Up @@ -120,6 +128,11 @@ function registerAppEventListeners(args: {
windowState.window.show();
});

app.on('open-url', (_event, url) => {
state.deepLinkUrl = url;
focusWindow(state);
});

app.on('ready', () => {
if (!state.isPrimaryInstance) {
console.warn('Quiting app and focusing existing instance.');
Expand All @@ -132,8 +145,8 @@ function registerAppEventListeners(args: {
}

async function setupDeepLinking() {
if (!app.isDefaultProtocolClient('standardnotes')) {
app.setAsDefaultProtocolClient('standardnotes');
if (!app.isDefaultProtocolClient(deepLinkScheme)) {
app.setAsDefaultProtocolClient(deepLinkScheme);
}
}

Expand Down

0 comments on commit 3956f5d

Please sign in to comment.