Path: blob/main/src/vs/workbench/browser/parts/views/checkbox.ts
5281 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import * as DOM from '../../../../base/browser/dom.js';6import type { IManagedHover } from '../../../../base/browser/ui/hover/hover.js';7import { IHoverDelegate } from '../../../../base/browser/ui/hover/hoverDelegate.js';8import { Checkbox } from '../../../../base/browser/ui/toggle/toggle.js';9import { Emitter, Event } from '../../../../base/common/event.js';10import { Disposable } from '../../../../base/common/lifecycle.js';11import { localize } from '../../../../nls.js';12import type { IHoverService } from '../../../../platform/hover/browser/hover.js';13import { defaultCheckboxStyles } from '../../../../platform/theme/browser/defaultStyles.js';14import { ITreeItem, ITreeItemCheckboxState } from '../../../common/views.js';1516export class CheckboxStateHandler extends Disposable {17private readonly _onDidChangeCheckboxState = this._register(new Emitter<ITreeItem[]>());18readonly onDidChangeCheckboxState: Event<ITreeItem[]> = this._onDidChangeCheckboxState.event;1920public setCheckboxState(node: ITreeItem) {21this._onDidChangeCheckboxState.fire([node]);22}23}2425export class TreeItemCheckbox extends Disposable {26private toggle: Checkbox | undefined;27private readonly checkboxContainer: HTMLDivElement;28private hover: IManagedHover | undefined;2930public static readonly checkboxClass = 'custom-view-tree-node-item-checkbox';3132constructor(33container: HTMLElement,34private readonly checkboxStateHandler: CheckboxStateHandler,35private readonly hoverDelegate: IHoverDelegate,36private readonly hoverService: IHoverService37) {38super();39this.checkboxContainer = <HTMLDivElement>container;40}4142public render(node: ITreeItem) {43if (node.checkbox) {44if (!this.toggle) {45this.createCheckbox(node);46}47else {48this.toggle.checked = node.checkbox.isChecked;49}50}51}5253private createCheckbox(node: ITreeItem) {54if (node.checkbox) {55this.toggle = new Checkbox('', node.checkbox.isChecked, { ...defaultCheckboxStyles, size: 15 });56this.setHover(node.checkbox);57this.setAccessibilityInformation(node.checkbox);58this.toggle.domNode.classList.add(TreeItemCheckbox.checkboxClass);59this.toggle.domNode.tabIndex = 1;60DOM.append(this.checkboxContainer, this.toggle.domNode);61this.registerListener(node);62}63}6465private registerListener(node: ITreeItem) {66if (this.toggle) {67this._register({ dispose: () => this.removeCheckbox() });68this._register(this.toggle);69this._register(this.toggle.onChange(() => {70this.setCheckbox(node);71}));72}73}7475private setHover(checkbox: ITreeItemCheckboxState) {76if (this.toggle) {77if (!this.hover) {78this.hover = this._register(this.hoverService.setupManagedHover(this.hoverDelegate, this.toggle.domNode, this.checkboxHoverContent(checkbox)));79} else {80this.hover.update(checkbox.tooltip);81}82}83}8485private setCheckbox(node: ITreeItem) {86if (this.toggle && node.checkbox) {87node.checkbox.isChecked = this.toggle.checked;88this.setHover(node.checkbox);8990this.setAccessibilityInformation(node.checkbox);91this.checkboxStateHandler.setCheckboxState(node);92}93}9495private checkboxHoverContent(checkbox: ITreeItemCheckboxState): string {96return checkbox.tooltip ? checkbox.tooltip :97checkbox.isChecked ? localize('checked', 'Checked') : localize('unchecked', 'Unchecked');98}99100private setAccessibilityInformation(checkbox: ITreeItemCheckboxState) {101if (this.toggle && checkbox.accessibilityInformation) {102this.toggle.setTitle(checkbox.accessibilityInformation.label);103if (checkbox.accessibilityInformation.role) {104this.toggle.domNode.role = checkbox.accessibilityInformation.role;105}106}107}108109private removeCheckbox() {110const children = this.checkboxContainer.children;111for (const child of children) {112child.remove();113}114}115}116117118