| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139 |
- /*---------------------------------------------------------------------------------------------
- * Copyright (c) Microsoft Corporation. All rights reserved.
- * Licensed under the MIT License. See License.txt in the project root for license information.
- *--------------------------------------------------------------------------------------------*/
- import { isFirefox } from '../../browser.js';
- import { EventType as TouchEventType, Gesture } from '../../touch.js';
- import { $, addDisposableListener, append, clearNode, createStyleSheet, Dimension, EventHelper, EventType, getActiveElement, isAncestor, isInShadowDOM } from '../../dom.js';
- import { StandardKeyboardEvent } from '../../keyboardEvent.js';
- import { StandardMouseEvent } from '../../mouseEvent.js';
- import { ActionBar } from '../actionbar/actionbar.js';
- import { ActionViewItem, BaseActionViewItem } from '../actionbar/actionViewItems.js';
- import { formatRule } from '../codicons/codiconStyles.js';
- import { layout } from '../contextview/contextview.js';
- import { DomScrollableElement } from '../scrollbar/scrollableElement.js';
- import { EmptySubmenuAction, Separator, SubmenuAction } from '../../../common/actions.js';
- import { RunOnceScheduler } from '../../../common/async.js';
- import { Codicon } from '../../../common/codicons.js';
- import { stripIcons } from '../../../common/iconLabels.js';
- import { DisposableStore } from '../../../common/lifecycle.js';
- import { isLinux, isMacintosh } from '../../../common/platform.js';
- import * as strings from '../../../common/strings.js';
- export const MENU_MNEMONIC_REGEX = /\(&([^\s&])\)|(^|[^&])&([^\s&])/;
- export const MENU_ESCAPED_MNEMONIC_REGEX = /(&)?(&)([^\s&])/g;
- export var Direction;
- (function (Direction) {
- Direction[Direction["Right"] = 0] = "Right";
- Direction[Direction["Left"] = 1] = "Left";
- })(Direction || (Direction = {}));
- export class Menu extends ActionBar {
- constructor(container, actions, options = {}) {
- container.classList.add('monaco-menu-container');
- container.setAttribute('role', 'presentation');
- const menuElement = document.createElement('div');
- menuElement.classList.add('monaco-menu');
- menuElement.setAttribute('role', 'presentation');
- super(menuElement, {
- orientation: 1 /* ActionsOrientation.VERTICAL */,
- actionViewItemProvider: action => this.doGetActionViewItem(action, options, parentData),
- context: options.context,
- actionRunner: options.actionRunner,
- ariaLabel: options.ariaLabel,
- ariaRole: 'menu',
- focusOnlyEnabledItems: true,
- triggerKeys: { keys: [3 /* KeyCode.Enter */, ...(isMacintosh || isLinux ? [10 /* KeyCode.Space */] : [])], keyDown: true }
- });
- this.menuElement = menuElement;
- this.actionsList.tabIndex = 0;
- this.menuDisposables = this._register(new DisposableStore());
- this.initializeOrUpdateStyleSheet(container, {});
- this._register(Gesture.addTarget(menuElement));
- addDisposableListener(menuElement, EventType.KEY_DOWN, (e) => {
- const event = new StandardKeyboardEvent(e);
- // Stop tab navigation of menus
- if (event.equals(2 /* KeyCode.Tab */)) {
- e.preventDefault();
- }
- });
- if (options.enableMnemonics) {
- this.menuDisposables.add(addDisposableListener(menuElement, EventType.KEY_DOWN, (e) => {
- const key = e.key.toLocaleLowerCase();
- if (this.mnemonics.has(key)) {
- EventHelper.stop(e, true);
- const actions = this.mnemonics.get(key);
- if (actions.length === 1) {
- if (actions[0] instanceof SubmenuMenuActionViewItem && actions[0].container) {
- this.focusItemByElement(actions[0].container);
- }
- actions[0].onClick(e);
- }
- if (actions.length > 1) {
- const action = actions.shift();
- if (action && action.container) {
- this.focusItemByElement(action.container);
- actions.push(action);
- }
- this.mnemonics.set(key, actions);
- }
- }
- }));
- }
- if (isLinux) {
- this._register(addDisposableListener(menuElement, EventType.KEY_DOWN, e => {
- const event = new StandardKeyboardEvent(e);
- if (event.equals(14 /* KeyCode.Home */) || event.equals(11 /* KeyCode.PageUp */)) {
- this.focusedItem = this.viewItems.length - 1;
- this.focusNext();
- EventHelper.stop(e, true);
- }
- else if (event.equals(13 /* KeyCode.End */) || event.equals(12 /* KeyCode.PageDown */)) {
- this.focusedItem = 0;
- this.focusPrevious();
- EventHelper.stop(e, true);
- }
- }));
- }
- this._register(addDisposableListener(this.domNode, EventType.MOUSE_OUT, e => {
- const relatedTarget = e.relatedTarget;
- if (!isAncestor(relatedTarget, this.domNode)) {
- this.focusedItem = undefined;
- this.updateFocus();
- e.stopPropagation();
- }
- }));
- this._register(addDisposableListener(this.actionsList, EventType.MOUSE_OVER, e => {
- let target = e.target;
- if (!target || !isAncestor(target, this.actionsList) || target === this.actionsList) {
- return;
- }
- while (target.parentElement !== this.actionsList && target.parentElement !== null) {
- target = target.parentElement;
- }
- if (target.classList.contains('action-item')) {
- const lastFocusedItem = this.focusedItem;
- this.setFocusedItem(target);
- if (lastFocusedItem !== this.focusedItem) {
- this.updateFocus();
- }
- }
- }));
- // Support touch on actions list to focus items (needed for submenus)
- this._register(Gesture.addTarget(this.actionsList));
- this._register(addDisposableListener(this.actionsList, TouchEventType.Tap, e => {
- let target = e.initialTarget;
- if (!target || !isAncestor(target, this.actionsList) || target === this.actionsList) {
- return;
- }
- while (target.parentElement !== this.actionsList && target.parentElement !== null) {
- target = target.parentElement;
- }
- if (target.classList.contains('action-item')) {
- const lastFocusedItem = this.focusedItem;
- this.setFocusedItem(target);
- if (lastFocusedItem !== this.focusedItem) {
- this.updateFocus();
- }
- }
- }));
- const parentData = {
- parent: this
- };
- this.mnemonics = new Map();
- // Scroll Logic
- this.scrollableElement = this._register(new DomScrollableElement(menuElement, {
- alwaysConsumeMouseWheel: true,
- horizontal: 2 /* ScrollbarVisibility.Hidden */,
- vertical: 3 /* ScrollbarVisibility.Visible */,
- verticalScrollbarSize: 7,
- handleMouseWheel: true,
- useShadows: true
- }));
- const scrollElement = this.scrollableElement.getDomNode();
- scrollElement.style.position = '';
- // Support scroll on menu drag
- this._register(addDisposableListener(menuElement, TouchEventType.Change, e => {
- EventHelper.stop(e, true);
- const scrollTop = this.scrollableElement.getScrollPosition().scrollTop;
- this.scrollableElement.setScrollPosition({ scrollTop: scrollTop - e.translationY });
- }));
- this._register(addDisposableListener(scrollElement, EventType.MOUSE_UP, e => {
- // Absorb clicks in menu dead space https://github.com/microsoft/vscode/issues/63575
- // We do this on the scroll element so the scroll bar doesn't dismiss the menu either
- e.preventDefault();
- }));
- menuElement.style.maxHeight = `${Math.max(10, window.innerHeight - container.getBoundingClientRect().top - 35)}px`;
- actions = actions.filter(a => {
- var _a;
- if ((_a = options.submenuIds) === null || _a === void 0 ? void 0 : _a.has(a.id)) {
- console.warn(`Found submenu cycle: ${a.id}`);
- return false;
- }
- return true;
- });
- this.push(actions, { icon: true, label: true, isMenu: true });
- container.appendChild(this.scrollableElement.getDomNode());
- this.scrollableElement.scanDomNode();
- this.viewItems.filter(item => !(item instanceof MenuSeparatorActionViewItem)).forEach((item, index, array) => {
- item.updatePositionInSet(index + 1, array.length);
- });
- }
- initializeOrUpdateStyleSheet(container, style) {
- if (!this.styleSheet) {
- if (isInShadowDOM(container)) {
- this.styleSheet = createStyleSheet(container);
- }
- else {
- if (!Menu.globalStyleSheet) {
- Menu.globalStyleSheet = createStyleSheet();
- }
- this.styleSheet = Menu.globalStyleSheet;
- }
- }
- this.styleSheet.textContent = getMenuWidgetCSS(style, isInShadowDOM(container));
- }
- style(style) {
- const container = this.getContainer();
- this.initializeOrUpdateStyleSheet(container, style);
- const fgColor = style.foregroundColor ? `${style.foregroundColor}` : '';
- const bgColor = style.backgroundColor ? `${style.backgroundColor}` : '';
- const border = style.borderColor ? `1px solid ${style.borderColor}` : '';
- const borderRadius = '5px';
- const shadow = style.shadowColor ? `0 2px 8px ${style.shadowColor}` : '';
- container.style.outline = border;
- container.style.borderRadius = borderRadius;
- container.style.color = fgColor;
- container.style.backgroundColor = bgColor;
- container.style.boxShadow = shadow;
- if (this.viewItems) {
- this.viewItems.forEach(item => {
- if (item instanceof BaseMenuActionViewItem || item instanceof MenuSeparatorActionViewItem) {
- item.style(style);
- }
- });
- }
- }
- getContainer() {
- return this.scrollableElement.getDomNode();
- }
- get onScroll() {
- return this.scrollableElement.onScroll;
- }
- focusItemByElement(element) {
- const lastFocusedItem = this.focusedItem;
- this.setFocusedItem(element);
- if (lastFocusedItem !== this.focusedItem) {
- this.updateFocus();
- }
- }
- setFocusedItem(element) {
- for (let i = 0; i < this.actionsList.children.length; i++) {
- const elem = this.actionsList.children[i];
- if (element === elem) {
- this.focusedItem = i;
- break;
- }
- }
- }
- updateFocus(fromRight) {
- super.updateFocus(fromRight, true, true);
- if (typeof this.focusedItem !== 'undefined') {
- // Workaround for #80047 caused by an issue in chromium
- // https://bugs.chromium.org/p/chromium/issues/detail?id=414283
- // When that's fixed, just call this.scrollableElement.scanDomNode()
- this.scrollableElement.setScrollPosition({
- scrollTop: Math.round(this.menuElement.scrollTop)
- });
- }
- }
- doGetActionViewItem(action, options, parentData) {
- if (action instanceof Separator) {
- return new MenuSeparatorActionViewItem(options.context, action, { icon: true });
- }
- else if (action instanceof SubmenuAction) {
- const menuActionViewItem = new SubmenuMenuActionViewItem(action, action.actions, parentData, Object.assign(Object.assign({}, options), { submenuIds: new Set([...(options.submenuIds || []), action.id]) }));
- if (options.enableMnemonics) {
- const mnemonic = menuActionViewItem.getMnemonic();
- if (mnemonic && menuActionViewItem.isEnabled()) {
- let actionViewItems = [];
- if (this.mnemonics.has(mnemonic)) {
- actionViewItems = this.mnemonics.get(mnemonic);
- }
- actionViewItems.push(menuActionViewItem);
- this.mnemonics.set(mnemonic, actionViewItems);
- }
- }
- return menuActionViewItem;
- }
- else {
- const menuItemOptions = { enableMnemonics: options.enableMnemonics, useEventAsContext: options.useEventAsContext };
- if (options.getKeyBinding) {
- const keybinding = options.getKeyBinding(action);
- if (keybinding) {
- const keybindingLabel = keybinding.getLabel();
- if (keybindingLabel) {
- menuItemOptions.keybinding = keybindingLabel;
- }
- }
- }
- const menuActionViewItem = new BaseMenuActionViewItem(options.context, action, menuItemOptions);
- if (options.enableMnemonics) {
- const mnemonic = menuActionViewItem.getMnemonic();
- if (mnemonic && menuActionViewItem.isEnabled()) {
- let actionViewItems = [];
- if (this.mnemonics.has(mnemonic)) {
- actionViewItems = this.mnemonics.get(mnemonic);
- }
- actionViewItems.push(menuActionViewItem);
- this.mnemonics.set(mnemonic, actionViewItems);
- }
- }
- return menuActionViewItem;
- }
- }
- }
- class BaseMenuActionViewItem extends BaseActionViewItem {
- constructor(ctx, action, options = {}) {
- options.isMenu = true;
- super(action, action, options);
- this.options = options;
- this.options.icon = options.icon !== undefined ? options.icon : false;
- this.options.label = options.label !== undefined ? options.label : true;
- this.cssClass = '';
- // Set mnemonic
- if (this.options.label && options.enableMnemonics) {
- const label = this.getAction().label;
- if (label) {
- const matches = MENU_MNEMONIC_REGEX.exec(label);
- if (matches) {
- this.mnemonic = (!!matches[1] ? matches[1] : matches[3]).toLocaleLowerCase();
- }
- }
- }
- // Add mouse up listener later to avoid accidental clicks
- this.runOnceToEnableMouseUp = new RunOnceScheduler(() => {
- if (!this.element) {
- return;
- }
- this._register(addDisposableListener(this.element, EventType.MOUSE_UP, e => {
- // removed default prevention as it conflicts
- // with BaseActionViewItem #101537
- // add back if issues arise and link new issue
- EventHelper.stop(e, true);
- // See https://developer.mozilla.org/en-US/Add-ons/WebExtensions/Interact_with_the_clipboard
- // > Writing to the clipboard
- // > You can use the "cut" and "copy" commands without any special
- // permission if you are using them in a short-lived event handler
- // for a user action (for example, a click handler).
- // => to get the Copy and Paste context menu actions working on Firefox,
- // there should be no timeout here
- if (isFirefox) {
- const mouseEvent = new StandardMouseEvent(e);
- // Allowing right click to trigger the event causes the issue described below,
- // but since the solution below does not work in FF, we must disable right click
- if (mouseEvent.rightButton) {
- return;
- }
- this.onClick(e);
- }
- // In all other cases, set timeout to allow context menu cancellation to trigger
- // otherwise the action will destroy the menu and a second context menu
- // will still trigger for right click.
- else {
- setTimeout(() => {
- this.onClick(e);
- }, 0);
- }
- }));
- this._register(addDisposableListener(this.element, EventType.CONTEXT_MENU, e => {
- EventHelper.stop(e, true);
- }));
- }, 100);
- this._register(this.runOnceToEnableMouseUp);
- }
- render(container) {
- super.render(container);
- if (!this.element) {
- return;
- }
- this.container = container;
- this.item = append(this.element, $('a.action-menu-item'));
- if (this._action.id === Separator.ID) {
- // A separator is a presentation item
- this.item.setAttribute('role', 'presentation');
- }
- else {
- this.item.setAttribute('role', 'menuitem');
- if (this.mnemonic) {
- this.item.setAttribute('aria-keyshortcuts', `${this.mnemonic}`);
- }
- }
- this.check = append(this.item, $('span.menu-item-check' + Codicon.menuSelection.cssSelector));
- this.check.setAttribute('role', 'none');
- this.label = append(this.item, $('span.action-label'));
- if (this.options.label && this.options.keybinding) {
- append(this.item, $('span.keybinding')).textContent = this.options.keybinding;
- }
- // Adds mouse up listener to actually run the action
- this.runOnceToEnableMouseUp.schedule();
- this.updateClass();
- this.updateLabel();
- this.updateTooltip();
- this.updateEnabled();
- this.updateChecked();
- }
- blur() {
- super.blur();
- this.applyStyle();
- }
- focus() {
- super.focus();
- if (this.item) {
- this.item.focus();
- }
- this.applyStyle();
- }
- updatePositionInSet(pos, setSize) {
- if (this.item) {
- this.item.setAttribute('aria-posinset', `${pos}`);
- this.item.setAttribute('aria-setsize', `${setSize}`);
- }
- }
- updateLabel() {
- var _a;
- if (!this.label) {
- return;
- }
- if (this.options.label) {
- clearNode(this.label);
- let label = stripIcons(this.getAction().label);
- if (label) {
- const cleanLabel = cleanMnemonic(label);
- if (!this.options.enableMnemonics) {
- label = cleanLabel;
- }
- this.label.setAttribute('aria-label', cleanLabel.replace(/&&/g, '&'));
- const matches = MENU_MNEMONIC_REGEX.exec(label);
- if (matches) {
- label = strings.escape(label);
- // This is global, reset it
- MENU_ESCAPED_MNEMONIC_REGEX.lastIndex = 0;
- let escMatch = MENU_ESCAPED_MNEMONIC_REGEX.exec(label);
- // We can't use negative lookbehind so if we match our negative and skip
- while (escMatch && escMatch[1]) {
- escMatch = MENU_ESCAPED_MNEMONIC_REGEX.exec(label);
- }
- const replaceDoubleEscapes = (str) => str.replace(/&&/g, '&');
- if (escMatch) {
- this.label.append(strings.ltrim(replaceDoubleEscapes(label.substr(0, escMatch.index)), ' '), $('u', { 'aria-hidden': 'true' }, escMatch[3]), strings.rtrim(replaceDoubleEscapes(label.substr(escMatch.index + escMatch[0].length)), ' '));
- }
- else {
- this.label.innerText = replaceDoubleEscapes(label).trim();
- }
- (_a = this.item) === null || _a === void 0 ? void 0 : _a.setAttribute('aria-keyshortcuts', (!!matches[1] ? matches[1] : matches[3]).toLocaleLowerCase());
- }
- else {
- this.label.innerText = label.replace(/&&/g, '&').trim();
- }
- }
- }
- }
- updateTooltip() {
- // menus should function like native menus and they do not have tooltips
- }
- updateClass() {
- if (this.cssClass && this.item) {
- this.item.classList.remove(...this.cssClass.split(' '));
- }
- if (this.options.icon && this.label) {
- this.cssClass = this.getAction().class || '';
- this.label.classList.add('icon');
- if (this.cssClass) {
- this.label.classList.add(...this.cssClass.split(' '));
- }
- this.updateEnabled();
- }
- else if (this.label) {
- this.label.classList.remove('icon');
- }
- }
- updateEnabled() {
- if (this.getAction().enabled) {
- if (this.element) {
- this.element.classList.remove('disabled');
- this.element.removeAttribute('aria-disabled');
- }
- if (this.item) {
- this.item.classList.remove('disabled');
- this.item.removeAttribute('aria-disabled');
- this.item.tabIndex = 0;
- }
- }
- else {
- if (this.element) {
- this.element.classList.add('disabled');
- this.element.setAttribute('aria-disabled', 'true');
- }
- if (this.item) {
- this.item.classList.add('disabled');
- this.item.setAttribute('aria-disabled', 'true');
- }
- }
- }
- updateChecked() {
- if (!this.item) {
- return;
- }
- const checked = this.getAction().checked;
- this.item.classList.toggle('checked', !!checked);
- if (checked !== undefined) {
- this.item.setAttribute('role', 'menuitemcheckbox');
- this.item.setAttribute('aria-checked', checked ? 'true' : 'false');
- }
- else {
- this.item.setAttribute('role', 'menuitem');
- this.item.setAttribute('aria-checked', '');
- }
- }
- getMnemonic() {
- return this.mnemonic;
- }
- applyStyle() {
- if (!this.menuStyle) {
- return;
- }
- const isSelected = this.element && this.element.classList.contains('focused');
- const fgColor = isSelected && this.menuStyle.selectionForegroundColor ? this.menuStyle.selectionForegroundColor : this.menuStyle.foregroundColor;
- const bgColor = isSelected && this.menuStyle.selectionBackgroundColor ? this.menuStyle.selectionBackgroundColor : undefined;
- const outline = isSelected && this.menuStyle.selectionBorderColor ? `1px solid ${this.menuStyle.selectionBorderColor}` : '';
- const outlineOffset = isSelected && this.menuStyle.selectionBorderColor ? `-1px` : '';
- if (this.item) {
- this.item.style.color = fgColor ? fgColor.toString() : '';
- this.item.style.backgroundColor = bgColor ? bgColor.toString() : '';
- this.item.style.outline = outline;
- this.item.style.outlineOffset = outlineOffset;
- }
- if (this.check) {
- this.check.style.color = fgColor ? fgColor.toString() : '';
- }
- }
- style(style) {
- this.menuStyle = style;
- this.applyStyle();
- }
- }
- class SubmenuMenuActionViewItem extends BaseMenuActionViewItem {
- constructor(action, submenuActions, parentData, submenuOptions) {
- super(action, action, submenuOptions);
- this.submenuActions = submenuActions;
- this.parentData = parentData;
- this.submenuOptions = submenuOptions;
- this.mysubmenu = null;
- this.submenuDisposables = this._register(new DisposableStore());
- this.mouseOver = false;
- this.expandDirection = submenuOptions && submenuOptions.expandDirection !== undefined ? submenuOptions.expandDirection : Direction.Right;
- this.showScheduler = new RunOnceScheduler(() => {
- if (this.mouseOver) {
- this.cleanupExistingSubmenu(false);
- this.createSubmenu(false);
- }
- }, 250);
- this.hideScheduler = new RunOnceScheduler(() => {
- if (this.element && (!isAncestor(getActiveElement(), this.element) && this.parentData.submenu === this.mysubmenu)) {
- this.parentData.parent.focus(false);
- this.cleanupExistingSubmenu(true);
- }
- }, 750);
- }
- render(container) {
- super.render(container);
- if (!this.element) {
- return;
- }
- if (this.item) {
- this.item.classList.add('monaco-submenu-item');
- this.item.tabIndex = 0;
- this.item.setAttribute('aria-haspopup', 'true');
- this.updateAriaExpanded('false');
- this.submenuIndicator = append(this.item, $('span.submenu-indicator' + Codicon.menuSubmenu.cssSelector));
- this.submenuIndicator.setAttribute('aria-hidden', 'true');
- }
- this._register(addDisposableListener(this.element, EventType.KEY_UP, e => {
- const event = new StandardKeyboardEvent(e);
- if (event.equals(17 /* KeyCode.RightArrow */) || event.equals(3 /* KeyCode.Enter */)) {
- EventHelper.stop(e, true);
- this.createSubmenu(true);
- }
- }));
- this._register(addDisposableListener(this.element, EventType.KEY_DOWN, e => {
- const event = new StandardKeyboardEvent(e);
- if (getActiveElement() === this.item) {
- if (event.equals(17 /* KeyCode.RightArrow */) || event.equals(3 /* KeyCode.Enter */)) {
- EventHelper.stop(e, true);
- }
- }
- }));
- this._register(addDisposableListener(this.element, EventType.MOUSE_OVER, e => {
- if (!this.mouseOver) {
- this.mouseOver = true;
- this.showScheduler.schedule();
- }
- }));
- this._register(addDisposableListener(this.element, EventType.MOUSE_LEAVE, e => {
- this.mouseOver = false;
- }));
- this._register(addDisposableListener(this.element, EventType.FOCUS_OUT, e => {
- if (this.element && !isAncestor(getActiveElement(), this.element)) {
- this.hideScheduler.schedule();
- }
- }));
- this._register(this.parentData.parent.onScroll(() => {
- if (this.parentData.submenu === this.mysubmenu) {
- this.parentData.parent.focus(false);
- this.cleanupExistingSubmenu(true);
- }
- }));
- }
- updateEnabled() {
- // override on submenu entry
- // native menus do not observe enablement on sumbenus
- // we mimic that behavior
- }
- onClick(e) {
- // stop clicking from trying to run an action
- EventHelper.stop(e, true);
- this.cleanupExistingSubmenu(false);
- this.createSubmenu(true);
- }
- cleanupExistingSubmenu(force) {
- if (this.parentData.submenu && (force || (this.parentData.submenu !== this.mysubmenu))) {
- // disposal may throw if the submenu has already been removed
- try {
- this.parentData.submenu.dispose();
- }
- catch (_a) { }
- this.parentData.submenu = undefined;
- this.updateAriaExpanded('false');
- if (this.submenuContainer) {
- this.submenuDisposables.clear();
- this.submenuContainer = undefined;
- }
- }
- }
- calculateSubmenuMenuLayout(windowDimensions, submenu, entry, expandDirection) {
- const ret = { top: 0, left: 0 };
- // Start with horizontal
- ret.left = layout(windowDimensions.width, submenu.width, { position: expandDirection === Direction.Right ? 0 /* LayoutAnchorPosition.Before */ : 1 /* LayoutAnchorPosition.After */, offset: entry.left, size: entry.width });
- // We don't have enough room to layout the menu fully, so we are overlapping the menu
- if (ret.left >= entry.left && ret.left < entry.left + entry.width) {
- if (entry.left + 10 + submenu.width <= windowDimensions.width) {
- ret.left = entry.left + 10;
- }
- entry.top += 10;
- entry.height = 0;
- }
- // Now that we have a horizontal position, try layout vertically
- ret.top = layout(windowDimensions.height, submenu.height, { position: 0 /* LayoutAnchorPosition.Before */, offset: entry.top, size: 0 });
- // We didn't have enough room below, but we did above, so we shift down to align the menu
- if (ret.top + submenu.height === entry.top && ret.top + entry.height + submenu.height <= windowDimensions.height) {
- ret.top += entry.height;
- }
- return ret;
- }
- createSubmenu(selectFirstItem = true) {
- if (!this.element) {
- return;
- }
- if (!this.parentData.submenu) {
- this.updateAriaExpanded('true');
- this.submenuContainer = append(this.element, $('div.monaco-submenu'));
- this.submenuContainer.classList.add('menubar-menu-items-holder', 'context-view');
- // Set the top value of the menu container before construction
- // This allows the menu constructor to calculate the proper max height
- const computedStyles = getComputedStyle(this.parentData.parent.domNode);
- const paddingTop = parseFloat(computedStyles.paddingTop || '0') || 0;
- // this.submenuContainer.style.top = `${this.element.offsetTop - this.parentData.parent.scrollOffset - paddingTop}px`;
- this.submenuContainer.style.zIndex = '1';
- this.submenuContainer.style.position = 'fixed';
- this.submenuContainer.style.top = '0';
- this.submenuContainer.style.left = '0';
- this.parentData.submenu = new Menu(this.submenuContainer, this.submenuActions.length ? this.submenuActions : [new EmptySubmenuAction()], this.submenuOptions);
- if (this.menuStyle) {
- this.parentData.submenu.style(this.menuStyle);
- }
- // layout submenu
- const entryBox = this.element.getBoundingClientRect();
- const entryBoxUpdated = {
- top: entryBox.top - paddingTop,
- left: entryBox.left,
- height: entryBox.height + 2 * paddingTop,
- width: entryBox.width
- };
- const viewBox = this.submenuContainer.getBoundingClientRect();
- const { top, left } = this.calculateSubmenuMenuLayout(new Dimension(window.innerWidth, window.innerHeight), Dimension.lift(viewBox), entryBoxUpdated, this.expandDirection);
- // subtract offsets caused by transform parent
- this.submenuContainer.style.left = `${left - viewBox.left}px`;
- this.submenuContainer.style.top = `${top - viewBox.top}px`;
- this.submenuDisposables.add(addDisposableListener(this.submenuContainer, EventType.KEY_UP, e => {
- const event = new StandardKeyboardEvent(e);
- if (event.equals(15 /* KeyCode.LeftArrow */)) {
- EventHelper.stop(e, true);
- this.parentData.parent.focus();
- this.cleanupExistingSubmenu(true);
- }
- }));
- this.submenuDisposables.add(addDisposableListener(this.submenuContainer, EventType.KEY_DOWN, e => {
- const event = new StandardKeyboardEvent(e);
- if (event.equals(15 /* KeyCode.LeftArrow */)) {
- EventHelper.stop(e, true);
- }
- }));
- this.submenuDisposables.add(this.parentData.submenu.onDidCancel(() => {
- this.parentData.parent.focus();
- this.cleanupExistingSubmenu(true);
- }));
- this.parentData.submenu.focus(selectFirstItem);
- this.mysubmenu = this.parentData.submenu;
- }
- else {
- this.parentData.submenu.focus(false);
- }
- }
- updateAriaExpanded(value) {
- var _a;
- if (this.item) {
- (_a = this.item) === null || _a === void 0 ? void 0 : _a.setAttribute('aria-expanded', value);
- }
- }
- applyStyle() {
- var _a;
- super.applyStyle();
- if (!this.menuStyle) {
- return;
- }
- const isSelected = this.element && this.element.classList.contains('focused');
- const fgColor = isSelected && this.menuStyle.selectionForegroundColor ? this.menuStyle.selectionForegroundColor : this.menuStyle.foregroundColor;
- if (this.submenuIndicator) {
- this.submenuIndicator.style.color = fgColor ? `${fgColor}` : '';
- }
- (_a = this.parentData.submenu) === null || _a === void 0 ? void 0 : _a.style(this.menuStyle);
- }
- dispose() {
- super.dispose();
- this.hideScheduler.dispose();
- if (this.mysubmenu) {
- this.mysubmenu.dispose();
- this.mysubmenu = null;
- }
- if (this.submenuContainer) {
- this.submenuContainer = undefined;
- }
- }
- }
- class MenuSeparatorActionViewItem extends ActionViewItem {
- style(style) {
- if (this.label) {
- this.label.style.borderBottomColor = style.separatorColor ? `${style.separatorColor}` : '';
- }
- }
- }
- export function cleanMnemonic(label) {
- const regex = MENU_MNEMONIC_REGEX;
- const matches = regex.exec(label);
- if (!matches) {
- return label;
- }
- const mnemonicInText = !matches[1];
- return label.replace(regex, mnemonicInText ? '$2$3' : '').trim();
- }
- function getMenuWidgetCSS(style, isForShadowDom) {
- let result = /* css */ `
- .monaco-menu {
- font-size: 13px;
- border-radius: 5px;
- min-width: 160px;
- }
- ${formatRule(Codicon.menuSelection)}
- ${formatRule(Codicon.menuSubmenu)}
- .monaco-menu .monaco-action-bar {
- text-align: right;
- overflow: hidden;
- white-space: nowrap;
- }
- .monaco-menu .monaco-action-bar .actions-container {
- display: flex;
- margin: 0 auto;
- padding: 0;
- width: 100%;
- justify-content: flex-end;
- }
- .monaco-menu .monaco-action-bar.vertical .actions-container {
- display: inline-block;
- }
- .monaco-menu .monaco-action-bar.reverse .actions-container {
- flex-direction: row-reverse;
- }
- .monaco-menu .monaco-action-bar .action-item {
- cursor: pointer;
- display: inline-block;
- transition: transform 50ms ease;
- position: relative; /* DO NOT REMOVE - this is the key to preventing the ghosting icon bug in Chrome 42 */
- }
- .monaco-menu .monaco-action-bar .action-item.disabled {
- cursor: default;
- }
- .monaco-menu .monaco-action-bar.animated .action-item.active {
- transform: scale(1.272019649, 1.272019649); /* 1.272019649 = √φ */
- }
- .monaco-menu .monaco-action-bar .action-item .icon,
- .monaco-menu .monaco-action-bar .action-item .codicon {
- display: inline-block;
- }
- .monaco-menu .monaco-action-bar .action-item .codicon {
- display: flex;
- align-items: center;
- }
- .monaco-menu .monaco-action-bar .action-label {
- font-size: 11px;
- margin-right: 4px;
- }
- .monaco-menu .monaco-action-bar .action-item.disabled .action-label,
- .monaco-menu .monaco-action-bar .action-item.disabled .action-label:hover {
- color: var(--vscode-disabledForeground);
- }
- /* Vertical actions */
- .monaco-menu .monaco-action-bar.vertical {
- text-align: left;
- }
- .monaco-menu .monaco-action-bar.vertical .action-item {
- display: block;
- }
- .monaco-menu .monaco-action-bar.vertical .action-label.separator {
- display: block;
- border-bottom: 1px solid var(--vscode-menu-separatorBackground);
- padding-top: 1px;
- padding: 30px;
- }
- .monaco-menu .secondary-actions .monaco-action-bar .action-label {
- margin-left: 6px;
- }
- /* Action Items */
- .monaco-menu .monaco-action-bar .action-item.select-container {
- overflow: hidden; /* somehow the dropdown overflows its container, we prevent it here to not push */
- flex: 1;
- max-width: 170px;
- min-width: 60px;
- display: flex;
- align-items: center;
- justify-content: center;
- margin-right: 10px;
- }
- .monaco-menu .monaco-action-bar.vertical {
- margin-left: 0;
- overflow: visible;
- }
- .monaco-menu .monaco-action-bar.vertical .actions-container {
- display: block;
- }
- .monaco-menu .monaco-action-bar.vertical .action-item {
- padding: 0;
- transform: none;
- display: flex;
- }
- .monaco-menu .monaco-action-bar.vertical .action-item.active {
- transform: none;
- }
- .monaco-menu .monaco-action-bar.vertical .action-menu-item {
- flex: 1 1 auto;
- display: flex;
- height: 2em;
- align-items: center;
- position: relative;
- }
- .monaco-menu .monaco-action-bar.vertical .action-menu-item:hover .keybinding,
- .monaco-menu .monaco-action-bar.vertical .action-menu-item:focus .keybinding {
- opacity: unset;
- }
- .monaco-menu .monaco-action-bar.vertical .action-label {
- flex: 1 1 auto;
- text-decoration: none;
- padding: 0 1em;
- background: none;
- font-size: 12px;
- line-height: 1;
- }
- .monaco-menu .monaco-action-bar.vertical .keybinding,
- .monaco-menu .monaco-action-bar.vertical .submenu-indicator {
- display: inline-block;
- flex: 2 1 auto;
- padding: 0 1em;
- text-align: right;
- font-size: 12px;
- line-height: 1;
- }
- .monaco-menu .monaco-action-bar.vertical .submenu-indicator {
- height: 100%;
- }
- .monaco-menu .monaco-action-bar.vertical .submenu-indicator.codicon {
- font-size: 16px !important;
- display: flex;
- align-items: center;
- }
- .monaco-menu .monaco-action-bar.vertical .submenu-indicator.codicon::before {
- margin-left: auto;
- margin-right: -20px;
- }
- .monaco-menu .monaco-action-bar.vertical .action-item.disabled .keybinding,
- .monaco-menu .monaco-action-bar.vertical .action-item.disabled .submenu-indicator {
- opacity: 0.4;
- }
- .monaco-menu .monaco-action-bar.vertical .action-label:not(.separator) {
- display: inline-block;
- box-sizing: border-box;
- margin: 0;
- }
- .monaco-menu .monaco-action-bar.vertical .action-item {
- position: static;
- overflow: visible;
- }
- .monaco-menu .monaco-action-bar.vertical .action-item .monaco-submenu {
- position: absolute;
- }
- .monaco-menu .monaco-action-bar.vertical .action-label.separator {
- width: 100%;
- height: 0px !important;
- opacity: 1;
- }
- .monaco-menu .monaco-action-bar.vertical .action-label.separator.text {
- padding: 0.7em 1em 0.1em 1em;
- font-weight: bold;
- opacity: 1;
- }
- .monaco-menu .monaco-action-bar.vertical .action-label:hover {
- color: inherit;
- }
- .monaco-menu .monaco-action-bar.vertical .menu-item-check {
- position: absolute;
- visibility: hidden;
- width: 1em;
- height: 100%;
- }
- .monaco-menu .monaco-action-bar.vertical .action-menu-item.checked .menu-item-check {
- visibility: visible;
- display: flex;
- align-items: center;
- justify-content: center;
- }
- /* Context Menu */
- .context-view.monaco-menu-container {
- outline: 0;
- border: none;
- animation: fadeIn 0.083s linear;
- -webkit-app-region: no-drag;
- }
- .context-view.monaco-menu-container :focus,
- .context-view.monaco-menu-container .monaco-action-bar.vertical:focus,
- .context-view.monaco-menu-container .monaco-action-bar.vertical :focus {
- outline: 0;
- }
- .hc-black .context-view.monaco-menu-container,
- .hc-light .context-view.monaco-menu-container,
- :host-context(.hc-black) .context-view.monaco-menu-container,
- :host-context(.hc-light) .context-view.monaco-menu-container {
- box-shadow: none;
- }
- .hc-black .monaco-menu .monaco-action-bar.vertical .action-item.focused,
- .hc-light .monaco-menu .monaco-action-bar.vertical .action-item.focused,
- :host-context(.hc-black) .monaco-menu .monaco-action-bar.vertical .action-item.focused,
- :host-context(.hc-light) .monaco-menu .monaco-action-bar.vertical .action-item.focused {
- background: none;
- }
- /* Vertical Action Bar Styles */
- .monaco-menu .monaco-action-bar.vertical {
- padding: .6em 0;
- }
- .monaco-menu .monaco-action-bar.vertical .action-menu-item {
- height: 2em;
- }
- .monaco-menu .monaco-action-bar.vertical .action-label:not(.separator),
- .monaco-menu .monaco-action-bar.vertical .keybinding {
- font-size: inherit;
- padding: 0 2em;
- }
- .monaco-menu .monaco-action-bar.vertical .menu-item-check {
- font-size: inherit;
- width: 2em;
- }
- .monaco-menu .monaco-action-bar.vertical .action-label.separator {
- font-size: inherit;
- margin: 5px 0 !important;
- padding: 0;
- border-radius: 0;
- }
- .linux .monaco-menu .monaco-action-bar.vertical .action-label.separator,
- :host-context(.linux) .monaco-menu .monaco-action-bar.vertical .action-label.separator {
- margin-left: 0;
- margin-right: 0;
- }
- .monaco-menu .monaco-action-bar.vertical .submenu-indicator {
- font-size: 60%;
- padding: 0 1.8em;
- }
- .linux .monaco-menu .monaco-action-bar.vertical .submenu-indicator {
- :host-context(.linux) .monaco-menu .monaco-action-bar.vertical .submenu-indicator {
- height: 100%;
- mask-size: 10px 10px;
- -webkit-mask-size: 10px 10px;
- }
- .monaco-menu .action-item {
- cursor: default;
- }`;
- if (isForShadowDom) {
- // Only define scrollbar styles when used inside shadow dom,
- // otherwise leave their styling to the global workbench styling.
- result += `
- /* Arrows */
- .monaco-scrollable-element > .scrollbar > .scra {
- cursor: pointer;
- font-size: 11px !important;
- }
- .monaco-scrollable-element > .visible {
- opacity: 1;
- /* Background rule added for IE9 - to allow clicks on dom node */
- background:rgba(0,0,0,0);
- transition: opacity 100ms linear;
- }
- .monaco-scrollable-element > .invisible {
- opacity: 0;
- pointer-events: none;
- }
- .monaco-scrollable-element > .invisible.fade {
- transition: opacity 800ms linear;
- }
- /* Scrollable Content Inset Shadow */
- .monaco-scrollable-element > .shadow {
- position: absolute;
- display: none;
- }
- .monaco-scrollable-element > .shadow.top {
- display: block;
- top: 0;
- left: 3px;
- height: 3px;
- width: 100%;
- }
- .monaco-scrollable-element > .shadow.left {
- display: block;
- top: 3px;
- left: 0;
- height: 100%;
- width: 3px;
- }
- .monaco-scrollable-element > .shadow.top-left-corner {
- display: block;
- top: 0;
- left: 0;
- height: 3px;
- width: 3px;
- }
- `;
- // Scrollbars
- const scrollbarShadowColor = style.scrollbarShadow;
- if (scrollbarShadowColor) {
- result += `
- .monaco-scrollable-element > .shadow.top {
- box-shadow: ${scrollbarShadowColor} 0 6px 6px -6px inset;
- }
- .monaco-scrollable-element > .shadow.left {
- box-shadow: ${scrollbarShadowColor} 6px 0 6px -6px inset;
- }
- .monaco-scrollable-element > .shadow.top.left {
- box-shadow: ${scrollbarShadowColor} 6px 6px 6px -6px inset;
- }
- `;
- }
- const scrollbarSliderBackgroundColor = style.scrollbarSliderBackground;
- if (scrollbarSliderBackgroundColor) {
- result += `
- .monaco-scrollable-element > .scrollbar > .slider {
- background: ${scrollbarSliderBackgroundColor};
- }
- `;
- }
- const scrollbarSliderHoverBackgroundColor = style.scrollbarSliderHoverBackground;
- if (scrollbarSliderHoverBackgroundColor) {
- result += `
- .monaco-scrollable-element > .scrollbar > .slider:hover {
- background: ${scrollbarSliderHoverBackgroundColor};
- }
- `;
- }
- const scrollbarSliderActiveBackgroundColor = style.scrollbarSliderActiveBackground;
- if (scrollbarSliderActiveBackgroundColor) {
- result += `
- .monaco-scrollable-element > .scrollbar > .slider.active {
- background: ${scrollbarSliderActiveBackgroundColor};
- }
- `;
- }
- }
- return result;
- }
|