Debian Codegraph @

node/@xterm/xterm/src/vs/base/common/platform.ts

raw
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 * as nls from 'vs/nls';
7
8 export const LANGUAGE_DEFAULT = 'en';
9
10 let _isWindows = false;
11 let _isMacintosh = false;
12 let _isLinux = false;
13 let _isLinuxSnap = false;
14 let _isNative = false;
15 let _isWeb = false;
16 let _isElectron = false;
17 let _isIOS = false;
18 let _isCI = false;
19 let _isMobile = false;
20 let _locale: string | undefined = undefined;
21 let _language: string = LANGUAGE_DEFAULT;
22 let _platformLocale: string = LANGUAGE_DEFAULT;
23 let _translationsConfigFile: string | undefined = undefined;
24 let _userAgent: string | undefined = undefined;
25
26 export interface IProcessEnvironment {
27 [key: string]: string | undefined;
28 }
29
30 /**
31 * This interface is intentionally not identical to node.js
32 * process because it also works in sandboxed environments
33 * where the process object is implemented differently. We
34 * define the properties here that we need for `platform`
35 * to work and nothing else.
36 */
37 export interface INodeProcess {
38 platform: string;
39 arch: string;
40 env: IProcessEnvironment;
41 versions?: {
42 node?: string;
43 electron?: string;
44 chrome?: string;
45 };
46 type?: string;
47 cwd: () => string;
48 }
49
50 declare const process: INodeProcess;
51
52 const $globalThis: any = globalThis;
53
54 let nodeProcess: INodeProcess | undefined = undefined;
55 if (typeof $globalThis.vscode !== 'undefined' && typeof $globalThis.vscode.process !== 'undefined') {
56 // Native environment (sandboxed)
57 nodeProcess = $globalThis.vscode.process;
58 } else if (typeof process !== 'undefined' && typeof process?.versions?.node === 'string') {
59 // Native environment (non-sandboxed)
60 nodeProcess = process;
61 }
62
63 const isElectronProcess = typeof nodeProcess?.versions?.electron === 'string';
64 const isElectronRenderer = isElectronProcess && nodeProcess?.type === 'renderer';
65
66 interface INavigator {
67 userAgent: string;
68 maxTouchPoints?: number;
69 language: string;
70 }
71 declare const navigator: INavigator;
72
73 // Native environment
74 if (typeof nodeProcess === 'object') {
75 _isWindows = (nodeProcess.platform === 'win32');
76 _isMacintosh = (nodeProcess.platform === 'darwin');
77 _isLinux = (nodeProcess.platform === 'linux');
78 _isLinuxSnap = _isLinux && !!nodeProcess.env['SNAP'] && !!nodeProcess.env['SNAP_REVISION'];
79 _isElectron = isElectronProcess;
80 _isCI = !!nodeProcess.env['CI'] || !!nodeProcess.env['BUILD_ARTIFACTSTAGINGDIRECTORY'];
81 _locale = LANGUAGE_DEFAULT;
82 _language = LANGUAGE_DEFAULT;
83 const rawNlsConfig = nodeProcess.env['VSCODE_NLS_CONFIG'];
84 if (rawNlsConfig) {
85 try {
86 const nlsConfig: nls.INLSConfiguration = JSON.parse(rawNlsConfig);
87 _locale = nlsConfig.userLocale;
88 _platformLocale = nlsConfig.osLocale;
89 _language = nlsConfig.resolvedLanguage || LANGUAGE_DEFAULT;
90 _translationsConfigFile = nlsConfig.languagePack?.translationsConfigFile;
91 } catch (e) {
92 }
93 }
94 _isNative = true;
95 }
96
97 // Web environment
98 else if (typeof navigator === 'object' && !isElectronRenderer) {
99 _userAgent = navigator.userAgent;
100 _isWindows = _userAgent.indexOf('Windows') >= 0;
101 _isMacintosh = _userAgent.indexOf('Macintosh') >= 0;
102 _isIOS = (_userAgent.indexOf('Macintosh') >= 0 || _userAgent.indexOf('iPad') >= 0 || _userAgent.indexOf('iPhone') >= 0) && !!navigator.maxTouchPoints && navigator.maxTouchPoints > 0;
103 _isLinux = _userAgent.indexOf('Linux') >= 0;
104 _isMobile = _userAgent?.indexOf('Mobi') >= 0;
105 _isWeb = true;
106 // VSCODE_GLOBALS: NLS
107 _language = globalThis._VSCODE_NLS_LANGUAGE || LANGUAGE_DEFAULT;
108 _locale = navigator.language.toLowerCase();
109 _platformLocale = _locale;
110 }
111
112 // Unknown environment
113 else {
114 console.error('Unable to resolve platform.');
115 }
116
117 export const enum Platform {
118 Web,
119 Mac,
120 Linux,
121 Windows
122 }
123 export type PlatformName = 'Web' | 'Windows' | 'Mac' | 'Linux';
124
125 export function PlatformToString(platform: Platform): PlatformName {
126 switch (platform) {
127 case Platform.Web: return 'Web';
128 case Platform.Mac: return 'Mac';
129 case Platform.Linux: return 'Linux';
130 case Platform.Windows: return 'Windows';
131 }
132 }
133
134 let _platform: Platform = Platform.Web;
135 if (_isMacintosh) {
136 _platform = Platform.Mac;
137 } else if (_isWindows) {
138 _platform = Platform.Windows;
139 } else if (_isLinux) {
140 _platform = Platform.Linux;
141 }
142
143 export const isWindows = _isWindows;
144 export const isMacintosh = _isMacintosh;
145 export const isLinux = _isLinux;
146 export const isLinuxSnap = _isLinuxSnap;
147 export const isNative = _isNative;
148 export const isElectron = _isElectron;
149 export const isWeb = _isWeb;
150 export const isWebWorker = (_isWeb && typeof $globalThis.importScripts === 'function');
151 export const webWorkerOrigin = isWebWorker ? $globalThis.origin : undefined;
152 export const isIOS = _isIOS;
153 export const isMobile = _isMobile;
154 /**
155 * Whether we run inside a CI environment, such as
156 * GH actions or Azure Pipelines.
157 */
158 export const isCI = _isCI;
159 export const platform = _platform;
160 export const userAgent = _userAgent;
161
162 /**
163 * The language used for the user interface. The format of
164 * the string is all lower case (e.g. zh-tw for Traditional
165 * Chinese or de for German)
166 */
167 export const language = _language;
168
169 export namespace Language {
170
171 export function value(): string {
172 return language;
173 }
174
175 export function isDefaultVariant(): boolean {
176 if (language.length === 2) {
177 return language === 'en';
178 } else if (language.length >= 3) {
179 return language[0] === 'e' && language[1] === 'n' && language[2] === '-';
180 } else {
181 return false;
182 }
183 }
184
185 export function isDefault(): boolean {
186 return language === 'en';
187 }
188 }
189
190 /**
191 * Desktop: The OS locale or the locale specified by --locale or `argv.json`.
192 * Web: matches `platformLocale`.
193 *
194 * The UI is not necessarily shown in the provided locale.
195 */
196 export const locale = _locale;
197
198 /**
199 * This will always be set to the OS/browser's locale regardless of
200 * what was specified otherwise. The format of the string is all
201 * lower case (e.g. zh-tw for Traditional Chinese). The UI is not
202 * necessarily shown in the provided locale.
203 */
204 export const platformLocale = _platformLocale;
205
206 /**
207 * The translations that are available through language packs.
208 */
209 export const translationsConfigFile = _translationsConfigFile;
210
211 export const setTimeout0IsFaster = (typeof $globalThis.postMessage === 'function' && !$globalThis.importScripts);
212
213 /**
214 * See https://html.spec.whatwg.org/multipage/timers-and-user-prompts.html#:~:text=than%204%2C%20then-,set%20timeout%20to%204,-.
215 *
216 * Works similarly to `setTimeout(0)` but doesn't suffer from the 4ms artificial delay
217 * that browsers set when the nesting level is > 5.
218 */
219 export const setTimeout0 = (() => {
220 if (setTimeout0IsFaster) {
221 interface IQueueElement {
222 id: number;
223 callback: () => void;
224 }
225 const pending: IQueueElement[] = [];
226
227 $globalThis.addEventListener('message', (e: any) => {
228 if (e.data && e.data.vscodeScheduleAsyncWork) {
229 for (let i = 0, len = pending.length; i < len; i++) {
230 const candidate = pending[i];
231 if (candidate.id === e.data.vscodeScheduleAsyncWork) {
232 pending.splice(i, 1);
233 candidate.callback();
234 return;
235 }
236 }
237 }
238 });
239 let lastId = 0;
240 return (callback: () => void) => {
241 const myId = ++lastId;
242 pending.push({
243 id: myId,
244 callback: callback
245 });
246 $globalThis.postMessage({ vscodeScheduleAsyncWork: myId }, '*');
247 };
248 }
249 return (callback: () => void) => setTimeout(callback);
250 })();
251
252 export const enum OperatingSystem {
253 Windows = 1,
254 Macintosh = 2,
255 Linux = 3
256 }
257 export const OS = (_isMacintosh || _isIOS ? OperatingSystem.Macintosh : (_isWindows ? OperatingSystem.Windows : OperatingSystem.Linux));
258
259 let _isLittleEndian = true;
260 let _isLittleEndianComputed = false;
261 export function isLittleEndian(): boolean {
262 if (!_isLittleEndianComputed) {
263 _isLittleEndianComputed = true;
264 const test = new Uint8Array(2);
265 test[0] = 1;
266 test[1] = 2;
267 const view = new Uint16Array(test.buffer);
268 _isLittleEndian = (view[0] === (2 << 8) + 1);
269 }
270 return _isLittleEndian;
271 }
272
273 export const isChrome = !!(userAgent && userAgent.indexOf('Chrome') >= 0);
274 export const isFirefox = !!(userAgent && userAgent.indexOf('Firefox') >= 0);
275 export const isSafari = !!(!isChrome && (userAgent && userAgent.indexOf('Safari') >= 0));
276 export const isEdge = !!(userAgent && userAgent.indexOf('Edg/') >= 0);
277 export const isAndroid = !!(userAgent && userAgent.indexOf('Android') >= 0);
278
279 export function isBigSurOrNewer(osVersion: string): boolean {
280 return parseFloat(osVersion) >= 20;
281 }