platform.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. var _a;
  2. /*---------------------------------------------------------------------------------------------
  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. *--------------------------------------------------------------------------------------------*/
  6. import * as nls from '../../nls.js';
  7. export const LANGUAGE_DEFAULT = 'en';
  8. let _isWindows = false;
  9. let _isMacintosh = false;
  10. let _isLinux = false;
  11. let _isLinuxSnap = false;
  12. let _isNative = false;
  13. let _isWeb = false;
  14. let _isElectron = false;
  15. let _isIOS = false;
  16. let _isCI = false;
  17. let _isMobile = false;
  18. let _locale = undefined;
  19. let _language = LANGUAGE_DEFAULT;
  20. let _platformLocale = LANGUAGE_DEFAULT;
  21. let _translationsConfigFile = undefined;
  22. let _userAgent = undefined;
  23. /**
  24. * @deprecated use `globalThis` instead
  25. */
  26. export const globals = (typeof self === 'object' ? self : typeof global === 'object' ? global : {});
  27. let nodeProcess = undefined;
  28. if (typeof globals.vscode !== 'undefined' && typeof globals.vscode.process !== 'undefined') {
  29. // Native environment (sandboxed)
  30. nodeProcess = globals.vscode.process;
  31. }
  32. else if (typeof process !== 'undefined') {
  33. // Native environment (non-sandboxed)
  34. nodeProcess = process;
  35. }
  36. const isElectronProcess = typeof ((_a = nodeProcess === null || nodeProcess === void 0 ? void 0 : nodeProcess.versions) === null || _a === void 0 ? void 0 : _a.electron) === 'string';
  37. const isElectronRenderer = isElectronProcess && (nodeProcess === null || nodeProcess === void 0 ? void 0 : nodeProcess.type) === 'renderer';
  38. // Web environment
  39. if (typeof navigator === 'object' && !isElectronRenderer) {
  40. _userAgent = navigator.userAgent;
  41. _isWindows = _userAgent.indexOf('Windows') >= 0;
  42. _isMacintosh = _userAgent.indexOf('Macintosh') >= 0;
  43. _isIOS = (_userAgent.indexOf('Macintosh') >= 0 || _userAgent.indexOf('iPad') >= 0 || _userAgent.indexOf('iPhone') >= 0) && !!navigator.maxTouchPoints && navigator.maxTouchPoints > 0;
  44. _isLinux = _userAgent.indexOf('Linux') >= 0;
  45. _isMobile = (_userAgent === null || _userAgent === void 0 ? void 0 : _userAgent.indexOf('Mobi')) >= 0;
  46. _isWeb = true;
  47. const configuredLocale = nls.getConfiguredDefaultLocale(
  48. // This call _must_ be done in the file that calls `nls.getConfiguredDefaultLocale`
  49. // to ensure that the NLS AMD Loader plugin has been loaded and configured.
  50. // This is because the loader plugin decides what the default locale is based on
  51. // how it's able to resolve the strings.
  52. nls.localize({ key: 'ensureLoaderPluginIsLoaded', comment: ['{Locked}'] }, '_'));
  53. _locale = configuredLocale || LANGUAGE_DEFAULT;
  54. _language = _locale;
  55. _platformLocale = navigator.language;
  56. }
  57. // Native environment
  58. else if (typeof nodeProcess === 'object') {
  59. _isWindows = (nodeProcess.platform === 'win32');
  60. _isMacintosh = (nodeProcess.platform === 'darwin');
  61. _isLinux = (nodeProcess.platform === 'linux');
  62. _isLinuxSnap = _isLinux && !!nodeProcess.env['SNAP'] && !!nodeProcess.env['SNAP_REVISION'];
  63. _isElectron = isElectronProcess;
  64. _isCI = !!nodeProcess.env['CI'] || !!nodeProcess.env['BUILD_ARTIFACTSTAGINGDIRECTORY'];
  65. _locale = LANGUAGE_DEFAULT;
  66. _language = LANGUAGE_DEFAULT;
  67. const rawNlsConfig = nodeProcess.env['VSCODE_NLS_CONFIG'];
  68. if (rawNlsConfig) {
  69. try {
  70. const nlsConfig = JSON.parse(rawNlsConfig);
  71. const resolved = nlsConfig.availableLanguages['*'];
  72. _locale = nlsConfig.locale;
  73. _platformLocale = nlsConfig.osLocale;
  74. // VSCode's default language is 'en'
  75. _language = resolved ? resolved : LANGUAGE_DEFAULT;
  76. _translationsConfigFile = nlsConfig._translationsConfigFile;
  77. }
  78. catch (e) {
  79. }
  80. }
  81. _isNative = true;
  82. }
  83. // Unknown environment
  84. else {
  85. console.error('Unable to resolve platform.');
  86. }
  87. let _platform = 0 /* Platform.Web */;
  88. if (_isMacintosh) {
  89. _platform = 1 /* Platform.Mac */;
  90. }
  91. else if (_isWindows) {
  92. _platform = 3 /* Platform.Windows */;
  93. }
  94. else if (_isLinux) {
  95. _platform = 2 /* Platform.Linux */;
  96. }
  97. export const isWindows = _isWindows;
  98. export const isMacintosh = _isMacintosh;
  99. export const isLinux = _isLinux;
  100. export const isNative = _isNative;
  101. export const isWeb = _isWeb;
  102. export const isWebWorker = (_isWeb && typeof globals.importScripts === 'function');
  103. export const isIOS = _isIOS;
  104. export const isMobile = _isMobile;
  105. export const userAgent = _userAgent;
  106. /**
  107. * The language used for the user interface. The format of
  108. * the string is all lower case (e.g. zh-tw for Traditional
  109. * Chinese)
  110. */
  111. export const language = _language;
  112. export const setTimeout0IsFaster = (typeof globals.postMessage === 'function' && !globals.importScripts);
  113. /**
  114. * See https://html.spec.whatwg.org/multipage/timers-and-user-prompts.html#:~:text=than%204%2C%20then-,set%20timeout%20to%204,-.
  115. *
  116. * Works similarly to `setTimeout(0)` but doesn't suffer from the 4ms artificial delay
  117. * that browsers set when the nesting level is > 5.
  118. */
  119. export const setTimeout0 = (() => {
  120. if (setTimeout0IsFaster) {
  121. const pending = [];
  122. globals.addEventListener('message', (e) => {
  123. if (e.data && e.data.vscodeScheduleAsyncWork) {
  124. for (let i = 0, len = pending.length; i < len; i++) {
  125. const candidate = pending[i];
  126. if (candidate.id === e.data.vscodeScheduleAsyncWork) {
  127. pending.splice(i, 1);
  128. candidate.callback();
  129. return;
  130. }
  131. }
  132. }
  133. });
  134. let lastId = 0;
  135. return (callback) => {
  136. const myId = ++lastId;
  137. pending.push({
  138. id: myId,
  139. callback: callback
  140. });
  141. globals.postMessage({ vscodeScheduleAsyncWork: myId }, '*');
  142. };
  143. }
  144. return (callback) => setTimeout(callback);
  145. })();
  146. export const OS = (_isMacintosh || _isIOS ? 2 /* OperatingSystem.Macintosh */ : (_isWindows ? 1 /* OperatingSystem.Windows */ : 3 /* OperatingSystem.Linux */));
  147. let _isLittleEndian = true;
  148. let _isLittleEndianComputed = false;
  149. export function isLittleEndian() {
  150. if (!_isLittleEndianComputed) {
  151. _isLittleEndianComputed = true;
  152. const test = new Uint8Array(2);
  153. test[0] = 1;
  154. test[1] = 2;
  155. const view = new Uint16Array(test.buffer);
  156. _isLittleEndian = (view[0] === (2 << 8) + 1);
  157. }
  158. return _isLittleEndian;
  159. }
  160. export const isChrome = !!(userAgent && userAgent.indexOf('Chrome') >= 0);
  161. export const isFirefox = !!(userAgent && userAgent.indexOf('Firefox') >= 0);
  162. export const isSafari = !!(!isChrome && (userAgent && userAgent.indexOf('Safari') >= 0));
  163. export const isEdge = !!(userAgent && userAgent.indexOf('Edg/') >= 0);
  164. export const isAndroid = !!(userAgent && userAgent.indexOf('Android') >= 0);