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

Use inversify to build the example electron app #4869

Closed
wants to merge 1 commit into from
Closed
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
- [plugin] fixed auto detection of new languages [#5753](https://github.com/theia-ide/theia/issues/5753)
- [vscode] unzip node_modules for built-in extensions [#5756](https://github.com/theia-ide/theia/pull/5756)
- [core] prevent the IDE from scrolling along with the text on mobile (e.g. on iPad) [#5742](https://github.com/theia-ide/theia/pull/5742)
- [core] added command to manually choose a keyboard layout
- [electron] use inversify to allow for contributions to the electron main process

Breaking changes:

Expand Down
246 changes: 45 additions & 201 deletions dev-packages/application-manager/src/generator/frontend-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ export class FrontendGenerator extends AbstractGenerator {
}
}

protected compileElectronMainModuleImports(): string {
return this.compileModuleImports(this.pck.electronMainModules, 'require');
}

protected compileIndexPreload(frontendModules: Map<string, string>): string {
const template = this.pck.props.generator.config.preloadTemplate;
if (!template) {
Expand Down Expand Up @@ -92,7 +96,7 @@ function start() {
themeService.loadUserTheme();

const application = container.get(FrontendApplication);
application.start();
return application.start();
}

module.exports = Promise.resolve()${this.compileFrontendModuleImports(frontendModules)}
Expand Down Expand Up @@ -122,215 +126,55 @@ if (process.env.LC_ALL) {
}
process.env.LC_NUMERIC = 'C';

require('reflect-metadata');
const electron = require('electron');
const { join, resolve } = require('path');
const { fork } = require('child_process');
const { app, shell, BrowserWindow, ipcMain, Menu } = electron;
const { resolve } = require('path');
const { Container } = require('inversify');
const { ElectronMainApplication, TheiaApplicationName, TheiaBackendMainPath, TheiaIndexHtmlPath } = require('@theia/core/lib/electron-main');
const { electronMainApplicationModule } = require('@theia/core/lib/electron-main/electron-main-application-module');

// We cannot use the \`process.cwd()\` as the application project path (the location of the \`package.json\` in other words)
// in a bundled electron application because it depends on the way we start it. For instance, on OS X, these are a differences:
// https://github.com/theia-ide/theia/issues/3297#issuecomment-439172274
process.env.THEIA_APP_PROJECT_PATH = resolve(__dirname, '..', '..');

// Set the electron version for both the dev and the production mode. (https://github.com/theia-ide/theia/issues/3254)
// Otherwise, the forked backend processes will not know that they're serving the electron frontend.
const { versions } = process;
// @ts-ignore
if (versions && typeof versions.electron !== 'undefined') {
// @ts-ignore
process.env.THEIA_ELECTRON_VERSION = versions.electron;
}

const mainPath = resolve(__dirname, '..', 'backend', 'main.js');
const indexHtmlPath = resolve(__dirname, '../../lib/index.html');
const applicationName = \`${this.pck.props.frontend.config.applicationName}\`;

const nativeKeymap = require('native-keymap');
const Storage = require('electron-store');
const electronStore = new Storage();

let canPreventStop = true;
const windows = [];

app.on('before-quit', async event => {
if (canPreventStop) {
// Pause the stop.
event.preventDefault();
let preventStop = false;
// Ask all opened windows whether they want to prevent the \`close\` event or not.
for (const window of windows) {
if (!preventStop) {
window.webContents.send('prevent-stop-request');
const preventStopPerWindow = await new Promise((resolve) => {
ipcMain.once('prevent-stop-response', (_, arg) => {
if (!!arg && 'preventStop' in arg && typeof arg.preventStop === 'boolean') {
resolve(arg.preventStop);
}
})
});
if (preventStopPerWindow) {
preventStop = true;
}
}
}
if (!preventStop) {
canPreventStop = false;
app.quit();
}
}
});
app.on('ready', () => {
const { screen } = electron;

// Remove the default electron menus, waiting for the application to set its own.
Menu.setApplicationMenu(Menu.buildFromTemplate([{
role: 'help', submenu: [{ role: 'toggledevtools'}]
}]));

function createNewWindow(theUrl) {

// We must center by hand because \`browserWindow.center()\` fails on multi-screen setups
// See: https://github.com/electron/electron/issues/3490
const { bounds } = screen.getDisplayNearestPoint(screen.getCursorScreenPoint());
const height = Math.floor(bounds.height * (2/3));
const width = Math.floor(bounds.width * (2/3));

const y = Math.floor(bounds.y + (bounds.height - height) / 2);
const x = Math.floor(bounds.x + (bounds.width - width) / 2);

const WINDOW_STATE = 'windowstate';
const windowState = electronStore.get(WINDOW_STATE, {
width, height, x, y
});

let windowOptions = {
show: false,
title: applicationName,
width: windowState.width,
height: windowState.height,
minWidth: 200,
minHeight: 120,
x: windowState.x,
y: windowState.y,
isMaximized: windowState.isMaximized
};

// Always hide the window, we will show the window when it is ready to be shown in any case.
const newWindow = new BrowserWindow(windowOptions);
if (windowOptions.isMaximized) {
newWindow.maximize();
}
newWindow.on('ready-to-show', () => newWindow.show());

// Prevent calls to "window.open" from opening an ElectronBrowser window,
// and rather open in the OS default web browser.
newWindow.webContents.on('new-window', (event, url) => {
event.preventDefault();
shell.openExternal(url);
});
const container = new Container();
container.bind(TheiaApplicationName).toConstantValue(applicationName);
container.bind(TheiaBackendMainPath).toConstantValue(mainPath);
container.bind(TheiaIndexHtmlPath).toConstantValue(indexHtmlPath);
container.load(electronMainApplicationModule);

// Save the window geometry state on every change
const saveWindowState = () => {
try {
let bounds;
if (newWindow.isMaximized()) {
bounds = electronStore.get(WINDOW_STATE, {});
} else {
bounds = newWindow.getBounds();
}
electronStore.set(WINDOW_STATE, {
isMaximized: newWindow.isMaximized(),
width: bounds.width,
height: bounds.height,
x: bounds.x,
y: bounds.y
});
} catch (e) {
console.error("Error while saving window state.", e);
}
};
let delayedSaveTimeout;
const saveWindowStateDelayed = () => {
if (delayedSaveTimeout) {
clearTimeout(delayedSaveTimeout);
}
delayedSaveTimeout = setTimeout(saveWindowState, 1000);
};
newWindow.on('close', saveWindowState);
newWindow.on('resize', saveWindowStateDelayed);
newWindow.on('move', saveWindowStateDelayed);
newWindow.on('closed', () => {
const index = windows.indexOf(newWindow);
if (index !== -1) {
windows.splice(index, 1);
}
if (windows.length === 0) {
app.quit();
}
});
function load(raw) {
paul-marechal marked this conversation as resolved.
Show resolved Hide resolved
return Promise.resolve(raw.default).then(module =>
container.load(module)
)
}

// Notify the renderer process on keyboard layout change
nativeKeymap.onDidChangeKeyboardLayout(() => {
if (!newWindow.isDestroyed()) {
const newLayout = {
info: nativeKeymap.getCurrentKeyboardLayout(),
mapping: nativeKeymap.getKeyMap()
};
newWindow.webContents.send('keyboardLayoutChanged', newLayout);
}
});
function start() {
const electronMainApplication = container.get(ElectronMainApplication);
return electronMainApplication.start(electron.app);
}

if (!!theUrl) {
newWindow.loadURL(theUrl);
Promise.resolve()${this.compileElectronMainModuleImports()}
.then(start).catch(reason => {
console.error('Failed to start the electron application.');
if (reason) {
console.error(reason);
}
windows.push(newWindow);
return newWindow;
}

app.on('window-all-closed', () => {
app.quit();
});
ipcMain.on('create-new-window', (event, url) => {
createNewWindow(url);
});
ipcMain.on('open-external', (event, url) => {
shell.openExternal(url);
});

// Check whether we are in bundled application or development mode.
// @ts-ignore
const devMode = process.defaultApp || /node_modules[\/]electron[\/]/.test(process.execPath);
const mainWindow = createNewWindow();
const loadMainWindow = (port) => {
if (!mainWindow.isDestroyed()) {
mainWindow.loadURL('file://' + join(__dirname, '../../lib/index.html') + '?port=' + port);
}
};

// We cannot use the \`process.cwd()\` as the application project path (the location of the \`package.json\` in other words)
// in a bundled electron application because it depends on the way we start it. For instance, on OS X, these are a differences:
// https://github.com/theia-ide/theia/issues/3297#issuecomment-439172274
process.env.THEIA_APP_PROJECT_PATH = resolve(__dirname, '..', '..');

// Set the electron version for both the dev and the production mode. (https://github.com/theia-ide/theia/issues/3254)
// Otherwise, the forked backend processes will not know that they're serving the electron frontend.
const { versions } = process;
// @ts-ignore
if (versions && typeof versions.electron !== 'undefined') {
// @ts-ignore
process.env.THEIA_ELECTRON_VERSION = versions.electron;
}

const mainPath = join(__dirname, '..', 'backend', 'main');
// We need to distinguish between bundled application and development mode when starting the clusters.
// See: https://github.com/electron/electron/issues/6337#issuecomment-230183287
if (devMode) {
require(mainPath).then(address => {
loadMainWindow(address.port);
}).catch((error) => {
console.error(error);
app.exit(1);
});
} else {
const cp = fork(mainPath, [], { env: Object.assign({}, process.env) });
cp.on('message', (message) => {
loadMainWindow(message);
});
cp.on('error', (error) => {
console.error(error);
app.exit(1);
});
app.on('quit', () => {
// If we forked the process for the clusters, we need to manually terminate it.
// See: https://github.com/theia-ide/theia/issues/835
process.kill(cp.pid);
});
}
});
`;
}

Expand Down
8 changes: 8 additions & 0 deletions dev-packages/application-package/src/application-package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ export class ApplicationPackage {
protected _frontendElectronModules: Map<string, string> | undefined;
protected _backendModules: Map<string, string> | undefined;
protected _backendElectronModules: Map<string, string> | undefined;
protected _electronMainModules: Map<string, string> | undefined;
protected _extensionPackages: ReadonlyArray<ExtensionPackage> | undefined;

/**
Expand Down Expand Up @@ -157,6 +158,13 @@ export class ApplicationPackage {
return this._backendElectronModules;
}

get electronMainModules(): Map<string, string> {
if (!this._electronMainModules) {
this._electronMainModules = this.computeModules('electronMain');
}
return this._electronMainModules;
}

protected computeModules<P extends keyof Extension, S extends keyof Extension = P>(primary: P, secondary?: S): Map<string, string> {
const result = new Map<string, string>();
let moduleIndex = 1;
Expand Down
1 change: 1 addition & 0 deletions dev-packages/application-package/src/extension-package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export interface Extension {
frontendElectron?: string;
backend?: string;
backendElectron?: string;
electronMain?: string;
}

export class ExtensionPackage {
Expand Down
1 change: 1 addition & 0 deletions dev-packages/electron/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"electron-replace-ffmpeg": "electron-replace-ffmpeg.js"
},
"dependencies": {
"@types/electron-store": "^1.3.1",
"electron": "^3.1.7",
"electron-download": "^4.1.1",
"electron-store": "^2.0.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/********************************************************************************
* Copyright (C) 2019 Ericsson and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the Eclipse
* Public License v. 2.0 are satisfied: GNU General Public License, version 2
* with the GNU Classpath Exception which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/

import { ContainerModule } from 'inversify';
import { bindContributionProvider } from '../common';
import { ElectronMainApplication, ElectronMainApplicationContribution } from './electron-main-application';

export const electronMainApplicationModule = new ContainerModule(bind => {
bind(ElectronMainApplication).toSelf().inSingletonScope();
bindContributionProvider(bind, ElectronMainApplicationContribution);
});
Loading