Skip to content

Commit 994fe20

Browse files
edgardmessiasJohnstonCode
authored andcommitted
refactor: Improved usage of StatusBar (#529)
1 parent 18687d2 commit 994fe20

File tree

5 files changed

+230
-86
lines changed

5 files changed

+230
-86
lines changed

src/repository.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import OperationsImpl from "./operationsImpl";
3131
import { PathNormalizer } from "./pathNormalizer";
3232
import { IRemoteRepository } from "./remoteRepository";
3333
import { Resource } from "./resource";
34-
import { SvnStatusBar } from "./statusBar";
34+
import { StatusBarCommands } from "./statusbar/statusBarCommands";
3535
import { svnErrorCodes } from "./svn";
3636
import { Repository as BaseRepository } from "./svnRepository";
3737
import { toSvnUri } from "./uri";
@@ -61,7 +61,7 @@ function shouldShowProgress(operation: Operation): boolean {
6161

6262
export class Repository implements IRemoteRepository {
6363
public sourceControl: SourceControl;
64-
public statusBar: SvnStatusBar;
64+
public statusBar: StatusBarCommands;
6565
public changes: ISvnResourceGroup;
6666
public unversioned: ISvnResourceGroup;
6767
public remoteChanges?: ISvnResourceGroup;
@@ -209,7 +209,7 @@ export class Repository implements IRemoteRepository {
209209
this.sourceControl.quickDiffProvider = this;
210210
this.disposables.push(this.sourceControl);
211211

212-
this.statusBar = new SvnStatusBar(this);
212+
this.statusBar = new StatusBarCommands(this);
213213
this.disposables.push(this.statusBar);
214214
this.statusBar.onDidChange(
215215
() => (this.sourceControl.statusBarCommands = this.statusBar.commands),

src/statusBar.ts

-83
This file was deleted.

src/statusbar/checkoutStatusBar.ts

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { Command, Disposable, Event, EventEmitter } from "vscode";
2+
import { Operation } from "../common/types";
3+
import { Repository } from "../repository";
4+
5+
export class CheckoutStatusBar {
6+
private _onDidChange = new EventEmitter<void>();
7+
get onDidChange(): Event<void> {
8+
return this._onDidChange.event;
9+
}
10+
private disposables: Disposable[] = [];
11+
12+
constructor(private repository: Repository) {
13+
repository.onDidChangeStatus(
14+
this._onDidChange.fire,
15+
this._onDidChange,
16+
this.disposables
17+
);
18+
19+
repository.onDidChangeOperations(
20+
this._onDidChange.fire,
21+
this._onDidChange,
22+
this.disposables
23+
);
24+
}
25+
26+
get command(): Command | undefined {
27+
if (!this.repository.currentBranch) {
28+
return;
29+
}
30+
31+
const isSwitchRunning =
32+
this.repository.operations.isRunning(Operation.SwitchBranch) ||
33+
this.repository.operations.isRunning(Operation.NewBranch);
34+
35+
const title = `$(git-branch) ${this.repository.currentBranch}${
36+
isSwitchRunning ? ` (Switching)` : ""
37+
}`;
38+
39+
return {
40+
command: "svn.switchBranch",
41+
tooltip: "Switch Branch...",
42+
title,
43+
arguments: [this.repository.sourceControl]
44+
};
45+
}
46+
47+
public dispose(): void {
48+
this.disposables.forEach(d => d.dispose());
49+
}
50+
}

src/statusbar/statusBarCommands.ts

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { Command, Disposable, Event, EventEmitter } from "vscode";
2+
import { Repository } from "../repository";
3+
import { anyEvent } from "../util";
4+
import { CheckoutStatusBar } from "./checkoutStatusBar";
5+
import { SyncStatusBar } from "./syncStatusBar";
6+
7+
export class StatusBarCommands {
8+
private checkoutStatusBar: CheckoutStatusBar;
9+
private syncStatusBar: SyncStatusBar;
10+
11+
private disposables: Disposable[] = [];
12+
13+
constructor(private repository: Repository) {
14+
this.checkoutStatusBar = new CheckoutStatusBar(repository);
15+
this.syncStatusBar = new SyncStatusBar(repository);
16+
17+
this.disposables.push(this.checkoutStatusBar, this.syncStatusBar);
18+
}
19+
20+
get onDidChange(): Event<void> {
21+
return anyEvent(
22+
this.syncStatusBar.onDidChange,
23+
this.checkoutStatusBar.onDidChange
24+
);
25+
}
26+
27+
get commands(): Command[] {
28+
const result: Command[] = [];
29+
30+
const checkout = this.checkoutStatusBar.command;
31+
32+
if (checkout) {
33+
result.push(checkout);
34+
}
35+
36+
const sync = this.syncStatusBar.command;
37+
38+
if (sync) {
39+
result.push(sync);
40+
}
41+
42+
return result;
43+
}
44+
45+
public dispose(): void {
46+
this.disposables.forEach(disposable => disposable.dispose());
47+
}
48+
}

src/statusbar/syncStatusBar.ts

+129
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
import { Command, Disposable, Event, EventEmitter } from "vscode";
2+
import { Operation } from "../common/types";
3+
import { Repository } from "../repository";
4+
5+
interface ISyncStatusBarState {
6+
isIncomplete: boolean;
7+
isOperationRunning: boolean;
8+
isStatusRemoteRunning: boolean;
9+
isSyncRunning: boolean;
10+
needCleanUp: boolean;
11+
remoteChangedFiles: number;
12+
}
13+
14+
export class SyncStatusBar {
15+
private static startState: ISyncStatusBarState = {
16+
isIncomplete: false,
17+
isOperationRunning: false,
18+
isStatusRemoteRunning: false,
19+
isSyncRunning: false,
20+
needCleanUp: false,
21+
remoteChangedFiles: 0
22+
};
23+
24+
private _onDidChange = new EventEmitter<void>();
25+
get onDidChange(): Event<void> {
26+
return this._onDidChange.event;
27+
}
28+
private disposables: Disposable[] = [];
29+
30+
private _state: ISyncStatusBarState = SyncStatusBar.startState;
31+
private get state() {
32+
return this._state;
33+
}
34+
private set state(state: ISyncStatusBarState) {
35+
this._state = state;
36+
this._onDidChange.fire();
37+
}
38+
39+
constructor(private repository: Repository) {
40+
repository.onDidChangeStatus(this.onModelChange, this, this.disposables);
41+
repository.onDidChangeOperations(
42+
this.onOperationsChange,
43+
this,
44+
this.disposables
45+
);
46+
this._onDidChange.fire();
47+
}
48+
49+
private onOperationsChange(): void {
50+
const isSyncRunning =
51+
this.repository.operations.isRunning(Operation.SwitchBranch) ||
52+
this.repository.operations.isRunning(Operation.NewBranch) ||
53+
this.repository.operations.isRunning(Operation.Update);
54+
55+
const isStatusRemoteRunning = this.repository.operations.isRunning(
56+
Operation.StatusRemote
57+
);
58+
59+
const isOperationRunning = !this.repository.operations.isIdle();
60+
61+
this.state = {
62+
...this.state,
63+
isStatusRemoteRunning,
64+
isOperationRunning,
65+
isSyncRunning
66+
};
67+
}
68+
69+
private onModelChange(): void {
70+
this.state = {
71+
...this.state,
72+
remoteChangedFiles: this.repository.remoteChangedFiles
73+
};
74+
}
75+
76+
get command(): Command | undefined {
77+
let icon = "$(sync)";
78+
let text = "";
79+
let command = "";
80+
let tooltip = "";
81+
82+
if (this.state.isSyncRunning) {
83+
command = "";
84+
icon = "$(sync~spin)";
85+
text = "";
86+
tooltip = "Updating Revision...";
87+
} else if (this.state.isStatusRemoteRunning) {
88+
command = "";
89+
icon = "$(sync~spin)";
90+
text = "";
91+
tooltip = "Checking remote updates...";
92+
} else if (this.state.isOperationRunning) {
93+
command = "";
94+
icon = "$(sync~spin)";
95+
text = "Running";
96+
tooltip = "Running...";
97+
} else if (this.state.needCleanUp) {
98+
command = "svn.cleanup";
99+
icon = "$(alert)";
100+
text = "Need cleanup";
101+
tooltip = "Run cleanup command";
102+
} else if (this.state.isIncomplete) {
103+
command = "svn.finishCheckout";
104+
icon = "$(issue-reopened)";
105+
text = "Incomplete (Need finish checkout)";
106+
tooltip = "Run update to complete";
107+
} else if (this.state.remoteChangedFiles > 0) {
108+
icon = "$(cloud-download)";
109+
command = "svn.update";
110+
tooltip = "Update Revision";
111+
text = `${this.state.remoteChangedFiles}↓`;
112+
} else {
113+
command = "svn.update";
114+
tooltip = "Update Revision";
115+
text = `Updated`;
116+
}
117+
118+
return {
119+
command,
120+
title: [icon, text].join(" ").trim(),
121+
tooltip,
122+
arguments: [this.repository]
123+
};
124+
}
125+
126+
public dispose(): void {
127+
this.disposables.forEach(d => d.dispose());
128+
}
129+
}

0 commit comments

Comments
 (0)