UNPKG

@theia/core

Version:

Theia is a cloud & desktop IDE framework implemented in TypeScript.

254 lines • 11.4 kB
// ***************************************************************************** // 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 { webUtils } from '@theia/electron/shared/electron'; import { Disposable } from '../common/disposable.js'; import { CHANNEL_ATTACH_SECURITY_TOKEN, CHANNEL_FOCUS_WINDOW, CHANNEL_GET_SECURITY_TOKEN, CHANNEL_INVOKE_MENU, CHANNEL_SET_MENU, CHANNEL_OPEN_POPUP, CHANNEL_CLOSE_POPUP, CHANNEL_ON_CLOSE_POPUP, CHANNEL_GET_TITLE_STYLE_AT_STARTUP, CHANNEL_MINIMIZE, CHANNEL_IS_MAXIMIZED, CHANNEL_MAXIMIZE, CHANNEL_UNMAXIMIZE, CHANNEL_CLOSE, CHANNEL_TOGGLE_DEVTOOLS, CHANNEL_ON_WINDOW_EVENT, CHANNEL_GET_ZOOM_LEVEL, CHANNEL_SET_ZOOM_LEVEL, CHANNEL_IS_FULL_SCREENABLE, CHANNEL_TOGGLE_FULL_SCREEN, CHANNEL_IS_FULL_SCREEN, CHANNEL_SET_MENU_BAR_VISIBLE, CHANNEL_REQUEST_CLOSE, CHANNEL_SET_TITLE_STYLE, CHANNEL_RESTART, CHANNEL_REQUEST_RELOAD, CHANNEL_APP_STATE_CHANGED, CHANNEL_SHOW_ITEM_IN_FOLDER, CHANNEL_READ_CLIPBOARD, CHANNEL_WRITE_CLIPBOARD, CHANNEL_KEYBOARD_LAYOUT_CHANGED, CHANNEL_IPC_CONNECTION, CHANNEL_REQUEST_SECONDARY_CLOSE, CHANNEL_SET_BACKGROUND_COLOR, CHANNEL_WC_METADATA, CHANNEL_ABOUT_TO_CLOSE, CHANNEL_OPEN_WITH_SYSTEM_APP, CHANNEL_OPEN_URL, 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'; // eslint-disable-next-line import/no-extraneous-dependencies import { ipcRenderer, contextBridge } from 'electron'; // a map of menuId => map<handler id => handler> const commandHandlers = new Map(); let nextHandlerId = 1; const mainMenuId = 1; let nextMenuId = mainMenuId + 1; let openUrlHandler; const subscribedSecondaryWindows = new Set(); ipcRenderer.on(CHANNEL_OPEN_URL, async (event, url, replyChannel) => { if (openUrlHandler) { event.sender.send(replyChannel, await openUrlHandler(url)); } else { event.sender.send(replyChannel, false); } }); function convertMenu(menu, handlerMap) { if (!menu) { return undefined; } return menu.map(item => { let handlerId = undefined; if (item.execute) { handlerId = nextHandlerId++; handlerMap.set(handlerId, item.execute); } return { id: item.id, submenu: convertMenu(item.submenu, handlerMap), accelerator: item.accelerator, label: item.label, handlerId: handlerId, checked: item.checked, enabled: item.enabled, role: item.role, type: item.type, visible: item.visible }; }); } const api = { WindowMetadata: { webcontentId: 'none' }, setMenuBarVisible: (visible, windowName) => ipcRenderer.send(CHANNEL_SET_MENU_BAR_VISIBLE, visible, windowName), setAutoHideMenuBar: (enabled, windowName) => ipcRenderer.send(CHANNEL_SET_AUTO_HIDE_MENU_BAR, enabled, windowName), setMenu: (menu) => { commandHandlers.delete(mainMenuId); const handlers = new Map(); commandHandlers.set(mainMenuId, handlers); ipcRenderer.send(CHANNEL_SET_MENU, mainMenuId, convertMenu(menu, handlers)); }, getSecurityToken: () => ipcRenderer.sendSync(CHANNEL_GET_SECURITY_TOKEN), focusWindow: (name) => ipcRenderer.send(CHANNEL_FOCUS_WINDOW, name), showItemInFolder: fsPath => { ipcRenderer.send(CHANNEL_SHOW_ITEM_IN_FOLDER, fsPath); }, getPathForFile: (file) => webUtils.getPathForFile(file), openWithSystemApp: location => { ipcRenderer.send(CHANNEL_OPEN_WITH_SYSTEM_APP, location); }, attachSecurityToken: (endpoint) => ipcRenderer.invoke(CHANNEL_ATTACH_SECURITY_TOKEN, endpoint), popup: async function (menu, x, y, onClosed, windowName) { const menuId = nextMenuId++; const handlers = new Map(); commandHandlers.set(menuId, handlers); const handle = await ipcRenderer.invoke(CHANNEL_OPEN_POPUP, menuId, convertMenu(menu, handlers), x, y, windowName); const closeListener = () => { ipcRenderer.removeListener(CHANNEL_ON_CLOSE_POPUP, closeListener); commandHandlers.delete(menuId); onClosed(); }; ipcRenderer.on(CHANNEL_ON_CLOSE_POPUP, closeListener); return handle; }, closePopup: function (handle) { ipcRenderer.send(CHANNEL_CLOSE_POPUP, handle); }, getTitleBarStyleAtStartup: function () { return ipcRenderer.invoke(CHANNEL_GET_TITLE_STYLE_AT_STARTUP); }, setTitleBarStyle: function (style) { ipcRenderer.send(CHANNEL_SET_TITLE_STYLE, style); }, setBackgroundColor: function (backgroundColor) { ipcRenderer.send(CHANNEL_SET_BACKGROUND_COLOR, backgroundColor); }, setTheme: function (theme) { ipcRenderer.send(CHANNEL_SET_THEME, theme); }, minimize: function (windowName) { ipcRenderer.send(CHANNEL_MINIMIZE, windowName); }, isMaximized: function (windowName) { return ipcRenderer.sendSync(CHANNEL_IS_MAXIMIZED, windowName); }, maximize: function (windowName) { ipcRenderer.send(CHANNEL_MAXIMIZE, windowName); }, unMaximize: function (windowName) { ipcRenderer.send(CHANNEL_UNMAXIMIZE, windowName); }, close: function (windowName) { ipcRenderer.send(CHANNEL_CLOSE, windowName); }, onAboutToClose(handler) { const h = (event, replyChannel) => { handler(); event.sender.send(replyChannel); }; ipcRenderer.on(CHANNEL_ABOUT_TO_CLOSE, h); return Disposable.create(() => ipcRenderer.off(CHANNEL_ABOUT_TO_CLOSE, h)); }, setOpenUrlHandler(handler) { openUrlHandler = handler; }, onWindowEvent: function (event, handler) { const h = (_event, evt) => { if (event === evt) { handler(); } }; ipcRenderer.on(CHANNEL_ON_WINDOW_EVENT, h); return Disposable.create(() => ipcRenderer.off(CHANNEL_ON_WINDOW_EVENT, h)); }, onSecondaryWindowEvent: function (windowName, event, handler) { if (!subscribedSecondaryWindows.has(windowName)) { subscribedSecondaryWindows.add(windowName); ipcRenderer.send(CHANNEL_SUBSCRIBE_SECONDARY_WINDOW_EVENT, windowName); } const h = (_event, name, evt) => { if (name === windowName && event === evt) { handler(); } }; ipcRenderer.on(CHANNEL_ON_SECONDARY_WINDOW_EVENT, h); return Disposable.create(() => ipcRenderer.off(CHANNEL_ON_SECONDARY_WINDOW_EVENT, h)); }, setCloseRequestHandler: function (handler) { ipcRenderer.on(CHANNEL_REQUEST_CLOSE, async (event, stopReason, confirmChannel, cancelChannel) => { try { if (await handler(stopReason)) { event.sender.send(confirmChannel); return; } ; } catch (e) { console.warn('exception in close handler ', e); } event.sender.send(cancelChannel); }); }, setSecondaryWindowCloseRequestHandler(windowName, handler) { // eslint-disable-next-line @typescript-eslint/no-explicit-any const listener = async (event, name, confirmChannel, cancelChannel) => { if (name === windowName) { try { if (await handler()) { event.sender.send(confirmChannel); ipcRenderer.removeListener(CHANNEL_REQUEST_SECONDARY_CLOSE, listener); return; } ; } catch (e) { console.warn('exception in close handler ', e); } event.sender.send(cancelChannel); } }; ipcRenderer.on(CHANNEL_REQUEST_SECONDARY_CLOSE, listener); }, toggleDevTools: function () { ipcRenderer.send(CHANNEL_TOGGLE_DEVTOOLS); }, openDevToolsForWindow: function (windowName) { ipcRenderer.send(CHANNEL_OPEN_DEVTOOLS_FOR_WINDOW, windowName); }, getZoomLevel: function () { return ipcRenderer.invoke(CHANNEL_GET_ZOOM_LEVEL); }, setZoomLevel: function (desired, windowName) { ipcRenderer.send(CHANNEL_SET_ZOOM_LEVEL, desired, windowName); }, isFullScreenable: function () { return ipcRenderer.sendSync(CHANNEL_IS_FULL_SCREENABLE); }, isFullScreen: function () { return ipcRenderer.sendSync(CHANNEL_IS_FULL_SCREEN); }, toggleFullScreen: function () { ipcRenderer.send(CHANNEL_TOGGLE_FULL_SCREEN); }, requestReload: (newUrl) => ipcRenderer.send(CHANNEL_REQUEST_RELOAD, newUrl), restart: () => ipcRenderer.send(CHANNEL_RESTART), applicationStateChanged: state => { ipcRenderer.send(CHANNEL_APP_STATE_CHANGED, state); }, readClipboard() { return ipcRenderer.invoke(CHANNEL_READ_CLIPBOARD); }, writeClipboard(text) { ipcRenderer.send(CHANNEL_WRITE_CLIPBOARD, text); }, onKeyboardLayoutChanged(handler) { return createDisposableListener(CHANNEL_KEYBOARD_LAYOUT_CHANGED, (event, layout) => { handler(layout); }); }, onData: handler => createDisposableListener(CHANNEL_IPC_CONNECTION, (event, data) => { handler(data); }), sendData: data => { ipcRenderer.send(CHANNEL_IPC_CONNECTION, data); }, useNativeElements: !('THEIA_ELECTRON_DISABLE_NATIVE_ELEMENTS' in process.env && process.env.THEIA_ELECTRON_DISABLE_NATIVE_ELEMENTS === '1'), updateRecentWorkspaces: (workspaceUris, categoryName) => { ipcRenderer.send(CHANNEL_UPDATE_RECENT_WORKSPACES, workspaceUris, categoryName); } }; // eslint-disable-next-line @typescript-eslint/no-explicit-any function createDisposableListener(channel, handler) { ipcRenderer.on(channel, handler); return Disposable.create(() => ipcRenderer.off(channel, handler)); } export function preload() { console.log('exposing theia core electron api'); ipcRenderer.on(CHANNEL_INVOKE_MENU, (_, menuId, handlerId) => { const map = commandHandlers.get(menuId); if (map) { const handler = map.get(handlerId); if (handler) { handler(); } } }); // Synchronous, so the metadata (including the parsed options of a forwarded launch) is in place // before any frontend code runs. api.WindowMetadata = ipcRenderer.sendSync(CHANNEL_WC_METADATA); contextBridge.exposeInMainWorld('electronTheiaCore', api); } //# sourceMappingURL=preload.js.map