Path: blob/main/extensions/copilot/test/simulation/fixtures/gen/commandCenterControl.ts
13399 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import { isActiveDocument, reset } from 'vs/base/browser/dom';6import { BaseActionViewItem, IBaseActionViewItemOptions } from 'vs/base/browser/ui/actionbar/actionViewItems';7import { getDefaultHoverDelegate } from 'vs/base/browser/ui/hover/hoverDelegateFactory';8import { IHoverDelegate } from 'vs/base/browser/ui/hover/hoverDelegate';9import { renderIcon } from 'vs/base/browser/ui/iconLabel/iconLabels';10import { IAction, SubmenuAction } from 'vs/base/common/actions';11import { Codicon } from 'vs/base/common/codicons';12import { Emitter, Event } from 'vs/base/common/event';13import { DisposableStore } from 'vs/base/common/lifecycle';14import { localize } from 'vs/nls';15import { createActionViewItem } from 'vs/platform/actions/browser/menuEntryActionViewItem';16import { HiddenItemStrategy, MenuWorkbenchToolBar, WorkbenchToolBar } from 'vs/platform/actions/browser/toolbar';17import { MenuId, MenuRegistry, SubmenuItemAction } from 'vs/platform/actions/common/actions';18import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';19import { IKeybindingService } from 'vs/platform/keybinding/common/keybinding';20import { IQuickInputService } from 'vs/platform/quickinput/common/quickInput';21import { WindowTitle } from 'vs/workbench/browser/parts/titlebar/windowTitle';22import { IEditorGroupsService } from 'vs/workbench/services/editor/common/editorGroupsService';23import { IHoverService } from 'vs/platform/hover/browser/hover';2425export class CommandCenterControl {2627private readonly _disposables = new DisposableStore();2829private readonly _onDidChangeVisibility = new Emitter<void>();30readonly onDidChangeVisibility: Event<void> = this._onDidChangeVisibility.event;3132readonly element: HTMLElement = document.createElement('div');3334constructor(35windowTitle: WindowTitle,36hoverDelegate: IHoverDelegate,37@IInstantiationService instantiationService: IInstantiationService,38@IQuickInputService quickInputService: IQuickInputService,39) {40this.element.classList.add('command-center');4142const titleToolbar = instantiationService.createInstance(MenuWorkbenchToolBar, this.element, MenuId.CommandCenter, {43contextMenu: MenuId.TitleBarContext,44hiddenItemStrategy: HiddenItemStrategy.NoHide,45toolbarOptions: {46primaryGroup: () => true,47},48telemetrySource: 'commandCenter',49actionViewItemProvider: (action, options) => {50if (action instanceof SubmenuItemAction && action.item.submenu === MenuId.CommandCenterCenter) {51return instantiationService.createInstance(CommandCenterCenterViewItem, action, windowTitle, { ...options, hoverDelegate });52} else {53return createActionViewItem(instantiationService, action, { ...options, hoverDelegate });54}55}56});5758this._disposables.add(Event.filter(quickInputService.onShow, () => isActiveDocument(this.element), this._disposables)(this._setVisibility.bind(this, false)));59this._disposables.add(Event.filter(quickInputService.onHide, () => isActiveDocument(this.element), this._disposables)(this._setVisibility.bind(this, true)));60this._disposables.add(titleToolbar);61}6263private _setVisibility(show: boolean): void {64this.element.classList.toggle('hide', !show);65this._onDidChangeVisibility.fire();66}6768dispose(): void {69this._disposables.dispose();70}71}727374class CommandCenterCenterViewItem extends BaseActionViewItem {7576private static readonly _quickOpenCommandId = 'workbench.action.quickOpenWithModes';7778private readonly _hoverDelegate: IHoverDelegate;7980constructor(81private readonly _submenu: SubmenuItemAction,82private readonly _windowTitle: WindowTitle,83options: IBaseActionViewItemOptions,84@IHoverService private readonly _hoverService: IHoverService,85@IKeybindingService private _keybindingService: IKeybindingService,86@IInstantiationService private _instaService: IInstantiationService,87@IEditorGroupsService private _editorGroupService: IEditorGroupsService,88) {89super(undefined, _submenu.actions.find(action => action.id === 'workbench.action.quickOpenWithModes') ?? _submenu.actions[0], options);90this._hoverDelegate = options.hoverDelegate ?? getDefaultHoverDelegate('mouse');91}9293override render(container: HTMLElement): void {94super.render(container);95container.classList.add('command-center-center');96container.classList.toggle('multiple', (this._submenu.actions.length > 1));9798const hover = this._store.add(this._hoverService.setupUpdatableHover(this._hoverDelegate, container, this.getTooltip()));99100// update label & tooltip when window title changes101this._store.add(this._windowTitle.onDidChange(() => {102hover.update(this.getTooltip());103}));104105const groups: (readonly IAction[])[] = [];106for (const action of this._submenu.actions) {107if (action instanceof SubmenuAction) {108groups.push(action.actions);109} else {110groups.push([action]);111}112}113114115for (let i = 0; i < groups.length; i++) {116const group = groups[i];117118// nested toolbar119const toolbar = this._instaService.createInstance(WorkbenchToolBar, container, {120hiddenItemStrategy: HiddenItemStrategy.NoHide,121telemetrySource: 'commandCenterCenter',122actionViewItemProvider: (action, options) => {123options = {124...options,125hoverDelegate: this._hoverDelegate,126};127128if (action.id !== CommandCenterCenterViewItem._quickOpenCommandId) {129return createActionViewItem(this._instaService, action, options);130}131132const that = this;133134return this._instaService.createInstance(class CommandCenterQuickPickItem extends BaseActionViewItem {135136constructor() {137super(undefined, action, options);138}139140override render(container: HTMLElement): void {141super.render(container);142container.classList.toggle('command-center-quick-pick');143144const action = this.action;145146// icon (search)147const searchIcon = document.createElement('span');148searchIcon.ariaHidden = 'true';149searchIcon.className = action.class ?? '';150searchIcon.classList.add('search-icon');151152// label: just workspace name and optional decorations153const label = this._getLabel();154const labelElement = document.createElement('span');155labelElement.classList.add('search-label');156labelElement.innerText = label;157reset(container, searchIcon, labelElement);158159const hover = this._store.add(that._hoverService.setupUpdatableHover(that._hoverDelegate, container, this.getTooltip()));160161// update label & tooltip when window title changes162this._store.add(that._windowTitle.onDidChange(() => {163hover.update(this.getTooltip());164labelElement.innerText = this._getLabel();165}));166167// update label & tooltip when tabs visibility changes168this._store.add(that._editorGroupService.onDidChangeEditorPartOptions(({ newPartOptions, oldPartOptions }) => {169if (newPartOptions.showTabs !== oldPartOptions.showTabs) {170hover.update(this.getTooltip());171labelElement.innerText = this._getLabel();172}173}));174}175176protected override getTooltip() {177return that.getTooltip();178}179180private _getLabel(): string {181const { prefix, suffix } = that._windowTitle.getTitleDecorations();182let label = that._windowTitle.workspaceName;183if (that._windowTitle.isCustomTitleFormat()) {184label = that._windowTitle.getWindowTitle();185} else if (that._editorGroupService.partOptions.showTabs === 'none') {186label = that._windowTitle.fileName ?? label;187}188189if (!label) {190label = localize('label.dfl', "Search");191}192if (prefix) {193label = localize('label1', "{0} {1}", prefix, label);194}195if (suffix) {196label = localize('label2', "{0} {1}", label, suffix);197}198199return label;200}201});202}203});204toolbar.setActions(group);205this._store.add(toolbar);206207208// spacer209if (i < groups.length - 1) {210const icon = renderIcon(Codicon.circleSmallFilled);211icon.style.padding = '0 12px';212icon.style.height = '100%';213icon.style.opacity = '0.5';214container.appendChild(icon);215}216}217}218219protected override getTooltip() {220221// tooltip: full windowTitle222const kb = this._keybindingService.lookupKeybinding(this.action.id)?.getLabel();223const title = kb224? localize('title', "Search {0} ({1}) \u2014 {2}", this._windowTitle.workspaceName, kb, this._windowTitle.value)225: localize('title2', "Search {0} \u2014 {1}", this._windowTitle.workspaceName, this._windowTitle.value);226227return title;228}229}230231MenuRegistry.appendMenuItem(MenuId.CommandCenter, {232submenu: MenuId.CommandCenterCenter,233title: localize('title3', "Command Center"),234icon: Codicon.shield,235order: 101,236});237238239