1
+ /*---------------------------------------------------------
2
+ * Copyright (C) Microsoft Corporation. All rights reserved.
3
+ *--------------------------------------------------------*/
4
+
5
+ import {
6
+ StatusBarItem ,
7
+ StatusBarAlignment ,
8
+ Disposable ,
9
+ window } from "vscode" ;
10
+
11
+ export function setAnimatedStatusBarMessage ( text : string , hideWhenDone : Thenable < any > ) : Disposable {
12
+ let animatedStatusBarItem : AnimatedStatusBarItem = new AnimatedStatusBarItem ( text ) ;
13
+ animatedStatusBarItem . show ( hideWhenDone ) ;
14
+ return animatedStatusBarItem ;
15
+ }
16
+
17
+ class AnimatedStatusBarItem implements StatusBarItem {
18
+ private readonly animationRate : number ;
19
+ private statusBarItem : StatusBarItem ;
20
+ private maxCount : number ;
21
+ private counter : number ;
22
+ private baseText : string ;
23
+ private timerInterval : number ;
24
+ private elapsedTime : number ;
25
+ private intervalId : NodeJS . Timer ;
26
+ private suffixStates : string [ ] ;
27
+
28
+ public get alignment ( ) : StatusBarAlignment {
29
+ return this . statusBarItem . alignment ;
30
+ }
31
+
32
+ public get priority ( ) : number {
33
+ return this . statusBarItem . priority ;
34
+ }
35
+
36
+ public get text ( ) : string {
37
+ return this . statusBarItem . text ;
38
+ }
39
+
40
+ public set text ( value : string ) {
41
+ this . statusBarItem . text = value ;
42
+ }
43
+
44
+ public get tooltip ( ) : string {
45
+ return this . statusBarItem . tooltip ;
46
+ }
47
+
48
+ public set tooltip ( value : string ) {
49
+ this . statusBarItem . tooltip = value ;
50
+ }
51
+
52
+ public get color ( ) : string {
53
+ return this . statusBarItem . color ;
54
+ }
55
+
56
+ public set color ( value : string ) {
57
+ this . statusBarItem . color = value ;
58
+ }
59
+
60
+ public get command ( ) : string {
61
+ return this . statusBarItem . command ;
62
+ }
63
+
64
+ public set command ( value : string ) {
65
+ this . statusBarItem . command = value ;
66
+ }
67
+
68
+ constructor ( baseText : string , alignment ?: StatusBarAlignment , priority ?: number ) {
69
+ this . animationRate = 1 ;
70
+ this . statusBarItem = window . createStatusBarItem ( alignment , priority ) ;
71
+ this . baseText = baseText ;
72
+ this . counter = 0 ;
73
+ this . suffixStates = [ " " , ". " , ".. " , "..." ] ;
74
+ this . maxCount = this . suffixStates . length ;
75
+ this . timerInterval = ( ( 1 / this . maxCount ) * 1000 ) / this . animationRate ;
76
+ this . elapsedTime = 0 ;
77
+ }
78
+
79
+ public show ( hideWhenDone ?: Thenable < any > ) : void {
80
+ this . statusBarItem . show ( ) ;
81
+ this . start ( ) ;
82
+ if ( hideWhenDone !== undefined ) {
83
+ hideWhenDone . then ( ( ) => this . hide ( ) ) ;
84
+ }
85
+ }
86
+
87
+ public hide ( ) : void {
88
+ this . stop ( ) ;
89
+ this . statusBarItem . hide ( ) ;
90
+ }
91
+
92
+ public dispose ( ) : void {
93
+ this . statusBarItem . dispose ( ) ;
94
+ }
95
+
96
+ private updateCounter ( ) : void {
97
+ this . counter = ( this . counter + 1 ) % this . maxCount ;
98
+ this . elapsedTime = this . elapsedTime + this . timerInterval ;
99
+ }
100
+
101
+ private updateText ( ) : void {
102
+ this . text = this . baseText + this . suffixStates [ this . counter ] ;
103
+ }
104
+
105
+ private update ( ) : void {
106
+ this . updateCounter ( ) ;
107
+ this . updateText ( ) ;
108
+ }
109
+
110
+ private reset ( ) : void {
111
+ this . counter = 0 ;
112
+ this . updateText ( ) ;
113
+ }
114
+
115
+ private start ( ) : void {
116
+ this . reset ( ) ;
117
+ this . intervalId = setInterval ( ( ) => this . update ( ) , this . timerInterval ) ;
118
+ }
119
+
120
+ private stop ( ) : void {
121
+ clearInterval ( this . intervalId ) ;
122
+ }
123
+ }
0 commit comments