Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/editor/browser/gpu/gpuUtils.ts
3294 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import { BugIndicatingError } from '../../../base/common/errors.js';
7
import { toDisposable, type IDisposable } from '../../../base/common/lifecycle.js';
8
9
export const quadVertices = new Float32Array([
10
1, 0,
11
1, 1,
12
0, 1,
13
0, 0,
14
0, 1,
15
1, 0,
16
]);
17
18
export function ensureNonNullable<T>(value: T | null): T {
19
if (!value) {
20
throw new Error(`Value "${value}" cannot be null`);
21
}
22
return value;
23
}
24
25
// TODO: Move capabilities into ElementSizeObserver?
26
export function observeDevicePixelDimensions(element: HTMLElement, parentWindow: Window & typeof globalThis, callback: (deviceWidth: number, deviceHeight: number) => void): IDisposable {
27
// Observe any resizes to the element and extract the actual pixel size of the element if the
28
// devicePixelContentBoxSize API is supported. This allows correcting rounding errors when
29
// converting between CSS pixels and device pixels which causes blurry rendering when device
30
// pixel ratio is not a round number.
31
let observer: ResizeObserver | undefined = new parentWindow.ResizeObserver((entries) => {
32
const entry = entries.find((entry) => entry.target === element);
33
if (!entry) {
34
return;
35
}
36
37
// Disconnect if devicePixelContentBoxSize isn't supported by the browser
38
if (!('devicePixelContentBoxSize' in entry)) {
39
observer?.disconnect();
40
observer = undefined;
41
return;
42
}
43
44
// Fire the callback, ignore events where the dimensions are 0x0 as the canvas is likely hidden
45
const width = entry.devicePixelContentBoxSize[0].inlineSize;
46
const height = entry.devicePixelContentBoxSize[0].blockSize;
47
if (width > 0 && height > 0) {
48
callback(width, height);
49
}
50
});
51
try {
52
observer.observe(element, { box: ['device-pixel-content-box'] } as any);
53
} catch {
54
observer.disconnect();
55
observer = undefined;
56
throw new BugIndicatingError('Could not observe device pixel dimensions');
57
}
58
return toDisposable(() => observer?.disconnect());
59
}
60
61