menuEntryActionViewItem.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. /*---------------------------------------------------------------------------------------------
  2. * Copyright (c) Microsoft Corporation. All rights reserved.
  3. * Licensed under the MIT License. See License.txt in the project root for license information.
  4. *--------------------------------------------------------------------------------------------*/
  5. var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
  6. var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  7. if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
  8. 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;
  9. return c > 3 && r && Object.defineProperty(target, key, r), r;
  10. };
  11. var __param = (this && this.__param) || function (paramIndex, decorator) {
  12. return function (target, key) { decorator(target, key, paramIndex); }
  13. };
  14. var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
  15. function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
  16. return new (P || (P = Promise))(function (resolve, reject) {
  17. function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
  18. function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
  19. function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
  20. step((generator = generator.apply(thisArg, _arguments || [])).next());
  21. });
  22. };
  23. import { $, addDisposableListener, append, asCSSUrl, EventType, ModifierKeyEmitter, prepend } from '../../../base/browser/dom.js';
  24. import { StandardKeyboardEvent } from '../../../base/browser/keyboardEvent.js';
  25. import { ActionViewItem, BaseActionViewItem } from '../../../base/browser/ui/actionbar/actionViewItems.js';
  26. import { DropdownMenuActionViewItem } from '../../../base/browser/ui/dropdown/dropdownActionViewItem.js';
  27. import { ActionRunner, Separator, SubmenuAction } from '../../../base/common/actions.js';
  28. import { UILabelProvider } from '../../../base/common/keybindingLabels.js';
  29. import { DisposableStore, MutableDisposable, toDisposable } from '../../../base/common/lifecycle.js';
  30. import { isLinux, isWindows, OS } from '../../../base/common/platform.js';
  31. import './menuEntryActionViewItem.css';
  32. import { localize } from '../../../nls.js';
  33. import { IMenuService, MenuItemAction, SubmenuItemAction } from '../common/actions.js';
  34. import { IContextKeyService } from '../../contextkey/common/contextkey.js';
  35. import { IContextMenuService } from '../../contextview/browser/contextView.js';
  36. import { IInstantiationService } from '../../instantiation/common/instantiation.js';
  37. import { IKeybindingService } from '../../keybinding/common/keybinding.js';
  38. import { INotificationService } from '../../notification/common/notification.js';
  39. import { IStorageService } from '../../storage/common/storage.js';
  40. import { ThemeIcon } from '../../theme/common/themeService.js';
  41. export function createAndFillInActionBarActions(menu, options, target, primaryGroup, primaryMaxCount, shouldInlineSubmenu, useSeparatorsInPrimaryActions) {
  42. const groups = menu.getActions(options);
  43. const isPrimaryAction = typeof primaryGroup === 'string' ? (actionGroup) => actionGroup === primaryGroup : primaryGroup;
  44. // Action bars handle alternative actions on their own so the alternative actions should be ignored
  45. fillInActions(groups, target, false, isPrimaryAction, primaryMaxCount, shouldInlineSubmenu, useSeparatorsInPrimaryActions);
  46. return asDisposable(groups);
  47. }
  48. function asDisposable(groups) {
  49. const disposables = new DisposableStore();
  50. for (const [, actions] of groups) {
  51. for (const action of actions) {
  52. disposables.add(action);
  53. }
  54. }
  55. return disposables;
  56. }
  57. function fillInActions(groups, target, useAlternativeActions, isPrimaryAction = actionGroup => actionGroup === 'navigation', primaryMaxCount = Number.MAX_SAFE_INTEGER, shouldInlineSubmenu = () => false, useSeparatorsInPrimaryActions = false) {
  58. let primaryBucket;
  59. let secondaryBucket;
  60. if (Array.isArray(target)) {
  61. primaryBucket = target;
  62. secondaryBucket = target;
  63. }
  64. else {
  65. primaryBucket = target.primary;
  66. secondaryBucket = target.secondary;
  67. }
  68. const submenuInfo = new Set();
  69. for (const [group, actions] of groups) {
  70. let target;
  71. if (isPrimaryAction(group)) {
  72. target = primaryBucket;
  73. if (target.length > 0 && useSeparatorsInPrimaryActions) {
  74. target.push(new Separator());
  75. }
  76. }
  77. else {
  78. target = secondaryBucket;
  79. if (target.length > 0) {
  80. target.push(new Separator());
  81. }
  82. }
  83. for (let action of actions) {
  84. if (useAlternativeActions) {
  85. action = action instanceof MenuItemAction && action.alt ? action.alt : action;
  86. }
  87. const newLen = target.push(action);
  88. // keep submenu info for later inlining
  89. if (action instanceof SubmenuAction) {
  90. submenuInfo.add({ group, action, index: newLen - 1 });
  91. }
  92. }
  93. }
  94. // ask the outside if submenu should be inlined or not. only ask when
  95. // there would be enough space
  96. for (const { group, action, index } of submenuInfo) {
  97. const target = isPrimaryAction(group) ? primaryBucket : secondaryBucket;
  98. // inlining submenus with length 0 or 1 is easy,
  99. // larger submenus need to be checked with the overall limit
  100. const submenuActions = action.actions;
  101. if ((submenuActions.length <= 1 || target.length + submenuActions.length - 2 <= primaryMaxCount) && shouldInlineSubmenu(action, group, target.length)) {
  102. target.splice(index, 1, ...submenuActions);
  103. }
  104. }
  105. // overflow items from the primary group into the secondary bucket
  106. if (primaryBucket !== secondaryBucket && primaryBucket.length > primaryMaxCount) {
  107. const overflow = primaryBucket.splice(primaryMaxCount, primaryBucket.length - primaryMaxCount);
  108. secondaryBucket.unshift(...overflow, new Separator());
  109. }
  110. }
  111. let MenuEntryActionViewItem = class MenuEntryActionViewItem extends ActionViewItem {
  112. constructor(_action, options, _keybindingService, _notificationService, _contextKeyService) {
  113. super(undefined, _action, { icon: !!(_action.class || _action.item.icon), label: !_action.class && !_action.item.icon, draggable: options === null || options === void 0 ? void 0 : options.draggable });
  114. this._keybindingService = _keybindingService;
  115. this._notificationService = _notificationService;
  116. this._contextKeyService = _contextKeyService;
  117. this._wantsAltCommand = false;
  118. this._itemClassDispose = this._register(new MutableDisposable());
  119. this._altKey = ModifierKeyEmitter.getInstance();
  120. }
  121. get _menuItemAction() {
  122. return this._action;
  123. }
  124. get _commandAction() {
  125. return this._wantsAltCommand && this._menuItemAction.alt || this._menuItemAction;
  126. }
  127. onClick(event) {
  128. return __awaiter(this, void 0, void 0, function* () {
  129. event.preventDefault();
  130. event.stopPropagation();
  131. try {
  132. yield this.actionRunner.run(this._commandAction, this._context);
  133. }
  134. catch (err) {
  135. this._notificationService.error(err);
  136. }
  137. });
  138. }
  139. render(container) {
  140. super.render(container);
  141. container.classList.add('menu-entry');
  142. this._updateItemClass(this._menuItemAction.item);
  143. let mouseOver = false;
  144. let alternativeKeyDown = this._altKey.keyStatus.altKey || ((isWindows || isLinux) && this._altKey.keyStatus.shiftKey);
  145. const updateAltState = () => {
  146. const wantsAltCommand = mouseOver && alternativeKeyDown;
  147. if (wantsAltCommand !== this._wantsAltCommand) {
  148. this._wantsAltCommand = wantsAltCommand;
  149. this.updateLabel();
  150. this.updateTooltip();
  151. this.updateClass();
  152. }
  153. };
  154. if (this._menuItemAction.alt) {
  155. this._register(this._altKey.event(value => {
  156. alternativeKeyDown = value.altKey || ((isWindows || isLinux) && value.shiftKey);
  157. updateAltState();
  158. }));
  159. }
  160. this._register(addDisposableListener(container, 'mouseleave', _ => {
  161. mouseOver = false;
  162. updateAltState();
  163. }));
  164. this._register(addDisposableListener(container, 'mouseenter', _ => {
  165. mouseOver = true;
  166. updateAltState();
  167. }));
  168. }
  169. updateLabel() {
  170. if (this.options.label && this.label) {
  171. this.label.textContent = this._commandAction.label;
  172. }
  173. }
  174. updateTooltip() {
  175. if (this.label) {
  176. const keybinding = this._keybindingService.lookupKeybinding(this._commandAction.id, this._contextKeyService);
  177. const keybindingLabel = keybinding && keybinding.getLabel();
  178. const tooltip = this._commandAction.tooltip || this._commandAction.label;
  179. let title = keybindingLabel
  180. ? localize('titleAndKb', "{0} ({1})", tooltip, keybindingLabel)
  181. : tooltip;
  182. if (!this._wantsAltCommand && this._menuItemAction.alt) {
  183. const altTooltip = this._menuItemAction.alt.tooltip || this._menuItemAction.alt.label;
  184. const altKeybinding = this._keybindingService.lookupKeybinding(this._menuItemAction.alt.id, this._contextKeyService);
  185. const altKeybindingLabel = altKeybinding && altKeybinding.getLabel();
  186. const altTitleSection = altKeybindingLabel
  187. ? localize('titleAndKb', "{0} ({1})", altTooltip, altKeybindingLabel)
  188. : altTooltip;
  189. title += `\n[${UILabelProvider.modifierLabels[OS].altKey}] ${altTitleSection}`;
  190. }
  191. this.label.title = title;
  192. }
  193. }
  194. updateClass() {
  195. if (this.options.icon) {
  196. if (this._commandAction !== this._menuItemAction) {
  197. if (this._menuItemAction.alt) {
  198. this._updateItemClass(this._menuItemAction.alt.item);
  199. }
  200. }
  201. else if (this._menuItemAction.alt) {
  202. this._updateItemClass(this._menuItemAction.item);
  203. }
  204. }
  205. }
  206. _updateItemClass(item) {
  207. var _a;
  208. this._itemClassDispose.value = undefined;
  209. const { element, label } = this;
  210. if (!element || !label) {
  211. return;
  212. }
  213. const icon = this._commandAction.checked && ((_a = item.toggled) === null || _a === void 0 ? void 0 : _a.icon) ? item.toggled.icon : item.icon;
  214. if (!icon) {
  215. return;
  216. }
  217. if (ThemeIcon.isThemeIcon(icon)) {
  218. // theme icons
  219. const iconClasses = ThemeIcon.asClassNameArray(icon);
  220. label.classList.add(...iconClasses);
  221. this._itemClassDispose.value = toDisposable(() => {
  222. label.classList.remove(...iconClasses);
  223. });
  224. }
  225. else {
  226. // icon path/url
  227. if (icon.light) {
  228. label.style.setProperty('--menu-entry-icon-light', asCSSUrl(icon.light));
  229. }
  230. if (icon.dark) {
  231. label.style.setProperty('--menu-entry-icon-dark', asCSSUrl(icon.dark));
  232. }
  233. label.classList.add('icon');
  234. this._itemClassDispose.value = toDisposable(() => {
  235. label.classList.remove('icon');
  236. label.style.removeProperty('--menu-entry-icon-light');
  237. label.style.removeProperty('--menu-entry-icon-dark');
  238. });
  239. }
  240. }
  241. };
  242. MenuEntryActionViewItem = __decorate([
  243. __param(2, IKeybindingService),
  244. __param(3, INotificationService),
  245. __param(4, IContextKeyService)
  246. ], MenuEntryActionViewItem);
  247. export { MenuEntryActionViewItem };
  248. let SubmenuEntryActionViewItem = class SubmenuEntryActionViewItem extends DropdownMenuActionViewItem {
  249. constructor(action, options, contextMenuService) {
  250. var _a, _b;
  251. const dropdownOptions = Object.assign({}, options !== null && options !== void 0 ? options : Object.create(null), {
  252. menuAsChild: (_a = options === null || options === void 0 ? void 0 : options.menuAsChild) !== null && _a !== void 0 ? _a : false,
  253. classNames: (_b = options === null || options === void 0 ? void 0 : options.classNames) !== null && _b !== void 0 ? _b : (ThemeIcon.isThemeIcon(action.item.icon) ? ThemeIcon.asClassName(action.item.icon) : undefined),
  254. });
  255. super(action, { getActions: () => action.actions }, contextMenuService, dropdownOptions);
  256. }
  257. render(container) {
  258. super.render(container);
  259. if (this.element) {
  260. container.classList.add('menu-entry');
  261. const { icon } = this._action.item;
  262. if (icon && !ThemeIcon.isThemeIcon(icon)) {
  263. this.element.classList.add('icon');
  264. if (icon.light) {
  265. this.element.style.setProperty('--menu-entry-icon-light', asCSSUrl(icon.light));
  266. }
  267. if (icon.dark) {
  268. this.element.style.setProperty('--menu-entry-icon-dark', asCSSUrl(icon.dark));
  269. }
  270. }
  271. }
  272. }
  273. };
  274. SubmenuEntryActionViewItem = __decorate([
  275. __param(2, IContextMenuService)
  276. ], SubmenuEntryActionViewItem);
  277. export { SubmenuEntryActionViewItem };
  278. let DropdownWithDefaultActionViewItem = class DropdownWithDefaultActionViewItem extends BaseActionViewItem {
  279. constructor(submenuAction, options, _keybindingService, _notificationService, _contextMenuService, _menuService, _instaService, _storageService) {
  280. var _a, _b, _c;
  281. super(null, submenuAction);
  282. this._keybindingService = _keybindingService;
  283. this._notificationService = _notificationService;
  284. this._contextMenuService = _contextMenuService;
  285. this._menuService = _menuService;
  286. this._instaService = _instaService;
  287. this._storageService = _storageService;
  288. this._container = null;
  289. this._storageKey = `${submenuAction.item.submenu._debugName}_lastActionId`;
  290. // determine default action
  291. let defaultAction;
  292. let defaultActionId = _storageService.get(this._storageKey, 1 /* WORKSPACE */);
  293. if (defaultActionId) {
  294. defaultAction = submenuAction.actions.find(a => defaultActionId === a.id);
  295. }
  296. if (!defaultAction) {
  297. defaultAction = submenuAction.actions[0];
  298. }
  299. this._defaultAction = this._instaService.createInstance(MenuEntryActionViewItem, defaultAction, undefined);
  300. const dropdownOptions = Object.assign({}, options !== null && options !== void 0 ? options : Object.create(null), {
  301. menuAsChild: (_a = options === null || options === void 0 ? void 0 : options.menuAsChild) !== null && _a !== void 0 ? _a : true,
  302. classNames: (_b = options === null || options === void 0 ? void 0 : options.classNames) !== null && _b !== void 0 ? _b : ['codicon', 'codicon-chevron-down'],
  303. actionRunner: (_c = options === null || options === void 0 ? void 0 : options.actionRunner) !== null && _c !== void 0 ? _c : new ActionRunner()
  304. });
  305. this._dropdown = new DropdownMenuActionViewItem(submenuAction, submenuAction.actions, this._contextMenuService, dropdownOptions);
  306. this._dropdown.actionRunner.onDidRun((e) => {
  307. if (e.action instanceof MenuItemAction) {
  308. this.update(e.action);
  309. }
  310. });
  311. }
  312. update(lastAction) {
  313. this._storageService.store(this._storageKey, lastAction.id, 1 /* WORKSPACE */, 0 /* USER */);
  314. this._defaultAction.dispose();
  315. this._defaultAction = this._instaService.createInstance(MenuEntryActionViewItem, lastAction, undefined);
  316. this._defaultAction.actionRunner = new class extends ActionRunner {
  317. runAction(action, context) {
  318. return __awaiter(this, void 0, void 0, function* () {
  319. yield action.run(undefined);
  320. });
  321. }
  322. }();
  323. if (this._container) {
  324. this._defaultAction.render(prepend(this._container, $('.action-container')));
  325. }
  326. }
  327. setActionContext(newContext) {
  328. super.setActionContext(newContext);
  329. this._defaultAction.setActionContext(newContext);
  330. this._dropdown.setActionContext(newContext);
  331. }
  332. render(container) {
  333. this._container = container;
  334. super.render(this._container);
  335. this._container.classList.add('monaco-dropdown-with-default');
  336. const primaryContainer = $('.action-container');
  337. this._defaultAction.render(append(this._container, primaryContainer));
  338. this._register(addDisposableListener(primaryContainer, EventType.KEY_DOWN, (e) => {
  339. const event = new StandardKeyboardEvent(e);
  340. if (event.equals(17 /* RightArrow */)) {
  341. this._defaultAction.element.tabIndex = -1;
  342. this._dropdown.focus();
  343. event.stopPropagation();
  344. }
  345. }));
  346. const dropdownContainer = $('.dropdown-action-container');
  347. this._dropdown.render(append(this._container, dropdownContainer));
  348. this._register(addDisposableListener(dropdownContainer, EventType.KEY_DOWN, (e) => {
  349. var _a;
  350. const event = new StandardKeyboardEvent(e);
  351. if (event.equals(15 /* LeftArrow */)) {
  352. this._defaultAction.element.tabIndex = 0;
  353. this._dropdown.setFocusable(false);
  354. (_a = this._defaultAction.element) === null || _a === void 0 ? void 0 : _a.focus();
  355. event.stopPropagation();
  356. }
  357. }));
  358. }
  359. focus(fromRight) {
  360. if (fromRight) {
  361. this._dropdown.focus();
  362. }
  363. else {
  364. this._defaultAction.element.tabIndex = 0;
  365. this._defaultAction.element.focus();
  366. }
  367. }
  368. blur() {
  369. this._defaultAction.element.tabIndex = -1;
  370. this._dropdown.blur();
  371. this._container.blur();
  372. }
  373. setFocusable(focusable) {
  374. if (focusable) {
  375. this._defaultAction.element.tabIndex = 0;
  376. }
  377. else {
  378. this._defaultAction.element.tabIndex = -1;
  379. this._dropdown.setFocusable(false);
  380. }
  381. }
  382. dispose() {
  383. this._defaultAction.dispose();
  384. this._dropdown.dispose();
  385. super.dispose();
  386. }
  387. };
  388. DropdownWithDefaultActionViewItem = __decorate([
  389. __param(2, IKeybindingService),
  390. __param(3, INotificationService),
  391. __param(4, IContextMenuService),
  392. __param(5, IMenuService),
  393. __param(6, IInstantiationService),
  394. __param(7, IStorageService)
  395. ], DropdownWithDefaultActionViewItem);
  396. /**
  397. * Creates action view items for menu actions or submenu actions.
  398. */
  399. export function createActionViewItem(instaService, action, options) {
  400. if (action instanceof MenuItemAction) {
  401. return instaService.createInstance(MenuEntryActionViewItem, action, undefined);
  402. }
  403. else if (action instanceof SubmenuItemAction) {
  404. if (action.item.rememberDefaultAction) {
  405. return instaService.createInstance(DropdownWithDefaultActionViewItem, action, options);
  406. }
  407. else {
  408. return instaService.createInstance(SubmenuEntryActionViewItem, action, options);
  409. }
  410. }
  411. else {
  412. return undefined;
  413. }
  414. }