Skip to content

Commit

Permalink
[core] Add and use application name
Browse files Browse the repository at this point in the history
Add customizable application name.
Display application name when no workspace is opened, instead of showing
the URL. It was particulary bad on Electron.

Also removes native menus when creating a new Electron BrowserWindow, which
will be re-added by the application when ready.

Signed-off-by: marechal-p <paul.marechal@ericsson.com>
  • Loading branch information
paul-marechal committed Nov 13, 2018
1 parent df3dd2c commit 4a33ffd
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 14 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## v0.3.17
- [plug-in] added `languages.registerCodeLensProvider` Plug-in API
- [core] `ctrl+alt+a` and `ctrl+alt+d` to switch tabs left/right
- [core] added `theia.applicationName` to application `package.json` and improved window title


## v0.3.16
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,19 +104,21 @@ process.env.LC_NUMERIC = 'C';
const { join } = require('path');
const { isMaster } = require('cluster');
const { fork } = require('child_process');
const { app, BrowserWindow, ipcMain } = require('electron');
const { app, BrowserWindow, ipcMain, Menu } = require('electron');
const applicationName = \`${this.pck.props.frontend.config.applicationName}\`;
const windows = [];
function createNewWindow(theUrl) {
const newWindow = new BrowserWindow({ width: 1024, height: 728, show: !!theUrl });
const newWindow = new BrowserWindow({ width: 1024, height: 728, show: !!theUrl, title: applicationName });
if (windows.length === 0) {
newWindow.webContents.on('new-window', (event, url, frameName, disposition, options) => {
// If the first electron window isn't visible, then all other new windows will remain invisible.
// https://github.com/electron/electron/issues/3751
options.show = true;
options.width = 1024;
options.height = 728;
options.title = applicationName;
});
}
windows.push(newWindow);
Expand Down Expand Up @@ -147,6 +149,9 @@ if (isMaster) {
createNewWindow(url);
});
app.on('ready', () => {
// Remove the default electron menus, waiting for the application to set its own.
Menu.setApplicationMenu(Menu.buildFromTemplate([]));
// Check whether we are in bundled application or development mode.
// @ts-ignore
const devMode = process.defaultApp || /node_modules[\/]electron[\/]/.test(process.execPath);
Expand Down
9 changes: 8 additions & 1 deletion dev-packages/application-package/src/application-props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ export namespace ApplicationProps {
config: {}
},
frontend: {
config: {}
config: {
applicationName: 'Theia'
}
}
};

Expand All @@ -93,6 +95,11 @@ export interface FrontendApplicationConfig extends ApplicationConfig {
*/
readonly defaultTheme?: string;

/**
* The name of the application. `Theia` by default.
*/
readonly applicationName: string;

}

/**
Expand Down
7 changes: 7 additions & 0 deletions examples/browser/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@
"private": true,
"name": "@theia/example-browser",
"version": "0.3.16",
"theia": {
"frontend": {
"config": {
"applicationName": "Theia Browser Example"
}
}
},
"dependencies": {
"@theia/callhierarchy": "^0.3.16",
"@theia/console": "^0.3.16",
Expand Down
7 changes: 6 additions & 1 deletion examples/electron/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
"name": "@theia/example-electron",
"version": "0.3.16",
"theia": {
"target": "electron"
"target": "electron",
"frontend": {
"config": {
"applicationName": "Theia Electron Example"
}
}
},
"dependencies": {
"@theia/callhierarchy": "^0.3.16",
Expand Down
8 changes: 4 additions & 4 deletions packages/core/src/common/application-protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ export interface ApplicationServer {
}

export interface ExtensionInfo {
name: string;
version: string;
readonly name: string;
readonly version: string;
}

export interface ApplicationInfo {
name: string;
version: string;
readonly name: string;
readonly version: string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,23 @@ export class ElectronMenuContribution implements FrontendApplicationContribution
}
}

const currentWindow = electron.remote.getCurrentWindow();
const createdMenuBar = this.factory.createMenuBar();

/**
* OSX window environment has its menu bar at the top of the screen,
* windows share the same space, as if the menus were global.
*/
if (isOSX) {
electron.remote.Menu.setApplicationMenu(createdMenuBar);

// Update the global menu bar based on the focused Electron BrowserWindow:
currentWindow.on('focus', () => {
electron.remote.Menu.setApplicationMenu(this.factory.createMenuBar());
});

} else {
electron.remote.getCurrentWindow().setMenu(createdMenuBar);
currentWindow.setMenu(createdMenuBar);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
********************************************************************************/

// tslint:disable:no-unused-expression
// tslint:disable:no-any

import { enableJSDOM } from '@theia/core/lib/browser/test/jsdom';

Expand Down
18 changes: 13 additions & 5 deletions packages/workspace/src/browser/workspace-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { ILogger, Disposable, DisposableCollection, Emitter, Event } from '@thei
import { WorkspacePreferences } from './workspace-preferences';
import * as jsoncparser from 'jsonc-parser';
import * as Ajv from 'ajv';
import { FrontendApplicationConfigProvider } from '@theia/core/lib/browser/frontend-application-config-provider';

export const THEIA_EXT = 'theia-workspace';
export const VSCODE_EXT = 'code-workspace';
Expand Down Expand Up @@ -64,6 +65,8 @@ export class WorkspaceService implements FrontendApplicationContribution {
@inject(WorkspacePreferences)
protected preferences: WorkspacePreferences;

protected applicationName = FrontendApplicationConfigProvider.get().applicationName;

@postConstruct()
protected async init(): Promise<void> {
const workspaceUri = await this.server.getMostRecentlyUsedWorkspace();
Expand Down Expand Up @@ -168,19 +171,24 @@ export class WorkspaceService implements FrontendApplicationContribution {
}
}

protected updateTitle(): void {
protected formatTitle(title?: string): string {
const name = this.applicationName;
return title ? `${title}${name}` : name;
}

protected updateTitle() {
let title: string | undefined;
if (this._workspace) {
const uri = new URI(this._workspace.uri);
const displayName = uri.displayName;
if (!this._workspace.isDirectory &&
(displayName.endsWith(`.${THEIA_EXT}`) || displayName.endsWith(`.${VSCODE_EXT}`))) {
document.title = displayName.slice(0, displayName.lastIndexOf('.'));
title = displayName.slice(0, displayName.lastIndexOf('.'));
} else {
document.title = displayName;
title = displayName;
}
} else {
document.title = window.location.href;
}
document.title = this.formatTitle(title);
}

/**
Expand Down

0 comments on commit 4a33ffd

Please sign in to comment.