-
Notifications
You must be signed in to change notification settings - Fork 28k
/
quickInputTree.ts
1677 lines (1478 loc) · 52.9 KB
/
quickInputTree.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 * as dom from 'vs/base/browser/dom';
import { Emitter, Event, IValueWithChangeEvent } from 'vs/base/common/event';
import { IHoverDelegate } from 'vs/base/browser/ui/hover/hoverDelegate';
import { IListVirtualDelegate } from 'vs/base/browser/ui/list/list';
import { IObjectTreeElement, ITreeEvent, ITreeNode, ITreeRenderer } from 'vs/base/browser/ui/tree/tree';
import { localize } from 'vs/nls';
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
import { WorkbenchObjectTree } from 'vs/platform/list/browser/listService';
import { IThemeService } from 'vs/platform/theme/common/themeService';
import { Disposable, DisposableStore } from 'vs/base/common/lifecycle';
import { IQuickPickItem, IQuickPickItemButtonEvent, IQuickPickSeparator, IQuickPickSeparatorButtonEvent, QuickPickItem } from 'vs/platform/quickinput/common/quickInput';
import { IMarkdownString } from 'vs/base/common/htmlContent';
import { IMatch } from 'vs/base/common/filters';
import { IListAccessibilityProvider, IListStyles } from 'vs/base/browser/ui/list/listWidget';
import { AriaRole } from 'vs/base/browser/ui/aria/aria';
import { StandardKeyboardEvent } from 'vs/base/browser/keyboardEvent';
import { KeyCode } from 'vs/base/common/keyCodes';
import { OS } from 'vs/base/common/platform';
import { memoize } from 'vs/base/common/decorators';
import { IIconLabelValueOptions, IconLabel } from 'vs/base/browser/ui/iconLabel/iconLabel';
import { KeybindingLabel } from 'vs/base/browser/ui/keybindingLabel/keybindingLabel';
import { ActionBar } from 'vs/base/browser/ui/actionbar/actionbar';
import { isDark } from 'vs/platform/theme/common/theme';
import { URI } from 'vs/base/common/uri';
import { quickInputButtonToAction } from 'vs/platform/quickinput/browser/quickInputUtils';
import { Lazy } from 'vs/base/common/lazy';
import { IParsedLabelWithIcons, getCodiconAriaLabel, matchesFuzzyIconAware, parseLabelWithIcons } from 'vs/base/common/iconLabels';
import { HoverPosition } from 'vs/base/browser/ui/hover/hoverWidget';
import { compareAnything } from 'vs/base/common/comparers';
import { ltrim } from 'vs/base/common/strings';
import { RenderIndentGuides } from 'vs/base/browser/ui/tree/abstractTree';
import { ThrottledDelayer } from 'vs/base/common/async';
import { isCancellationError } from 'vs/base/common/errors';
import type { IHoverWidget, IUpdatableHoverTooltipMarkdownString } from 'vs/base/browser/ui/hover/hover';
import { QuickPickFocus } from '../common/quickInput';
const $ = dom.$;
interface IQuickInputItemLazyParts {
readonly saneLabel: string;
readonly saneSortLabel: string;
readonly saneAriaLabel: string;
}
interface IQuickPickElement extends IQuickInputItemLazyParts {
readonly hasCheckbox: boolean;
readonly index: number;
readonly item?: IQuickPickItem;
readonly saneDescription?: string;
readonly saneDetail?: string;
readonly saneTooltip?: string | IMarkdownString | HTMLElement;
hidden: boolean;
element?: HTMLElement;
labelHighlights?: IMatch[];
descriptionHighlights?: IMatch[];
detailHighlights?: IMatch[];
separator?: IQuickPickSeparator;
}
interface IQuickInputItemTemplateData {
entry: HTMLDivElement;
checkbox: HTMLInputElement;
icon: HTMLDivElement;
label: IconLabel;
keybinding: KeybindingLabel;
detail: IconLabel;
separator: HTMLDivElement;
actionBar: ActionBar;
element: IQuickPickElement;
toDisposeElement: DisposableStore;
toDisposeTemplate: DisposableStore;
}
class BaseQuickPickItemElement implements IQuickPickElement {
private readonly _init: Lazy<IQuickInputItemLazyParts>;
constructor(
readonly index: number,
readonly hasCheckbox: boolean,
mainItem: QuickPickItem
) {
this._init = new Lazy(() => {
const saneLabel = mainItem.label ?? '';
const saneSortLabel = parseLabelWithIcons(saneLabel).text.trim();
const saneAriaLabel = mainItem.ariaLabel || [saneLabel, this.saneDescription, this.saneDetail]
.map(s => getCodiconAriaLabel(s))
.filter(s => !!s)
.join(', ');
return {
saneLabel,
saneSortLabel,
saneAriaLabel
};
});
this._saneDescription = mainItem.description;
this._saneTooltip = mainItem.tooltip;
}
// #region Lazy Getters
get saneLabel() {
return this._init.value.saneLabel;
}
get saneSortLabel() {
return this._init.value.saneSortLabel;
}
get saneAriaLabel() {
return this._init.value.saneAriaLabel;
}
// #endregion
// #region Getters and Setters
private _element?: HTMLElement;
get element() {
return this._element;
}
set element(value: HTMLElement | undefined) {
this._element = value;
}
private _hidden = false;
get hidden() {
return this._hidden;
}
set hidden(value: boolean) {
this._hidden = value;
}
private _saneDescription?: string;
get saneDescription() {
return this._saneDescription;
}
set saneDescription(value: string | undefined) {
this._saneDescription = value;
}
protected _saneDetail?: string;
get saneDetail() {
return this._saneDetail;
}
set saneDetail(value: string | undefined) {
this._saneDetail = value;
}
private _saneTooltip?: string | IMarkdownString | HTMLElement;
get saneTooltip() {
return this._saneTooltip;
}
set saneTooltip(value: string | IMarkdownString | HTMLElement | undefined) {
this._saneTooltip = value;
}
protected _labelHighlights?: IMatch[];
get labelHighlights() {
return this._labelHighlights;
}
set labelHighlights(value: IMatch[] | undefined) {
this._labelHighlights = value;
}
protected _descriptionHighlights?: IMatch[];
get descriptionHighlights() {
return this._descriptionHighlights;
}
set descriptionHighlights(value: IMatch[] | undefined) {
this._descriptionHighlights = value;
}
protected _detailHighlights?: IMatch[];
get detailHighlights() {
return this._detailHighlights;
}
set detailHighlights(value: IMatch[] | undefined) {
this._detailHighlights = value;
}
}
class QuickPickItemElement extends BaseQuickPickItemElement {
readonly onChecked: Event<boolean>;
constructor(
index: number,
hasCheckbox: boolean,
readonly fireButtonTriggered: (event: IQuickPickItemButtonEvent<IQuickPickItem>) => void,
private _onChecked: Emitter<{ element: IQuickPickElement; checked: boolean }>,
readonly item: IQuickPickItem,
private _separator: IQuickPickSeparator | undefined,
) {
super(index, hasCheckbox, item);
this.onChecked = hasCheckbox
? Event.map(Event.filter<{ element: IQuickPickElement; checked: boolean }>(this._onChecked.event, e => e.element === this), e => e.checked)
: Event.None;
this._saneDetail = item.detail;
this._labelHighlights = item.highlights?.label;
this._descriptionHighlights = item.highlights?.description;
this._detailHighlights = item.highlights?.detail;
}
get separator() {
return this._separator;
}
set separator(value: IQuickPickSeparator | undefined) {
this._separator = value;
}
private _checked = false;
get checked() {
return this._checked;
}
set checked(value: boolean) {
if (value !== this._checked) {
this._checked = value;
this._onChecked.fire({ element: this, checked: value });
}
}
get checkboxDisabled() {
return !!this.item.disabled;
}
}
enum QuickPickSeparatorFocusReason {
/**
* No item is hovered or active
*/
NONE = 0,
/**
* Some item within this section is hovered
*/
MOUSE_HOVER = 1,
/**
* Some item within this section is active
*/
ACTIVE_ITEM = 2
}
class QuickPickSeparatorElement extends BaseQuickPickItemElement {
children = new Array<QuickPickItemElement>();
/**
* If this item is >0, it means that there is some item in the list that is either:
* * hovered over
* * active
*/
focusInsideSeparator = QuickPickSeparatorFocusReason.NONE;
constructor(
index: number,
readonly fireSeparatorButtonTriggered: (event: IQuickPickSeparatorButtonEvent) => void,
readonly separator: IQuickPickSeparator,
) {
super(index, false, separator);
}
}
class QuickInputItemDelegate implements IListVirtualDelegate<IQuickPickElement> {
getHeight(element: IQuickPickElement): number {
if (element instanceof QuickPickSeparatorElement) {
return 30;
}
return element.saneDetail ? 44 : 22;
}
getTemplateId(element: IQuickPickElement): string {
if (element instanceof QuickPickItemElement) {
return QuickPickItemElementRenderer.ID;
} else {
return QuickPickSeparatorElementRenderer.ID;
}
}
}
class QuickInputAccessibilityProvider implements IListAccessibilityProvider<IQuickPickElement> {
getWidgetAriaLabel(): string {
return localize('quickInput', "Quick Input");
}
getAriaLabel(element: IQuickPickElement): string | null {
return element.separator?.label
? `${element.saneAriaLabel}, ${element.separator.label}`
: element.saneAriaLabel;
}
getWidgetRole(): AriaRole {
return 'listbox';
}
getRole(element: IQuickPickElement) {
return element.hasCheckbox ? 'checkbox' : 'option';
}
isChecked(element: IQuickPickElement): IValueWithChangeEvent<boolean> | undefined {
if (!element.hasCheckbox || !(element instanceof QuickPickItemElement)) {
return undefined;
}
return {
get value() { return element.checked; },
onDidChange: e => element.onChecked(() => e()),
};
}
}
abstract class BaseQuickInputListRenderer<T extends IQuickPickElement> implements ITreeRenderer<T, void, IQuickInputItemTemplateData> {
abstract templateId: string;
constructor(
private readonly hoverDelegate: IHoverDelegate | undefined
) { }
// TODO: only do the common stuff here and have a subclass handle their specific stuff
renderTemplate(container: HTMLElement): IQuickInputItemTemplateData {
const data: IQuickInputItemTemplateData = Object.create(null);
data.toDisposeElement = new DisposableStore();
data.toDisposeTemplate = new DisposableStore();
data.entry = dom.append(container, $('.quick-input-list-entry'));
// Checkbox
const label = dom.append(data.entry, $('label.quick-input-list-label'));
data.toDisposeTemplate.add(dom.addStandardDisposableListener(label, dom.EventType.CLICK, e => {
if (!data.checkbox.offsetParent) { // If checkbox not visible:
e.preventDefault(); // Prevent toggle of checkbox when it is immediately shown afterwards. #91740
}
}));
data.checkbox = <HTMLInputElement>dom.append(label, $('input.quick-input-list-checkbox'));
data.checkbox.type = 'checkbox';
// Rows
const rows = dom.append(label, $('.quick-input-list-rows'));
const row1 = dom.append(rows, $('.quick-input-list-row'));
const row2 = dom.append(rows, $('.quick-input-list-row'));
// Label
data.label = new IconLabel(row1, { supportHighlights: true, supportDescriptionHighlights: true, supportIcons: true, hoverDelegate: this.hoverDelegate });
data.toDisposeTemplate.add(data.label);
data.icon = <HTMLInputElement>dom.prepend(data.label.element, $('.quick-input-list-icon'));
// Keybinding
const keybindingContainer = dom.append(row1, $('.quick-input-list-entry-keybinding'));
data.keybinding = new KeybindingLabel(keybindingContainer, OS);
data.toDisposeTemplate.add(data.keybinding);
// Detail
const detailContainer = dom.append(row2, $('.quick-input-list-label-meta'));
data.detail = new IconLabel(detailContainer, { supportHighlights: true, supportIcons: true, hoverDelegate: this.hoverDelegate });
data.toDisposeTemplate.add(data.detail);
// Separator
data.separator = dom.append(data.entry, $('.quick-input-list-separator'));
// Actions
data.actionBar = new ActionBar(data.entry, this.hoverDelegate ? { hoverDelegate: this.hoverDelegate } : undefined);
data.actionBar.domNode.classList.add('quick-input-list-entry-action-bar');
data.toDisposeTemplate.add(data.actionBar);
return data;
}
disposeTemplate(data: IQuickInputItemTemplateData): void {
data.toDisposeElement.dispose();
data.toDisposeTemplate.dispose();
}
disposeElement(_element: ITreeNode<IQuickPickElement, void>, _index: number, data: IQuickInputItemTemplateData): void {
data.toDisposeElement.clear();
data.actionBar.clear();
}
// TODO: only do the common stuff here and have a subclass handle their specific stuff
abstract renderElement(node: ITreeNode<IQuickPickElement, void>, index: number, data: IQuickInputItemTemplateData): void;
}
class QuickPickItemElementRenderer extends BaseQuickInputListRenderer<QuickPickItemElement> {
static readonly ID = 'quickpickitem';
// Follow what we do in the separator renderer
private readonly _itemsWithSeparatorsFrequency = new Map<QuickPickItemElement, number>();
constructor(
hoverDelegate: IHoverDelegate | undefined,
@IThemeService private readonly themeService: IThemeService,
) {
super(hoverDelegate);
}
get templateId() {
return QuickPickItemElementRenderer.ID;
}
override renderTemplate(container: HTMLElement): IQuickInputItemTemplateData {
const data = super.renderTemplate(container);
data.toDisposeTemplate.add(dom.addStandardDisposableListener(data.checkbox, dom.EventType.CHANGE, e => {
(data.element as QuickPickItemElement).checked = data.checkbox.checked;
}));
return data;
}
renderElement(node: ITreeNode<QuickPickItemElement, void>, index: number, data: IQuickInputItemTemplateData): void {
const element = node.element;
data.element = element;
element.element = data.entry ?? undefined;
const mainItem: IQuickPickItem = element.item;
data.checkbox.checked = element.checked;
data.toDisposeElement.add(element.onChecked(checked => data.checkbox.checked = checked));
data.checkbox.disabled = element.checkboxDisabled;
const { labelHighlights, descriptionHighlights, detailHighlights } = element;
// Icon
if (mainItem.iconPath) {
const icon = isDark(this.themeService.getColorTheme().type) ? mainItem.iconPath.dark : (mainItem.iconPath.light ?? mainItem.iconPath.dark);
const iconUrl = URI.revive(icon);
data.icon.className = 'quick-input-list-icon';
data.icon.style.backgroundImage = dom.asCSSUrl(iconUrl);
} else {
data.icon.style.backgroundImage = '';
data.icon.className = mainItem.iconClass ? `quick-input-list-icon ${mainItem.iconClass}` : '';
}
// Label
let descriptionTitle: IUpdatableHoverTooltipMarkdownString | undefined;
// if we have a tooltip, that will be the hover,
// with the saneDescription as fallback if it
// is defined
if (!element.saneTooltip && element.saneDescription) {
descriptionTitle = {
markdown: {
value: element.saneDescription,
supportThemeIcons: true
},
markdownNotSupportedFallback: element.saneDescription
};
}
const options: IIconLabelValueOptions = {
matches: labelHighlights || [],
// If we have a tooltip, we want that to be shown and not any other hover
descriptionTitle,
descriptionMatches: descriptionHighlights || [],
labelEscapeNewLines: true
};
options.extraClasses = mainItem.iconClasses;
options.italic = mainItem.italic;
options.strikethrough = mainItem.strikethrough;
data.entry.classList.remove('quick-input-list-separator-as-item');
data.label.setLabel(element.saneLabel, element.saneDescription, options);
// Keybinding
data.keybinding.set(mainItem.keybinding);
// Detail
if (element.saneDetail) {
let title: IUpdatableHoverTooltipMarkdownString | undefined;
// If we have a tooltip, we want that to be shown and not any other hover
if (!element.saneTooltip) {
title = {
markdown: {
value: element.saneDetail,
supportThemeIcons: true
},
markdownNotSupportedFallback: element.saneDetail
};
}
data.detail.element.style.display = '';
data.detail.setLabel(element.saneDetail, undefined, {
matches: detailHighlights,
title,
labelEscapeNewLines: true
});
} else {
data.detail.element.style.display = 'none';
}
// Separator
if (element.separator?.label) {
data.separator.textContent = element.separator.label;
data.separator.style.display = '';
this.addItemWithSeparator(element);
} else {
data.separator.style.display = 'none';
}
data.entry.classList.toggle('quick-input-list-separator-border', !!element.separator);
// Actions
const buttons = mainItem.buttons;
if (buttons && buttons.length) {
data.actionBar.push(buttons.map((button, index) => quickInputButtonToAction(
button,
`id-${index}`,
() => element.fireButtonTriggered({ button, item: element.item })
)), { icon: true, label: false });
data.entry.classList.add('has-actions');
} else {
data.entry.classList.remove('has-actions');
}
}
override disposeElement(element: ITreeNode<QuickPickItemElement, void>, _index: number, data: IQuickInputItemTemplateData): void {
this.removeItemWithSeparator(element.element);
super.disposeElement(element, _index, data);
}
isItemWithSeparatorVisible(item: QuickPickItemElement): boolean {
return this._itemsWithSeparatorsFrequency.has(item);
}
private addItemWithSeparator(item: QuickPickItemElement): void {
this._itemsWithSeparatorsFrequency.set(item, (this._itemsWithSeparatorsFrequency.get(item) || 0) + 1);
}
private removeItemWithSeparator(item: QuickPickItemElement): void {
const frequency = this._itemsWithSeparatorsFrequency.get(item) || 0;
if (frequency > 1) {
this._itemsWithSeparatorsFrequency.set(item, frequency - 1);
} else {
this._itemsWithSeparatorsFrequency.delete(item);
}
}
}
class QuickPickSeparatorElementRenderer extends BaseQuickInputListRenderer<QuickPickSeparatorElement> {
static readonly ID = 'quickpickseparator';
// This is a frequency map because sticky scroll re-uses the same renderer to render a second
// instance of the same separator.
private readonly _visibleSeparatorsFrequency = new Map<QuickPickSeparatorElement, number>();
get templateId() {
return QuickPickSeparatorElementRenderer.ID;
}
get visibleSeparators(): QuickPickSeparatorElement[] {
return [...this._visibleSeparatorsFrequency.keys()];
}
isSeparatorVisible(separator: QuickPickSeparatorElement): boolean {
return this._visibleSeparatorsFrequency.has(separator);
}
override renderElement(node: ITreeNode<QuickPickSeparatorElement, void>, index: number, data: IQuickInputItemTemplateData): void {
const element = node.element;
data.element = element;
element.element = data.entry ?? undefined;
element.element.classList.toggle('focus-inside', !!element.focusInsideSeparator);
const mainItem: IQuickPickSeparator = element.separator;
const { labelHighlights, descriptionHighlights, detailHighlights } = element;
// Icon
data.icon.style.backgroundImage = '';
data.icon.className = '';
// Label
let descriptionTitle: IUpdatableHoverTooltipMarkdownString | undefined;
// if we have a tooltip, that will be the hover,
// with the saneDescription as fallback if it
// is defined
if (!element.saneTooltip && element.saneDescription) {
descriptionTitle = {
markdown: {
value: element.saneDescription,
supportThemeIcons: true
},
markdownNotSupportedFallback: element.saneDescription
};
}
const options: IIconLabelValueOptions = {
matches: labelHighlights || [],
// If we have a tooltip, we want that to be shown and not any other hover
descriptionTitle,
descriptionMatches: descriptionHighlights || [],
labelEscapeNewLines: true
};
data.entry.classList.add('quick-input-list-separator-as-item');
data.label.setLabel(element.saneLabel, element.saneDescription, options);
// Detail
if (element.saneDetail) {
let title: IUpdatableHoverTooltipMarkdownString | undefined;
// If we have a tooltip, we want that to be shown and not any other hover
if (!element.saneTooltip) {
title = {
markdown: {
value: element.saneDetail,
supportThemeIcons: true
},
markdownNotSupportedFallback: element.saneDetail
};
}
data.detail.element.style.display = '';
data.detail.setLabel(element.saneDetail, undefined, {
matches: detailHighlights,
title,
labelEscapeNewLines: true
});
} else {
data.detail.element.style.display = 'none';
}
// Separator
data.separator.style.display = 'none';
data.entry.classList.add('quick-input-list-separator-border');
// Actions
const buttons = mainItem.buttons;
if (buttons && buttons.length) {
data.actionBar.push(buttons.map((button, index) => quickInputButtonToAction(
button,
`id-${index}`,
() => element.fireSeparatorButtonTriggered({ button, separator: element.separator })
)), { icon: true, label: false });
data.entry.classList.add('has-actions');
} else {
data.entry.classList.remove('has-actions');
}
this.addSeparator(element);
}
override disposeElement(element: ITreeNode<QuickPickSeparatorElement, void>, _index: number, data: IQuickInputItemTemplateData): void {
this.removeSeparator(element.element);
if (!this.isSeparatorVisible(element.element)) {
element.element.element?.classList.remove('focus-inside');
}
super.disposeElement(element, _index, data);
}
private addSeparator(separator: QuickPickSeparatorElement): void {
this._visibleSeparatorsFrequency.set(separator, (this._visibleSeparatorsFrequency.get(separator) || 0) + 1);
}
private removeSeparator(separator: QuickPickSeparatorElement): void {
const frequency = this._visibleSeparatorsFrequency.get(separator) || 0;
if (frequency > 1) {
this._visibleSeparatorsFrequency.set(separator, frequency - 1);
} else {
this._visibleSeparatorsFrequency.delete(separator);
}
}
}
export class QuickInputTree extends Disposable {
private readonly _onKeyDown = new Emitter<StandardKeyboardEvent>();
/**
* Event that is fired when the tree receives a keydown.
*/
readonly onKeyDown: Event<StandardKeyboardEvent> = this._onKeyDown.event;
private readonly _onLeave = new Emitter<void>();
/**
* Event that is fired when the tree would no longer have focus.
*/
readonly onLeave: Event<void> = this._onLeave.event;
private readonly _onChangedAllVisibleChecked = new Emitter<boolean>();
onChangedAllVisibleChecked: Event<boolean> = this._onChangedAllVisibleChecked.event;
private readonly _onChangedCheckedCount = new Emitter<number>();
onChangedCheckedCount: Event<number> = this._onChangedCheckedCount.event;
private readonly _onChangedVisibleCount = new Emitter<number>();
onChangedVisibleCount: Event<number> = this._onChangedVisibleCount.event;
private readonly _onChangedCheckedElements = new Emitter<IQuickPickItem[]>();
onChangedCheckedElements: Event<IQuickPickItem[]> = this._onChangedCheckedElements.event;
private readonly _onButtonTriggered = new Emitter<IQuickPickItemButtonEvent<IQuickPickItem>>();
onButtonTriggered = this._onButtonTriggered.event;
private readonly _onSeparatorButtonTriggered = new Emitter<IQuickPickSeparatorButtonEvent>();
onSeparatorButtonTriggered = this._onSeparatorButtonTriggered.event;
private readonly _onTriggerEmptySelectionOrFocus = new Emitter<ITreeEvent<IQuickPickElement | null>>();
private readonly _container: HTMLElement;
private readonly _tree: WorkbenchObjectTree<IQuickPickElement, void>;
private readonly _separatorRenderer: QuickPickSeparatorElementRenderer;
private readonly _itemRenderer: QuickPickItemElementRenderer;
private readonly _elementChecked = new Emitter<{ element: IQuickPickElement; checked: boolean }>();
private _inputElements = new Array<QuickPickItem>();
private _elementTree = new Array<IQuickPickElement>();
private _itemElements = new Array<QuickPickItemElement>();
// Elements that apply to the current set of elements
private readonly _elementDisposable = this._register(new DisposableStore());
private _lastHover: IHoverWidget | undefined;
// This is used to prevent setting the checked state of a single element from firing the checked events
// so that we can batch them together. This can probably be improved by handling events differently,
// but this works for now. An observable would probably be ideal for this.
private _shouldFireCheckedEvents = true;
constructor(
private parent: HTMLElement,
private hoverDelegate: IHoverDelegate,
private linkOpenerDelegate: (content: string) => void,
id: string,
@IInstantiationService instantiationService: IInstantiationService
) {
super();
this._container = dom.append(this.parent, $('.quick-input-list'));
this._separatorRenderer = new QuickPickSeparatorElementRenderer(hoverDelegate);
this._itemRenderer = instantiationService.createInstance(QuickPickItemElementRenderer, hoverDelegate);
this._tree = this._register(instantiationService.createInstance(
WorkbenchObjectTree<IQuickPickElement, void>,
'QuickInput',
this._container,
new QuickInputItemDelegate(),
[this._itemRenderer, this._separatorRenderer],
{
accessibilityProvider: new QuickInputAccessibilityProvider(),
setRowLineHeight: false,
multipleSelectionSupport: false,
hideTwistiesOfChildlessElements: true,
renderIndentGuides: RenderIndentGuides.None,
findWidgetEnabled: false,
indent: 0,
horizontalScrolling: false,
allowNonCollapsibleParents: true,
alwaysConsumeMouseWheel: true
}
));
this._tree.getHTMLElement().id = id;
this._registerListeners();
}
//#region public getters/setters
@memoize
get onDidChangeFocus() {
return Event.map(
Event.any(this._tree.onDidChangeFocus, this._onTriggerEmptySelectionOrFocus.event),
e => e.elements.filter((e): e is QuickPickItemElement => e instanceof QuickPickItemElement).map(e => e.item)
);
}
@memoize
get onDidChangeSelection() {
return Event.map(
Event.any(this._tree.onDidChangeSelection, this._onTriggerEmptySelectionOrFocus.event),
e => ({
items: e.elements.filter((e): e is QuickPickItemElement => e instanceof QuickPickItemElement).map(e => e.item),
event: e.browserEvent
}));
}
get scrollTop() {
return this._tree.scrollTop;
}
set scrollTop(scrollTop: number) {
this._tree.scrollTop = scrollTop;
}
get ariaLabel() {
return this._tree.ariaLabel;
}
set ariaLabel(label: string | null) {
this._tree.ariaLabel = label ?? '';
}
set enabled(value: boolean) {
this._tree.getHTMLElement().style.pointerEvents = value ? '' : 'none';
}
private _matchOnDescription = false;
get matchOnDescription() {
return this._matchOnDescription;
}
set matchOnDescription(value: boolean) {
this._matchOnDescription = value;
}
private _matchOnDetail = false;
get matchOnDetail() {
return this._matchOnDetail;
}
set matchOnDetail(value: boolean) {
this._matchOnDetail = value;
}
private _matchOnLabel = true;
get matchOnLabel() {
return this._matchOnLabel;
}
set matchOnLabel(value: boolean) {
this._matchOnLabel = value;
}
private _matchOnLabelMode: 'fuzzy' | 'contiguous' = 'fuzzy';
get matchOnLabelMode() {
return this._matchOnLabelMode;
}
set matchOnLabelMode(value: 'fuzzy' | 'contiguous') {
this._matchOnLabelMode = value;
}
private _matchOnMeta = true;
get matchOnMeta() {
return this._matchOnMeta;
}
set matchOnMeta(value: boolean) {
this._matchOnMeta = value;
}
private _sortByLabel = true;
get sortByLabel() {
return this._sortByLabel;
}
set sortByLabel(value: boolean) {
this._sortByLabel = value;
}
private _shouldLoop = true;
get shouldLoop() {
return this._shouldLoop;
}
set shouldLoop(value: boolean) {
this._shouldLoop = value;
}
//#endregion
//#region register listeners
private _registerListeners() {
this._registerOnKeyDown();
this._registerOnContainerClick();
this._registerOnMouseMiddleClick();
this._registerOnElementChecked();
this._registerOnContextMenu();
this._registerHoverListeners();
this._registerSelectionChangeListener();
this._registerSeparatorActionShowingListeners();
}
private _registerOnKeyDown() {
// TODO: Should this be added at a higher level?
this._register(this._tree.onKeyDown(e => {
const event = new StandardKeyboardEvent(e);
switch (event.keyCode) {
case KeyCode.Space:
this.toggleCheckbox();
break;
}
this._onKeyDown.fire(event);
}));
}
private _registerOnContainerClick() {
this._register(dom.addDisposableListener(this._container, dom.EventType.CLICK, e => {
if (e.x || e.y) { // Avoid 'click' triggered by 'space' on checkbox.
this._onLeave.fire();
}
}));
}
private _registerOnMouseMiddleClick() {
this._register(dom.addDisposableListener(this._container, dom.EventType.AUXCLICK, e => {
if (e.button === 1) {
this._onLeave.fire();
}
}));
}
private _registerOnElementChecked() {
this._register(this._elementChecked.event(_ => this._fireCheckedEvents()));
}
private _registerOnContextMenu() {
this._register(this._tree.onContextMenu(e => {
if (e.element) {
e.browserEvent.preventDefault();
// we want to treat a context menu event as
// a gesture to open the item at the index
// since we do not have any context menu
// this enables for example macOS to Ctrl-
// click on an item to open it.
this._tree.setSelection([e.element]);
}
}));
}
private _registerHoverListeners() {
const delayer = this._register(new ThrottledDelayer(this.hoverDelegate.delay));
this._register(this._tree.onMouseOver(async e => {
// If we hover over an anchor element, we don't want to show the hover because
// the anchor may have a tooltip that we want to show instead.
if (e.browserEvent.target instanceof HTMLAnchorElement) {
delayer.cancel();
return;
}
if (
// anchors are an exception as called out above so we skip them here
!(e.browserEvent.relatedTarget instanceof HTMLAnchorElement) &&
// check if the mouse is still over the same element
dom.isAncestor(e.browserEvent.relatedTarget as Node, e.element?.element as Node)
) {
return;
}
try {
await delayer.trigger(async () => {
if (e.element instanceof QuickPickItemElement) {
this.showHover(e.element);
}
});
} catch (e) {
// Ignore cancellation errors due to mouse out
if (!isCancellationError(e)) {
throw e;
}
}
}));
this._register(this._tree.onMouseOut(e => {
// onMouseOut triggers every time a new element has been moused over
// even if it's on the same list item. We only want one event, so we
// check if the mouse is still over the same element.
if (dom.isAncestor(e.browserEvent.relatedTarget as Node, e.element?.element as Node)) {
return;
}
delayer.cancel();
}));
}
/**
* Register's focus change and mouse events so that we can track when items inside of a
* separator's section are focused or hovered so that we can display the separator's actions
*/
private _registerSeparatorActionShowingListeners() {
this._register(this._tree.onDidChangeFocus(e => {
const parent = e.elements[0]
? this._tree.getParentElement(e.elements[0]) as QuickPickSeparatorElement
// treat null as focus lost and when we have no separators
: null;
for (const separator of this._separatorRenderer.visibleSeparators) {
const value = separator === parent;
// get bitness of ACTIVE_ITEM and check if it changed
const currentActive = !!(separator.focusInsideSeparator & QuickPickSeparatorFocusReason.ACTIVE_ITEM);
if (currentActive !== value) {
if (value) {
separator.focusInsideSeparator |= QuickPickSeparatorFocusReason.ACTIVE_ITEM;
} else {
separator.focusInsideSeparator &= ~QuickPickSeparatorFocusReason.ACTIVE_ITEM;
}
this._tree.rerender(separator);
}
}
}));
this._register(this._tree.onMouseOver(e => {
const parent = e.element
? this._tree.getParentElement(e.element) as QuickPickSeparatorElement
: null;
for (const separator of this._separatorRenderer.visibleSeparators) {
if (separator !== parent) {
continue;
}
const currentMouse = !!(separator.focusInsideSeparator & QuickPickSeparatorFocusReason.MOUSE_HOVER);
if (!currentMouse) {
separator.focusInsideSeparator |= QuickPickSeparatorFocusReason.MOUSE_HOVER;
this._tree.rerender(separator);
}
}
}));
this._register(this._tree.onMouseOut(e => {
const parent = e.element
? this._tree.getParentElement(e.element) as QuickPickSeparatorElement
: null;
for (const separator of this._separatorRenderer.visibleSeparators) {
if (separator !== parent) {
continue;
}
const currentMouse = !!(separator.focusInsideSeparator & QuickPickSeparatorFocusReason.MOUSE_HOVER);
if (currentMouse) {
separator.focusInsideSeparator &= ~QuickPickSeparatorFocusReason.MOUSE_HOVER;
this._tree.rerender(separator);
}
}
}));
}
private _registerSelectionChangeListener() {
// When the user selects a separator, the separator will move to the top and focus will be
// set to the first element after the separator.