Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Packager Superpowers #710

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions src/packager/electron-superpowers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
const exposeBrowserWindowMethods = [
// https://www.electronjs.org/docs/latest/api/browser-window
'focus',
'blur',
'isFocused',
'show',
'showInactive',
'hide',
'isVisible',
'maximize',
'unmaximize',
'isMaximized',
'minimize',
'restore',
'isMinimized',
'isNormal',
'setAspectRatio',
'setBounds',
'getBounds',
'setContentBounds',
'getContentBounds',
'getNormalBounds',
'setEnabled',
'isEnabled',
'setSize',
'getSize',
'setContentSize',
'getContentSize',
'setMinimumSize',
'getMinimumSize',
'setMaximumSize',
'getMaximumSize',
'setResizable',
'isResizable',
'setMovable',
'isMovable',
'setMinimizable',
'isMinimizable',
'setMaximizable',
'isMaximizable',
'setClosable',
'isClosable',
'setAlwaysOnTop',
'isAlwaysOnTop',
'moveTop',
'center',
'setPosition',
'getPosition',
'setProgressBar',
'setOpacity',
'getOpacity',
'setFocusable',
'isFocusable'
];

const main = `
const {ipcMain, BrowserWindow} = require('electron');

const ALLOWED_METHODS = ${JSON.stringify(exposeBrowserWindowMethods)};

ipcMain.handle('superpowers/BrowserWindow', (event, name, ...args) => {
if (!ALLOWED_METHODS.includes(name)) {
throw new Error('Unsupported BrowserWindow method: ' + name);
}
return BrowserWindow.fromWebContents(event.sender)[name](...args);
});
`.trimStart();

const preload = `
const {contextBridge, ipcRenderer} = require('electron');

contextBridge.exposeInMainWorld('PackagerSuperpowers', {
BrowserWindow: (name, ...args) => ipcRenderer.invoke('superpowers/BrowserWindow', name, ...args)
});
`.trimStart();

export default {
main,
preload
};
10 changes: 9 additions & 1 deletion src/packager/packager.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {APP_NAME, WEBSITE, COPYRIGHT_NOTICE, ACCENT_COLOR} from './brand';
import {OutdatedPackagerError} from '../common/errors';
import {darken} from './colors';
import {Adapter} from './adapter';
import electronSuperpowerScripts from './electron-superpowers';

const PROGRESS_LOADED_SCRIPTS = 0.1;

Expand Down Expand Up @@ -512,6 +513,8 @@ cd "$(dirname "$0")"
const contentsPrefix = isMac ? `${rootPrefix}${packageName}.app/Contents/` : rootPrefix;
const resourcesPrefix = isMac ? `${contentsPrefix}Resources/app/` : `${contentsPrefix}resources/app/`;
const electronMainName = 'electron-main.js';
const electronSuperpowersMainName = 'electron-superpowers.js';
const electronSuperpowersPreloadName = 'electron-preload.js';
const iconName = 'icon.png';

const icon = await Adapter.getAppIcon(this.options.app.icon);
Expand All @@ -525,7 +528,7 @@ cd "$(dirname "$0")"
zip.file(`${resourcesPrefix}package.json`, JSON.stringify(manifest, null, 4));

const mainJS = `'use strict';
const {app, BrowserWindow, Menu, shell, screen, dialog} = require('electron');
const {app, BrowserWindow, Menu, shell, screen, dialog, ipcMain} = require('electron');
const path = require('path');

const isWindows = process.platform === 'win32';
Expand Down Expand Up @@ -558,6 +561,7 @@ const createWindow = (windowOptions) => {
sandbox: true,
contextIsolation: true,
nodeIntegration: false,
preload: path.resolve(__dirname, ${JSON.stringify(electronSuperpowersPreloadName)}),
},
show: true,
width: 480,
Expand Down Expand Up @@ -695,8 +699,12 @@ app.on('window-all-closed', () => {
app.whenReady().then(() => {
createProjectWindow(defaultProjectURL);
});

require('./' + ${JSON.stringify(electronSuperpowersMainName)});
`;
zip.file(`${resourcesPrefix}${electronMainName}`, mainJS);
zip.file(`${resourcesPrefix}${electronSuperpowersMainName}`, electronSuperpowerScripts.main)
zip.file(`${resourcesPrefix}${electronSuperpowersPreloadName}`, electronSuperpowerScripts.preload);

for (const [path, data] of Object.entries(projectZip.files)) {
setFileFast(zip, `${resourcesPrefix}${path}`, data);
Expand Down