Handling responsively:// protocol

This commit is contained in:
Manoj Vivek 2023-04-27 07:47:02 +05:30
parent 6bc3f11d42
commit 3205020c0d
3 changed files with 38 additions and 5 deletions

View file

@ -29,3 +29,5 @@ export const IPC_MAIN_CHANNELS = {
} as const; } as const;
export type Channels = typeof IPC_MAIN_CHANNELS[keyof typeof IPC_MAIN_CHANNELS]; export type Channels = typeof IPC_MAIN_CHANNELS[keyof typeof IPC_MAIN_CHANNELS];
export const PROTOCOL = 'responsively';

View file

@ -13,7 +13,7 @@ import { app, BrowserWindow, shell, ipcMain, screen } from 'electron';
import { autoUpdater } from 'electron-updater'; import { autoUpdater } from 'electron-updater';
import log from 'electron-log'; import log from 'electron-log';
import cli from './cli'; import cli from './cli';
import { IPC_MAIN_CHANNELS } from '../common/constants'; import { IPC_MAIN_CHANNELS, PROTOCOL } from '../common/constants';
import MenuBuilder from './menu'; import MenuBuilder from './menu';
import { isValidCliArgURL, resolveHtmlPath } from './util'; import { isValidCliArgURL, resolveHtmlPath } from './util';
import { BROWSER_SYNC_HOST, initInstance } from './browser-sync'; import { BROWSER_SYNC_HOST, initInstance } from './browser-sync';
@ -26,6 +26,19 @@ import { initNativeFunctionHandlers } from './native-functions';
import { WebPermissionHandlers } from './web-permissions'; import { WebPermissionHandlers } from './web-permissions';
import { initHttpBasicAuthHandlers } from './http-basic-auth'; import { initHttpBasicAuthHandlers } from './http-basic-auth';
import { initAppMetaHandlers } from './app-meta'; import { initAppMetaHandlers } from './app-meta';
import { openUrl } from './protocol-handler';
if (process.defaultApp) {
if (process.argv.length >= 2) {
app.setAsDefaultProtocolClient(PROTOCOL, process.execPath, [
path.resolve(process.argv[1]),
]);
}
} else {
app.setAsDefaultProtocolClient(PROTOCOL);
}
let urlToOpen: string | undefined = cli.input[0];
export default class AppUpdater { export default class AppUpdater {
constructor() { constructor() {
@ -103,10 +116,8 @@ const createWindow = async () => {
mainWindow.on('ready-to-show', async () => { mainWindow.on('ready-to-show', async () => {
await initInstance(); await initInstance();
if (isValidCliArgURL(cli.input[0])) { if (urlToOpen !== undefined && isValidCliArgURL(urlToOpen)) {
mainWindow?.webContents.send(IPC_MAIN_CHANNELS.OPEN_URL, { openUrl(urlToOpen, mainWindow);
url: cli.input[0],
});
} }
if (!mainWindow) { if (!mainWindow) {
@ -143,6 +154,16 @@ const createWindow = async () => {
new AppUpdater(); new AppUpdater();
}; };
app.on('open-url', async (event, url) => {
if (mainWindow == null) {
// Will be handled by 'ready-to-show' event
urlToOpen = url.replace(`${PROTOCOL}://`, '');
await createWindow();
return;
}
openUrl(url, mainWindow);
});
/** /**
* Add event listeners... * Add event listeners...
*/ */

View file

@ -0,0 +1,10 @@
import { BrowserWindow } from 'electron';
import { IPC_MAIN_CHANNELS } from '../../common/constants';
// eslint-disable-next-line import/prefer-default-export
export const openUrl = (url: string, mainWindow: BrowserWindow | null) => {
console.log('open-url', url);
mainWindow?.webContents.send(IPC_MAIN_CHANNELS.OPEN_URL, {
url,
});
};