2 * Copyright (c) 2017 The xterm.js authors. All rights reserved.
6 import { Disposable } from 'common/Lifecycle';
8 export type ScreenDprListener = (newDevicePixelRatio?: number, oldDevicePixelRatio?: number) => void;
11 * The screen device pixel ratio monitor allows listening for when the
12 * window.devicePixelRatio value changes. This is done not with polling but with
13 * the use of window.matchMedia to watch media queries. When the event fires,
14 * the listener will be reattached using a different media query to ensure that
15 * any further changes will register.
17 * The listener should fire on both window zoom changes and switching to a
18 * monitor with a different DPI.
20 export class ScreenDprMonitor extends Disposable {
21 private _currentDevicePixelRatio: number = window.devicePixelRatio;
22 private _outerListener: ((this: MediaQueryList, ev: MediaQueryListEvent) => any) | undefined;
23 private _listener: ScreenDprListener | undefined;
24 private _resolutionMediaMatchList: MediaQueryList | undefined;
26 public setListener(listener: ScreenDprListener): void {
30 this._listener = listener;
31 this._outerListener = () => {
32 if (!this._listener) {
35 this._listener(window.devicePixelRatio, this._currentDevicePixelRatio);
41 public dispose(): void {
46 private _updateDpr(): void {
47 if (!this._resolutionMediaMatchList || !this._outerListener) {
51 // Clear listeners for old DPR
52 this._resolutionMediaMatchList.removeListener(this._outerListener);
54 // Add listeners for new DPR
55 this._currentDevicePixelRatio = window.devicePixelRatio;
56 this._resolutionMediaMatchList = window.matchMedia(`screen and (resolution: ${window.devicePixelRatio}dppx)`);
57 this._resolutionMediaMatchList.addListener(this._outerListener);
60 public clearListener(): void {
61 if (!this._resolutionMediaMatchList || !this._listener || !this._outerListener) {
64 this._resolutionMediaMatchList.removeListener(this._outerListener);
65 this._resolutionMediaMatchList = undefined;
66 this._listener = undefined;
67 this._outerListener = undefined;