@theia/core
Version:
Theia is a cloud & desktop IDE framework implemented in TypeScript.
393 lines • 19.4 kB
JavaScript
// *****************************************************************************
// Copyright (C) 2023 STMicroelectronics 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-only WITH Classpath-exception-2.0
// *****************************************************************************
import { __decorate } from "tslib";
import { ipcMain, BrowserWindow, Menu, webContents, session, shell, clipboard, app } from '@theia/electron/shared/electron';
import * as nativeKeymap from '@theia/electron/shared/native-keymap';
import { inject, injectable } from 'inversify';
import { ElectronSecurityToken } from '../electron-common/electron-token.js';
import { CHANNEL_GET_SECURITY_TOKEN, CHANNEL_SET_MENU, CHANNEL_INVOKE_MENU, CHANNEL_FOCUS_WINDOW, CHANNEL_ATTACH_SECURITY_TOKEN, CHANNEL_OPEN_POPUP, CHANNEL_ON_CLOSE_POPUP, CHANNEL_CLOSE_POPUP, CHANNEL_GET_TITLE_STYLE_AT_STARTUP, CHANNEL_MINIMIZE, CHANNEL_MAXIMIZE, CHANNEL_UNMAXIMIZE, CHANNEL_CLOSE, CHANNEL_ON_WINDOW_EVENT, CHANNEL_TOGGLE_DEVTOOLS, CHANNEL_SET_ZOOM_LEVEL, CHANNEL_GET_ZOOM_LEVEL, CHANNEL_IS_FULL_SCREENABLE, CHANNEL_REQUEST_CLOSE, CHANNEL_RESTART, CHANNEL_SET_TITLE_STYLE, CHANNEL_REQUEST_RELOAD, CHANNEL_APP_STATE_CHANGED, CHANNEL_SHOW_ITEM_IN_FOLDER, CHANNEL_READ_CLIPBOARD, CHANNEL_WRITE_CLIPBOARD, CHANNEL_IPC_CONNECTION, CHANNEL_IS_FULL_SCREEN, CHANNEL_SET_MENU_BAR_VISIBLE, CHANNEL_TOGGLE_FULL_SCREEN, CHANNEL_IS_MAXIMIZED, CHANNEL_REQUEST_SECONDARY_CLOSE, CHANNEL_SET_BACKGROUND_COLOR, CHANNEL_WC_METADATA, CHANNEL_ABOUT_TO_CLOSE, CHANNEL_OPEN_WITH_SYSTEM_APP, CHANNEL_OPEN_URL, electronMenuAccelerator, CHANNEL_SET_THEME, CHANNEL_OPEN_DEVTOOLS_FOR_WINDOW, CHANNEL_UPDATE_RECENT_WORKSPACES, CHANNEL_SET_AUTO_HIDE_MENU_BAR, CHANNEL_ON_SECONDARY_WINDOW_EVENT, CHANNEL_SUBSCRIBE_SECONDARY_WINDOW_EVENT } from '../electron-common/electron-api.js';
import { DisposableCollection, isOSX, isWindows, URI } from '../common/index.js';
import { FileUri } from '../node/index.js';
import { createDisposableListener } from './event-utils.js';
import * as path from 'node:path';
let TheiaMainApi = class TheiaMainApi {
constructor() {
this.openPopups = new Map();
this.subscribedSecondaryWindowNames = new Set();
}
onStart(application) {
ipcMain.on(CHANNEL_WC_METADATA, event => {
// Answered synchronously, so the window has its metadata — including the parsed options
// of a forwarded launch — before any frontend code runs. The window is identified by the
// IPC sender, so it can only ever read its own.
event.returnValue = {
webcontentId: event.sender.id.toString(),
launchArgs: application.getLaunchArgs(event.sender.id)
};
});
// electron security token
ipcMain.on(CHANNEL_GET_SECURITY_TOKEN, event => {
event.returnValue = this.electronSecurityToken.value;
});
ipcMain.handle(CHANNEL_ATTACH_SECURITY_TOKEN, (event, endpoint) => session.defaultSession.cookies.set({
url: endpoint,
name: ElectronSecurityToken,
value: JSON.stringify(this.electronSecurityToken),
httpOnly: true,
sameSite: 'no_restriction'
}));
// application menu
ipcMain.on(CHANNEL_SET_MENU, (event, menuId, menu) => {
let electronMenu;
if (menu) {
electronMenu = Menu.buildFromTemplate(this.fromMenuDto(event.sender, menuId, menu));
}
else {
// eslint-disable-next-line no-null/no-null
electronMenu = null;
}
if (isOSX) {
Menu.setApplicationMenu(electronMenu);
}
else {
BrowserWindow.fromWebContents(event.sender)?.setMenu(electronMenu);
}
});
ipcMain.on(CHANNEL_SET_MENU_BAR_VISIBLE, (event, visible, windowName) => {
const electronWindow = this.resolveWindow(event, windowName);
if (electronWindow) {
electronWindow.setMenuBarVisibility(visible);
}
else {
console.warn(`There is no known secondary window '${windowName}'. Thus, the menu bar could not be made visible.`);
}
});
ipcMain.on(CHANNEL_SET_AUTO_HIDE_MENU_BAR, (event, enabled, windowName) => {
const electronWindow = this.resolveWindow(event, windowName);
if (electronWindow) {
electronWindow.autoHideMenuBar = enabled;
}
else {
console.warn(`There is no known secondary window '${windowName}'. Thus, autoHideMenuBar could not be set.`);
}
});
// popup menu
ipcMain.handle(CHANNEL_OPEN_POPUP, (event, menuId, menu, x, y, windowName) => {
const zoom = event.sender.getZoomFactor();
// TODO: Remove the offset once Electron fixes https://github.com/electron/electron/issues/31641
const offset = process.platform === 'win32' ? 0 : 2;
// x and y values must be Ints or else there is a conversion error
x = Math.round(x * zoom) + offset;
y = Math.round(y * zoom) + offset;
const popup = Menu.buildFromTemplate(this.fromMenuDto(event.sender, menuId, menu));
this.openPopups.set(menuId, popup);
const electronWindow = this.resolveWindow(event, windowName);
popup.popup({
window: electronWindow,
x,
y,
callback: () => {
this.openPopups.delete(menuId);
event.sender.send(CHANNEL_ON_CLOSE_POPUP, menuId);
}
});
});
ipcMain.handle(CHANNEL_CLOSE_POPUP, (event, handle) => {
if (this.openPopups.has(handle)) {
this.openPopups.get(handle).closePopup();
}
});
// focus windows for secondary window support
ipcMain.on(CHANNEL_FOCUS_WINDOW, (event, windowName) => {
const electronWindow = this.resolveWindow(event, windowName);
if (electronWindow) {
if (electronWindow.isMinimized()) {
electronWindow.restore();
}
electronWindow.focus();
}
else {
console.warn(`There is no known secondary window '${windowName}'. Thus, the window could not be focussed.`);
}
});
ipcMain.on(CHANNEL_SHOW_ITEM_IN_FOLDER, (event, fsPath) => {
shell.showItemInFolder(fsPath);
});
ipcMain.on(CHANNEL_OPEN_WITH_SYSTEM_APP, (event, uri) => {
shell.openExternal(uri);
});
ipcMain.handle(CHANNEL_GET_TITLE_STYLE_AT_STARTUP, event => application.getTitleBarStyleAtStartup(event.sender));
ipcMain.on(CHANNEL_SET_TITLE_STYLE, (event, style) => application.setTitleBarStyle(event.sender, style));
ipcMain.on(CHANNEL_SET_BACKGROUND_COLOR, (event, backgroundColor) => application.setBackgroundColor(event.sender, backgroundColor));
ipcMain.on(CHANNEL_SET_THEME, (event, theme) => application.setTheme(theme));
ipcMain.on(CHANNEL_MINIMIZE, (event, windowName) => {
this.resolveWindow(event, windowName)?.minimize();
});
ipcMain.on(CHANNEL_IS_MAXIMIZED, (event, windowName) => {
event.returnValue = this.resolveWindow(event, windowName)?.isMaximized() ?? false;
});
ipcMain.on(CHANNEL_MAXIMIZE, (event, windowName) => {
this.resolveWindow(event, windowName)?.maximize();
});
ipcMain.on(CHANNEL_UNMAXIMIZE, (event, windowName) => {
this.resolveWindow(event, windowName)?.unmaximize();
});
ipcMain.on(CHANNEL_CLOSE, (event, windowName) => {
this.resolveWindow(event, windowName)?.close();
});
ipcMain.on(CHANNEL_SUBSCRIBE_SECONDARY_WINDOW_EVENT, (event, windowName) => {
if (this.subscribedSecondaryWindowNames.has(windowName)) {
return;
}
const targetWindow = this.resolveWindow(event, windowName);
if (targetWindow) {
this.subscribedSecondaryWindowNames.add(windowName);
const sender = event.sender;
const onMaximize = () => sender.send(CHANNEL_ON_SECONDARY_WINDOW_EVENT, windowName, 'maximize');
const onUnmaximize = () => sender.send(CHANNEL_ON_SECONDARY_WINDOW_EVENT, windowName, 'unmaximize');
const onFocus = () => sender.send(CHANNEL_ON_SECONDARY_WINDOW_EVENT, windowName, 'focus');
targetWindow.on('maximize', onMaximize);
targetWindow.on('unmaximize', onUnmaximize);
targetWindow.on('focus', onFocus);
targetWindow.once('closed', () => {
targetWindow.removeListener('maximize', onMaximize);
targetWindow.removeListener('unmaximize', onUnmaximize);
targetWindow.removeListener('focus', onFocus);
this.subscribedSecondaryWindowNames.delete(windowName);
});
}
});
ipcMain.on(CHANNEL_RESTART, event => {
application.restart(event.sender);
});
ipcMain.on(CHANNEL_TOGGLE_DEVTOOLS, event => {
event.sender.toggleDevTools();
});
ipcMain.on(CHANNEL_OPEN_DEVTOOLS_FOR_WINDOW, (event, windowName) => {
const electronWindow = this.resolveWindow(event, windowName);
if (electronWindow) {
electronWindow.webContents.openDevTools();
}
else {
console.warn(`There is no known window '${windowName}'. Thus, the devtools could not be opened.`);
}
});
ipcMain.on(CHANNEL_SET_ZOOM_LEVEL, (event, zoomLevel, windowName) => {
const electronWindow = this.resolveWindow(event, windowName);
if (electronWindow) {
electronWindow.webContents.setZoomLevel(zoomLevel);
}
else {
console.warn(`There is no known window '${windowName}'. Thus, the zoom level could not be set.`);
}
});
ipcMain.handle(CHANNEL_GET_ZOOM_LEVEL, event => event.sender.getZoomLevel());
ipcMain.on(CHANNEL_TOGGLE_FULL_SCREEN, event => {
const win = BrowserWindow.fromWebContents(event.sender);
if (win) {
win.setFullScreen(!win.isFullScreen());
}
});
ipcMain.on(CHANNEL_IS_FULL_SCREENABLE, event => {
event.returnValue = BrowserWindow.fromWebContents(event.sender)?.isFullScreenable();
});
ipcMain.on(CHANNEL_IS_FULL_SCREEN, event => {
event.returnValue = BrowserWindow.fromWebContents(event.sender)?.isFullScreen();
});
ipcMain.handle(CHANNEL_READ_CLIPBOARD, () => clipboard.readText().catch(error => {
console.warn('Could not read from the clipboard', error);
return '';
}));
ipcMain.on(CHANNEL_WRITE_CLIPBOARD, (event, text) => {
clipboard.writeText(text).catch(error => console.warn('Could not write to the clipboard', error));
});
nativeKeymap.onDidChangeKeyboardLayout(() => {
const newLayout = {
info: nativeKeymap.getCurrentKeyboardLayout(),
mapping: nativeKeymap.getKeyMap()
};
for (const webContent of webContents.getAllWebContents()) {
webContent.send('keyboardLayoutChanged', newLayout);
}
});
ipcMain.on(CHANNEL_UPDATE_RECENT_WORKSPACES, (_event, workspaces, categoryName) => {
if (!isWindows) {
return;
}
const jumpListSettings = app.getJumpListSettings();
const isDev = !app.isPackaged;
const appPath = app.getAppPath();
const items = workspaces
.filter(w => new URI(w).scheme === 'file')
.map(workspace => {
const uri = new URI(workspace);
const wspathPretty = uri.path.fsPath();
const wspath = FileUri.fsPath(uri);
const item = {
type: 'task',
// Windows is picky about the length of some attributes. See: https://github.com/microsoft/vscode/issues/111177#issuecomment-739942612
title: uri.path.base.substring(0, 255),
description: wspathPretty.substring(0, 255),
program: process.execPath,
args: isDev ? `"${path.resolve(appPath, 'theia-electron-main.js')}" "${wspath}"` : `"${wspath}"`,
iconPath: process.execPath,
iconIndex: 0
};
// We need to remove items that the user has explicitly removed from the jump list. Otherwise
// Windows will not show our custom category at all.
if (jumpListSettings.removedItems.some(removedItem => removedItem.args === item.args)) {
return undefined;
}
return item;
})
.filter(item => !!item);
const jumpList = [{
type: 'custom',
name: categoryName || 'Recent Workspaces',
items
}];
const result = app.setJumpList(jumpList);
if (result !== 'ok') {
console.warn(`Could not set Jump List with recent workspaces (result = "${result}")`);
}
});
}
resolveWindow(event, windowName) {
if (windowName) {
return BrowserWindow.getAllWindows().find(w => w.webContents.mainFrame.name === windowName);
}
return BrowserWindow.fromWebContents(event.sender) ?? undefined;
}
fromMenuDto(sender, menuId, menuDto) {
return menuDto.map(dto => {
const result = {
id: dto.id,
label: dto.label,
type: dto.type,
checked: dto.checked,
enabled: dto.enabled,
visible: dto.visible,
role: dto.role,
...electronMenuAccelerator(dto)
};
if (dto.submenu) {
result.submenu = this.fromMenuDto(sender, menuId, dto.submenu);
}
if (dto.handlerId) {
result.click = () => {
sender.send(CHANNEL_INVOKE_MENU, menuId, dto.handlerId);
};
}
return result;
});
}
};
__decorate([
inject(ElectronSecurityToken)
], TheiaMainApi.prototype, "electronSecurityToken", void 0);
TheiaMainApi = __decorate([
injectable()
], TheiaMainApi);
export { TheiaMainApi };
let nextReplyChannel = 0;
export var TheiaRendererAPI;
(function (TheiaRendererAPI) {
function sendWindowEvent(wc, event) {
wc.send(CHANNEL_ON_WINDOW_EVENT, event);
}
TheiaRendererAPI.sendWindowEvent = sendWindowEvent;
function openUrl(wc, url) {
return new Promise(resolve => {
const channelNr = nextReplyChannel++;
const replyChannel = `openUrl${channelNr}`;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const l = createDisposableListener(ipcMain, replyChannel, (e, args) => {
l.dispose();
resolve(args[0]);
});
wc.send(CHANNEL_OPEN_URL, url, replyChannel);
});
}
TheiaRendererAPI.openUrl = openUrl;
function sendAboutToClose(wc) {
return new Promise(resolve => {
const channelNr = nextReplyChannel++;
const replyChannel = `aboutToClose${channelNr}`;
const l = createDisposableListener(ipcMain, replyChannel, e => {
l.dispose();
resolve();
});
wc.send(CHANNEL_ABOUT_TO_CLOSE, replyChannel);
});
}
TheiaRendererAPI.sendAboutToClose = sendAboutToClose;
function requestClose(wc, stopReason) {
const channelNr = nextReplyChannel++;
const confirmChannel = `confirm-${channelNr}`;
const cancelChannel = `cancel-${channelNr}`;
const disposables = new DisposableCollection();
return new Promise(resolve => {
createDisposableListener(ipcMain, confirmChannel, e => {
resolve(true);
}, disposables);
createDisposableListener(ipcMain, cancelChannel, e => {
resolve(false);
}, disposables);
wc.send(CHANNEL_REQUEST_CLOSE, stopReason, confirmChannel, cancelChannel);
}).finally(() => disposables.dispose());
}
TheiaRendererAPI.requestClose = requestClose;
function requestSecondaryClose(mainWindow, secondaryWindow) {
const channelNr = nextReplyChannel++;
const confirmChannel = `confirm-${channelNr}`;
const cancelChannel = `cancel-${channelNr}`;
const disposables = new DisposableCollection();
return new Promise(resolve => {
createDisposableListener(ipcMain, confirmChannel, e => {
resolve(true);
}, disposables);
createDisposableListener(ipcMain, cancelChannel, e => {
resolve(false);
}, disposables);
mainWindow.send(CHANNEL_REQUEST_SECONDARY_CLOSE, secondaryWindow.mainFrame.name, confirmChannel, cancelChannel);
}).finally(() => disposables.dispose());
}
TheiaRendererAPI.requestSecondaryClose = requestSecondaryClose;
function onRequestReload(wc, handler) {
return createWindowListener(wc, CHANNEL_REQUEST_RELOAD, handler);
}
TheiaRendererAPI.onRequestReload = onRequestReload;
function onApplicationStateChanged(wc, handler) {
return createWindowListener(wc, CHANNEL_APP_STATE_CHANGED, state => handler(state));
}
TheiaRendererAPI.onApplicationStateChanged = onApplicationStateChanged;
function onIpcData(handler) {
return createDisposableListener(ipcMain, CHANNEL_IPC_CONNECTION, (event, data) => handler(event.sender, data));
}
TheiaRendererAPI.onIpcData = onIpcData;
function sendData(wc, data) {
wc.send(CHANNEL_IPC_CONNECTION, data);
}
TheiaRendererAPI.sendData = sendData;
function createWindowListener(wc, channel, handler) {
return createDisposableListener(ipcMain, channel, (event, ...args) => {
if (wc.id === event.sender.id) {
handler(...args);
}
});
}
})(TheiaRendererAPI || (TheiaRendererAPI = {}));
//# sourceMappingURL=electron-api-main.js.map