|
| 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