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