Path: blob/main/extensions/copilot/src/util/vs/base/common/platform.ts
13405 views
//!!! DO NOT modify, this file was COPIED from 'microsoft/vscode'12/*---------------------------------------------------------------------------------------------3* Copyright (c) Microsoft Corporation. All rights reserved.4* Licensed under the MIT License. See License.txt in the project root for license information.5*--------------------------------------------------------------------------------------------*/67import * as nls from '../../nls';89export const LANGUAGE_DEFAULT = 'en';1011let _isWindows = false;12let _isMacintosh = false;13let _isLinux = false;14let _isLinuxSnap = false;15let _isNative = false;16let _isWeb = false;17let _isElectron = false;18let _isIOS = false;19let _isCI = false;20let _isMobile = false;21let _locale: string | undefined = undefined;22let _language: string = LANGUAGE_DEFAULT;23let _platformLocale: string = LANGUAGE_DEFAULT;24let _translationsConfigFile: string | undefined = undefined;25let _userAgent: string | undefined = undefined;2627export interface IProcessEnvironment {28[key: string]: string | undefined;29}3031/**32* This interface is intentionally not identical to node.js33* process because it also works in sandboxed environments34* where the process object is implemented differently. We35* define the properties here that we need for `platform`36* to work and nothing else.37*/38export interface INodeProcess {39platform: string;40arch: string;41env: IProcessEnvironment;42versions?: {43node?: string;44electron?: string;45chrome?: string;46};47type?: string;48cwd: () => string;49}5051declare const process: INodeProcess;5253const $globalThis: any = globalThis;5455let nodeProcess: INodeProcess | undefined = undefined;56if (typeof $globalThis.vscode !== 'undefined' && typeof $globalThis.vscode.process !== 'undefined') {57// Native environment (sandboxed)58nodeProcess = $globalThis.vscode.process;59} else if (typeof process !== 'undefined' && typeof process?.versions?.node === 'string') {60// Native environment (non-sandboxed)61nodeProcess = process;62}6364const isElectronProcess = typeof nodeProcess?.versions?.electron === 'string';65const isElectronRenderer = isElectronProcess && nodeProcess?.type === 'renderer';6667interface INavigator {68userAgent: string;69maxTouchPoints?: number;70language: string;71}72declare const navigator: INavigator;7374// Native environment75if (typeof nodeProcess === 'object') {76_isWindows = (nodeProcess.platform === 'win32');77_isMacintosh = (nodeProcess.platform === 'darwin');78_isLinux = (nodeProcess.platform === 'linux');79_isLinuxSnap = _isLinux && !!nodeProcess.env['SNAP'] && !!nodeProcess.env['SNAP_REVISION'];80_isElectron = isElectronProcess;81_isCI = !!nodeProcess.env['CI'] || !!nodeProcess.env['BUILD_ARTIFACTSTAGINGDIRECTORY'] || !!nodeProcess.env['GITHUB_WORKSPACE'];82_locale = LANGUAGE_DEFAULT;83_language = LANGUAGE_DEFAULT;84const rawNlsConfig = nodeProcess.env['VSCODE_NLS_CONFIG'];85if (rawNlsConfig) {86try {87const nlsConfig: nls.INLSConfiguration = JSON.parse(rawNlsConfig);88_locale = nlsConfig.userLocale;89_platformLocale = nlsConfig.osLocale;90_language = nlsConfig.resolvedLanguage || LANGUAGE_DEFAULT;91_translationsConfigFile = nlsConfig.languagePack?.translationsConfigFile;92} catch (e) {93}94}95_isNative = true;96}9798// Web environment99else if (typeof navigator === 'object' && !isElectronRenderer) {100_userAgent = navigator.userAgent;101_isWindows = _userAgent.indexOf('Windows') >= 0;102_isMacintosh = _userAgent.indexOf('Macintosh') >= 0;103_isIOS = (_userAgent.indexOf('Macintosh') >= 0 || _userAgent.indexOf('iPad') >= 0 || _userAgent.indexOf('iPhone') >= 0) && !!navigator.maxTouchPoints && navigator.maxTouchPoints > 0;104_isLinux = _userAgent.indexOf('Linux') >= 0;105_isMobile = _userAgent?.indexOf('Mobi') >= 0;106_isWeb = true;107_language = nls.getNLSLanguage() || LANGUAGE_DEFAULT;108_locale = navigator.language.toLowerCase();109_platformLocale = _locale;110}111112// Unknown environment113else {114console.error('Unable to resolve platform.');115}116117export const enum Platform {118Web,119Mac,120Linux,121Windows122}123export type PlatformName = 'Web' | 'Windows' | 'Mac' | 'Linux';124125export function PlatformToString(platform: Platform): PlatformName {126switch (platform) {127case Platform.Web: return 'Web';128case Platform.Mac: return 'Mac';129case Platform.Linux: return 'Linux';130case Platform.Windows: return 'Windows';131}132}133134let _platform: Platform = Platform.Web;135if (_isMacintosh) {136_platform = Platform.Mac;137} else if (_isWindows) {138_platform = Platform.Windows;139} else if (_isLinux) {140_platform = Platform.Linux;141}142143export const isWindows = _isWindows;144export const isMacintosh = _isMacintosh;145export const isLinux = _isLinux;146export const isLinuxSnap = _isLinuxSnap;147export const isNative = _isNative;148export const isElectron = _isElectron;149export const isWeb = _isWeb;150export const isWebWorker = (_isWeb && typeof $globalThis.importScripts === 'function');151export const webWorkerOrigin = isWebWorker ? $globalThis.origin : undefined;152export const isIOS = _isIOS;153export const isMobile = _isMobile;154/**155* Whether we run inside a CI environment, such as156* GH actions or Azure Pipelines.157*/158export const isCI = _isCI;159export const platform = _platform;160export const userAgent = _userAgent;161162/**163* The language used for the user interface. The format of164* the string is all lower case (e.g. zh-tw for Traditional165* Chinese or de for German)166*/167export const language = _language;168169export namespace Language {170171export function value(): string {172return language;173}174175export function isDefaultVariant(): boolean {176if (language.length === 2) {177return language === 'en';178} else if (language.length >= 3) {179return language[0] === 'e' && language[1] === 'n' && language[2] === '-';180} else {181return false;182}183}184185export function isDefault(): boolean {186return language === 'en';187}188}189190/**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*/196export const locale = _locale;197198/**199* This will always be set to the OS/browser's locale regardless of200* what was specified otherwise. The format of the string is all201* lower case (e.g. zh-tw for Traditional Chinese). The UI is not202* necessarily shown in the provided locale.203*/204export const platformLocale = _platformLocale;205206/**207* The translations that are available through language packs.208*/209export const translationsConfigFile = _translationsConfigFile;210211export const setTimeout0IsFaster = (typeof $globalThis.postMessage === 'function' && !$globalThis.importScripts);212213/**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 delay217* that browsers set when the nesting level is > 5.218*/219export const setTimeout0 = (() => {220if (setTimeout0IsFaster) {221interface IQueueElement {222id: number;223callback: () => void;224}225const pending: IQueueElement[] = [];226227$globalThis.addEventListener('message', (e: any) => {228if (e.data && e.data.vscodeScheduleAsyncWork) {229for (let i = 0, len = pending.length; i < len; i++) {230const candidate = pending[i];231if (candidate.id === e.data.vscodeScheduleAsyncWork) {232pending.splice(i, 1);233candidate.callback();234return;235}236}237}238});239let lastId = 0;240return (callback: () => void) => {241const myId = ++lastId;242pending.push({243id: myId,244callback: callback245});246$globalThis.postMessage({ vscodeScheduleAsyncWork: myId }, '*');247};248}249return (callback: () => void) => setTimeout(callback);250})();251252export const enum OperatingSystem {253Windows = 1,254Macintosh = 2,255Linux = 3256}257export const OS = (_isMacintosh || _isIOS ? OperatingSystem.Macintosh : (_isWindows ? OperatingSystem.Windows : OperatingSystem.Linux));258259let _isLittleEndian = true;260let _isLittleEndianComputed = false;261export function isLittleEndian(): boolean {262if (!_isLittleEndianComputed) {263_isLittleEndianComputed = true;264const test = new Uint8Array(2);265test[0] = 1;266test[1] = 2;267const view = new Uint16Array(test.buffer);268_isLittleEndian = (view[0] === (2 << 8) + 1);269}270return _isLittleEndian;271}272273export const isChrome = !!(userAgent && userAgent.indexOf('Chrome') >= 0);274export const isFirefox = !!(userAgent && userAgent.indexOf('Firefox') >= 0);275export const isSafari = !!(!isChrome && (userAgent && userAgent.indexOf('Safari') >= 0));276export const isEdge = !!(userAgent && userAgent.indexOf('Edg/') >= 0);277export const isAndroid = !!(userAgent && userAgent.indexOf('Android') >= 0);278279export function isTahoeOrNewer(osVersion: string): boolean {280return parseFloat(osVersion) >= 25;281}282283284