Debian Codegraph @

node/@xterm/xterm/src/browser/services/CoreBrowserService.ts

raw
1 /**
2 * Copyright (c) 2019 The xterm.js authors. All rights reserved.
3 * @license MIT
4 */
5
6 import { ICoreBrowserService } from './Services';
7 import { Emitter, Event } from 'vs/base/common/event';
8 import { addDisposableListener } from 'vs/base/browser/dom';
9 import { Disposable, MutableDisposable, toDisposable } from 'vs/base/common/lifecycle';
10
11 export class CoreBrowserService extends Disposable implements ICoreBrowserService {
12 public serviceBrand: undefined;
13
14 private _isFocused = false;
15 private _cachedIsFocused: boolean | undefined = undefined;
16 private _screenDprMonitor = this._register(new ScreenDprMonitor(this._window));
17
18 private readonly _onDprChange = this._register(new Emitter<number>());
19 public readonly onDprChange = this._onDprChange.event;
20 private readonly _onWindowChange = this._register(new Emitter<Window & typeof globalThis>());
21 public readonly onWindowChange = this._onWindowChange.event;
22
23 constructor(
24 private _textarea: HTMLTextAreaElement,
25 private _window: Window & typeof globalThis,
26 public readonly mainDocument: Document
27 ) {
28 super();
29
30 // Monitor device pixel ratio
31 this._register(this.onWindowChange(w => this._screenDprMonitor.setWindow(w)));
32 this._register(Event.forward(this._screenDprMonitor.onDprChange, this._onDprChange));
33
34 this._register(addDisposableListener(this._textarea, 'focus', () => this._isFocused = true));
35 this._register(addDisposableListener(this._textarea, 'blur', () => this._isFocused = false));
36 }
37
38 public get window(): Window & typeof globalThis {
39 return this._window;
40 }
41
42 public set window(value: Window & typeof globalThis) {
43 if (this._window !== value) {
44 this._window = value;
45 this._onWindowChange.fire(this._window);
46 }
47 }
48
49 public get dpr(): number {
50 return this.window.devicePixelRatio;
51 }
52
53 public get isFocused(): boolean {
54 if (this._cachedIsFocused === undefined) {
55 this._cachedIsFocused = this._isFocused && this._textarea.ownerDocument.hasFocus();
56 queueMicrotask(() => this._cachedIsFocused = undefined);
57 }
58 return this._cachedIsFocused;
59 }
60 }
61
62
63 /**
64 * The screen device pixel ratio monitor allows listening for when the
65 * window.devicePixelRatio value changes. This is done not with polling but with
66 * the use of window.matchMedia to watch media queries. When the event fires,
67 * the listener will be reattached using a different media query to ensure that
68 * any further changes will _register.
69 *
70 * The listener should fire on both window zoom changes and switching to a
71 * monitor with a different DPI.
72 */
73 class ScreenDprMonitor extends Disposable {
74 private _currentDevicePixelRatio: number;
75 private _outerListener: ((this: MediaQueryList, ev: MediaQueryListEvent) => any) | undefined;
76 private _resolutionMediaMatchList: MediaQueryList | undefined;
77 private _windowResizeListener = this._register(new MutableDisposable());
78
79 private readonly _onDprChange = this._register(new Emitter<number>());
80 public readonly onDprChange = this._onDprChange.event;
81
82 constructor(private _parentWindow: Window) {
83 super();
84
85 // Initialize listener and dpr value
86 this._outerListener = () => this._setDprAndFireIfDiffers();
87 this._currentDevicePixelRatio = this._parentWindow.devicePixelRatio;
88 this._updateDpr();
89
90 // Monitor active window resize
91 this._setWindowResizeListener();
92
93 // Setup additional disposables
94 this._register(toDisposable(() => this.clearListener()));
95 }
96
97
98 public setWindow(parentWindow: Window): void {
99 this._parentWindow = parentWindow;
100 this._setWindowResizeListener();
101 this._setDprAndFireIfDiffers();
102 }
103
104 private _setWindowResizeListener(): void {
105 this._windowResizeListener.value = addDisposableListener(this._parentWindow, 'resize', () => this._setDprAndFireIfDiffers());
106 }
107
108 private _setDprAndFireIfDiffers(): void {
109 if (this._parentWindow.devicePixelRatio !== this._currentDevicePixelRatio) {
110 this._onDprChange.fire(this._parentWindow.devicePixelRatio);
111 }
112 this._updateDpr();
113 }
114
115 private _updateDpr(): void {
116 if (!this._outerListener) {
117 return;
118 }
119
120 // Clear listeners for old DPR
121 this._resolutionMediaMatchList?.removeListener(this._outerListener);
122
123 // Add listeners for new DPR
124 this._currentDevicePixelRatio = this._parentWindow.devicePixelRatio;
125 this._resolutionMediaMatchList = this._parentWindow.matchMedia(`screen and (resolution: ${this._parentWindow.devicePixelRatio}dppx)`);
126 this._resolutionMediaMatchList.addListener(this._outerListener);
127 }
128
129 public clearListener(): void {
130 if (!this._resolutionMediaMatchList || !this._outerListener) {
131 return;
132 }
133 this._resolutionMediaMatchList.removeListener(this._outerListener);
134 this._resolutionMediaMatchList = undefined;
135 this._outerListener = undefined;
136 }
137 }