browser.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. import { Emitter } from '../common/event.js';
  6. class WindowManager {
  7. constructor() {
  8. // --- Zoom Level
  9. this._zoomLevel = 0;
  10. this._lastZoomLevelChangeTime = 0;
  11. this._onDidChangeZoomLevel = new Emitter();
  12. this.onDidChangeZoomLevel = this._onDidChangeZoomLevel.event;
  13. // --- Zoom Factor
  14. this._zoomFactor = 1;
  15. }
  16. getZoomLevel() {
  17. return this._zoomLevel;
  18. }
  19. getTimeSinceLastZoomLevelChanged() {
  20. return Date.now() - this._lastZoomLevelChangeTime;
  21. }
  22. getZoomFactor() {
  23. return this._zoomFactor;
  24. }
  25. // --- Pixel Ratio
  26. getPixelRatio() {
  27. let ctx = document.createElement('canvas').getContext('2d');
  28. let dpr = window.devicePixelRatio || 1;
  29. let bsr = ctx.webkitBackingStorePixelRatio ||
  30. ctx.mozBackingStorePixelRatio ||
  31. ctx.msBackingStorePixelRatio ||
  32. ctx.oBackingStorePixelRatio ||
  33. ctx.backingStorePixelRatio || 1;
  34. return dpr / bsr;
  35. }
  36. }
  37. WindowManager.INSTANCE = new WindowManager();
  38. export function getZoomLevel() {
  39. return WindowManager.INSTANCE.getZoomLevel();
  40. }
  41. /** Returns the time (in ms) since the zoom level was changed */
  42. export function getTimeSinceLastZoomLevelChanged() {
  43. return WindowManager.INSTANCE.getTimeSinceLastZoomLevelChanged();
  44. }
  45. export function onDidChangeZoomLevel(callback) {
  46. return WindowManager.INSTANCE.onDidChangeZoomLevel(callback);
  47. }
  48. /** The zoom scale for an index, e.g. 1, 1.2, 1.4 */
  49. export function getZoomFactor() {
  50. return WindowManager.INSTANCE.getZoomFactor();
  51. }
  52. export function getPixelRatio() {
  53. return WindowManager.INSTANCE.getPixelRatio();
  54. }
  55. const userAgent = navigator.userAgent;
  56. export const isFirefox = (userAgent.indexOf('Firefox') >= 0);
  57. export const isWebKit = (userAgent.indexOf('AppleWebKit') >= 0);
  58. export const isChrome = (userAgent.indexOf('Chrome') >= 0);
  59. export const isSafari = (!isChrome && (userAgent.indexOf('Safari') >= 0));
  60. export const isWebkitWebView = (!isChrome && !isSafari && isWebKit);
  61. export const isEdgeLegacyWebView = (userAgent.indexOf('Edge/') >= 0) && (userAgent.indexOf('WebView/') >= 0);
  62. export const isElectron = (userAgent.indexOf('Electron/') >= 0);
  63. export const isAndroid = (userAgent.indexOf('Android') >= 0);
  64. export const isStandalone = (window.matchMedia && window.matchMedia('(display-mode: standalone)').matches);