|
4 | 4 | */ |
5 | 5 |
|
6 | 6 | import type { Terminal, ITerminalAddon, IDisposable } from '@xterm/xterm'; |
7 | | -import type { ProgressAddon as IProgressApi, IProgress, ProgressHandler } from '@xterm/addon-progress'; |
| 7 | +import type { ProgressAddon as IProgressApi, IProgress } from '@xterm/addon-progress'; |
| 8 | +import { Emitter } from 'vs/base/common/event'; |
| 9 | +import { Disposable } from 'vs/base/common/lifecycle'; |
8 | 10 |
|
9 | 11 |
|
10 | 12 | export const enum ProgressState { |
@@ -32,19 +34,14 @@ function toInt(s: string): number { |
32 | 34 | } |
33 | 35 |
|
34 | 36 |
|
35 | | -export class ProgressAddon implements ITerminalAddon, IProgressApi { |
36 | | - private _seqHandler: IDisposable | undefined; |
| 37 | +export class ProgressAddon extends Disposable implements ITerminalAddon, IProgressApi { |
37 | 38 | private _st: ProgressState = ProgressState.REMOVE; |
38 | 39 | private _pr = 0; |
39 | | - private _handlers: ProgressHandler[] = []; |
40 | | - |
41 | | - public dispose(): void { |
42 | | - this._seqHandler?.dispose(); |
43 | | - this._handlers.length = 0; |
44 | | - } |
| 40 | + private readonly _onChange = this._register(new Emitter<IProgress>()); |
| 41 | + public readonly onChange = this._onChange.event; |
45 | 42 |
|
46 | 43 | public activate(terminal: Terminal): void { |
47 | | - this._seqHandler = terminal.parser.registerOscHandler(9, data => { |
| 44 | + this._register(terminal.parser.registerOscHandler(9, data => { |
48 | 45 | if (!data.startsWith('4;')) { |
49 | 46 | return false; |
50 | 47 | } |
@@ -77,38 +74,20 @@ export class ProgressAddon implements ITerminalAddon, IProgressApi { |
77 | 74 | break; |
78 | 75 | } |
79 | 76 | return true; |
80 | | - }); |
81 | | - } |
82 | | - |
83 | | - public register(handler: ProgressHandler): IDisposable { |
84 | | - const handlers = this._handlers; |
85 | | - handlers.push(handler); |
86 | | - return { |
87 | | - dispose: () => { |
88 | | - const idx = handlers.indexOf(handler); |
89 | | - if (idx !== -1) { |
90 | | - handlers.splice(idx, 1); |
91 | | - } |
92 | | - } |
93 | | - }; |
| 77 | + })); |
94 | 78 | } |
95 | 79 |
|
96 | 80 | public get progress(): IProgress { |
97 | 81 | return { state: this._st, value: this._pr }; |
98 | 82 | } |
99 | 83 |
|
100 | 84 | public set progress(progress: IProgress) { |
101 | | - if (0 <= progress.state && progress.state <= 4) |
102 | | - { |
103 | | - this._st = progress.state; |
104 | | - this._pr = Math.min(Math.max(progress.value, 0), 100); |
105 | | - |
106 | | - // call progress handlers |
107 | | - for (let i = 0; i < this._handlers.length; ++i) { |
108 | | - this._handlers[i](this._st, this._pr); |
109 | | - } |
110 | | - } else { |
| 85 | + if (progress.state < 0 || progress.state > 4) { |
111 | 86 | console.warn(`progress state out of bounds, not applied`); |
| 87 | + return; |
112 | 88 | } |
| 89 | + this._st = progress.state; |
| 90 | + this._pr = Math.min(Math.max(progress.value, 0), 100); |
| 91 | + this._onChange.fire({ state: this._st, value: this._pr }); |
113 | 92 | } |
114 | 93 | } |
0 commit comments