-
Notifications
You must be signed in to change notification settings - Fork 30.4k
/
Copy pathprogressbar.ts
216 lines (171 loc) · 5.81 KB
/
progressbar.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { hide, show } from 'vs/base/browser/dom';
import { RunOnceScheduler } from 'vs/base/common/async';
import { Disposable } from 'vs/base/common/lifecycle';
import { isNumber } from 'vs/base/common/types';
import 'vs/css!./progressbar';
const CSS_DONE = 'done';
const CSS_ACTIVE = 'active';
const CSS_INFINITE = 'infinite';
const CSS_INFINITE_LONG_RUNNING = 'infinite-long-running';
const CSS_DISCRETE = 'discrete';
export interface IProgressBarOptions extends IProgressBarStyles {
}
export interface IProgressBarStyles {
progressBarBackground: string | undefined;
}
export const unthemedProgressBarOptions: IProgressBarOptions = {
progressBarBackground: undefined
};
/**
* A progress bar with support for infinite or discrete progress.
*/
export class ProgressBar extends Disposable {
/**
* After a certain time of showing the progress bar, switch
* to long-running mode and throttle animations to reduce
* the pressure on the GPU process.
*
* https://github.com/microsoft/vscode/issues/97900
* https://github.com/microsoft/vscode/issues/138396
*/
private static readonly LONG_RUNNING_INFINITE_THRESHOLD = 10000;
private workedVal: number;
private element!: HTMLElement;
private bit!: HTMLElement;
private totalWork: number | undefined;
private showDelayedScheduler: RunOnceScheduler;
private longRunningScheduler: RunOnceScheduler;
constructor(container: HTMLElement, options?: IProgressBarOptions) {
super();
this.workedVal = 0;
this.showDelayedScheduler = this._register(new RunOnceScheduler(() => show(this.element), 0));
this.longRunningScheduler = this._register(new RunOnceScheduler(() => this.infiniteLongRunning(), ProgressBar.LONG_RUNNING_INFINITE_THRESHOLD));
this.create(container, options);
}
private create(container: HTMLElement, options?: IProgressBarOptions): void {
this.element = document.createElement('div');
this.element.classList.add('monaco-progress-container');
this.element.setAttribute('role', 'progressbar');
this.element.setAttribute('aria-valuemin', '0');
container.appendChild(this.element);
this.bit = document.createElement('div');
this.bit.classList.add('progress-bit');
this.bit.style.backgroundColor = options?.progressBarBackground || '#0E70C0';
this.element.appendChild(this.bit);
}
private off(): void {
this.bit.style.width = 'inherit';
this.bit.style.opacity = '1';
this.element.classList.remove(CSS_ACTIVE, CSS_INFINITE, CSS_INFINITE_LONG_RUNNING, CSS_DISCRETE);
this.workedVal = 0;
this.totalWork = undefined;
this.longRunningScheduler.cancel();
}
/**
* Indicates to the progress bar that all work is done.
*/
done(): ProgressBar {
return this.doDone(true);
}
/**
* Stops the progressbar from showing any progress instantly without fading out.
*/
stop(): ProgressBar {
return this.doDone(false);
}
private doDone(delayed: boolean): ProgressBar {
this.element.classList.add(CSS_DONE);
// discrete: let it grow to 100% width and hide afterwards
if (!this.element.classList.contains(CSS_INFINITE)) {
this.bit.style.width = 'inherit';
if (delayed) {
setTimeout(() => this.off(), 200);
} else {
this.off();
}
}
// infinite: let it fade out and hide afterwards
else {
this.bit.style.opacity = '0';
if (delayed) {
setTimeout(() => this.off(), 200);
} else {
this.off();
}
}
return this;
}
/**
* Use this mode to indicate progress that has no total number of work units.
*/
infinite(): ProgressBar {
this.bit.style.width = '2%';
this.bit.style.opacity = '1';
this.element.classList.remove(CSS_DISCRETE, CSS_DONE, CSS_INFINITE_LONG_RUNNING);
this.element.classList.add(CSS_ACTIVE, CSS_INFINITE);
this.longRunningScheduler.schedule();
return this;
}
private infiniteLongRunning(): void {
this.element.classList.add(CSS_INFINITE_LONG_RUNNING);
}
/**
* Tells the progress bar the total number of work. Use in combination with workedVal() to let
* the progress bar show the actual progress based on the work that is done.
*/
total(value: number): ProgressBar {
this.workedVal = 0;
this.totalWork = value;
this.element.setAttribute('aria-valuemax', value.toString());
return this;
}
/**
* Finds out if this progress bar is configured with total work
*/
hasTotal(): boolean {
return isNumber(this.totalWork);
}
/**
* Tells the progress bar that an increment of work has been completed.
*/
worked(value: number): ProgressBar {
value = Math.max(1, Number(value));
return this.doSetWorked(this.workedVal + value);
}
/**
* Tells the progress bar the total amount of work that has been completed.
*/
setWorked(value: number): ProgressBar {
value = Math.max(1, Number(value));
return this.doSetWorked(value);
}
private doSetWorked(value: number): ProgressBar {
const totalWork = this.totalWork || 100;
this.workedVal = value;
this.workedVal = Math.min(totalWork, this.workedVal);
this.element.classList.remove(CSS_INFINITE, CSS_INFINITE_LONG_RUNNING, CSS_DONE);
this.element.classList.add(CSS_ACTIVE, CSS_DISCRETE);
this.element.setAttribute('aria-valuenow', value.toString());
this.bit.style.width = 100 * (this.workedVal / (totalWork)) + '%';
return this;
}
getContainer(): HTMLElement {
return this.element;
}
show(delay?: number): void {
this.showDelayedScheduler.cancel();
if (typeof delay === 'number') {
this.showDelayedScheduler.schedule(delay);
} else {
show(this.element);
}
}
hide(): void {
hide(this.element);
this.showDelayedScheduler.cancel();
}
}