UNPKG

@theia/core

Version:

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

221 lines • 9.53 kB
var TabBarToolbar_1; import { __decorate } from "tslib"; import { jsxs as _jsxs, jsx as _jsx } from "react/jsx-runtime"; // ***************************************************************************** // Copyright (C) 2018 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 { inject, injectable, postConstruct } from 'inversify'; import * as React from 'react'; import { buttonKeyboardProps, isActivationKey } from '../../keyboard/keyboard-utils.js'; import { ContextKeyService } from '../../context-key-service.js'; import { CommandRegistry, Disposable, DisposableCollection, nls } from '../../../common/index.js'; import { ContextMenuRenderer } from '../../context-menu-renderer.js'; import { LabelParser } from '../../label-parser.js'; import { codicon, ReactWidget } from '../../widgets/index.js'; import { TabBarToolbarRegistry } from './tab-bar-toolbar-registry.js'; import { TAB_BAR_TOOLBAR_CONTEXT_MENU, TabBarDelegator, TabBarToolbarAction } from './tab-bar-toolbar-types.js'; import { KeybindingRegistry } from '../../keybinding.js'; import { GroupImpl, MenuModelRegistry } from '../../../common/menu/index.js'; /** * Factory for instantiating tab-bar toolbars. */ export const TabBarToolbarFactory = Symbol('TabBarToolbarFactory'); export function toAnchor(event) { const itemBox = event.currentTarget.closest('.' + TabBarToolbar.Styles.TAB_BAR_TOOLBAR_ITEM)?.getBoundingClientRect(); return itemBox ? { y: itemBox.bottom, x: itemBox.left } : event.nativeEvent; } /** * Tab-bar toolbar widget representing the active [tab-bar toolbar items](TabBarToolbarItem). */ let TabBarToolbar = TabBarToolbar_1 = class TabBarToolbar extends ReactWidget { constructor() { super(); this.inline = new Map(); this.more = new Map(); this.toDisposeOnUpdateItems = new DisposableCollection(); this.toolbarContextKeys = new Set(); this.keybindingContextKeys = new Set(); this.toDisposeOnSetCurrent = new DisposableCollection(); this.showMoreContextMenu = (event) => { event.stopPropagation(); event.preventDefault(); const anchor = toAnchor(event); this.renderMoreContextMenu(anchor); }; this.handleMoreKeyDown = (event) => { if (isActivationKey(event)) { event.preventDefault(); event.stopPropagation(); const { left, bottom } = event.currentTarget.getBoundingClientRect(); this.renderMoreContextMenu({ x: left, y: bottom }); } }; this.addClass(TabBarToolbar_1.Styles.TAB_BAR_TOOLBAR); this.hide(); } init() { this.contextMatcher = this.contextKeyService; this.toDispose.pushAll([ this.keybindings.onKeybindingsChanged(() => this.maybeUpdate()), this.contextKeyService.onDidChange(e => { if (this.current && e.affects(this.toolbarContextKeys)) { this.updateTarget(this.current); } else if (e.affects(this.keybindingContextKeys)) { this.maybeUpdate(); } }), Disposable.create(() => { this.toDisposeOnUpdateItems.dispose(); this.toDisposeOnSetCurrent.dispose(); }) ]); } updateItems(items, current, contextKeys = new Set()) { this.toDisposeOnUpdateItems.dispose(); this.toDisposeOnUpdateItems = new DisposableCollection(); this.inline.clear(); this.more.clear(); this.toolbarContextKeys = contextKeys; for (const item of items.sort(TabBarToolbarAction.PRIORITY_COMPARATOR).reverse()) { if (!('toMenuNode' in item) || item.group === undefined || item.group === 'navigation') { this.inline.set(item.id, item); } else { this.more.set(item.id, item); } if (item.onDidChange) { this.toDisposeOnUpdateItems.push(item.onDidChange(() => this.maybeUpdate())); } } this.setCurrent(current); if (items.length) { this.show(); } else { this.hide(); } this.maybeUpdate(); } updateTarget(current) { const operativeWidget = TabBarDelegator.is(current) ? current.getTabBarDelegate() : current; const items = operativeWidget ? this.toolbarRegistry.visibleItems(operativeWidget) : []; const contextKeys = operativeWidget ? this.toolbarRegistry.collectContextKeys(operativeWidget) : new Set(); this.updateItems(items, operativeWidget, contextKeys); } setCurrent(current) { this.toDisposeOnSetCurrent.dispose(); this.toDispose.push(this.toDisposeOnSetCurrent); this.current = current; this.contextMatcher = current ? this.toolbarRegistry.contextMatcherFor(current) : this.contextKeyService; if (current) { const resetCurrent = () => { this.setCurrent(undefined); this.maybeUpdate(); }; current.disposed.connect(resetCurrent); this.toDisposeOnSetCurrent.push(Disposable.create(() => current.disposed.disconnect(resetCurrent))); } } render() { this.keybindingContextKeys.clear(); return _jsxs(React.Fragment, { children: [this.renderMore(), [...this.inline.values()].map(item => item.render(this.current, this.contextMatcher))] }); } renderMore() { return !!this.more.size && _jsx("div", { className: TabBarToolbar_1.Styles.TAB_BAR_TOOLBAR_ITEM + ' enabled', children: _jsx("div", { id: '__more__', className: codicon('ellipsis', true), ...buttonKeyboardProps(nls.localizeByDefault('More Actions...')), onClick: this.showMoreContextMenu, onKeyDown: this.handleMoreKeyDown, title: nls.localizeByDefault('More Actions...') }) }, '__more__'); } renderMoreContextMenu(anchor) { const toDisposeOnHide = new DisposableCollection(); this.addClass('menu-open'); toDisposeOnHide.push(Disposable.create(() => this.removeClass('menu-open'))); const menu = new GroupImpl('contextMenu'); for (const item of this.more.values()) { if (item.toMenuNode) { const node = item.toMenuNode(); if (node) { if (item.group) { menu.getOrCreate([item.group], 0, 1).addNode(node); } else { menu.addNode(node); } } } } return this.contextMenuRenderer.render({ menu: MenuModelRegistry.removeSingleRootNodes(menu), menuPath: TAB_BAR_TOOLBAR_CONTEXT_MENU, args: [this.current], anchor, context: this.current?.node || this.node, contextKeyService: this.contextMatcher, includeAnchorArg: false, onHide: () => toDisposeOnHide.dispose() }); } shouldHandleMouseEvent(event) { return event.target instanceof Element && this.node.contains(event.target); } commandIsEnabled(command) { return this.commands.isEnabled(command, this.current); } commandIsToggled(command) { return this.commands.isToggled(command, this.current); } evaluateWhenClause(whenClause) { return whenClause ? this.contextKeyService.match(whenClause, this.current?.node) : true; } maybeUpdate() { if (!this.isDisposed) { this.update(); } } }; __decorate([ inject(CommandRegistry) ], TabBarToolbar.prototype, "commands", void 0); __decorate([ inject(LabelParser) ], TabBarToolbar.prototype, "labelParser", void 0); __decorate([ inject(MenuModelRegistry) ], TabBarToolbar.prototype, "menus", void 0); __decorate([ inject(ContextMenuRenderer) ], TabBarToolbar.prototype, "contextMenuRenderer", void 0); __decorate([ inject(TabBarToolbarRegistry) ], TabBarToolbar.prototype, "toolbarRegistry", void 0); __decorate([ inject(ContextKeyService) ], TabBarToolbar.prototype, "contextKeyService", void 0); __decorate([ inject(KeybindingRegistry) ], TabBarToolbar.prototype, "keybindings", void 0); __decorate([ postConstruct() ], TabBarToolbar.prototype, "init", null); TabBarToolbar = TabBarToolbar_1 = __decorate([ injectable() ], TabBarToolbar); export { TabBarToolbar }; (function (TabBarToolbar) { let Styles; (function (Styles) { Styles.TAB_BAR_TOOLBAR = 'lm-TabBar-toolbar'; Styles.TAB_BAR_TOOLBAR_ITEM = 'item'; })(Styles = TabBarToolbar.Styles || (TabBarToolbar.Styles = {})); })(TabBarToolbar || (TabBarToolbar = {})); //# sourceMappingURL=tab-bar-toolbar.js.map