Skip to content

Commit 57db72d

Browse files
committed
More adoption of MutableDisposable
1 parent ec02bab commit 57db72d

File tree

4 files changed

+34
-44
lines changed

4 files changed

+34
-44
lines changed

addons/xterm-addon-search/src/SearchAddon.ts

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import { Terminal, IDisposable, ITerminalAddon, IDecoration } from 'xterm';
77
import { EventEmitter } from 'common/EventEmitter';
8-
import { Disposable, toDisposable, disposeArray } from 'common/Lifecycle';
8+
import { Disposable, toDisposable, disposeArray, MutableDisposable } from 'common/Lifecycle';
99

1010
export interface ISearchOptions {
1111
regex?: boolean;
@@ -66,7 +66,7 @@ export class SearchAddon extends Disposable implements ITerminalAddon {
6666
private _cachedSearchTerm: string | undefined;
6767
private _highlightedLines: Set<number> = new Set();
6868
private _highlightDecorations: IHighlight[] = [];
69-
private _selectedDecoration: IHighlight | undefined;
69+
private _selectedDecoration: MutableDisposable<IHighlight> = this.register(new MutableDisposable());
7070
private _highlightLimit: number;
7171
private _lastSearchOptions: ISearchOptions | undefined;
7272
private _highlightTimeout: number | undefined;
@@ -110,7 +110,7 @@ export class SearchAddon extends Disposable implements ITerminalAddon {
110110
}
111111

112112
public clearDecorations(retainCachedSearchTerm?: boolean): void {
113-
this.clearActiveDecoration();
113+
this._selectedDecoration.clear();
114114
disposeArray(this._highlightDecorations);
115115
this._highlightDecorations = [];
116116
this._highlightedLines.clear();
@@ -119,11 +119,6 @@ export class SearchAddon extends Disposable implements ITerminalAddon {
119119
}
120120
}
121121

122-
public clearActiveDecoration(): void {
123-
this._selectedDecoration?.dispose();
124-
this._selectedDecoration = undefined;
125-
}
126-
127122
/**
128123
* Find the next instance of the term, then scroll to and select it. If it
129124
* doesn't exist, do nothing.
@@ -320,8 +315,8 @@ export class SearchAddon extends Disposable implements ITerminalAddon {
320315
private _fireResults(searchOptions?: ISearchOptions): void {
321316
if (searchOptions?.decorations) {
322317
let resultIndex = -1;
323-
if (this._selectedDecoration) {
324-
const selectedMatch = this._selectedDecoration.match;
318+
if (this._selectedDecoration.value) {
319+
const selectedMatch = this._selectedDecoration.value.match;
325320
for (let i = 0; i < this._highlightDecorations.length; i++) {
326321
const match = this._highlightDecorations[i].match;
327322
if (match.row === selectedMatch.row && match.col === selectedMatch.col && match.size === selectedMatch.size) {
@@ -642,7 +637,7 @@ export class SearchAddon extends Disposable implements ITerminalAddon {
642637
*/
643638
private _selectResult(result: ISearchResult | undefined, options?: ISearchDecorationOptions, noScroll?: boolean): boolean {
644639
const terminal = this._terminal!;
645-
this.clearActiveDecoration();
640+
this._selectedDecoration.clear();
646641
if (!result) {
647642
terminal.clearSelection();
648643
return false;
@@ -666,7 +661,7 @@ export class SearchAddon extends Disposable implements ITerminalAddon {
666661
disposables.push(marker);
667662
disposables.push(decoration.onRender((e) => this._applyStyles(e, options.activeMatchBorder, true)));
668663
disposables.push(decoration.onDispose(() => disposeArray(disposables)));
669-
this._selectedDecoration = { decoration, match: result, dispose() { decoration.dispose(); } };
664+
this._selectedDecoration.value = { decoration, match: result, dispose() { decoration.dispose(); } };
670665
}
671666
}
672667
}

addons/xterm-addon-webgl/src/WebglRenderer.ts

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ export class WebglRenderer extends Disposable implements IRenderer {
4141

4242
private _canvas: HTMLCanvasElement;
4343
private _gl: IWebGL2RenderingContext;
44-
private _rectangleRenderer?: RectangleRenderer;
45-
private _glyphRenderer?: GlyphRenderer;
44+
private _rectangleRenderer: MutableDisposable<RectangleRenderer> = this.register(new MutableDisposable());
45+
private _glyphRenderer: MutableDisposable<GlyphRenderer> = this.register(new MutableDisposable());
4646

4747
public readonly dimensions: IRenderDimensions;
4848

@@ -128,7 +128,7 @@ export class WebglRenderer extends Disposable implements IRenderer {
128128

129129
this._core.screenElement!.appendChild(this._canvas);
130130

131-
[this._rectangleRenderer, this._glyphRenderer] = this._initializeWebGLState();
131+
[this._rectangleRenderer.value, this._glyphRenderer.value] = this._initializeWebGLState();
132132

133133
this._isAttached = this._coreBrowserService.window.document.body.contains(this._core.screenElement!);
134134

@@ -182,10 +182,10 @@ export class WebglRenderer extends Disposable implements IRenderer {
182182
this._core.screenElement!.style.width = `${this.dimensions.css.canvas.width}px`;
183183
this._core.screenElement!.style.height = `${this.dimensions.css.canvas.height}px`;
184184

185-
this._rectangleRenderer?.setDimensions(this.dimensions);
186-
this._rectangleRenderer?.handleResize();
187-
this._glyphRenderer?.setDimensions(this.dimensions);
188-
this._glyphRenderer?.handleResize();
185+
this._rectangleRenderer.value?.setDimensions(this.dimensions);
186+
this._rectangleRenderer.value?.handleResize();
187+
this._glyphRenderer.value?.setDimensions(this.dimensions);
188+
this._glyphRenderer.value?.handleResize();
189189

190190
this._refreshCharAtlas();
191191

@@ -241,17 +241,14 @@ export class WebglRenderer extends Disposable implements IRenderer {
241241
* Initializes members dependent on WebGL context state.
242242
*/
243243
private _initializeWebGLState(): [RectangleRenderer, GlyphRenderer] {
244-
// Dispose any previous rectangle and glyph renderers before creating new ones.
245-
this._rectangleRenderer?.dispose();
246-
this._glyphRenderer?.dispose();
247-
248-
this._rectangleRenderer = this.register(new RectangleRenderer(this._terminal, this._gl, this.dimensions, this._themeService));
249-
this._glyphRenderer = this.register(new GlyphRenderer(this._terminal, this._gl, this.dimensions));
244+
this._rectangleRenderer.value = new RectangleRenderer(this._terminal, this._gl, this.dimensions, this._themeService);
245+
this._glyphRenderer.value = new GlyphRenderer(this._terminal, this._gl, this.dimensions);
250246

251247
// Update dimensions and acquire char atlas
252248
this.handleCharSizeChanged();
253249

254-
return [this._rectangleRenderer, this._glyphRenderer];
250+
return [this._rectangleRenderer.value, this._glyphRenderer.value
251+
];
255252
}
256253

257254
/**
@@ -284,7 +281,7 @@ export class WebglRenderer extends Disposable implements IRenderer {
284281
}
285282
this._charAtlas = atlas;
286283
this._charAtlas.warmUp();
287-
this._glyphRenderer?.setAtlas(this._charAtlas);
284+
this._glyphRenderer.value?.setAtlas(this._charAtlas);
288285
}
289286

290287
/**
@@ -340,14 +337,14 @@ export class WebglRenderer extends Disposable implements IRenderer {
340337
l.handleGridChanged(this._terminal, start, end);
341338
}
342339

343-
if (!this._glyphRenderer || !this._rectangleRenderer) {
340+
if (!this._glyphRenderer.value || !this._rectangleRenderer.value) {
344341
return;
345342
}
346343

347344
// Tell renderer the frame is beginning
348345
// upon a model clear also refresh the full viewport model
349346
// (also triggered by an atlas page merge, part of #4480)
350-
if (this._glyphRenderer.beginFrame()) {
347+
if (this._glyphRenderer.value.beginFrame()) {
351348
this._clearModel(true);
352349
this._updateModel(0, this._terminal.rows - 1);
353350
} else {
@@ -356,10 +353,10 @@ export class WebglRenderer extends Disposable implements IRenderer {
356353
}
357354

358355
// Render
359-
this._rectangleRenderer?.renderBackgrounds();
360-
this._glyphRenderer?.render(this._model);
356+
this._rectangleRenderer.value.renderBackgrounds();
357+
this._glyphRenderer.value.render(this._model);
361358
if (!this._cursorBlinkStateManager.value || this._cursorBlinkStateManager.value.isCursorVisible) {
362-
this._rectangleRenderer?.renderCursor();
359+
this._rectangleRenderer.value.renderCursor();
363360
}
364361
}
365362

@@ -502,7 +499,7 @@ export class WebglRenderer extends Disposable implements IRenderer {
502499
this._model.cells[i + RENDER_MODEL_FG_OFFSET] = this._cellColorResolver.result.fg;
503500
this._model.cells[i + RENDER_MODEL_EXT_OFFSET] = this._cellColorResolver.result.ext;
504501

505-
this._glyphRenderer!.updateCell(x, y, code, this._cellColorResolver.result.bg, this._cellColorResolver.result.fg, this._cellColorResolver.result.ext, chars, lastBg);
502+
this._glyphRenderer.value!.updateCell(x, y, code, this._cellColorResolver.result.bg, this._cellColorResolver.result.fg, this._cellColorResolver.result.ext, chars, lastBg);
506503

507504
if (isJoined) {
508505
// Restore work cell
@@ -511,7 +508,7 @@ export class WebglRenderer extends Disposable implements IRenderer {
511508
// Null out non-first cells
512509
for (x++; x < lastCharX; x++) {
513510
j = ((y * terminal.cols) + x) * RENDER_MODEL_INDICIES_PER_CELL;
514-
this._glyphRenderer!.updateCell(x, y, NULL_CELL_CODE, 0, 0, 0, NULL_CELL_CHAR, 0);
511+
this._glyphRenderer.value!.updateCell(x, y, NULL_CELL_CODE, 0, 0, 0, NULL_CELL_CHAR, 0);
515512
this._model.cells[j] = NULL_CELL_CODE;
516513
this._model.cells[j + RENDER_MODEL_BG_OFFSET] = this._cellColorResolver.result.bg;
517514
this._model.cells[j + RENDER_MODEL_FG_OFFSET] = this._cellColorResolver.result.fg;
@@ -521,9 +518,9 @@ export class WebglRenderer extends Disposable implements IRenderer {
521518
}
522519
}
523520
if (modelUpdated) {
524-
this._rectangleRenderer!.updateBackgrounds(this._model);
521+
this._rectangleRenderer.value!.updateBackgrounds(this._model);
525522
}
526-
this._rectangleRenderer!.updateCursor(this._model);
523+
this._rectangleRenderer.value!.updateCursor(this._model);
527524
}
528525

529526
/**

addons/xterm-addon-webgl/src/renderLayer/BaseRenderLayer.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ export abstract class BaseRenderLayer extends Disposable implements IRenderLayer
4949
}));
5050
this.register(toDisposable(() => {
5151
this._canvas.remove();
52-
this._charAtlas?.dispose();
5352
}));
5453
}
5554

src/browser/Terminal.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ import { ThemeService } from 'browser/services/ThemeService';
4444
import { color, rgba } from 'common/Color';
4545
import { CoreTerminal } from 'common/CoreTerminal';
4646
import { EventEmitter, IEvent, forwardEvent } from 'common/EventEmitter';
47-
import { toDisposable } from 'common/Lifecycle';
47+
import { MutableDisposable, toDisposable } from 'common/Lifecycle';
4848
import * as Browser from 'common/Platform';
4949
import { ColorRequestType, CoreMouseAction, CoreMouseButton, CoreMouseEventType, IColorEvent, ITerminalOptions, KeyboardResultType, ScrollSource, SpecialColorIndex } from 'common/Types';
5050
import { DEFAULT_ATTR_DATA } from 'common/buffer/BufferLine';
@@ -118,7 +118,7 @@ export class Terminal extends CoreTerminal implements ITerminal {
118118
public linkifier2: ILinkifier2;
119119
public viewport: IViewport | undefined;
120120
private _compositionHelper: ICompositionHelper | undefined;
121-
private _accessibilityManager: AccessibilityManager | undefined;
121+
private _accessibilityManager: MutableDisposable<AccessibilityManager> = this.register(new MutableDisposable());
122122

123123
private readonly _onCursorMove = this.register(new EventEmitter<void>());
124124
public readonly onCursorMove = this._onCursorMove.event;
@@ -252,12 +252,11 @@ export class Terminal extends CoreTerminal implements ITerminal {
252252

253253
private _handleScreenReaderModeOptionChange(value: boolean): void {
254254
if (value) {
255-
if (!this._accessibilityManager && this._renderService) {
256-
this._accessibilityManager = this._instantiationService.createInstance(AccessibilityManager, this);
255+
if (!this._accessibilityManager.value && this._renderService) {
256+
this._accessibilityManager.value = this._instantiationService.createInstance(AccessibilityManager, this);
257257
}
258258
} else {
259-
this._accessibilityManager?.dispose();
260-
this._accessibilityManager = undefined;
259+
this._accessibilityManager.clear();
261260
}
262261
}
263262

@@ -535,7 +534,7 @@ export class Terminal extends CoreTerminal implements ITerminal {
535534
if (this.options.screenReaderMode) {
536535
// Note that this must be done *after* the renderer is created in order to
537536
// ensure the correct order of the dprchange event
538-
this._accessibilityManager = this._instantiationService.createInstance(AccessibilityManager, this);
537+
this._accessibilityManager.value = this._instantiationService.createInstance(AccessibilityManager, this);
539538
}
540539
this.register(this.optionsService.onSpecificOptionChange('screenReaderMode', e => this._handleScreenReaderModeOptionChange(e)));
541540

0 commit comments

Comments
 (0)