Path: blob/main/src/vs/base/browser/ui/countBadge/countBadge.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 { $, append } from '../../dom.js';6import { format } from '../../../common/strings.js';7import './countBadge.css';8import { Disposable, IDisposable, MutableDisposable, toDisposable } from '../../../common/lifecycle.js';9import { getBaseLayerHoverDelegate } from '../hover/hoverDelegate2.js';1011export interface ICountBadgeOptions {12readonly count?: number;13readonly countFormat?: string;14readonly titleFormat?: string;15}1617export interface ICountBadgeStyles {18readonly badgeBackground: string | undefined;19readonly badgeForeground: string | undefined;20readonly badgeBorder: string | undefined;21}2223export const unthemedCountStyles: ICountBadgeStyles = {24badgeBackground: '#4D4D4D',25badgeForeground: '#FFFFFF',26badgeBorder: undefined27};2829export class CountBadge extends Disposable {3031private element: HTMLElement;32private count: number = 0;33private countFormat: string;34private titleFormat: string;35private readonly hover = this._register(new MutableDisposable<IDisposable>());3637constructor(container: HTMLElement, private readonly options: ICountBadgeOptions, private readonly styles: ICountBadgeStyles) {3839super();40this.element = append(container, $('.monaco-count-badge'));41this._register(toDisposable(() => container.removeChild(this.element)));42this.countFormat = this.options.countFormat || '{0}';43this.titleFormat = this.options.titleFormat || '';44this.setCount(this.options.count || 0);45this.updateHover();46}4748setCount(count: number) {49this.count = count;50this.render();51}5253setCountFormat(countFormat: string) {54this.countFormat = countFormat;55this.render();56}5758setTitleFormat(titleFormat: string) {59this.titleFormat = titleFormat;60this.updateHover();61this.render();62}6364private updateHover(): void {65if (this.titleFormat !== '' && !this.hover.value) {66this.hover.value = getBaseLayerHoverDelegate().setupDelayedHoverAtMouse(this.element, () => ({ content: format(this.titleFormat, this.count), appearance: { compact: true } }));67} else if (this.titleFormat === '' && this.hover.value) {68this.hover.value = undefined;69}70}7172private render() {73this.element.textContent = format(this.countFormat, this.count);7475this.element.style.backgroundColor = this.styles.badgeBackground ?? '';76this.element.style.color = this.styles.badgeForeground ?? '';7778if (this.styles.badgeBorder) {79this.element.style.border = `1px solid ${this.styles.badgeBorder}`;80}81}82}838485