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;
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 log from 'electron-log';
import cli from './cli';
import { IPC_MAIN_CHANNELS } from '../common/constants';
import { IPC_MAIN_CHANNELS, PROTOCOL } from '../common/constants';
import MenuBuilder from './menu';
import { isValidCliArgURL, resolveHtmlPath } from './util';
import { BROWSER_SYNC_HOST, initInstance } from './browser-sync';
@ -26,6 +26,19 @@ import { initNativeFunctionHandlers } from './native-functions';
import { WebPermissionHandlers } from './web-permissions';
import { initHttpBasicAuthHandlers } from './http-basic-auth';
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 {
constructor() {
@ -103,10 +116,8 @@ const createWindow = async () => {
mainWindow.on('ready-to-show', async () => {
await initInstance();
if (isValidCliArgURL(cli.input[0])) {
mainWindow?.webContents.send(IPC_MAIN_CHANNELS.OPEN_URL, {
url: cli.input[0],
});
if (urlToOpen !== undefined && isValidCliArgURL(urlToOpen)) {
openUrl(urlToOpen, mainWindow);
}
if (!mainWindow) {
@ -143,6 +154,16 @@ const createWindow = async () => {
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...
*/

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,
});
};