UNPKG

@theia/core

Version:

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

286 lines • 12.4 kB
// ***************************************************************************** // Copyright (C) 2017 TypeFox 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"; /* eslint-disable @typescript-eslint/no-explicit-any */ import { inject, injectable, postConstruct } from 'inversify'; import { isOSX, MAIN_MENU_BAR, CompoundMenuNode, Group, RenderedMenuNode, CommandMenu, AcceleratorSource, PreferenceService } from '../../common/index.js'; import { CommonCommands } from '../../browser/index.js'; import debounce from '../../../shared/lodash.debounce/index.cjs'; import { BrowserMainMenuFactory } from '../../browser/menu/browser-menu-plugin.js'; import { electronMenuAcceleratorMetadata } from '../../electron-common/electron-api.js'; function traverseMenuDto(items, callback) { for (const item of items) { callback(item); if (item.submenu) { traverseMenuDto(item.submenu, callback); } } } function traverseMenuModel(effectivePath, item, callback) { callback(item, effectivePath); if (CompoundMenuNode.is(item)) { for (const child of item.children) { traverseMenuModel([...effectivePath, child.id], child, callback); } } } let ElectronMainMenuFactory = class ElectronMainMenuFactory extends BrowserMainMenuFactory { constructor() { super(...arguments); this.setMenuBar = debounce(() => this.doSetMenuBar(), 100); } postConstruct() { this.keybindingRegistry.onKeybindingsChanged(() => { this.setMenuBar(); }); this.menuProvider.onDidChange(() => { this.setMenuBar(); }); this.preferencesService.ready.then(() => { this.preferencesService.onPreferenceChanged(debounce(e => { if (e.preferenceName === 'window.menuBarVisibility') { this.setMenuBar(); } if (this.menu) { const menuModel = this.menuProvider.getMenu(MAIN_MENU_BAR); const toggledMap = new Map(); traverseMenuDto(this.menu, item => { if (item.id) { toggledMap.set(item.id, item); } }); let anyChanged = false; traverseMenuModel(MAIN_MENU_BAR, menuModel, ((item, path) => { if (CommandMenu.is(item)) { const isToggled = item.isToggled(path); const menuItem = toggledMap.get(item.id); if (menuItem && isToggled !== menuItem.checked) { anyChanged = true; menuItem.type = isToggled ? 'checkbox' : 'normal'; menuItem.checked = isToggled; } } })); if (anyChanged) { window.electronTheiaCore.setMenu(this.menu); } } }, 10)); }); } doSetMenuBar() { const preference = this.preferencesService.get('window.menuBarVisibility') || 'classic'; const isFullScreen = window.electronTheiaCore.isFullScreen(); const isCustomTitleBar = this.titleBarStyle === 'custom'; switch (preference) { case 'visible': this.menu = this.createElectronMenuBar(); window.electronTheiaCore.setMenu(this.menu); window.electronTheiaCore.setMenuBarVisible(true); window.electronTheiaCore.setAutoHideMenuBar(false); break; case 'toggle': if (isCustomTitleBar) { // With custom title bar, the Lumino menu bar handles toggle via Alt-key listener. // Do not set autoHideMenuBar on the native window to prevent it from appearing. window.electronTheiaCore.setMenuBarVisible(false); window.electronTheiaCore.setAutoHideMenuBar(false); } else { // With native title bar, Electron's autoHideMenuBar handles Alt toggling. this.menu = this.createElectronMenuBar(); window.electronTheiaCore.setMenu(this.menu); window.electronTheiaCore.setMenuBarVisible(false); window.electronTheiaCore.setAutoHideMenuBar(true); } break; case 'hidden': window.electronTheiaCore.setMenuBarVisible(false); window.electronTheiaCore.setAutoHideMenuBar(false); break; case 'compact': window.electronTheiaCore.setMenuBarVisible(false); window.electronTheiaCore.setAutoHideMenuBar(false); break; case 'classic': default: this.menu = this.createElectronMenuBar(); window.electronTheiaCore.setMenu(this.menu); window.electronTheiaCore.setMenuBarVisible(!isFullScreen); window.electronTheiaCore.setAutoHideMenuBar(isFullScreen); break; } } createElectronMenuBar() { const menuModel = this.menuProvider.getMenu(MAIN_MENU_BAR); const menu = this.fillMenuTemplate([], MAIN_MENU_BAR, menuModel, [], this.contextKeyService, { honorDisabled: false }, false); if (isOSX) { menu.unshift(this.createOSXMenu()); } return menu; } createElectronContextMenu(menuPath, menu, contextMatcher, args, context, skipSingleRootNode) { return this.fillMenuTemplate([], menuPath, menu, args, contextMatcher, { showDisabled: true, context }, true); } fillMenuTemplate(parentItems, menuPath, menu, args = [], contextMatcher, options, skipRoot) { const showDisabled = options?.showDisabled !== false; const honorDisabled = options?.honorDisabled !== false; const effectivePath = menu.effectiveMenuPath || menuPath; if (CompoundMenuNode.is(menu) && menu.children.length && menu.isVisible(effectivePath, contextMatcher, options.context, ...args)) { if (Group.is(menu) && menu.id === 'inline') { return parentItems; } if (menu.contextKeyOverlays) { const overlays = menu.contextKeyOverlays; contextMatcher = this.services.contextKeyService.createOverlay(Object.keys(overlays).map(key => [key, overlays[key]])); } const children = menu.children; const myItems = []; children.forEach(child => this.fillMenuTemplate(myItems, [...effectivePath, child.id], child, args, contextMatcher, options, false)); if (myItems.length === 0) { return parentItems; } if (!skipRoot && RenderedMenuNode.is(menu)) { parentItems.push({ label: menu.label, submenu: myItems }); } else { if (parentItems.length && parentItems[parentItems.length - 1].type !== 'separator') { parentItems.push({ type: 'separator' }); } parentItems.push(...myItems); parentItems.push({ type: 'separator' }); } } else if (CommandMenu.is(menu)) { if (!menu.isVisible(effectivePath, contextMatcher, options.context, ...args)) { return parentItems; } // We should omit rendering context-menu items which are disabled. if (!showDisabled && !menu.isEnabled(effectivePath, ...args)) { return parentItems; } const acceleratorSequence = AcceleratorSource.is(menu) ? menu.getAccelerator(options.context, 'physical') : []; const menuItem = { id: menu.id, label: menu.label, type: menu.isToggled(effectivePath, ...args) ? 'checkbox' : 'normal', checked: menu.isToggled(effectivePath, ...args), enabled: !honorDisabled || menu.isEnabled(effectivePath, ...args), // see https://github.com/eclipse-theia/theia/issues/446 visible: true, ...electronMenuAcceleratorMetadata(acceleratorSequence), execute: async () => { const wasToggled = menuItem.checked; await menu.run(effectivePath, ...args); const isToggled = menu.isToggled(effectivePath, ...args); if (isToggled !== wasToggled) { menuItem.type = isToggled ? 'checkbox' : 'normal'; menuItem.checked = isToggled; window.electronTheiaCore.setMenu(this.menu); } } }; // Only assign Electron roles when no custom args are present. // Custom args indicate that command handlers have context-dependent // behavior (e.g. chat view copying the whole message when there is // no DOM selection) that would be bypassed by the native role. const role = args.length === 0 ? this.roleFor(menu.id) : undefined; if (role) { menuItem.role = role; delete menuItem.execute; } parentItems.push(menuItem); } return parentItems; } undefinedOrMatch(contextKeyService, expression, context) { if (expression) { return contextKeyService.match(expression, context); } return true; } roleFor(id) { let role; switch (id) { case CommonCommands.UNDO.id: role = 'undo'; break; case CommonCommands.REDO.id: role = 'redo'; break; case CommonCommands.CUT.id: role = 'cut'; break; case CommonCommands.COPY.id: role = 'copy'; break; case CommonCommands.PASTE.id: role = 'paste'; break; case CommonCommands.SELECT_ALL.id: role = 'selectAll'; break; default: break; } return role; } createOSXMenu() { return { label: 'Theia', submenu: [ { role: 'about' }, { type: 'separator' }, { role: 'services', submenu: [] }, { type: 'separator' }, { role: 'hide' }, { role: 'hideOthers' }, { role: 'unhide' }, { type: 'separator' }, { role: 'quit' } ] }; } }; __decorate([ inject(PreferenceService) ], ElectronMainMenuFactory.prototype, "preferencesService", void 0); __decorate([ postConstruct() ], ElectronMainMenuFactory.prototype, "postConstruct", null); ElectronMainMenuFactory = __decorate([ injectable() ], ElectronMainMenuFactory); export { ElectronMainMenuFactory }; //# sourceMappingURL=electron-main-menu-factory.js.map