UNPKG

12.6 kBJavaScriptView Raw
1"use strict";
2// *****************************************************************************
3// Copyright (C) 2017 TypeFox and others.
4//
5// This program and the accompanying materials are made available under the
6// terms of the Eclipse Public License v. 2.0 which is available at
7// http://www.eclipse.org/legal/epl-2.0.
8//
9// This Source Code may also be made available under the following Secondary
10// Licenses when the conditions for such availability set forth in the Eclipse
11// Public License v. 2.0 are satisfied: GNU General Public License, version 2
12// with the GNU Classpath Exception which is available at
13// https://www.gnu.org/software/classpath/license.html.
14//
15// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
16// *****************************************************************************
17var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
18 var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
19 if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
20 else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
21 return c > 3 && r && Object.defineProperty(target, key, r), r;
22};
23var __metadata = (this && this.__metadata) || function (k, v) {
24 if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
25};
26Object.defineProperty(exports, "__esModule", { value: true });
27exports.ElectronMainMenuFactory = void 0;
28/* eslint-disable @typescript-eslint/no-explicit-any */
29const inversify_1 = require("inversify");
30const common_1 = require("../../common");
31const browser_1 = require("../../browser");
32const debounce = require("lodash.debounce");
33const theia_dock_panel_1 = require("../../browser/shell/theia-dock-panel");
34const browser_menu_plugin_1 = require("../../browser/menu/browser-menu-plugin");
35let ElectronMainMenuFactory = class ElectronMainMenuFactory extends browser_menu_plugin_1.BrowserMainMenuFactory {
36 constructor() {
37 super(...arguments);
38 this._toggledCommands = new Set();
39 }
40 postConstruct() {
41 this.preferencesService.onPreferenceChanged(debounce(e => {
42 if (e.preferenceName === 'window.menuBarVisibility') {
43 this.setMenuBar();
44 }
45 if (this._menu) {
46 for (const cmd of this._toggledCommands) {
47 const menuItem = this.findMenuById(this._menu, cmd);
48 if (menuItem) {
49 menuItem.checked = this.commandRegistry.isToggled(cmd);
50 }
51 }
52 window.electronTheiaCore.setMenu(this._menu);
53 }
54 }, 10));
55 this.keybindingRegistry.onKeybindingsChanged(() => {
56 this.setMenuBar();
57 });
58 }
59 async setMenuBar() {
60 await this.preferencesService.ready;
61 const createdMenuBar = this.createElectronMenuBar();
62 window.electronTheiaCore.setMenu(createdMenuBar);
63 }
64 createElectronMenuBar() {
65 const preference = this.preferencesService.get('window.menuBarVisibility') || 'classic';
66 const maxWidget = document.getElementsByClassName(theia_dock_panel_1.MAXIMIZED_CLASS);
67 if (preference === 'visible' || (preference === 'classic' && maxWidget.length === 0)) {
68 const menuModel = this.menuProvider.getMenu(common_1.MAIN_MENU_BAR);
69 this._menu = this.fillMenuTemplate([], menuModel, [], { honorDisabled: false, rootMenuPath: common_1.MAIN_MENU_BAR });
70 if (common_1.isOSX) {
71 this._menu.unshift(this.createOSXMenu());
72 }
73 return this._menu;
74 }
75 this._menu = undefined;
76 // eslint-disable-next-line no-null/no-null
77 return undefined;
78 }
79 createElectronContextMenu(menuPath, args, context, contextKeyService, skipSingleRootNode) {
80 const menuModel = skipSingleRootNode ? this.menuProvider.removeSingleRootNode(this.menuProvider.getMenu(menuPath), menuPath) : this.menuProvider.getMenu(menuPath);
81 return this.fillMenuTemplate([], menuModel, args, { showDisabled: true, context, rootMenuPath: menuPath, contextKeyService });
82 }
83 fillMenuTemplate(parentItems, menu, args = [], options) {
84 var _a, _b;
85 const showDisabled = (options === null || options === void 0 ? void 0 : options.showDisabled) !== false;
86 const honorDisabled = (options === null || options === void 0 ? void 0 : options.honorDisabled) !== false;
87 if (common_1.CompoundMenuNode.is(menu) && menu.children.length && this.undefinedOrMatch((_a = options.contextKeyService) !== null && _a !== void 0 ? _a : this.contextKeyService, menu.when, options.context)) {
88 const role = common_1.CompoundMenuNode.getRole(menu);
89 if (role === 1 /* Group */ && menu.id === 'inline') {
90 return parentItems;
91 }
92 const children = common_1.CompoundMenuNode.getFlatChildren(menu.children);
93 const myItems = [];
94 children.forEach(child => this.fillMenuTemplate(myItems, child, args, options));
95 if (myItems.length === 0) {
96 return parentItems;
97 }
98 if (role === 0 /* Submenu */) {
99 parentItems.push({ label: menu.label, submenu: myItems });
100 }
101 else if (role === 1 /* Group */ && menu.id !== 'inline') {
102 if (parentItems.length && parentItems[parentItems.length - 1].type !== 'separator') {
103 parentItems.push({ type: 'separator' });
104 }
105 parentItems.push(...myItems);
106 parentItems.push({ type: 'separator' });
107 }
108 }
109 else if (menu.command) {
110 const node = menu.altNode && this.context.altPressed ? menu.altNode : menu;
111 const commandId = node.command;
112 // That is only a sanity check at application startup.
113 if (!this.commandRegistry.getCommand(commandId)) {
114 console.debug(`Skipping menu item with missing command: "${commandId}".`);
115 return parentItems;
116 }
117 if (!this.menuCommandExecutor.isVisible(options.rootMenuPath, commandId, ...args)
118 || !this.undefinedOrMatch((_b = options.contextKeyService) !== null && _b !== void 0 ? _b : this.contextKeyService, node.when, options.context)) {
119 return parentItems;
120 }
121 // We should omit rendering context-menu items which are disabled.
122 if (!showDisabled && !this.menuCommandExecutor.isEnabled(options.rootMenuPath, commandId, ...args)) {
123 return parentItems;
124 }
125 const bindings = this.keybindingRegistry.getKeybindingsForCommand(commandId);
126 const accelerator = bindings[0] && this.acceleratorFor(bindings[0]);
127 const menuItem = {
128 id: node.id,
129 label: node.label,
130 type: this.commandRegistry.getToggledHandler(commandId, ...args) ? 'checkbox' : 'normal',
131 checked: this.commandRegistry.isToggled(commandId, ...args),
132 enabled: !honorDisabled || this.commandRegistry.isEnabled(commandId, ...args),
133 visible: true,
134 accelerator,
135 execute: () => this.execute(commandId, args, options.rootMenuPath)
136 };
137 if (common_1.isOSX) {
138 const role = this.roleFor(node.id);
139 if (role) {
140 menuItem.role = role;
141 delete menuItem.execute;
142 }
143 }
144 parentItems.push(menuItem);
145 if (this.commandRegistry.getToggledHandler(commandId, ...args)) {
146 this._toggledCommands.add(commandId);
147 }
148 }
149 return parentItems;
150 }
151 undefinedOrMatch(contextKeyService, expression, context) {
152 if (expression) {
153 return contextKeyService.match(expression, context);
154 }
155 return true;
156 }
157 /**
158 * Return a user visible representation of a keybinding.
159 */
160 acceleratorFor(keybinding) {
161 const bindingKeySequence = this.keybindingRegistry.resolveKeybinding(keybinding);
162 // FIXME see https://github.com/electron/electron/issues/11740
163 // Key Sequences can't be represented properly in the electron menu.
164 //
165 // We can do what VS Code does, and append the chords as a suffix to the menu label.
166 // https://github.com/eclipse-theia/theia/issues/1199#issuecomment-430909480
167 if (bindingKeySequence.length > 1) {
168 return '';
169 }
170 const keyCode = bindingKeySequence[0];
171 return this.keybindingRegistry.acceleratorForKeyCode(keyCode, '+', true);
172 }
173 roleFor(id) {
174 let role;
175 switch (id) {
176 case browser_1.CommonCommands.UNDO.id:
177 role = 'undo';
178 break;
179 case browser_1.CommonCommands.REDO.id:
180 role = 'redo';
181 break;
182 case browser_1.CommonCommands.CUT.id:
183 role = 'cut';
184 break;
185 case browser_1.CommonCommands.COPY.id:
186 role = 'copy';
187 break;
188 case browser_1.CommonCommands.PASTE.id:
189 role = 'paste';
190 break;
191 case browser_1.CommonCommands.SELECT_ALL.id:
192 role = 'selectAll';
193 break;
194 default:
195 break;
196 }
197 return role;
198 }
199 async execute(cmd, args, menuPath) {
200 try {
201 // This is workaround for https://github.com/eclipse-theia/theia/issues/446.
202 // Electron menus do not update based on the `isEnabled`, `isVisible` property of the command.
203 // We need to check if we can execute it.
204 if (this.menuCommandExecutor.isEnabled(menuPath, cmd, ...args)) {
205 await this.menuCommandExecutor.executeCommand(menuPath, cmd, ...args);
206 if (this._menu && this.menuCommandExecutor.isVisible(menuPath, cmd, ...args)) {
207 const item = this.findMenuById(this._menu, cmd);
208 if (item) {
209 item.checked = this.menuCommandExecutor.isToggled(menuPath, cmd, ...args);
210 window.electronTheiaCore.setMenu(this._menu);
211 }
212 }
213 }
214 }
215 catch {
216 // no-op
217 }
218 }
219 findMenuById(items, id) {
220 for (const item of items) {
221 if (item.id === id) {
222 return item;
223 }
224 if (item.submenu) {
225 const found = this.findMenuById(item.submenu, id);
226 if (found) {
227 return found;
228 }
229 }
230 }
231 return undefined;
232 }
233 createOSXMenu() {
234 return {
235 label: 'Theia',
236 submenu: [
237 {
238 role: 'about'
239 },
240 {
241 type: 'separator'
242 },
243 {
244 role: 'services',
245 submenu: []
246 },
247 {
248 type: 'separator'
249 },
250 {
251 role: 'hide'
252 },
253 {
254 role: 'hideOthers'
255 },
256 {
257 role: 'unhide'
258 },
259 {
260 type: 'separator'
261 },
262 {
263 role: 'quit'
264 }
265 ]
266 };
267 }
268};
269__decorate([
270 (0, inversify_1.inject)(browser_1.PreferenceService),
271 __metadata("design:type", Object)
272], ElectronMainMenuFactory.prototype, "preferencesService", void 0);
273__decorate([
274 (0, inversify_1.postConstruct)(),
275 __metadata("design:type", Function),
276 __metadata("design:paramtypes", []),
277 __metadata("design:returntype", void 0)
278], ElectronMainMenuFactory.prototype, "postConstruct", null);
279ElectronMainMenuFactory = __decorate([
280 (0, inversify_1.injectable)()
281], ElectronMainMenuFactory);
282exports.ElectronMainMenuFactory = ElectronMainMenuFactory;
283//# sourceMappingURL=electron-main-menu-factory.js.map
\No newline at end of file