-
Notifications
You must be signed in to change notification settings - Fork 30.1k
/
Copy pathtitleControl.ts
415 lines (325 loc) · 16.4 KB
/
titleControl.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import 'vs/css!./media/titlecontrol';
import { localize } from 'vs/nls';
import { applyDragImage, DataTransfers } from 'vs/base/browser/dnd';
import { addDisposableListener, Dimension, EventType } from 'vs/base/browser/dom';
import { StandardMouseEvent } from 'vs/base/browser/mouseEvent';
import { ActionsOrientation, IActionViewItem, prepareActions } from 'vs/base/browser/ui/actionbar/actionbar';
import { ToolBar } from 'vs/base/browser/ui/toolbar/toolbar';
import { IAction, WorkbenchActionExecutedEvent, WorkbenchActionExecutedClassification, SubmenuAction, ActionRunner } from 'vs/base/common/actions';
import { ResolvedKeybinding } from 'vs/base/common/keyCodes';
import { dispose, DisposableStore } from 'vs/base/common/lifecycle';
import { createActionViewItem, createAndFillInActionBarActions, createAndFillInContextMenuActions } from 'vs/platform/actions/browser/menuEntryActionViewItem';
import { IMenu, IMenuService, MenuId } from 'vs/platform/actions/common/actions';
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
import { IContextKeyService, IContextKey } from 'vs/platform/contextkey/common/contextkey';
import { IContextMenuService } from 'vs/platform/contextview/browser/contextView';
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
import { IKeybindingService } from 'vs/platform/keybinding/common/keybinding';
import { INotificationService } from 'vs/platform/notification/common/notification';
import { IQuickInputService } from 'vs/platform/quickinput/common/quickInput';
import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
import { listActiveSelectionBackground, listActiveSelectionForeground } from 'vs/platform/theme/common/colorRegistry';
import { IThemeService, registerThemingParticipant, Themable } from 'vs/platform/theme/common/themeService';
import { DraggedEditorGroupIdentifier, DraggedEditorIdentifier, fillEditorsDragData, LocalSelectionTransfer } from 'vs/workbench/browser/dnd';
import { EditorPane } from 'vs/workbench/browser/parts/editor/editorPane';
import { BreadcrumbsConfig } from 'vs/workbench/browser/parts/editor/breadcrumbs';
import { BreadcrumbsControl, IBreadcrumbsControlOptions } from 'vs/workbench/browser/parts/editor/breadcrumbsControl';
import { IEditorGroupsAccessor, IEditorGroupTitleHeight, IEditorGroupView } from 'vs/workbench/browser/parts/editor/editor';
import { IEditorCommandsContext, IEditorInput, EditorResourceAccessor, IEditorPartOptions, SideBySideEditor, ActiveEditorPinnedContext, ActiveEditorStickyContext, EditorsOrder } from 'vs/workbench/common/editor';
import { ResourceContextKey } from 'vs/workbench/common/resources';
import { AnchorAlignment } from 'vs/base/browser/ui/contextview/contextview';
import { IFileService } from 'vs/platform/files/common/files';
import { withNullAsUndefined, withUndefinedAsNull, assertIsDefined } from 'vs/base/common/types';
import { isFirefox } from 'vs/base/browser/browser';
import { isPromiseCanceledError } from 'vs/base/common/errors';
export interface IToolbarActions {
primary: IAction[];
secondary: IAction[];
}
export interface ITitleControlDimensions {
/**
* The size of the parent container the title control is layed out in.
*/
container: Dimension;
/**
* The maximum size the title control is allowed to consume based on
* other controls that are positioned inside the container.
*/
available: Dimension;
}
export class EditorCommandsContextActionRunner extends ActionRunner {
constructor(
private context: IEditorCommandsContext
) {
super();
}
override run(action: IAction): Promise<void> {
return super.run(action, this.context);
}
}
export abstract class TitleControl extends Themable {
protected readonly groupTransfer = LocalSelectionTransfer.getInstance<DraggedEditorGroupIdentifier>();
protected readonly editorTransfer = LocalSelectionTransfer.getInstance<DraggedEditorIdentifier>();
protected breadcrumbsControl: BreadcrumbsControl | undefined = undefined;
private editorActionsToolbar: ToolBar | undefined;
private resourceContext: ResourceContextKey;
private editorPinnedContext: IContextKey<boolean>;
private editorStickyContext: IContextKey<boolean>;
private readonly editorToolBarMenuDisposables = this._register(new DisposableStore());
private contextMenu: IMenu;
constructor(
parent: HTMLElement,
protected accessor: IEditorGroupsAccessor,
protected group: IEditorGroupView,
@IContextMenuService private readonly contextMenuService: IContextMenuService,
@IInstantiationService protected instantiationService: IInstantiationService,
@IContextKeyService private readonly contextKeyService: IContextKeyService,
@IKeybindingService private readonly keybindingService: IKeybindingService,
@ITelemetryService private readonly telemetryService: ITelemetryService,
@INotificationService private readonly notificationService: INotificationService,
@IMenuService private readonly menuService: IMenuService,
@IQuickInputService protected quickInputService: IQuickInputService,
@IThemeService themeService: IThemeService,
@IConfigurationService protected configurationService: IConfigurationService,
@IFileService private readonly fileService: IFileService
) {
super(themeService);
this.resourceContext = this._register(instantiationService.createInstance(ResourceContextKey));
this.editorPinnedContext = ActiveEditorPinnedContext.bindTo(contextKeyService);
this.editorStickyContext = ActiveEditorStickyContext.bindTo(contextKeyService);
this.contextMenu = this._register(this.menuService.createMenu(MenuId.EditorTitleContext, this.contextKeyService));
this.create(parent);
}
protected abstract create(parent: HTMLElement): void;
protected createBreadcrumbsControl(container: HTMLElement, options: IBreadcrumbsControlOptions): void {
const config = this._register(BreadcrumbsConfig.IsEnabled.bindTo(this.configurationService));
this._register(config.onDidChange(() => {
const value = config.getValue();
if (!value && this.breadcrumbsControl) {
this.breadcrumbsControl.dispose();
this.breadcrumbsControl = undefined;
this.handleBreadcrumbsEnablementChange();
} else if (value && !this.breadcrumbsControl) {
this.breadcrumbsControl = this.instantiationService.createInstance(BreadcrumbsControl, container, options, this.group);
this.breadcrumbsControl.update();
this.handleBreadcrumbsEnablementChange();
}
}));
if (config.getValue()) {
this.breadcrumbsControl = this.instantiationService.createInstance(BreadcrumbsControl, container, options, this.group);
}
this._register(this.fileService.onDidChangeFileSystemProviderRegistrations(() => {
if (this.breadcrumbsControl?.update()) {
this.handleBreadcrumbsEnablementChange();
}
}));
}
protected abstract handleBreadcrumbsEnablementChange(): void;
protected createEditorActionsToolBar(container: HTMLElement): void {
const context: IEditorCommandsContext = { groupId: this.group.id };
this.editorActionsToolbar = this._register(new ToolBar(container, this.contextMenuService, {
actionViewItemProvider: action => this.actionViewItemProvider(action),
orientation: ActionsOrientation.HORIZONTAL,
ariaLabel: localize('ariaLabelEditorActions', "Editor actions"),
getKeyBinding: action => this.getKeybinding(action),
actionRunner: this._register(new EditorCommandsContextActionRunner(context)),
anchorAlignmentProvider: () => AnchorAlignment.RIGHT
}));
// Context
this.editorActionsToolbar.context = context;
// Action Run Handling
this._register(this.editorActionsToolbar.actionRunner.onDidRun(e => {
// Notify for Error
if (e.error && !isPromiseCanceledError(e.error)) {
this.notificationService.error(e.error);
}
// Log in telemetry
this.telemetryService.publicLog2<WorkbenchActionExecutedEvent, WorkbenchActionExecutedClassification>('workbenchActionExecuted', { id: e.action.id, from: 'editorPart' });
}));
}
private actionViewItemProvider(action: IAction): IActionViewItem | undefined {
const activeEditorPane = this.group.activeEditorPane;
// Check Active Editor
if (activeEditorPane instanceof EditorPane) {
const result = activeEditorPane.getActionViewItem(action);
if (result) {
return result;
}
}
// Check extensions
return createActionViewItem(this.instantiationService, action);
}
protected updateEditorActionsToolbar(): void {
const { primaryEditorActions, secondaryEditorActions } = this.prepareEditorActions(this.getEditorActions());
const editorActionsToolbar = assertIsDefined(this.editorActionsToolbar);
editorActionsToolbar.setActions(primaryEditorActions, secondaryEditorActions);
}
protected prepareEditorActions(editorActions: IToolbarActions): { primaryEditorActions: IAction[]; secondaryEditorActions: IAction[]; } {
let primaryEditorActions: IAction[];
let secondaryEditorActions: IAction[];
// Primary actions only for the active group
if (this.accessor.activeGroup === this.group) {
primaryEditorActions = prepareActions(editorActions.primary);
} else {
primaryEditorActions = [];
}
// Secondary actions for all groups
secondaryEditorActions = prepareActions(editorActions.secondary);
return { primaryEditorActions, secondaryEditorActions };
}
private getEditorActions(): IToolbarActions {
const primary: IAction[] = [];
const secondary: IAction[] = [];
// Dispose previous listeners
this.editorToolBarMenuDisposables.clear();
// Update contexts
this.contextKeyService.bufferChangeEvents(() => {
this.resourceContext.set(withUndefinedAsNull(EditorResourceAccessor.getOriginalUri(this.group.activeEditor, { supportSideBySide: SideBySideEditor.PRIMARY })));
this.editorPinnedContext.set(this.group.activeEditor ? this.group.isPinned(this.group.activeEditor) : false);
this.editorStickyContext.set(this.group.activeEditor ? this.group.isSticky(this.group.activeEditor) : false);
});
// Editor actions require the editor control to be there, so we retrieve it via service
const activeEditorPane = this.group.activeEditorPane;
if (activeEditorPane instanceof EditorPane) {
const scopedContextKeyService = activeEditorPane.scopedContextKeyService ?? this.contextKeyService;
const titleBarMenu = this.menuService.createMenu(MenuId.EditorTitle, scopedContextKeyService, true);
this.editorToolBarMenuDisposables.add(titleBarMenu);
this.editorToolBarMenuDisposables.add(titleBarMenu.onDidChange(() => {
this.updateEditorActionsToolbar(); // Update editor toolbar whenever contributed actions change
}));
const shouldInlineGroup = (action: SubmenuAction, group: string) => group === 'navigation' && action.actions.length <= 1;
this.editorToolBarMenuDisposables.add(createAndFillInActionBarActions(
titleBarMenu, { arg: this.resourceContext.get(), shouldForwardArgs: true }, { primary, secondary },
'navigation', 9, shouldInlineGroup
));
}
return { primary, secondary };
}
protected clearEditorActionsToolbar(): void {
this.editorActionsToolbar?.setActions([], []);
}
protected enableGroupDragging(element: HTMLElement): void {
// Drag start
this._register(addDisposableListener(element, EventType.DRAG_START, e => {
if (e.target !== element) {
return; // only if originating from tabs container
}
// Set editor group as transfer
this.groupTransfer.setData([new DraggedEditorGroupIdentifier(this.group.id)], DraggedEditorGroupIdentifier.prototype);
if (e.dataTransfer) {
e.dataTransfer.effectAllowed = 'copyMove';
}
// Drag all tabs of the group if tabs are enabled
let hasDataTransfer = false;
if (this.accessor.partOptions.showTabs) {
hasDataTransfer = this.doFillResourceDataTransfers(this.group.getEditors(EditorsOrder.SEQUENTIAL), e);
}
// Otherwise only drag the active editor
else {
if (this.group.activeEditor) {
hasDataTransfer = this.doFillResourceDataTransfers([this.group.activeEditor], e);
}
}
// Firefox: requires to set a text data transfer to get going
if (!hasDataTransfer && isFirefox) {
e.dataTransfer?.setData(DataTransfers.TEXT, String(this.group.label));
}
// Drag Image
if (this.group.activeEditor) {
let label = this.group.activeEditor.getName();
if (this.accessor.partOptions.showTabs && this.group.count > 1) {
label = localize('draggedEditorGroup', "{0} (+{1})", label, this.group.count - 1);
}
applyDragImage(e, label, 'monaco-editor-group-drag-image');
}
}));
// Drag end
this._register(addDisposableListener(element, EventType.DRAG_END, () => {
this.groupTransfer.clearData(DraggedEditorGroupIdentifier.prototype);
}));
}
protected doFillResourceDataTransfers(editors: readonly IEditorInput[], e: DragEvent): boolean {
if (editors.length) {
this.instantiationService.invokeFunction(fillEditorsDragData, editors.map(editor => ({ editor, groupId: this.group.id })), e);
return true;
}
return false;
}
protected onContextMenu(editor: IEditorInput, e: Event, node: HTMLElement): void {
// Update contexts based on editor picked and remember previous to restore
const currentResourceContext = this.resourceContext.get();
this.resourceContext.set(withUndefinedAsNull(EditorResourceAccessor.getOriginalUri(editor, { supportSideBySide: SideBySideEditor.PRIMARY })));
const currentPinnedContext = !!this.editorPinnedContext.get();
this.editorPinnedContext.set(this.group.isPinned(editor));
const currentStickyContext = !!this.editorStickyContext.get();
this.editorStickyContext.set(this.group.isSticky(editor));
// Find target anchor
let anchor: HTMLElement | { x: number, y: number } = node;
if (e instanceof MouseEvent) {
const event = new StandardMouseEvent(e);
anchor = { x: event.posx, y: event.posy };
}
// Fill in contributed actions
const actions: IAction[] = [];
const actionsDisposable = createAndFillInContextMenuActions(this.contextMenu, { shouldForwardArgs: true, arg: this.resourceContext.get() }, actions);
// Show it
this.contextMenuService.showContextMenu({
getAnchor: () => anchor,
getActions: () => actions,
getActionsContext: () => ({ groupId: this.group.id, editorIndex: this.group.getIndexOfEditor(editor) }),
getKeyBinding: action => this.getKeybinding(action),
onHide: () => {
// restore previous contexts
this.resourceContext.set(currentResourceContext || null);
this.editorPinnedContext.set(currentPinnedContext);
this.editorStickyContext.set(currentStickyContext);
// restore focus to active group
this.accessor.activeGroup.focus();
// Cleanup
dispose(actionsDisposable);
}
});
}
private getKeybinding(action: IAction): ResolvedKeybinding | undefined {
return this.keybindingService.lookupKeybinding(action.id);
}
protected getKeybindingLabel(action: IAction): string | undefined {
const keybinding = this.getKeybinding(action);
return keybinding ? withNullAsUndefined(keybinding.getLabel()) : undefined;
}
abstract openEditor(editor: IEditorInput): void;
abstract closeEditor(editor: IEditorInput): void;
abstract closeEditors(editors: IEditorInput[]): void;
abstract moveEditor(editor: IEditorInput, fromIndex: number, targetIndex: number): void;
abstract pinEditor(editor: IEditorInput): void;
abstract stickEditor(editor: IEditorInput): void;
abstract unstickEditor(editor: IEditorInput): void;
abstract setActive(isActive: boolean): void;
abstract updateEditorLabel(editor: IEditorInput): void;
abstract updateEditorCapabilities(editor: IEditorInput): void;
abstract updateEditorLabels(): void;
abstract updateEditorDirty(editor: IEditorInput): void;
abstract updateOptions(oldOptions: IEditorPartOptions, newOptions: IEditorPartOptions): void;
abstract layout(dimensions: ITitleControlDimensions): Dimension;
abstract getHeight(): IEditorGroupTitleHeight;
override dispose(): void {
dispose(this.breadcrumbsControl);
this.breadcrumbsControl = undefined;
super.dispose();
}
}
registerThemingParticipant((theme, collector) => {
// Drag Feedback
const dragImageBackground = theme.getColor(listActiveSelectionBackground);
const dragImageForeground = theme.getColor(listActiveSelectionForeground);
collector.addRule(`
.monaco-editor-group-drag-image {
background: ${dragImageBackground};
color: ${dragImageForeground};
}
`);
});