Skip to content

Commit

Permalink
Merge pull request xtermjs#4166 from Tyriar/event_with_emitter
Browse files Browse the repository at this point in the history
Create new event with emitter object to simplify code
  • Loading branch information
Tyriar authored Oct 2, 2022
2 parents 1f8e6f0 + 5b785d2 commit d22f7c9
Show file tree
Hide file tree
Showing 33 changed files with 269 additions and 313 deletions.
11 changes: 5 additions & 6 deletions addons/xterm-addon-canvas/src/CanvasRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { IColorSet, ILinkifier2 } from 'browser/Types';
import { ICharacterJoinerService, ICharSizeService, ICoreBrowserService } from 'browser/services/Services';
import { IBufferService, IOptionsService, IDecorationService, ICoreService } from 'common/services/Services';
import { removeTerminalFromCache } from './atlas/CharAtlasCache';
import { EventEmitter, IEvent } from 'common/EventEmitter';
import { initEvent, EventEmitter, IEvent } from 'common/EventEmitter';
import { observeDevicePixelDimensions } from 'browser/renderer/DevicePixelObserver';

let nextRendererId = 1;
Expand All @@ -27,8 +27,7 @@ export class CanvasRenderer extends Disposable implements IRenderer {

public dimensions: IRenderDimensions;

private readonly _onRequestRedraw = new EventEmitter<IRequestRedrawEvent>();
public readonly onRequestRedraw = this._onRequestRedraw.event;
public readonly onRequestRedraw = initEvent<IRequestRedrawEvent>();

constructor(
private _colors: IColorSet,
Expand All @@ -48,7 +47,7 @@ export class CanvasRenderer extends Disposable implements IRenderer {
new TextRenderLayer(this._screenElement, 0, this._colors, allowTransparency, this._id, this._bufferService, this._optionsService, characterJoinerService, decorationService, this._coreBrowserService),
new SelectionRenderLayer(this._screenElement, 1, this._colors, this._id, this._bufferService, this._coreBrowserService, decorationService, this._optionsService),
new LinkRenderLayer(this._screenElement, 2, this._colors, this._id, linkifier2, this._bufferService, this._optionsService, decorationService, this._coreBrowserService),
new CursorRenderLayer(this._screenElement, 3, this._colors, this._id, this._onRequestRedraw, this._bufferService, this._optionsService, coreService, this._coreBrowserService, decorationService)
new CursorRenderLayer(this._screenElement, 3, this._colors, this._id, this.onRequestRedraw, this._bufferService, this._optionsService, coreService, this._coreBrowserService, decorationService)
];
this.dimensions = {
scaledCharWidth: 0,
Expand Down Expand Up @@ -128,7 +127,7 @@ export class CanvasRenderer extends Disposable implements IRenderer {
this._runOperation(l => l.onSelectionChanged(start, end, columnSelectMode));
// Selection foreground requires a full re-render
if (this._colors.selectionForeground) {
this._onRequestRedraw.fire({ start: 0, end: this._bufferService.rows - 1 });
this.onRequestRedraw.fire({ start: 0, end: this._bufferService.rows - 1 });
}
}

Expand Down Expand Up @@ -201,6 +200,6 @@ export class CanvasRenderer extends Disposable implements IRenderer {
}

private _requestRedrawViewport(): void {
this._onRequestRedraw.fire({ start: 0, end: this._bufferService.rows - 1 });
this.onRequestRedraw.fire({ start: 0, end: this._bufferService.rows - 1 });
}
}
11 changes: 5 additions & 6 deletions addons/xterm-addon-search/src/SearchAddon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/

import { Terminal, IDisposable, ITerminalAddon, IBufferRange, IDecoration } from 'xterm';
import { EventEmitter } from 'common/EventEmitter';
import { initEvent } from 'common/EventEmitter';

export interface ISearchOptions {
regex?: boolean;
Expand Down Expand Up @@ -72,8 +72,7 @@ export class SearchAddon implements ITerminalAddon {

private _resultIndex: number | undefined;

private readonly _onDidChangeResults = new EventEmitter<{ resultIndex: number, resultCount: number } | undefined>();
public readonly onDidChangeResults = this._onDidChangeResults.event;
public readonly onDidChangeResults = initEvent<{ resultIndex: number, resultCount: number } | undefined>();

public activate(terminal: Terminal): void {
this._terminal = terminal;
Expand All @@ -89,7 +88,7 @@ export class SearchAddon implements ITerminalAddon {
this._highlightTimeout = setTimeout(() => {
this.findPrevious(this._cachedSearchTerm!, { ...this._lastSearchOptions, incremental: true, noScroll: true });
this._resultIndex = this._searchResults ? this._searchResults.size - 1 : -1;
this._onDidChangeResults.fire({ resultIndex: this._resultIndex, resultCount: this._searchResults?.size ?? -1 });
this.onDidChangeResults.fire({ resultIndex: this._resultIndex, resultCount: this._searchResults?.size ?? -1 });
}, 200);
}
}
Expand Down Expand Up @@ -325,9 +324,9 @@ export class SearchAddon implements ITerminalAddon {
private _fireResults(term: string, found: boolean, searchOptions?: ISearchOptions): boolean {
if (searchOptions?.decorations) {
if (this._resultIndex !== undefined && this._searchResults?.size !== undefined) {
this._onDidChangeResults.fire({ resultIndex: this._resultIndex, resultCount: this._searchResults.size });
this.onDidChangeResults.fire({ resultIndex: this._resultIndex, resultCount: this._searchResults.size });
} else {
this._onDidChangeResults.fire(undefined);
this.onDidChangeResults.fire(undefined);
}
}
this._cachedSearchTerm = term;
Expand Down
14 changes: 6 additions & 8 deletions addons/xterm-addon-webgl/src/WebglAddon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,20 @@ import { Terminal, ITerminalAddon, IEvent } from 'xterm';
import { WebglRenderer } from './WebglRenderer';
import { ICharacterJoinerService, ICoreBrowserService, IRenderService } from 'browser/services/Services';
import { IColorSet } from 'browser/Types';
import { EventEmitter, forwardEvent } from 'common/EventEmitter';
import { EventEmitter, forwardEvent, initEvent } from 'common/EventEmitter';
import { isSafari } from 'common/Platform';
import { ICoreService, IDecorationService } from 'common/services/Services';

export class WebglAddon implements ITerminalAddon {
private _terminal?: Terminal;
private _renderer?: WebglRenderer;

private readonly _onChangeTextureAtlas = new EventEmitter<HTMLElement>();
public readonly onChangeTextureAtlas = this._onChangeTextureAtlas.event;
private readonly _onContextLoss = new EventEmitter<void>();
public readonly onContextLoss = this._onContextLoss.event;
public readonly onChangeTextureAtlas = initEvent<HTMLElement>();
public readonly onContextLoss = initEvent<void>();

constructor(
private _preserveDrawingBuffer?: boolean
) {}
) { }

public activate(terminal: Terminal): void {
if (!terminal.element) {
Expand All @@ -39,8 +37,8 @@ export class WebglAddon implements ITerminalAddon {
const decorationService: IDecorationService = (terminal as any)._core._decorationService;
const colors: IColorSet = (terminal as any)._core._colorManager.colors;
this._renderer = new WebglRenderer(terminal, colors, characterJoinerService, coreBrowserService, coreService, decorationService, this._preserveDrawingBuffer);
forwardEvent(this._renderer.onContextLoss, this._onContextLoss);
forwardEvent(this._renderer.onChangeTextureAtlas, this._onChangeTextureAtlas);
forwardEvent(this._renderer.onContextLoss, this.onContextLoss);
forwardEvent(this._renderer.onChangeTextureAtlas, this.onChangeTextureAtlas);
renderService.setRenderer(this._renderer);
}

Expand Down
25 changes: 11 additions & 14 deletions addons/xterm-addon-webgl/src/WebglRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { IRenderLayer } from './renderLayer/Types';
import { IRenderDimensions, IRenderer, IRequestRedrawEvent } from 'browser/renderer/Types';
import { observeDevicePixelDimensions } from 'browser/renderer/DevicePixelObserver';
import { ITerminal, IColorSet } from 'browser/Types';
import { EventEmitter } from 'common/EventEmitter';
import { EventEmitter, initEvent } from 'common/EventEmitter';
import { CellData } from 'common/buffer/CellData';
import { addDisposableDomListener } from 'browser/Lifecycle';
import { ICharacterJoinerService, ICoreBrowserService } from 'browser/services/Services';
Expand Down Expand Up @@ -53,12 +53,9 @@ export class WebglRenderer extends Disposable implements IRenderer {
private _isAttached: boolean;
private _contextRestorationTimeout: number | undefined;

private readonly _onChangeTextureAtlas = new EventEmitter<HTMLCanvasElement>();
public readonly onChangeTextureAtlas = this._onChangeTextureAtlas.event;
private readonly _onRequestRedraw = new EventEmitter<IRequestRedrawEvent>();
public readonly onRequestRedraw = this._onRequestRedraw.event;
private readonly _onContextLoss = new EventEmitter<void>();
public readonly onContextLoss = this._onContextLoss.event;
public readonly onChangeTextureAtlas = initEvent<HTMLCanvasElement>();
public readonly onRequestRedraw = initEvent<IRequestRedrawEvent>();
public readonly onContextLoss = initEvent<void>();

constructor(
private _terminal: Terminal,
Expand All @@ -75,7 +72,7 @@ export class WebglRenderer extends Disposable implements IRenderer {

this._renderLayers = [
new LinkRenderLayer(this._core.screenElement!, 2, this._colors, this._core, this._coreBrowserService),
new CursorRenderLayer(_terminal, this._core.screenElement!, 3, this._colors, this._onRequestRedraw, this._coreBrowserService, coreService)
new CursorRenderLayer(_terminal, this._core.screenElement!, 3, this._colors, this.onRequestRedraw, this._coreBrowserService, coreService)
];
this.dimensions = {
scaledCharWidth: 0,
Expand Down Expand Up @@ -115,7 +112,7 @@ export class WebglRenderer extends Disposable implements IRenderer {
this._contextRestorationTimeout = setTimeout(() => {
this._contextRestorationTimeout = undefined;
console.warn('webgl context not restored; firing onContextLoss');
this._onContextLoss.fire(e);
this.onContextLoss.fire(e);
}, 3000 /* ms */);
}));
this.register(addDisposableDomListener(this._canvas, 'webglcontextrestored', (e) => {
Expand Down Expand Up @@ -283,7 +280,7 @@ export class WebglRenderer extends Disposable implements IRenderer {
throw new Error('The webgl renderer only works with the webgl char atlas');
}
if (this._charAtlas !== atlas) {
this._onChangeTextureAtlas.fire(atlas.cacheCanvas);
this.onChangeTextureAtlas.fire(atlas.cacheCanvas);
}
this._charAtlas = atlas;
this._charAtlas.warmUp();
Expand Down Expand Up @@ -422,9 +419,9 @@ export class WebglRenderer extends Disposable implements IRenderer {

// Nothing has changed, no updates needed
if (this._model.cells[i] === code &&
this._model.cells[i + RENDER_MODEL_BG_OFFSET] === this._workColors.bg &&
this._model.cells[i + RENDER_MODEL_FG_OFFSET] === this._workColors.fg &&
this._model.cells[i + RENDER_MODEL_EXT_OFFSET] === this._workColors.ext) {
this._model.cells[i + RENDER_MODEL_BG_OFFSET] === this._workColors.bg &&
this._model.cells[i + RENDER_MODEL_FG_OFFSET] === this._workColors.fg &&
this._model.cells[i + RENDER_MODEL_EXT_OFFSET] === this._workColors.ext) {
continue;
}

Expand Down Expand Up @@ -676,7 +673,7 @@ export class WebglRenderer extends Disposable implements IRenderer {
}

private _requestRedrawViewport(): void {
this._onRequestRedraw.fire({ start: 0, end: this._terminal.rows - 1 });
this.onRequestRedraw.fire({ start: 0, end: this._terminal.rows - 1 });
}
}

Expand Down
10 changes: 4 additions & 6 deletions src/browser/Linkifier2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { ILinkifier2, ILinkProvider, IBufferCellPosition, ILink, ILinkifierEvent
import { IDisposable } from 'common/Types';
import { IMouseService, IRenderService } from './services/Services';
import { IBufferService } from 'common/services/Services';
import { EventEmitter, IEvent } from 'common/EventEmitter';
import { EventEmitter, IEvent, initEvent } from 'common/EventEmitter';
import { Disposable, getDisposeArrayDisposable, disposeArray } from 'common/Lifecycle';
import { addDisposableDomListener } from 'browser/Lifecycle';

Expand All @@ -26,10 +26,8 @@ export class Linkifier2 extends Disposable implements ILinkifier2 {
private _activeProviderReplies: Map<Number, ILinkWithState[] | undefined> | undefined;
private _activeLine: number = -1;

private readonly _onShowLinkUnderline = this.register(new EventEmitter<ILinkifierEvent>());
public readonly onShowLinkUnderline = this._onShowLinkUnderline.event;
private readonly _onHideLinkUnderline = this.register(new EventEmitter<ILinkifierEvent>());
public readonly onHideLinkUnderline = this._onHideLinkUnderline.event;
public readonly onShowLinkUnderline = this.register(initEvent<ILinkifierEvent>());
public readonly onHideLinkUnderline = this.register(initEvent<ILinkifierEvent>());

constructor(
@IBufferService private readonly _bufferService: IBufferService
Expand Down Expand Up @@ -343,7 +341,7 @@ export class Linkifier2 extends Disposable implements ILinkifier2 {
const range = link.range;
const scrollOffset = this._bufferService.buffer.ydisp;
const event = this._createLinkUnderlineEvent(range.start.x - 1, range.start.y - scrollOffset - 1, range.end.x, range.end.y - scrollOffset - 1, undefined);
const emitter = showEvent ? this._onShowLinkUnderline : this._onHideLinkUnderline;
const emitter = showEvent ? this.onShowLinkUnderline : this.onHideLinkUnderline;
emitter.fire(event);
}

Expand Down
55 changes: 22 additions & 33 deletions src/browser/Terminal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import { ITheme, IMarker, IDisposable, ILinkProvider, IDecorationOptions, IDecor
import { DomRenderer } from 'browser/renderer/dom/DomRenderer';
import { KeyboardResultType, CoreMouseEventType, CoreMouseButton, CoreMouseAction, ITerminalOptions, ScrollSource, IColorEvent, ColorIndex, ColorRequestType } from 'common/Types';
import { evaluateKeyboardEvent } from 'common/input/Keyboard';
import { EventEmitter, IEvent, forwardEvent } from 'common/EventEmitter';
import { EventEmitter, IEvent, forwardEvent, initEvent } from 'common/EventEmitter';
import { DEFAULT_ATTR_DATA } from 'common/buffer/BufferLine';
import { ColorManager } from 'browser/ColorManager';
import { RenderService } from 'browser/services/RenderService';
Expand Down Expand Up @@ -122,27 +122,16 @@ export class Terminal extends CoreTerminal implements ITerminal {
private _colorManager: ColorManager | undefined;
private _theme: ITheme | undefined;

private readonly _onCursorMove = new EventEmitter<void>();
public readonly onCursorMove = this._onCursorMove.event;
private readonly _onKey = new EventEmitter<{ key: string, domEvent: KeyboardEvent }>();
public readonly onKey = this._onKey.event;
private readonly _onRender = new EventEmitter<{ start: number, end: number }>();
public readonly onRender = this._onRender.event;
private readonly _onSelectionChange = new EventEmitter<void>();
public readonly onSelectionChange = this._onSelectionChange.event;
private readonly _onTitleChange = new EventEmitter<string>();
public readonly onTitleChange = this._onTitleChange.event;
private readonly _onBell = new EventEmitter<void>();
public readonly onBell = this._onBell.event;

private readonly _onFocus = new EventEmitter<void>();
public readonly onFocus = this._onFocus.event;
private readonly _onBlur = new EventEmitter<void>();
public readonly onBlur = this._onBlur.event;
private readonly _onA11yCharEmitter = new EventEmitter<string>();
public readonly onA11yChar = this._onA11yCharEmitter.event;
private readonly _onA11yTabEmitter = new EventEmitter<number>();
public readonly onA11yTab = this._onA11yTabEmitter.event;
public readonly onCursorMove = initEvent<void>();
public readonly onKey = initEvent<{ key: string, domEvent: KeyboardEvent }>();
public readonly onRender = initEvent<{ start: number, end: number }>();
public readonly onSelectionChange = initEvent<void>();
public readonly onTitleChange = initEvent<string>();
public readonly onBell = initEvent<void>();
public readonly onFocus = initEvent<void>();
public readonly onBlur = initEvent<void>();
public readonly onA11yChar = initEvent<string>();
public readonly onA11yTab = initEvent<number>();

/**
* Creates a new `Terminal` object.
Expand All @@ -169,16 +158,16 @@ export class Terminal extends CoreTerminal implements ITerminal {
this._instantiationService.setService(IDecorationService, this._decorationService);

// Setup InputHandler listeners
this.register(this._inputHandler.onRequestBell(() => this._onBell.fire()));
this.register(this._inputHandler.onRequestBell(() => this.onBell.fire()));
this.register(this._inputHandler.onRequestRefreshRows((start, end) => this.refresh(start, end)));
this.register(this._inputHandler.onRequestSendFocus(() => this._reportFocus()));
this.register(this._inputHandler.onRequestReset(() => this.reset()));
this.register(this._inputHandler.onRequestWindowsOptionsReport(type => this._reportWindowsOptions(type)));
this.register(this._inputHandler.onColor((event) => this._handleColorEvent(event)));
this.register(forwardEvent(this._inputHandler.onCursorMove, this._onCursorMove));
this.register(forwardEvent(this._inputHandler.onTitleChange, this._onTitleChange));
this.register(forwardEvent(this._inputHandler.onA11yChar, this._onA11yCharEmitter));
this.register(forwardEvent(this._inputHandler.onA11yTab, this._onA11yTabEmitter));
this.register(forwardEvent(this._inputHandler.onCursorMove, this.onCursorMove));
this.register(forwardEvent(this._inputHandler.onTitleChange, this.onTitleChange));
this.register(forwardEvent(this._inputHandler.onA11yChar, this.onA11yChar));
this.register(forwardEvent(this._inputHandler.onA11yTab, this.onA11yTab));

// Setup listeners
this.register(this._bufferService.onResize(e => this._afterResize(e.cols, e.rows)));
Expand Down Expand Up @@ -326,7 +315,7 @@ export class Terminal extends CoreTerminal implements ITerminal {
this.updateCursorStyle(ev);
this.element!.classList.add('focus');
this._showCursor();
this._onFocus.fire();
this.onFocus.fire();
}

/**
Expand All @@ -349,7 +338,7 @@ export class Terminal extends CoreTerminal implements ITerminal {
this.coreService.triggerDataEvent(C0.ESC + '[O');
}
this.element!.classList.remove('focus');
this._onBlur.fire();
this.onBlur.fire();
}

private _syncTextArea(): void {
Expand Down Expand Up @@ -512,7 +501,7 @@ export class Terminal extends CoreTerminal implements ITerminal {
const renderer = this._createRenderer();
this._renderService = this.register(this._instantiationService.createInstance(RenderService, renderer, this.rows, this.screenElement));
this._instantiationService.setService(IRenderService, this._renderService);
this.register(this._renderService.onRenderedViewportChange(e => this._onRender.fire(e)));
this.register(this._renderService.onRenderedViewportChange(e => this.onRender.fire(e)));
this.onResize(e => this._renderService!.resize(e.cols, e.rows));

this._compositionView = document.createElement('div');
Expand Down Expand Up @@ -552,7 +541,7 @@ export class Terminal extends CoreTerminal implements ITerminal {
));
this._instantiationService.setService(ISelectionService, this._selectionService);
this.register(this._selectionService.onRequestScrollLines(e => this.scrollLines(e.amount, e.suppressScrollEvent)));
this.register(this._selectionService.onSelectionChange(() => this._onSelectionChange.fire()));
this.register(this._selectionService.onSelectionChange(() => this.onSelectionChange.fire()));
this.register(this._selectionService.onRequestRedraw(e => this._renderService!.onSelectionChanged(e.start, e.end, e.columnSelectMode)));
this.register(this._selectionService.onLinuxMouseSelection(text => {
// If there's a new selection, put it into the textarea, focus and select it
Expand Down Expand Up @@ -1101,7 +1090,7 @@ export class Terminal extends CoreTerminal implements ITerminal {
this.textarea!.value = '';
}

this._onKey.fire({ key: result.key, domEvent: event });
this.onKey.fire({ key: result.key, domEvent: event });
this._showCursor();
this.coreService.triggerDataEvent(result.key, true);

Expand Down Expand Up @@ -1184,7 +1173,7 @@ export class Terminal extends CoreTerminal implements ITerminal {

key = String.fromCharCode(key);

this._onKey.fire({ key, domEvent: ev });
this.onKey.fire({ key, domEvent: ev });
this._showCursor();
this.coreService.triggerDataEvent(key, true);

Expand Down
Loading

0 comments on commit d22f7c9

Please sign in to comment.