UNPKG

@theia/core

Version:

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

336 lines • 13.8 kB
// ***************************************************************************** // 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 { find, toArray } from '@lumino/algorithm'; import { DockPanel } from '@lumino/widgets'; import { Signal } from '@lumino/signaling'; import { Disposable, DisposableCollection } from '../../common/disposable.js'; import { Emitter, environment } from '../../common/index.js'; import { ToolbarAwareTabBar } from './tab-bars.js'; import { TabDropHelper } from './tab-drop-helper.js'; export const ACTIVE_TABBAR_CLASS = 'theia-tabBar-active'; export const MAIN_AREA_ID = 'theia-main-content-panel'; export const BOTTOM_AREA_ID = 'theia-bottom-content-panel'; /** * This specialization of DockPanel adds various events that are used for implementing the * side panels of the application shell. */ export class TheiaDockPanel extends DockPanel { get onDidChangeCurrent() { return this.onDidChangeCurrentEmitter.event; } constructor(options, preferences, maximizeCallback) { super(options); this.preferences = preferences; this.maximizeCallback = maximizeCallback; /** * Emitted when a widget is added to the panel. */ this.widgetAdded = new Signal(this); /** * Emitted when a widget is activated by calling `activateWidget`. */ this.widgetActivated = new Signal(this); /** * Emitted when a widget is removed from the panel. */ this.widgetRemoved = new Signal(this); this.onDidChangeCurrentEmitter = new Emitter(); this.disableDND = false; this.tabWithDNDDisabledStyling = undefined; this.tabDropHelper = new TabDropHelper({ before: 'theia-mod-drop-before', after: 'theia-mod-drop-after' }); this.toDisposeOnMarkAsCurrent = new DisposableCollection(); this.disableDND = TheiaDockPanel.isTheiaDockPanelIOptions(options) && options.disableDragAndDrop; if (TheiaDockPanel.isTheiaDockPanelIOptions(options)) { this.canDropWidget = options.canDropWidget; this.onWillDropWidget = options.onWillDropWidget; } this['_onCurrentChanged'] = (sender, args) => { this.markAsCurrent(args.currentTitle || undefined); super['_onCurrentChanged'](sender, args); }; this['_createTabBar'] = () => { // necessary for https://github.com/eclipse-theia/theia/issues/15273 const tabBar = super['_createTabBar'](); if (tabBar instanceof ToolbarAwareTabBar) { tabBar.setDockPanel(this); if (!this.disableDND) { tabBar.enableNativeTabDrag(); } } if (this.disableDND) { tabBar['tabDetachRequested'].disconnect(this['_onTabDetachRequested'], this); tabBar['tabDetachRequested'].connect(this.onTabDetachRequestedWithDisabledDND, this); // eslint-disable-next-line @typescript-eslint/no-explicit-any, no-null/no-null let dragDataValue = null; Object.defineProperty(tabBar, '_dragData', { get: () => dragDataValue, // eslint-disable-next-line @typescript-eslint/no-explicit-any set: (value) => { dragDataValue = value; // eslint-disable-next-line no-null/no-null if (value === null) { this.onNullTabDragDataWithDisabledDND(); } }, configurable: true }); } return tabBar; }; this['_onTabActivateRequested'] = (sender, args) => { this.markAsCurrent(args.title); super['_onTabActivateRequested'](sender, args); }; this['_onTabCloseRequested'] = (sender, args) => { if (TheiaDockPanel.isTheiaDockPanelIOptions(options) && options.closeHandler !== undefined) { if (options.closeHandler(sender, args)) { return; } } super['_onTabCloseRequested'](sender, args); }; } onTabDetachRequestedWithDisabledDND(sender, args) { // don't process the detach request at all. We still want to support other drag starts, e.g. tab reorder // provide visual feedback that DnD is disabled by adding not-allowed class const tab = sender.contentNode.children[args.index]; if (tab) { tab.classList.add('theia-drag-not-allowed'); this.tabWithDNDDisabledStyling = tab; } } onNullTabDragDataWithDisabledDND() { if (this.tabWithDNDDisabledStyling) { this.tabWithDNDDisabledStyling.classList.remove('theia-drag-not-allowed'); this.tabWithDNDDisabledStyling = undefined; } } handleEvent(event) { switch (event.type) { case 'lm-dragenter': case 'lm-dragleave': case 'lm-dragover': case 'lm-drop': if (this.disableDND) { return; } if ((event.type === 'lm-dragenter' || event.type === 'lm-drop') && !this.isDropAllowed(event)) { // not accepting the drag enter keeps this panel from becoming the drop target return; } if (event.type === 'lm-drop') { this.notifyWillDrop(event); } if (this.handleTabBarDragEvent(event)) { return; } break; } super.handleEvent(event); } isDropAllowed(event) { const widget = this.canDropWidget && this.getDraggedWidget(event); return !widget || this.canDropWidget(widget); } notifyWillDrop(event) { const widget = this.onWillDropWidget && this.getDraggedWidget(event); if (widget) { this.onWillDropWidget(widget); } } /** * Handles dragging a widget over the tab bars of this panel: the widget is inserted as a tab where it is dropped, * instead of after the current tab of the tab bar as Lumino does. * * @returns `true` if the event has been handled */ handleTabBarDragEvent(event) { if (event.type !== 'lm-dragover' && event.type !== 'lm-drop') { if (event.type === 'lm-dragleave' && !(event.relatedTarget && this.node.contains(event.relatedTarget))) { this.tabDropHelper.hideInsertionMarker(); } return false; } const tabBar = this.findTabBarAt(event.clientX, event.clientY); const widget = tabBar && this.getDraggedWidget(event); if (!tabBar || !widget) { this.tabDropHelper.hideInsertionMarker(); return false; } event.preventDefault(); event.stopPropagation(); this.overlay.hide(0); const index = this.tabDropHelper.findInsertionIndex(tabBar, event.clientX, event.clientY); if (event.type === 'lm-dragover') { this.tabDropHelper.showInsertionMarker(tabBar, index); } else { this.tabDropHelper.hideInsertionMarker(); this.insertTab(tabBar, index, widget); } event.dropAction = event.proposedAction; return true; } getDraggedWidget(event) { if (this['_tabsConstrained'] && event.source !== this) { return undefined; } const widget = this.tabDropHelper.getDraggedWidget(event); return widget && !widget.contains(this) ? widget : undefined; } findTabBarAt(clientX, clientY) { return find(this.tabBars(), tabBar => { const barRect = tabBar.node.getBoundingClientRect(); const contentRect = tabBar.contentNode.getBoundingClientRect(); return clientX >= barRect.left && clientX < barRect.right && clientY >= contentRect.top && clientY < contentRect.bottom; }); } insertTab(tabBar, index, widget) { const titles = tabBar.titles; const currentIndex = titles.indexOf(widget.title); if (currentIndex !== -1) { const toIndex = index > currentIndex ? index - 1 : index; if (toIndex !== currentIndex) { tabBar.insertTab(toIndex, widget.title); // `insertTab` does not emit the signal that Lumino emits when a tab is moved with the pointer tabBar.tabMoved.emit({ fromIndex: currentIndex, toIndex, title: widget.title }); } } else if (index < titles.length) { this.addWidget(widget, { mode: 'tab-before', ref: titles[index].owner }); } else if (titles.length > 0) { this.addWidget(widget, { mode: 'tab-after', ref: titles[titles.length - 1].owner }); } else { this.addWidget(widget); } this.activateWidget(widget); } toggleMaximized() { if (this.maximizeCallback) { this.maximizeCallback(this); } } isElectron() { return environment.electron.is(); } get currentTitle() { return this._currentTitle; } get currentTabBar() { return this._currentTitle && this.findTabBar(this._currentTitle); } findTabBar(title) { return find(this.tabBars(), bar => bar.titles.includes(title)); } markAsCurrent(title) { this.toDisposeOnMarkAsCurrent.dispose(); this._currentTitle = title; this.markActiveTabBar(title); if (title) { const resetCurrent = () => this.markAsCurrent(undefined); title.owner.disposed.connect(resetCurrent); this.toDisposeOnMarkAsCurrent.push(Disposable.create(() => title.owner.disposed.disconnect(resetCurrent))); } this.onDidChangeCurrentEmitter.fire(title); } markActiveTabBar(title) { const tabBars = toArray(this.tabBars()); tabBars.forEach(tabBar => tabBar.removeClass(ACTIVE_TABBAR_CLASS)); const activeTabBar = title && this.findTabBar(title); if (activeTabBar) { activeTabBar.addClass(ACTIVE_TABBAR_CLASS); } else if (tabBars.length > 0) { // At least one tabbar needs to be active tabBars[0].addClass(ACTIVE_TABBAR_CLASS); } } addWidget(widget, options) { if (this.mode === 'single-document' && widget.parent === this) { return; } super.addWidget(widget, options); if (options?.closeRef) { options.ref?.close(); } this.widgetAdded.emit(widget); this.markActiveTabBar(widget.title); } activateWidget(widget) { super.activateWidget(widget); this.widgetActivated.emit(widget); this.markActiveTabBar(widget.title); } onChildRemoved(msg) { super.onChildRemoved(msg); this.widgetRemoved.emit(msg.child); } nextTabBarWidget(widget) { const current = this.findTabBar(widget.title); const next = current && this.nextTabBarInPanel(current); return next && next.currentTitle && next.currentTitle.owner || undefined; } nextTabBarInPanel(tabBar) { const tabBars = toArray(this.tabBars()); const index = tabBars.indexOf(tabBar); if (index !== -1) { return tabBars[index + 1]; } return undefined; } previousTabBarWidget(widget) { const current = this.findTabBar(widget.title); const previous = current && this.previousTabBarInPanel(current); return previous && previous.currentTitle && previous.currentTitle.owner || undefined; } previousTabBarInPanel(tabBar) { const tabBars = toArray(this.tabBars()); const index = tabBars.indexOf(tabBar); if (index !== -1) { return tabBars[index - 1]; } return undefined; } dispose() { super.dispose(); this.onDidChangeCurrentEmitter.dispose(); this._currentTitle = undefined; this.toDisposeOnMarkAsCurrent.dispose(); } } (function (TheiaDockPanel) { TheiaDockPanel.Factory = Symbol('TheiaDockPanel#Factory'); function isTheiaDockPanelIOptions(options) { if (options === undefined) { return false; } if ('disableDragAndDrop' in options) { if (options.disableDragAndDrop !== undefined && typeof options.disableDragAndDrop !== 'boolean') { return false; } } if ('closeHandler' in options) { if (options.closeHandler !== undefined && typeof options.closeHandler !== 'function') { return false; } } return true; } TheiaDockPanel.isTheiaDockPanelIOptions = isTheiaDockPanelIOptions; })(TheiaDockPanel || (TheiaDockPanel = {})); //# sourceMappingURL=theia-dock-panel.js.map