Skip to content

Commit

Permalink
feat: Add basic multi-window support
Browse files Browse the repository at this point in the history
  • Loading branch information
Illusion47586 committed Jun 17, 2022
1 parent 3e51642 commit 43ae7ba
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 23 deletions.
26 changes: 17 additions & 9 deletions src/main/main.ts
Expand Up @@ -14,10 +14,11 @@ import { app, BrowserWindow, shell, ipcMain } from 'electron';
import { autoUpdater } from 'electron-updater';
import log from 'electron-log';
import { release } from 'os';
import MenuBuilder from './menu';
import chalk from 'chalk';
import { isExtMatch, resolveHtmlPath } from './util';
import { extractJulia, runPluto } from './pluto';
import { extractJulia, isPlutoRunning, runPluto } from './pluto';
import { arg, checkIfCalledViaCLI } from './cli';
import MenuBuilder from './menu';

export default class AppUpdater {
constructor() {
Expand Down Expand Up @@ -103,9 +104,9 @@ const createWindow = async (
await extractJulia(getAssetPath);

if (checkIfCalledViaCLI(process.argv)) {
url = arg.url;
project = arg.project;
notebook =
url ??= arg.url;
project ??= arg.project;
notebook ??=
arg.notebook ?? (typeof arg._[0] === 'string' && isExtMatch(arg._[0]))
? (arg._[0] as string)
: undefined;
Expand All @@ -117,6 +118,7 @@ const createWindow = async (
await installExtensions();
}

console.log(chalk.bgGreenBright('Creating a new window.'));
mainWindow = new BrowserWindow({
title: '⚡ Pluto ⚡',
height: 600,
Expand All @@ -131,12 +133,16 @@ const createWindow = async (
},
});

if (url) mainWindow.loadURL(url);
else {
mainWindow.loadURL(resolveHtmlPath('index.html'));
mainWindow.loadURL(resolveHtmlPath('index.html'));

if (!isPlutoRunning()) {
runPluto(mainWindow, getAssetPath, project, notebook);
}

if (url) {
mainWindow.loadURL(url);
}

mainWindow.on('ready-to-show', () => {
if (!mainWindow) {
throw new Error('"mainWindow" is not defined');
Expand All @@ -152,7 +158,7 @@ const createWindow = async (
mainWindow = null;
});

const menuBuilder = new MenuBuilder(mainWindow);
const menuBuilder = new MenuBuilder(mainWindow, createWindow);
menuBuilder.buildMenu();

// Open urls in the user's browser
Expand Down Expand Up @@ -193,3 +199,5 @@ app
});
})
.catch(log.error);

export { createWindow };
53 changes: 39 additions & 14 deletions src/main/menu.ts
@@ -1,3 +1,4 @@
/* eslint-disable no-underscore-dangle */
import {
app,
Menu,
Expand All @@ -18,17 +19,29 @@ interface DarwinMenuItemConstructorOptions extends MenuItemConstructorOptions {
export default class MenuBuilder {
mainWindow: BrowserWindow;

constructor(mainWindow: BrowserWindow) {
private _createWindow: (
url?: string,
project?: string,
notebook?: string
) => Promise<void>;

constructor(
mainWindow: BrowserWindow,
createWindow: (
url?: string,
project?: string,
notebook?: string
) => Promise<void>
) {
this.mainWindow = mainWindow;
this._createWindow = createWindow;
}

buildMenu(): Menu {
if (
this.setupContextMenu(
process.env.NODE_ENV === 'development' ||
process.env.DEBUG_PROD === 'true'
) {
this.setupDevelopmentEnvironment();
}
process.env.DEBUG_PROD === 'true'
);

const template =
process.platform === 'darwin'
Expand All @@ -41,18 +54,30 @@ export default class MenuBuilder {
return menu;
}

setupDevelopmentEnvironment(): void {
setupContextMenu(isDebug = false): void {
this.mainWindow.webContents.on('context-menu', (_, props) => {
const { x, y } = props;
const { x, y, linkURL } = props;

Menu.buildFromTemplate([
{
label: 'Inspect element',
const template = isDebug
? [
{
label: 'Inspect element',
click: () => {
this.mainWindow.webContents.inspectElement(x, y);
},
},
]
: [];

if (linkURL.length > 0)
template.push({
label: 'Open in new window',
click: () => {
this.mainWindow.webContents.inspectElement(x, y);
this._createWindow(linkURL);
},
},
]).popup({ window: this.mainWindow });
});

Menu.buildFromTemplate(template).popup({ window: this.mainWindow });
});
}

Expand Down

0 comments on commit 43ae7ba

Please sign in to comment.