diff --git a/src/browser/AccessibilityManager.ts b/src/browser/AccessibilityManager.ts index e8cd5888..ed2c12b0 100644 --- a/src/browser/AccessibilityManager.ts +++ b/src/browser/AccessibilityManager.ts @@ -98,10 +98,10 @@ export class AccessibilityManager extends Disposable { this.register(this._renderService.onDimensionsChange(() => this._refreshRowsDimensions())); this._screenDprMonitor = this.register(instantiationService.createInstance(ScreenDprMonitor)); - this.register(this._screenDprMonitor); - this._screenDprMonitor.setListener(() => this._refreshRowsDimensions()); + this.register(this._screenDprMonitor.onDprChange(() => this._refreshRowsDimensions())); // This shouldn't be needed on modern browsers but is present in case the // media query that drives the ScreenDprMonitor isn't supported + // TODO: Listen to window change this.register(addDisposableDomListener(window, 'resize', () => this._refreshRowsDimensions())); this._refreshRows(); diff --git a/src/browser/ScreenDprMonitor.ts b/src/browser/ScreenDprMonitor.ts index 5bb2aa72..6ff016e5 100644 --- a/src/browser/ScreenDprMonitor.ts +++ b/src/browser/ScreenDprMonitor.ts @@ -4,10 +4,9 @@ */ import { ICoreBrowserService } from 'browser/services/Services'; +import { EventEmitter } from 'common/EventEmitter'; import { Disposable, toDisposable } from 'common/Lifecycle'; -export type ScreenDprListener = (newDevicePixelRatio?: number, oldDevicePixelRatio?: number) => void; - /** * The screen device pixel ratio monitor allows listening for when the * window.devicePixelRatio value changes. This is done not with polling but with @@ -21,39 +20,41 @@ export type ScreenDprListener = (newDevicePixelRatio?: number, oldDevicePixelRat export class ScreenDprMonitor extends Disposable { private _currentDevicePixelRatio: number; private _outerListener: ((this: MediaQueryList, ev: MediaQueryListEvent) => any) | undefined; - private _listener: ScreenDprListener | undefined; private _resolutionMediaMatchList: MediaQueryList | undefined; private _parentWindow: Window; + private readonly _onDprChange = this.register(new EventEmitter()); + public readonly onDprChange = this._onDprChange.event; + constructor(@ICoreBrowserService coreBrowserService: ICoreBrowserService) { super(); - this._parentWindow = coreBrowserService.window; - this.register(coreBrowserService.onWindowChange(w => { - this._parentWindow = w; - if (this._listener && this._parentWindow.devicePixelRatio !== this._currentDevicePixelRatio) { - this._listener(this._parentWindow.devicePixelRatio, this._currentDevicePixelRatio); - } - this._updateDpr(); - })); - this._currentDevicePixelRatio = this._parentWindow.devicePixelRatio; - this.register(toDisposable(() => { - this.clearListener(); - })); - } - public setListener(listener: ScreenDprListener): void { - if (this._listener) { - this.clearListener(); - } - this._listener = listener; + this._parentWindow = coreBrowserService.window; + + // Initialize listener and dpr value this._outerListener = () => { - if (!this._listener) { - return; + if (this._parentWindow.devicePixelRatio !== this._currentDevicePixelRatio) { + this._onDprChange.fire(this._parentWindow.devicePixelRatio); } - this._listener(this._parentWindow.devicePixelRatio, this._currentDevicePixelRatio); this._updateDpr(); }; + this._currentDevicePixelRatio = this._parentWindow.devicePixelRatio; this._updateDpr(); + + // Listen for window changes + this.register(coreBrowserService.onWindowChange(w => { + this._parentWindow = w; + if (this._parentWindow.devicePixelRatio !== this._currentDevicePixelRatio) { + this._onDprChange.fire(this._parentWindow.devicePixelRatio); + } + this._updateDpr(); + })); + + // Setup additional disposables + this.register(toDisposable(() => this.clearListener())); + } + + public setListener(): void { } private _updateDpr(): void { @@ -71,12 +72,11 @@ export class ScreenDprMonitor extends Disposable { } public clearListener(): void { - if (!this._resolutionMediaMatchList || !this._listener || !this._outerListener) { + if (!this._resolutionMediaMatchList || !this._outerListener) { return; } this._resolutionMediaMatchList.removeListener(this._outerListener); this._resolutionMediaMatchList = undefined; - this._listener = undefined; this._outerListener = undefined; } } diff --git a/src/browser/decorations/BufferDecorationRenderer.ts b/src/browser/decorations/BufferDecorationRenderer.ts index 0cac7bc7..3a0d7862 100644 --- a/src/browser/decorations/BufferDecorationRenderer.ts +++ b/src/browser/decorations/BufferDecorationRenderer.ts @@ -34,6 +34,7 @@ export class BufferDecorationRenderer extends Disposable { this._dimensionsChanged = true; this._queueRefresh(); })); + // TODO: Listen to window change this.register(addDisposableDomListener(window, 'resize', () => this._queueRefresh())); this.register(this._bufferService.buffers.onBufferActivate(() => { this._altBufferIsActive = this._bufferService.buffer === this._bufferService.buffers.alt; diff --git a/src/browser/decorations/OverviewRulerRenderer.ts b/src/browser/decorations/OverviewRulerRenderer.ts index 8648248a..57b6dfc6 100644 --- a/src/browser/decorations/OverviewRulerRenderer.ts +++ b/src/browser/decorations/OverviewRulerRenderer.ts @@ -112,7 +112,7 @@ export class OverviewRulerRenderer extends Disposable { // overview ruler width changed this.register(this._optionsService.onSpecificOptionChange('overviewRulerWidth', () => this._queueRefresh(true))); // device pixel ratio changed - // TODO: Observe DPR instead + // TODO: Observe DPR instead / listen to window change this.register(addDisposableDomListener(this._coreBrowserService.window, 'resize', () => this._queueRefresh(true))); // set the canvas dimensions this._queueRefresh(true); diff --git a/src/browser/services/RenderService.ts b/src/browser/services/RenderService.ts index 9fc3f8a9..60c27798 100644 --- a/src/browser/services/RenderService.ts +++ b/src/browser/services/RenderService.ts @@ -68,7 +68,7 @@ export class RenderService extends Disposable implements IRenderService { this.register(this._renderDebouncer); this._screenDprMonitor = this.register(instantiationService.createInstance(ScreenDprMonitor)); - this._screenDprMonitor.setListener(() => this.handleDevicePixelRatioChange()); + this.register(this._screenDprMonitor.onDprChange(() => this.handleDevicePixelRatioChange())); this.register(bufferService.onResize(() => this._fullRefresh())); this.register(bufferService.buffers.onBufferActivate(() => this._renderer.value?.clear())); @@ -106,6 +106,7 @@ export class RenderService extends Disposable implements IRenderService { // dprchange should handle this case, we need this as well for browsers that don't support the // matchMedia query. + // TODO: Listen to window change this.register(addDisposableDomListener(coreBrowserService.window, 'resize', () => this.handleDevicePixelRatioChange())); this.register(themeService.onChangeColors(() => this._fullRefresh()));