Skip to content

Commit 00c6a13

Browse files
author
Kapil Borle
committed
Add animated status bar creation method
* AnimatedStatusBarItem now implements vscode.StatusBarItem interface * setAnimatedStatusBarMessage methods provides a simple way of creating an animated status message
1 parent 8456749 commit 00c6a13

File tree

2 files changed

+117
-62
lines changed

2 files changed

+117
-62
lines changed

src/animatedStatusBar.ts

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
import {
2+
StatusBarItem,
3+
StatusBarAlignment,
4+
Disposable,
5+
window} from "vscode";
6+
7+
export function setAnimatedStatusBarMessage(text: string, hideWhenDone: Thenable<any>): Disposable {
8+
let animatedStatusBarItem: AnimatedStatuBarItem = new AnimatedStatuBarItem(text);
9+
animatedStatusBarItem.show(hideWhenDone);
10+
return animatedStatusBarItem;
11+
}
12+
13+
class AnimatedStatuBarItem implements StatusBarItem {
14+
private statusBarItem: StatusBarItem;
15+
private maxCount: number;
16+
private counter: number;
17+
private baseText: string;
18+
private timerInterval: number;
19+
private ticks: number;
20+
private intervalId: NodeJS.Timer;
21+
22+
get alignment(): StatusBarAlignment {
23+
return this.statusBarItem.alignment;
24+
}
25+
26+
get priority(): number {
27+
return this.statusBarItem.priority;
28+
}
29+
30+
get text(): string {
31+
return this.statusBarItem.text;
32+
}
33+
34+
set text(value: string) {
35+
this.statusBarItem.text = value;
36+
}
37+
38+
get tooltip(): string {
39+
return this.statusBarItem.tooltip;
40+
}
41+
42+
set tooltip(value: string) {
43+
this.statusBarItem.tooltip = value;
44+
}
45+
46+
get color(): string {
47+
return this.statusBarItem.color;
48+
}
49+
50+
set color(value: string) {
51+
this.statusBarItem.color = value;
52+
}
53+
54+
get command(): string {
55+
return this.statusBarItem.command;
56+
}
57+
58+
set command(value: string) {
59+
this.statusBarItem.command = value;
60+
}
61+
62+
constructor(baseText: string, alignment?: StatusBarAlignment, priority?: number) {
63+
this.statusBarItem = window.createStatusBarItem(alignment, priority);
64+
this.baseText = baseText;
65+
this.maxCount = 4;
66+
this.counter = 0;
67+
this.timerInterval = 300;
68+
this.ticks = 0;
69+
}
70+
71+
show(hideWhenDone?: Thenable<any>): void {
72+
this.statusBarItem.show();
73+
this._start();
74+
if (hideWhenDone !== undefined) {
75+
hideWhenDone.then(() => this.hide());
76+
}
77+
}
78+
79+
hide(): void {
80+
this._stop();
81+
this.statusBarItem.hide();
82+
}
83+
84+
dispose(): void {
85+
this.statusBarItem.dispose();
86+
}
87+
88+
_updateCounter(): void {
89+
this.counter = (this.counter + 1) % this.maxCount;
90+
this.ticks = this.ticks + 1;
91+
}
92+
93+
_updateText(): void {
94+
this.text = this.baseText + ".".repeat(this.counter) + " ".repeat(this.maxCount - this.counter - 1);
95+
}
96+
97+
_update(): void {
98+
this._updateCounter();
99+
this._updateText();
100+
}
101+
102+
_reset(): void {
103+
this.counter = 0;
104+
this._updateText();
105+
}
106+
107+
_start(): void {
108+
this._reset();
109+
this.intervalId = setInterval(() => this._update(), this.timerInterval);
110+
}
111+
112+
_stop(): void {
113+
clearInterval(this.intervalId);
114+
}
115+
}

src/features/DocumentFormatter.ts

Lines changed: 2 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import Window = vscode.window;
1818
import { IFeature } from '../feature';
1919
import * as Settings from '../settings';
2020
import * as Utils from '../utils';
21+
import * as AnimatedStatusBar from '../animatedStatusBar';
2122

2223
export namespace ScriptFileMarkersRequest {
2324
export const type: RequestType<any, any, void> = { get method(): string { return "powerShell/getScriptFileMarkers"; } };
@@ -80,66 +81,6 @@ function editComparer(leftOperand: ScriptRegion, rightOperand: ScriptRegion): nu
8081
}
8182
}
8283

83-
class AnimatedStatuBarItem {
84-
private statusBarItem: vscode.StatusBarItem;
85-
private maxCount: number;
86-
private counter: number;
87-
private baseText: string;
88-
private text: string;
89-
private timerInterval: number;
90-
private ticks: number;
91-
private intervalId: NodeJS.Timer;
92-
93-
constructor() {
94-
this.statusBarItem = Window.createStatusBarItem();
95-
this.baseText = "formatting";
96-
this.maxCount = 4;
97-
this.counter = 0;
98-
this.timerInterval = 300;
99-
this.ticks = 0;
100-
}
101-
102-
updateCounter() {
103-
this.counter = (this.counter + 1) % this.maxCount;
104-
this.ticks = this.ticks + 1;
105-
}
106-
107-
updateText() {
108-
this.text = this.baseText + ".".repeat(this.counter) + " ".repeat(this.maxCount - this.counter - 1);
109-
}
110-
111-
update() {
112-
this.updateCounter();
113-
this.updateText();
114-
this.statusBarItem.text = this.text;
115-
}
116-
117-
reset() {
118-
this.counter = 0;
119-
}
120-
121-
start(hideWhenDone?: Thenable<any>) {
122-
this.intervalId = setInterval(() => this.update(), this.timerInterval);
123-
this.show();
124-
if (hideWhenDone !== undefined) {
125-
hideWhenDone.then(() => this.stop());
126-
}
127-
}
128-
129-
stop() {
130-
clearInterval(this.intervalId);
131-
this.hide();
132-
}
133-
134-
show() {
135-
this.statusBarItem.show();
136-
}
137-
138-
hide() {
139-
this.statusBarItem.hide();
140-
}
141-
}
142-
14384
class PSDocumentFormattingEditProvider implements DocumentFormattingEditProvider, DocumentRangeFormattingEditProvider {
14485
private languageClient: LanguageClient;
14586

@@ -171,9 +112,8 @@ class PSDocumentFormattingEditProvider implements DocumentFormattingEditProvider
171112
options: FormattingOptions,
172113
token: CancellationToken): TextEdit[] | Thenable<TextEdit[]> {
173114

174-
let status: AnimatedStatuBarItem = new AnimatedStatuBarItem();
175115
let textEdits: Thenable<TextEdit[]> = this.executeRulesInOrder(document, range, options, 0);
176-
status.start(textEdits);
116+
AnimatedStatusBar.setAnimatedStatusBarMessage("formatting", textEdits);
177117
return textEdits;
178118
}
179119

0 commit comments

Comments
 (0)