-
Notifications
You must be signed in to change notification settings - Fork 29.4k
/
multiEditorTabsControl.ts
2393 lines (1971 loc) · 93.5 KB
/
multiEditorTabsControl.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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*---------------------------------------------------------------------------------------------
* 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/multieditortabscontrol';
import { isMacintosh, isWindows } from 'vs/base/common/platform';
import { shorten } from 'vs/base/common/labels';
import { EditorResourceAccessor, Verbosity, IEditorPartOptions, SideBySideEditor, DEFAULT_EDITOR_ASSOCIATION, EditorInputCapabilities, IUntypedEditorInput, preventEditorClose, EditorCloseMethod, EditorsOrder, IToolbarActions } from 'vs/workbench/common/editor';
import { EditorInput } from 'vs/workbench/common/editor/editorInput';
import { computeEditorAriaLabel } from 'vs/workbench/browser/editor';
import { StandardKeyboardEvent } from 'vs/base/browser/keyboardEvent';
import { EventType as TouchEventType, GestureEvent, Gesture } from 'vs/base/browser/touch';
import { KeyCode } from 'vs/base/common/keyCodes';
import { ResourceLabels, IResourceLabel, DEFAULT_LABELS_CONTAINER } from 'vs/workbench/browser/labels';
import { ActionBar } from 'vs/base/browser/ui/actionbar/actionbar';
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 { IContextKeyService } from 'vs/platform/contextkey/common/contextkey';
import { MenuId } from 'vs/platform/actions/common/actions';
import { EditorCommandsContextActionRunner, EditorTabsControl } from 'vs/workbench/browser/parts/editor/editorTabsControl';
import { IQuickInputService } from 'vs/platform/quickinput/common/quickInput';
import { IDisposable, dispose, DisposableStore, combinedDisposable, MutableDisposable, toDisposable } from 'vs/base/common/lifecycle';
import { ScrollableElement } from 'vs/base/browser/ui/scrollbar/scrollableElement';
import { ScrollbarVisibility } from 'vs/base/common/scrollable';
import { getOrSet } from 'vs/base/common/map';
import { IThemeService, registerThemingParticipant } from 'vs/platform/theme/common/themeService';
import { TAB_INACTIVE_BACKGROUND, TAB_ACTIVE_BACKGROUND, TAB_ACTIVE_FOREGROUND, TAB_INACTIVE_FOREGROUND, TAB_BORDER, EDITOR_DRAG_AND_DROP_BACKGROUND, TAB_UNFOCUSED_ACTIVE_FOREGROUND, TAB_UNFOCUSED_INACTIVE_FOREGROUND, TAB_UNFOCUSED_ACTIVE_BACKGROUND, TAB_UNFOCUSED_ACTIVE_BORDER, TAB_ACTIVE_BORDER, TAB_HOVER_BACKGROUND, TAB_HOVER_BORDER, TAB_UNFOCUSED_HOVER_BACKGROUND, TAB_UNFOCUSED_HOVER_BORDER, EDITOR_GROUP_HEADER_TABS_BACKGROUND, WORKBENCH_BACKGROUND, TAB_ACTIVE_BORDER_TOP, TAB_UNFOCUSED_ACTIVE_BORDER_TOP, TAB_ACTIVE_MODIFIED_BORDER, TAB_INACTIVE_MODIFIED_BORDER, TAB_UNFOCUSED_ACTIVE_MODIFIED_BORDER, TAB_UNFOCUSED_INACTIVE_MODIFIED_BORDER, TAB_UNFOCUSED_INACTIVE_BACKGROUND, TAB_HOVER_FOREGROUND, TAB_UNFOCUSED_HOVER_FOREGROUND, EDITOR_GROUP_HEADER_TABS_BORDER, TAB_LAST_PINNED_BORDER } from 'vs/workbench/common/theme';
import { activeContrastBorder, contrastBorder, editorBackground } from 'vs/platform/theme/common/colorRegistry';
import { ResourcesDropHandler, DraggedEditorIdentifier, DraggedEditorGroupIdentifier, extractTreeDropData, isWindowDraggedOver } from 'vs/workbench/browser/dnd';
import { Color } from 'vs/base/common/color';
import { INotificationService } from 'vs/platform/notification/common/notification';
import { MergeGroupMode, IMergeGroupOptions } from 'vs/workbench/services/editor/common/editorGroupsService';
import { addDisposableListener, EventType, EventHelper, Dimension, scheduleAtNextAnimationFrame, findParentWithClass, clearNode, DragAndDropObserver, isMouseEvent, getWindow } from 'vs/base/browser/dom';
import { localize } from 'vs/nls';
import { IEditorGroupsView, EditorServiceImpl, IEditorGroupView, IInternalEditorOpenOptions, IEditorPartsView } from 'vs/workbench/browser/parts/editor/editor';
import { CloseOneEditorAction, UnpinEditorAction } from 'vs/workbench/browser/parts/editor/editorActions';
import { assertAllDefined, assertIsDefined } from 'vs/base/common/types';
import { IEditorService } from 'vs/workbench/services/editor/common/editorService';
import { basenameOrAuthority } from 'vs/base/common/resources';
import { RunOnceScheduler } from 'vs/base/common/async';
import { IPathService } from 'vs/workbench/services/path/common/pathService';
import { IPath, win32, posix } from 'vs/base/common/path';
import { coalesce, insert } from 'vs/base/common/arrays';
import { isHighContrast } from 'vs/platform/theme/common/theme';
import { isSafari } from 'vs/base/browser/browser';
import { equals } from 'vs/base/common/objects';
import { EditorActivation, IEditorOptions } from 'vs/platform/editor/common/editor';
import { UNLOCK_GROUP_COMMAND_ID } from 'vs/workbench/browser/parts/editor/editorCommands';
import { StandardMouseEvent } from 'vs/base/browser/mouseEvent';
import { ITreeViewsDnDService } from 'vs/editor/common/services/treeViewsDndService';
import { DraggedTreeItemsIdentifier } from 'vs/editor/common/services/treeViewsDnd';
import { IEditorResolverService } from 'vs/workbench/services/editor/common/editorResolverService';
import { IEditorTitleControlDimensions } from 'vs/workbench/browser/parts/editor/editorTitleControl';
import { StickyEditorGroupModel, UnstickyEditorGroupModel } from 'vs/workbench/common/editor/filteredEditorGroupModel';
import { IReadonlyEditorGroupModel } from 'vs/workbench/common/editor/editorGroupModel';
import { IHostService } from 'vs/workbench/services/host/browser/host';
interface IEditorInputLabel {
readonly editor: EditorInput;
readonly name?: string;
description?: string;
readonly forceDescription?: boolean;
readonly title?: string;
readonly ariaLabel?: string;
}
interface IMultiEditorTabsControlLayoutOptions {
/**
* Whether to force revealing the active tab, even when
* the dimensions have not changed. This can be the case
* when a tab was made active and needs to be revealed.
*/
readonly forceRevealActiveTab?: true;
}
interface IScheduledMultiEditorTabsControlLayout extends IDisposable {
/**
* Associated options with the layout call.
*/
options?: IMultiEditorTabsControlLayoutOptions;
}
export class MultiEditorTabsControl extends EditorTabsControl {
private static readonly SCROLLBAR_SIZES = {
default: 3 as const,
large: 10 as const
};
private static readonly TAB_WIDTH = {
compact: 38 as const,
shrink: 80 as const,
fit: 120 as const
};
private static readonly DRAG_OVER_OPEN_TAB_THRESHOLD = 1500;
private static readonly MOUSE_WHEEL_EVENT_THRESHOLD = 150;
private static readonly MOUSE_WHEEL_DISTANCE_THRESHOLD = 1.5;
private titleContainer: HTMLElement | undefined;
private tabsAndActionsContainer: HTMLElement | undefined;
private tabsContainer: HTMLElement | undefined;
private tabsScrollbar: ScrollableElement | undefined;
private tabSizingFixedDisposables: DisposableStore | undefined;
private readonly closeEditorAction = this._register(this.instantiationService.createInstance(CloseOneEditorAction, CloseOneEditorAction.ID, CloseOneEditorAction.LABEL));
private readonly unpinEditorAction = this._register(this.instantiationService.createInstance(UnpinEditorAction, UnpinEditorAction.ID, UnpinEditorAction.LABEL));
private readonly tabResourceLabels = this._register(this.instantiationService.createInstance(ResourceLabels, DEFAULT_LABELS_CONTAINER));
private tabLabels: IEditorInputLabel[] = [];
private activeTabLabel: IEditorInputLabel | undefined;
private tabActionBars: ActionBar[] = [];
private tabDisposables: IDisposable[] = [];
private dimensions: IEditorTitleControlDimensions & { used?: Dimension } = {
container: Dimension.None,
available: Dimension.None
};
private readonly layoutScheduler = this._register(new MutableDisposable<IScheduledMultiEditorTabsControlLayout>());
private blockRevealActiveTab: boolean | undefined;
private path: IPath = isWindows ? win32 : posix;
private lastMouseWheelEventTime = 0;
private isMouseOverTabs = false;
constructor(
parent: HTMLElement,
editorPartsView: IEditorPartsView,
groupsView: IEditorGroupsView,
groupView: IEditorGroupView,
tabsModel: IReadonlyEditorGroupModel,
@IContextMenuService contextMenuService: IContextMenuService,
@IInstantiationService instantiationService: IInstantiationService,
@IContextKeyService contextKeyService: IContextKeyService,
@IKeybindingService keybindingService: IKeybindingService,
@INotificationService notificationService: INotificationService,
@IQuickInputService quickInputService: IQuickInputService,
@IThemeService themeService: IThemeService,
@IEditorService private readonly editorService: EditorServiceImpl,
@IPathService private readonly pathService: IPathService,
@ITreeViewsDnDService private readonly treeViewsDragAndDropService: ITreeViewsDnDService,
@IEditorResolverService editorResolverService: IEditorResolverService,
@IHostService hostService: IHostService,
) {
super(parent, editorPartsView, groupsView, groupView, tabsModel, contextMenuService, instantiationService, contextKeyService, keybindingService, notificationService, quickInputService, themeService, editorResolverService, hostService);
// Resolve the correct path library for the OS we are on
// If we are connected to remote, this accounts for the
// remote OS.
(async () => this.path = await this.pathService.path)();
// React to decorations changing for our resource labels
this._register(this.tabResourceLabels.onDidChangeDecorations(() => this.doHandleDecorationsChange()));
}
protected override create(parent: HTMLElement): void {
super.create(parent);
this.titleContainer = parent;
// Tabs and Actions Container (are on a single row with flex side-by-side)
this.tabsAndActionsContainer = document.createElement('div');
this.tabsAndActionsContainer.classList.add('tabs-and-actions-container');
this.titleContainer.appendChild(this.tabsAndActionsContainer);
// Tabs Container
this.tabsContainer = document.createElement('div');
this.tabsContainer.setAttribute('role', 'tablist');
this.tabsContainer.draggable = true;
this.tabsContainer.classList.add('tabs-container');
this._register(Gesture.addTarget(this.tabsContainer));
this.tabSizingFixedDisposables = this._register(new DisposableStore());
this.updateTabSizing(false);
// Tabs Scrollbar
this.tabsScrollbar = this.createTabsScrollbar(this.tabsContainer);
this.tabsAndActionsContainer.appendChild(this.tabsScrollbar.getDomNode());
// Tabs Container listeners
this.registerTabsContainerListeners(this.tabsContainer, this.tabsScrollbar);
// Create Editor Toolbar
this.createEditorActionsToolBar(this.tabsAndActionsContainer, ['editor-actions']);
// Set tabs control visibility
this.updateTabsControlVisibility();
}
private createTabsScrollbar(scrollable: HTMLElement): ScrollableElement {
const tabsScrollbar = this._register(new ScrollableElement(scrollable, {
horizontal: ScrollbarVisibility.Auto,
horizontalScrollbarSize: this.getTabsScrollbarSizing(),
vertical: ScrollbarVisibility.Hidden,
scrollYToX: true,
useShadows: false
}));
this._register(tabsScrollbar.onScroll(e => {
if (e.scrollLeftChanged) {
scrollable.scrollLeft = e.scrollLeft;
}
}));
return tabsScrollbar;
}
private updateTabsScrollbarSizing(): void {
this.tabsScrollbar?.updateOptions({
horizontalScrollbarSize: this.getTabsScrollbarSizing()
});
}
private updateTabSizing(fromEvent: boolean): void {
const [tabsContainer, tabSizingFixedDisposables] = assertAllDefined(this.tabsContainer, this.tabSizingFixedDisposables);
tabSizingFixedDisposables.clear();
const options = this.groupsView.partOptions;
if (options.tabSizing === 'fixed') {
tabsContainer.style.setProperty('--tab-sizing-fixed-min-width', `${options.tabSizingFixedMinWidth}px`);
tabsContainer.style.setProperty('--tab-sizing-fixed-max-width', `${options.tabSizingFixedMaxWidth}px`);
// For https://github.com/microsoft/vscode/issues/40290 we want to
// preserve the current tab widths as long as the mouse is over the
// tabs so that you can quickly close them via mouse click. For that
// we track mouse movements over the tabs container.
tabSizingFixedDisposables.add(addDisposableListener(tabsContainer, EventType.MOUSE_ENTER, () => {
this.isMouseOverTabs = true;
}));
tabSizingFixedDisposables.add(addDisposableListener(tabsContainer, EventType.MOUSE_LEAVE, () => {
this.isMouseOverTabs = false;
this.updateTabsFixedWidth(false);
}));
} else if (fromEvent) {
tabsContainer.style.removeProperty('--tab-sizing-fixed-min-width');
tabsContainer.style.removeProperty('--tab-sizing-fixed-max-width');
this.updateTabsFixedWidth(false);
}
}
private updateTabsFixedWidth(fixed: boolean): void {
this.forEachTab((editor, tabIndex, tabContainer) => {
if (fixed) {
const { width } = tabContainer.getBoundingClientRect();
tabContainer.style.setProperty('--tab-sizing-current-width', `${width}px`);
} else {
tabContainer.style.removeProperty('--tab-sizing-current-width');
}
});
}
private getTabsScrollbarSizing(): number {
if (this.groupsView.partOptions.titleScrollbarSizing !== 'large') {
return MultiEditorTabsControl.SCROLLBAR_SIZES.default;
}
return MultiEditorTabsControl.SCROLLBAR_SIZES.large;
}
private registerTabsContainerListeners(tabsContainer: HTMLElement, tabsScrollbar: ScrollableElement): void {
// Forward scrolling inside the container to our custom scrollbar
this._register(addDisposableListener(tabsContainer, EventType.SCROLL, () => {
if (tabsContainer.classList.contains('scroll')) {
tabsScrollbar.setScrollPosition({
scrollLeft: tabsContainer.scrollLeft // during DND the container gets scrolled so we need to update the custom scrollbar
});
}
}));
// New file when double-clicking on tabs container (but not tabs)
for (const eventType of [TouchEventType.Tap, EventType.DBLCLICK]) {
this._register(addDisposableListener(tabsContainer, eventType, (e: MouseEvent | GestureEvent) => {
if (eventType === EventType.DBLCLICK) {
if (e.target !== tabsContainer) {
return; // ignore if target is not tabs container
}
} else {
if ((<GestureEvent>e).tapCount !== 2) {
return; // ignore single taps
}
if ((<GestureEvent>e).initialTarget !== tabsContainer) {
return; // ignore if target is not tabs container
}
}
EventHelper.stop(e);
this.editorService.openEditor({
resource: undefined,
options: {
pinned: true,
index: this.groupView.count, // always at the end
override: DEFAULT_EDITOR_ASSOCIATION.id
}
}, this.groupView.id);
}));
}
// Prevent auto-scrolling (https://github.com/microsoft/vscode/issues/16690)
this._register(addDisposableListener(tabsContainer, EventType.MOUSE_DOWN, e => {
if (e.button === 1) {
e.preventDefault();
}
}));
// Drag & Drop support
let lastDragEvent: DragEvent | undefined = undefined;
let isNewWindowOperation = false;
this._register(new DragAndDropObserver(tabsContainer, {
onDragStart: e => {
isNewWindowOperation = this.onGroupDragStart(e, tabsContainer);
},
onDrag: e => {
lastDragEvent = e;
},
onDragEnter: e => {
// Always enable support to scroll while dragging
tabsContainer.classList.add('scroll');
// Return if the target is not on the tabs container
if (e.target !== tabsContainer) {
return;
}
// Return if transfer is unsupported
if (!this.isSupportedDropTransfer(e)) {
if (e.dataTransfer) {
e.dataTransfer.dropEffect = 'none';
}
return;
}
// Update the dropEffect to "copy" if there is no local data to be dragged because
// in that case we can only copy the data into and not move it from its source
if (!this.editorTransfer.hasData(DraggedEditorIdentifier.prototype)) {
if (e.dataTransfer) {
e.dataTransfer.dropEffect = 'copy';
}
}
this.updateDropFeedback(tabsContainer, true, e);
},
onDragLeave: e => {
this.updateDropFeedback(tabsContainer, false, e);
tabsContainer.classList.remove('scroll');
},
onDragEnd: e => {
this.updateDropFeedback(tabsContainer, false, e);
tabsContainer.classList.remove('scroll');
this.onGroupDragEnd(e, lastDragEvent, tabsContainer, isNewWindowOperation);
},
onDrop: e => {
this.updateDropFeedback(tabsContainer, false, e);
tabsContainer.classList.remove('scroll');
if (e.target === tabsContainer) {
const isGroupTransfer = this.groupTransfer.hasData(DraggedEditorGroupIdentifier.prototype);
this.onDrop(e, isGroupTransfer ? this.groupView.count : this.tabsModel.count, tabsContainer);
}
}
}));
// Mouse-wheel support to switch to tabs optionally
this._register(addDisposableListener(tabsContainer, EventType.MOUSE_WHEEL, (e: WheelEvent) => {
const activeEditor = this.groupView.activeEditor;
if (!activeEditor || this.groupView.count < 2) {
return; // need at least 2 open editors
}
// Shift-key enables or disables this behaviour depending on the setting
if (this.groupsView.partOptions.scrollToSwitchTabs === true) {
if (e.shiftKey) {
return; // 'on': only enable this when Shift-key is not pressed
}
} else {
if (!e.shiftKey) {
return; // 'off': only enable this when Shift-key is pressed
}
}
// Ignore event if the last one happened too recently (https://github.com/microsoft/vscode/issues/96409)
// The restriction is relaxed according to the absolute value of `deltaX` and `deltaY`
// to support discrete (mouse wheel) and contiguous scrolling (touchpad) equally well
const now = Date.now();
if (now - this.lastMouseWheelEventTime < MultiEditorTabsControl.MOUSE_WHEEL_EVENT_THRESHOLD - 2 * (Math.abs(e.deltaX) + Math.abs(e.deltaY))) {
return;
}
this.lastMouseWheelEventTime = now;
// Figure out scrolling direction but ignore it if too subtle
let tabSwitchDirection: number;
if (e.deltaX + e.deltaY < - MultiEditorTabsControl.MOUSE_WHEEL_DISTANCE_THRESHOLD) {
tabSwitchDirection = -1;
} else if (e.deltaX + e.deltaY > MultiEditorTabsControl.MOUSE_WHEEL_DISTANCE_THRESHOLD) {
tabSwitchDirection = 1;
} else {
return;
}
const nextEditor = this.groupView.getEditorByIndex(this.groupView.getIndexOfEditor(activeEditor) + tabSwitchDirection);
if (!nextEditor) {
return;
}
// Open it
this.groupView.openEditor(nextEditor);
// Disable normal scrolling, opening the editor will already reveal it properly
EventHelper.stop(e, true);
}));
// Context menu
const showContextMenu = (e: Event) => {
EventHelper.stop(e);
// Find target anchor
let anchor: HTMLElement | StandardMouseEvent = tabsContainer;
if (isMouseEvent(e)) {
anchor = new StandardMouseEvent(getWindow(this.parent), e);
}
// Show it
this.contextMenuService.showContextMenu({
getAnchor: () => anchor,
menuId: MenuId.EditorTabsBarContext,
contextKeyService: this.contextKeyService,
menuActionOptions: { shouldForwardArgs: true },
getActionsContext: () => ({ groupId: this.groupView.id }),
getKeyBinding: action => this.getKeybinding(action),
onHide: () => this.groupView.focus()
});
};
this._register(addDisposableListener(tabsContainer, TouchEventType.Contextmenu, e => showContextMenu(e)));
this._register(addDisposableListener(tabsContainer, EventType.CONTEXT_MENU, e => showContextMenu(e)));
}
private doHandleDecorationsChange(): void {
// A change to decorations potentially has an impact on the size of tabs
// so we need to trigger a layout in that case to adjust things
this.layout(this.dimensions);
}
protected override updateEditorActionsToolbar(): void {
super.updateEditorActionsToolbar();
// Changing the actions in the toolbar can have an impact on the size of the
// tab container, so we need to layout the tabs to make sure the active is visible
this.layout(this.dimensions);
}
openEditor(editor: EditorInput, options?: IInternalEditorOpenOptions): boolean {
const changed = this.handleOpenedEditors();
// Respect option to focus tab control if provided
if (options?.focusTabControl) {
this.withTab(editor, (editor, tabIndex, tabContainer) => tabContainer.focus());
}
return changed;
}
openEditors(editors: EditorInput[]): boolean {
return this.handleOpenedEditors();
}
private handleOpenedEditors(): boolean {
// Set tabs control visibility
this.updateTabsControlVisibility();
// Create tabs as needed
const [tabsContainer, tabsScrollbar] = assertAllDefined(this.tabsContainer, this.tabsScrollbar);
for (let i = tabsContainer.children.length; i < this.tabsModel.count; i++) {
tabsContainer.appendChild(this.createTab(i, tabsContainer, tabsScrollbar));
}
// Make sure to recompute tab labels and detect
// if a label change occurred that requires a
// redraw of tabs.
const activeEditorChanged = this.didActiveEditorChange();
const oldActiveTabLabel = this.activeTabLabel;
const oldTabLabelsLength = this.tabLabels.length;
this.computeTabLabels();
// Redraw and update in these cases
let didChange = false;
if (
activeEditorChanged || // active editor changed
oldTabLabelsLength !== this.tabLabels.length || // number of tabs changed
!this.equalsEditorInputLabel(oldActiveTabLabel, this.activeTabLabel) // active editor label changed
) {
this.redraw({ forceRevealActiveTab: true });
didChange = true;
}
// Otherwise only layout for revealing
else {
this.layout(this.dimensions, { forceRevealActiveTab: true });
}
return didChange;
}
private didActiveEditorChange(): boolean {
if (
!this.activeTabLabel?.editor && this.tabsModel.activeEditor || // active editor changed from null => editor
this.activeTabLabel?.editor && !this.tabsModel.activeEditor || // active editor changed from editor => null
(!this.activeTabLabel?.editor || !this.tabsModel.isActive(this.activeTabLabel.editor)) // active editor changed from editorA => editorB
) {
return true;
}
return false;
}
private equalsEditorInputLabel(labelA: IEditorInputLabel | undefined, labelB: IEditorInputLabel | undefined): boolean {
if (labelA === labelB) {
return true;
}
if (!labelA || !labelB) {
return false;
}
return labelA.name === labelB.name &&
labelA.description === labelB.description &&
labelA.forceDescription === labelB.forceDescription &&
labelA.title === labelB.title &&
labelA.ariaLabel === labelB.ariaLabel;
}
beforeCloseEditor(editor: EditorInput): void {
// Fix tabs width if the mouse is over tabs and before closing
// a tab (except the last tab) when tab sizing is 'fixed'.
// This helps keeping the close button stable under
// the mouse and allows for rapid closing of tabs.
if (this.isMouseOverTabs && this.groupsView.partOptions.tabSizing === 'fixed') {
const closingLastTab = this.tabsModel.isLast(editor);
this.updateTabsFixedWidth(!closingLastTab);
}
}
closeEditor(editor: EditorInput): void {
this.handleClosedEditors();
}
closeEditors(editors: EditorInput[]): void {
this.handleClosedEditors();
}
private handleClosedEditors(): void {
// There are tabs to show
if (this.tabsModel.count) {
// Remove tabs that got closed
const tabsContainer = assertIsDefined(this.tabsContainer);
while (tabsContainer.children.length > this.tabsModel.count) {
// Remove one tab from container (must be the last to keep indexes in order!)
tabsContainer.lastChild?.remove();
// Remove associated tab label and widget
dispose(this.tabDisposables.pop());
}
// A removal of a label requires to recompute all labels
this.computeTabLabels();
// Redraw all tabs
this.redraw({ forceRevealActiveTab: true });
}
// No tabs to show
else {
if (this.tabsContainer) {
clearNode(this.tabsContainer);
}
this.tabDisposables = dispose(this.tabDisposables);
this.tabResourceLabels.clear();
this.tabLabels = [];
this.activeTabLabel = undefined;
this.tabActionBars = [];
this.clearEditorActionsToolbar();
this.updateTabsControlVisibility();
}
}
moveEditor(editor: EditorInput, fromTabIndex: number, targeTabIndex: number): void {
// Move the editor label
const editorLabel = this.tabLabels[fromTabIndex];
this.tabLabels.splice(fromTabIndex, 1);
this.tabLabels.splice(targeTabIndex, 0, editorLabel);
// Redraw tabs in the range of the move
this.forEachTab((editor, tabIndex, tabContainer, tabLabelWidget, tabLabel, tabActionBar) => {
this.redrawTab(editor, tabIndex, tabContainer, tabLabelWidget, tabLabel, tabActionBar);
},
Math.min(fromTabIndex, targeTabIndex), // from: smallest of fromTabIndex/targeTabIndex
Math.max(fromTabIndex, targeTabIndex) // to: largest of fromTabIndex/targeTabIndex
);
// Moving an editor requires a layout to keep the active editor visible
this.layout(this.dimensions, { forceRevealActiveTab: true });
}
pinEditor(editor: EditorInput): void {
this.withTab(editor, (editor, tabIndex, tabContainer, tabLabelWidget, tabLabel) => this.redrawTabLabel(editor, tabIndex, tabContainer, tabLabelWidget, tabLabel));
}
stickEditor(editor: EditorInput): void {
this.doHandleStickyEditorChange(editor);
}
unstickEditor(editor: EditorInput): void {
this.doHandleStickyEditorChange(editor);
}
private doHandleStickyEditorChange(editor: EditorInput): void {
// Update tab
this.withTab(editor, (editor, tabIndex, tabContainer, tabLabelWidget, tabLabel, tabActionBar) => this.redrawTab(editor, tabIndex, tabContainer, tabLabelWidget, tabLabel, tabActionBar));
// Sticky change has an impact on each tab's border because
// it potentially moves the border to the last pinned tab
this.forEachTab((editor, tabIndex, tabContainer, tabLabelWidget, tabLabel) => {
this.redrawTabBorders(tabIndex, tabContainer);
});
// A change to the sticky state requires a layout to keep the active editor visible
this.layout(this.dimensions, { forceRevealActiveTab: true });
}
setActive(isGroupActive: boolean): void {
// Activity has an impact on each tab's active indication
this.forEachTab((editor, tabIndex, tabContainer, tabLabelWidget, tabLabel, tabActionBar) => {
this.redrawTabActiveAndDirty(isGroupActive, editor, tabContainer, tabActionBar);
});
// Activity has an impact on the toolbar, so we need to update and layout
this.updateEditorActionsToolbar();
this.layout(this.dimensions, { forceRevealActiveTab: true });
}
private updateEditorLabelScheduler = this._register(new RunOnceScheduler(() => this.doUpdateEditorLabels(), 0));
updateEditorLabel(editor: EditorInput): void {
// Update all labels to account for changes to tab labels
// Since this method may be called a lot of times from
// individual editors, we collect all those requests and
// then run the update once because we have to update
// all opened tabs in the group at once.
this.updateEditorLabelScheduler.schedule();
}
private doUpdateEditorLabels(): void {
// A change to a label requires to recompute all labels
this.computeTabLabels();
// As such we need to redraw each label
this.forEachTab((editor, tabIndex, tabContainer, tabLabelWidget, tabLabel) => {
this.redrawTabLabel(editor, tabIndex, tabContainer, tabLabelWidget, tabLabel);
});
// A change to a label requires a layout to keep the active editor visible
this.layout(this.dimensions);
}
updateEditorDirty(editor: EditorInput): void {
this.withTab(editor, (editor, tabIndex, tabContainer, tabLabelWidget, tabLabel, tabActionBar) => this.redrawTabActiveAndDirty(this.groupsView.activeGroup === this.groupView, editor, tabContainer, tabActionBar));
}
override updateOptions(oldOptions: IEditorPartOptions, newOptions: IEditorPartOptions): void {
super.updateOptions(oldOptions, newOptions);
// A change to a label format options requires to recompute all labels
if (oldOptions.labelFormat !== newOptions.labelFormat) {
this.computeTabLabels();
}
// Update tabs scrollbar sizing
if (oldOptions.titleScrollbarSizing !== newOptions.titleScrollbarSizing) {
this.updateTabsScrollbarSizing();
}
// Update tabs sizing
if (
oldOptions.tabSizingFixedMinWidth !== newOptions.tabSizingFixedMinWidth ||
oldOptions.tabSizingFixedMaxWidth !== newOptions.tabSizingFixedMaxWidth ||
oldOptions.tabSizing !== newOptions.tabSizing
) {
this.updateTabSizing(true);
}
// Redraw tabs when other options change
if (
oldOptions.labelFormat !== newOptions.labelFormat ||
oldOptions.tabActionLocation !== newOptions.tabActionLocation ||
oldOptions.tabActionCloseVisibility !== newOptions.tabActionCloseVisibility ||
oldOptions.tabActionUnpinVisibility !== newOptions.tabActionUnpinVisibility ||
oldOptions.tabSizing !== newOptions.tabSizing ||
oldOptions.pinnedTabSizing !== newOptions.pinnedTabSizing ||
oldOptions.showIcons !== newOptions.showIcons ||
oldOptions.hasIcons !== newOptions.hasIcons ||
oldOptions.highlightModifiedTabs !== newOptions.highlightModifiedTabs ||
oldOptions.wrapTabs !== newOptions.wrapTabs ||
!equals(oldOptions.decorations, newOptions.decorations)
) {
this.redraw();
}
}
override updateStyles(): void {
this.redraw();
}
private forEachTab(fn: (editor: EditorInput, tabIndex: number, tabContainer: HTMLElement, tabLabelWidget: IResourceLabel, tabLabel: IEditorInputLabel, tabActionBar: ActionBar) => void, fromTabIndex?: number, toTabIndex?: number): void {
this.tabsModel.getEditors(EditorsOrder.SEQUENTIAL).forEach((editor: EditorInput, tabIndex: number) => {
if (typeof fromTabIndex === 'number' && fromTabIndex > tabIndex) {
return; // do nothing if we are not yet at `fromIndex`
}
if (typeof toTabIndex === 'number' && toTabIndex < tabIndex) {
return; // do nothing if we are beyond `toIndex`
}
this.doWithTab(tabIndex, editor, fn);
});
}
private withTab(editor: EditorInput, fn: (editor: EditorInput, tabIndex: number, tabContainer: HTMLElement, tabLabelWidget: IResourceLabel, tabLabel: IEditorInputLabel, tabActionBar: ActionBar) => void): void {
this.doWithTab(this.tabsModel.indexOf(editor), editor, fn);
}
private doWithTab(tabIndex: number, editor: EditorInput, fn: (editor: EditorInput, tabIndex: number, tabContainer: HTMLElement, tabLabelWidget: IResourceLabel, tabLabel: IEditorInputLabel, tabActionBar: ActionBar) => void): void {
const tabsContainer = assertIsDefined(this.tabsContainer);
const tabContainer = tabsContainer.children[tabIndex] as HTMLElement;
const tabResourceLabel = this.tabResourceLabels.get(tabIndex);
const tabLabel = this.tabLabels[tabIndex];
const tabActionBar = this.tabActionBars[tabIndex];
if (tabContainer && tabResourceLabel && tabLabel) {
fn(editor, tabIndex, tabContainer, tabResourceLabel, tabLabel, tabActionBar);
}
}
private createTab(tabIndex: number, tabsContainer: HTMLElement, tabsScrollbar: ScrollableElement): HTMLElement {
// Tab Container
const tabContainer = document.createElement('div');
tabContainer.draggable = true;
tabContainer.setAttribute('role', 'tab');
tabContainer.classList.add('tab');
// Gesture Support
this._register(Gesture.addTarget(tabContainer));
// Tab Border Top
const tabBorderTopContainer = document.createElement('div');
tabBorderTopContainer.classList.add('tab-border-top-container');
tabContainer.appendChild(tabBorderTopContainer);
// Tab Editor Label
const editorLabel = this.tabResourceLabels.create(tabContainer, { hoverDelegate: this.getHoverDelegate() });
// Tab Actions
const tabActionsContainer = document.createElement('div');
tabActionsContainer.classList.add('tab-actions');
tabContainer.appendChild(tabActionsContainer);
const that = this;
const tabActionRunner = new EditorCommandsContextActionRunner({
groupId: this.groupView.id,
get editorIndex() { return that.toEditorIndex(tabIndex); }
});
const tabActionBar = new ActionBar(tabActionsContainer, { ariaLabel: localize('ariaLabelTabActions', "Tab actions"), actionRunner: tabActionRunner });
const tabActionListener = tabActionBar.onWillRun(e => {
if (e.action.id === this.closeEditorAction.id) {
this.blockRevealActiveTabOnce();
}
});
const tabActionBarDisposable = combinedDisposable(tabActionBar, tabActionListener, toDisposable(insert(this.tabActionBars, tabActionBar)));
// Tab Border Bottom
const tabBorderBottomContainer = document.createElement('div');
tabBorderBottomContainer.classList.add('tab-border-bottom-container');
tabContainer.appendChild(tabBorderBottomContainer);
// Eventing
const eventsDisposable = this.registerTabListeners(tabContainer, tabIndex, tabsContainer, tabsScrollbar);
this.tabDisposables.push(combinedDisposable(eventsDisposable, tabActionBarDisposable, tabActionRunner, editorLabel));
return tabContainer;
}
private toEditorIndex(tabIndex: number): number {
// Given a `tabIndex` that is relative to the tabs model
// returns the `editorIndex` relative to the entire group
const editor = assertIsDefined(this.tabsModel.getEditorByIndex(tabIndex));
return this.groupView.getIndexOfEditor(editor);
}
private registerTabListeners(tab: HTMLElement, tabIndex: number, tabsContainer: HTMLElement, tabsScrollbar: ScrollableElement): IDisposable {
const disposables = new DisposableStore();
const handleClickOrTouch = (e: MouseEvent | GestureEvent, preserveFocus: boolean): void => {
tab.blur(); // prevent flicker of focus outline on tab until editor got focus
if (isMouseEvent(e) && (e.button !== 0 /* middle/right mouse button */ || (isMacintosh && e.ctrlKey /* macOS context menu */))) {
if (e.button === 1) {
e.preventDefault(); // required to prevent auto-scrolling (https://github.com/microsoft/vscode/issues/16690)
}
return undefined;
}
if (this.originatesFromTabActionBar(e)) {
return; // not when clicking on actions
}
// Open tabs editor
const editor = this.tabsModel.getEditorByIndex(tabIndex);
if (editor) {
// Even if focus is preserved make sure to activate the group.
this.groupView.openEditor(editor, { preserveFocus, activation: EditorActivation.ACTIVATE });
}
return undefined;
};
const showContextMenu = (e: Event) => {
EventHelper.stop(e);
const editor = this.tabsModel.getEditorByIndex(tabIndex);
if (editor) {
this.onTabContextMenu(editor, e, tab);
}
};
// Open on Click / Touch
disposables.add(addDisposableListener(tab, EventType.MOUSE_DOWN, e => handleClickOrTouch(e, false)));
disposables.add(addDisposableListener(tab, TouchEventType.Tap, (e: GestureEvent) => handleClickOrTouch(e, true))); // Preserve focus on touch #125470
// Touch Scroll Support
disposables.add(addDisposableListener(tab, TouchEventType.Change, (e: GestureEvent) => {
tabsScrollbar.setScrollPosition({ scrollLeft: tabsScrollbar.getScrollPosition().scrollLeft - e.translationX });
}));
// Prevent flicker of focus outline on tab until editor got focus
disposables.add(addDisposableListener(tab, EventType.MOUSE_UP, e => {
EventHelper.stop(e);
tab.blur();
}));
// Close on mouse middle click
disposables.add(addDisposableListener(tab, EventType.AUXCLICK, e => {
if (e.button === 1 /* Middle Button*/) {
EventHelper.stop(e, true /* for https://github.com/microsoft/vscode/issues/56715 */);
const editor = this.tabsModel.getEditorByIndex(tabIndex);
if (editor) {
if (preventEditorClose(this.tabsModel, editor, EditorCloseMethod.MOUSE, this.groupsView.partOptions)) {
return;
}
this.blockRevealActiveTabOnce();
this.closeEditorAction.run({ groupId: this.groupView.id, editorIndex: this.groupView.getIndexOfEditor(editor) });
}
}
}));
// Context menu on Shift+F10
disposables.add(addDisposableListener(tab, EventType.KEY_DOWN, e => {
const event = new StandardKeyboardEvent(e);
if (event.shiftKey && event.keyCode === KeyCode.F10) {
showContextMenu(e);
}
}));
// Context menu on touch context menu gesture
disposables.add(addDisposableListener(tab, TouchEventType.Contextmenu, (e: GestureEvent) => {
showContextMenu(e);
}));
// Keyboard accessibility
disposables.add(addDisposableListener(tab, EventType.KEY_UP, e => {
const event = new StandardKeyboardEvent(e);
let handled = false;
// Run action on Enter/Space
if (event.equals(KeyCode.Enter) || event.equals(KeyCode.Space)) {
handled = true;
const editor = this.tabsModel.getEditorByIndex(tabIndex);
if (editor) {
this.groupView.openEditor(editor);
}
}
// Navigate in editors
else if ([KeyCode.LeftArrow, KeyCode.RightArrow, KeyCode.UpArrow, KeyCode.DownArrow, KeyCode.Home, KeyCode.End].some(kb => event.equals(kb))) {
let editorIndex = this.toEditorIndex(tabIndex);
if (event.equals(KeyCode.LeftArrow) || event.equals(KeyCode.UpArrow)) {
editorIndex = editorIndex - 1;
} else if (event.equals(KeyCode.RightArrow) || event.equals(KeyCode.DownArrow)) {
editorIndex = editorIndex + 1;
} else if (event.equals(KeyCode.Home)) {
editorIndex = 0;
} else {
editorIndex = this.groupView.count - 1;
}
const target = this.groupView.getEditorByIndex(editorIndex);
if (target) {
handled = true;
this.groupView.openEditor(target, { preserveFocus: true }, { focusTabControl: true });
}
}
if (handled) {
EventHelper.stop(e, true);
}
// moving in the tabs container can have an impact on scrolling position, so we need to update the custom scrollbar
tabsScrollbar.setScrollPosition({
scrollLeft: tabsContainer.scrollLeft
});
}));
// Double click: either pin or toggle maximized
for (const eventType of [TouchEventType.Tap, EventType.DBLCLICK]) {
disposables.add(addDisposableListener(tab, eventType, (e: MouseEvent | GestureEvent) => {
if (eventType === EventType.DBLCLICK) {
EventHelper.stop(e);
} else if ((<GestureEvent>e).tapCount !== 2) {
return; // ignore single taps
}
const editor = this.tabsModel.getEditorByIndex(tabIndex);
if (editor && this.tabsModel.isPinned(editor)) {
switch (this.groupsView.partOptions.doubleClickTabToToggleEditorGroupSizes) {
case 'maximize':
this.groupsView.toggleMaximizeGroup(this.groupView);
break;
case 'expand':
this.groupsView.toggleExpandGroup(this.groupView);
break;
case 'off':
break;
}
} else {
this.groupView.pinEditor(editor);
}
}));
}
// Context menu
disposables.add(addDisposableListener(tab, EventType.CONTEXT_MENU, e => {
EventHelper.stop(e, true);
const editor = this.tabsModel.getEditorByIndex(tabIndex);