Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/workbench/browser/parts/views/checkbox.ts
3296 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import * as DOM from '../../../../base/browser/dom.js';
7
import type { IManagedHover } from '../../../../base/browser/ui/hover/hover.js';
8
import { IHoverDelegate } from '../../../../base/browser/ui/hover/hoverDelegate.js';
9
import { Toggle } from '../../../../base/browser/ui/toggle/toggle.js';
10
import { Codicon } from '../../../../base/common/codicons.js';
11
import { Emitter, Event } from '../../../../base/common/event.js';
12
import { Disposable } from '../../../../base/common/lifecycle.js';
13
import { localize } from '../../../../nls.js';
14
import type { IHoverService } from '../../../../platform/hover/browser/hover.js';
15
import { defaultToggleStyles } from '../../../../platform/theme/browser/defaultStyles.js';
16
import { ITreeItem, ITreeItemCheckboxState } from '../../../common/views.js';
17
18
export class CheckboxStateHandler extends Disposable {
19
private readonly _onDidChangeCheckboxState = this._register(new Emitter<ITreeItem[]>());
20
readonly onDidChangeCheckboxState: Event<ITreeItem[]> = this._onDidChangeCheckboxState.event;
21
22
public setCheckboxState(node: ITreeItem) {
23
this._onDidChangeCheckboxState.fire([node]);
24
}
25
}
26
27
export class TreeItemCheckbox extends Disposable {
28
private toggle: Toggle | undefined;
29
private readonly checkboxContainer: HTMLDivElement;
30
private hover: IManagedHover | undefined;
31
32
public static readonly checkboxClass = 'custom-view-tree-node-item-checkbox';
33
34
constructor(
35
container: HTMLElement,
36
private readonly checkboxStateHandler: CheckboxStateHandler,
37
private readonly hoverDelegate: IHoverDelegate,
38
private readonly hoverService: IHoverService
39
) {
40
super();
41
this.checkboxContainer = <HTMLDivElement>container;
42
}
43
44
public render(node: ITreeItem) {
45
if (node.checkbox) {
46
if (!this.toggle) {
47
this.createCheckbox(node);
48
}
49
else {
50
this.toggle.checked = node.checkbox.isChecked;
51
this.toggle.setIcon(this.toggle.checked ? Codicon.check : undefined);
52
}
53
}
54
}
55
56
private createCheckbox(node: ITreeItem) {
57
if (node.checkbox) {
58
this.toggle = new Toggle({
59
isChecked: node.checkbox.isChecked,
60
title: '',
61
icon: node.checkbox.isChecked ? Codicon.check : undefined,
62
...defaultToggleStyles
63
});
64
this.setHover(node.checkbox);
65
this.setAccessibilityInformation(node.checkbox);
66
this.toggle.domNode.classList.add(TreeItemCheckbox.checkboxClass);
67
this.toggle.domNode.tabIndex = 1;
68
DOM.append(this.checkboxContainer, this.toggle.domNode);
69
this.registerListener(node);
70
}
71
}
72
73
private registerListener(node: ITreeItem) {
74
if (this.toggle) {
75
this._register({ dispose: () => this.removeCheckbox() });
76
this._register(this.toggle);
77
this._register(this.toggle.onChange(() => {
78
this.setCheckbox(node);
79
}));
80
}
81
}
82
83
private setHover(checkbox: ITreeItemCheckboxState) {
84
if (this.toggle) {
85
if (!this.hover) {
86
this.hover = this._register(this.hoverService.setupManagedHover(this.hoverDelegate, this.toggle.domNode, this.checkboxHoverContent(checkbox)));
87
} else {
88
this.hover.update(checkbox.tooltip);
89
}
90
}
91
}
92
93
private setCheckbox(node: ITreeItem) {
94
if (this.toggle && node.checkbox) {
95
node.checkbox.isChecked = this.toggle.checked;
96
this.toggle.setIcon(this.toggle.checked ? Codicon.check : undefined);
97
this.setHover(node.checkbox);
98
99
this.setAccessibilityInformation(node.checkbox);
100
this.checkboxStateHandler.setCheckboxState(node);
101
}
102
}
103
104
private checkboxHoverContent(checkbox: ITreeItemCheckboxState): string {
105
return checkbox.tooltip ? checkbox.tooltip :
106
checkbox.isChecked ? localize('checked', 'Checked') : localize('unchecked', 'Unchecked');
107
}
108
109
private setAccessibilityInformation(checkbox: ITreeItemCheckboxState) {
110
if (this.toggle && checkbox.accessibilityInformation) {
111
this.toggle.domNode.ariaLabel = checkbox.accessibilityInformation.label;
112
if (checkbox.accessibilityInformation.role) {
113
this.toggle.domNode.role = checkbox.accessibilityInformation.role;
114
}
115
}
116
}
117
118
private removeCheckbox() {
119
const children = this.checkboxContainer.children;
120
for (const child of children) {
121
child.remove();
122
}
123
}
124
}
125
126