Path: blob/main/src/vs/workbench/browser/parts/views/checkbox.ts
3296 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 { Toggle } from '../../../../base/browser/ui/toggle/toggle.js';9import { Codicon } from '../../../../base/common/codicons.js';10import { Emitter, Event } from '../../../../base/common/event.js';11import { Disposable } from '../../../../base/common/lifecycle.js';12import { localize } from '../../../../nls.js';13import type { IHoverService } from '../../../../platform/hover/browser/hover.js';14import { defaultToggleStyles } from '../../../../platform/theme/browser/defaultStyles.js';15import { ITreeItem, ITreeItemCheckboxState } from '../../../common/views.js';1617export class CheckboxStateHandler extends Disposable {18private readonly _onDidChangeCheckboxState = this._register(new Emitter<ITreeItem[]>());19readonly onDidChangeCheckboxState: Event<ITreeItem[]> = this._onDidChangeCheckboxState.event;2021public setCheckboxState(node: ITreeItem) {22this._onDidChangeCheckboxState.fire([node]);23}24}2526export class TreeItemCheckbox extends Disposable {27private toggle: Toggle | undefined;28private readonly checkboxContainer: HTMLDivElement;29private hover: IManagedHover | undefined;3031public static readonly checkboxClass = 'custom-view-tree-node-item-checkbox';3233constructor(34container: HTMLElement,35private readonly checkboxStateHandler: CheckboxStateHandler,36private readonly hoverDelegate: IHoverDelegate,37private readonly hoverService: IHoverService38) {39super();40this.checkboxContainer = <HTMLDivElement>container;41}4243public render(node: ITreeItem) {44if (node.checkbox) {45if (!this.toggle) {46this.createCheckbox(node);47}48else {49this.toggle.checked = node.checkbox.isChecked;50this.toggle.setIcon(this.toggle.checked ? Codicon.check : undefined);51}52}53}5455private createCheckbox(node: ITreeItem) {56if (node.checkbox) {57this.toggle = new Toggle({58isChecked: node.checkbox.isChecked,59title: '',60icon: node.checkbox.isChecked ? Codicon.check : undefined,61...defaultToggleStyles62});63this.setHover(node.checkbox);64this.setAccessibilityInformation(node.checkbox);65this.toggle.domNode.classList.add(TreeItemCheckbox.checkboxClass);66this.toggle.domNode.tabIndex = 1;67DOM.append(this.checkboxContainer, this.toggle.domNode);68this.registerListener(node);69}70}7172private registerListener(node: ITreeItem) {73if (this.toggle) {74this._register({ dispose: () => this.removeCheckbox() });75this._register(this.toggle);76this._register(this.toggle.onChange(() => {77this.setCheckbox(node);78}));79}80}8182private setHover(checkbox: ITreeItemCheckboxState) {83if (this.toggle) {84if (!this.hover) {85this.hover = this._register(this.hoverService.setupManagedHover(this.hoverDelegate, this.toggle.domNode, this.checkboxHoverContent(checkbox)));86} else {87this.hover.update(checkbox.tooltip);88}89}90}9192private setCheckbox(node: ITreeItem) {93if (this.toggle && node.checkbox) {94node.checkbox.isChecked = this.toggle.checked;95this.toggle.setIcon(this.toggle.checked ? Codicon.check : undefined);96this.setHover(node.checkbox);9798this.setAccessibilityInformation(node.checkbox);99this.checkboxStateHandler.setCheckboxState(node);100}101}102103private checkboxHoverContent(checkbox: ITreeItemCheckboxState): string {104return checkbox.tooltip ? checkbox.tooltip :105checkbox.isChecked ? localize('checked', 'Checked') : localize('unchecked', 'Unchecked');106}107108private setAccessibilityInformation(checkbox: ITreeItemCheckboxState) {109if (this.toggle && checkbox.accessibilityInformation) {110this.toggle.domNode.ariaLabel = checkbox.accessibilityInformation.label;111if (checkbox.accessibilityInformation.role) {112this.toggle.domNode.role = checkbox.accessibilityInformation.role;113}114}115}116117private removeCheckbox() {118const children = this.checkboxContainer.children;119for (const child of children) {120child.remove();121}122}123}124125126