Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/base/browser/ui/countBadge/countBadge.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 { $, append } from '../../dom.js';
7
import { format } from '../../../common/strings.js';
8
import './countBadge.css';
9
import { Disposable, IDisposable, MutableDisposable, toDisposable } from '../../../common/lifecycle.js';
10
import { getBaseLayerHoverDelegate } from '../hover/hoverDelegate2.js';
11
12
export interface ICountBadgeOptions {
13
readonly count?: number;
14
readonly countFormat?: string;
15
readonly titleFormat?: string;
16
}
17
18
export interface ICountBadgeStyles {
19
readonly badgeBackground: string | undefined;
20
readonly badgeForeground: string | undefined;
21
readonly badgeBorder: string | undefined;
22
}
23
24
export const unthemedCountStyles: ICountBadgeStyles = {
25
badgeBackground: '#4D4D4D',
26
badgeForeground: '#FFFFFF',
27
badgeBorder: undefined
28
};
29
30
export class CountBadge extends Disposable {
31
32
private element: HTMLElement;
33
private count: number = 0;
34
private countFormat: string;
35
private titleFormat: string;
36
private readonly hover = this._register(new MutableDisposable<IDisposable>());
37
38
constructor(container: HTMLElement, private readonly options: ICountBadgeOptions, private readonly styles: ICountBadgeStyles) {
39
40
super();
41
this.element = append(container, $('.monaco-count-badge'));
42
this._register(toDisposable(() => container.removeChild(this.element)));
43
this.countFormat = this.options.countFormat || '{0}';
44
this.titleFormat = this.options.titleFormat || '';
45
this.setCount(this.options.count || 0);
46
this.updateHover();
47
}
48
49
setCount(count: number) {
50
this.count = count;
51
this.render();
52
}
53
54
setCountFormat(countFormat: string) {
55
this.countFormat = countFormat;
56
this.render();
57
}
58
59
setTitleFormat(titleFormat: string) {
60
this.titleFormat = titleFormat;
61
this.updateHover();
62
this.render();
63
}
64
65
private updateHover(): void {
66
if (this.titleFormat !== '' && !this.hover.value) {
67
this.hover.value = getBaseLayerHoverDelegate().setupDelayedHoverAtMouse(this.element, () => ({ content: format(this.titleFormat, this.count), appearance: { compact: true } }));
68
} else if (this.titleFormat === '' && this.hover.value) {
69
this.hover.value = undefined;
70
}
71
}
72
73
private render() {
74
this.element.textContent = format(this.countFormat, this.count);
75
76
this.element.style.backgroundColor = this.styles.badgeBackground ?? '';
77
this.element.style.color = this.styles.badgeForeground ?? '';
78
79
if (this.styles.badgeBorder) {
80
this.element.style.border = `1px solid ${this.styles.badgeBorder}`;
81
}
82
}
83
}
84
85