| 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 { CodeWindow, mainWindow } from 'vs/base/browser/window'; |
| 7 |
import { Emitter } from 'vs/base/common/event'; |
| 8 |
|
| 9 |
class WindowManager { |
| 10 |
|
| 11 |
static readonly INSTANCE = new WindowManager(); |
| 12 |
|
| 13 |
// --- Zoom Level |
| 14 |
|
| 15 |
private readonly mapWindowIdToZoomLevel = new Map<number, number>(); |
| 16 |
|
| 17 |
private readonly _onDidChangeZoomLevel = new Emitter<number>(); |
| 18 |
readonly onDidChangeZoomLevel = this._onDidChangeZoomLevel.event; |
| 19 |
|
| 20 |
getZoomLevel(targetWindow: Window): number { |
| 21 |
return this.mapWindowIdToZoomLevel.get(this.getWindowId(targetWindow)) ?? 0; |
| 22 |
} |
| 23 |
setZoomLevel(zoomLevel: number, targetWindow: Window): void { |
| 24 |
if (this.getZoomLevel(targetWindow) === zoomLevel) { |
| 25 |
return; |
| 26 |
} |
| 27 |
|
| 28 |
const targetWindowId = this.getWindowId(targetWindow); |
| 29 |
this.mapWindowIdToZoomLevel.set(targetWindowId, zoomLevel); |
| 30 |
this._onDidChangeZoomLevel.fire(targetWindowId); |
| 31 |
} |
| 32 |
|
| 33 |
// --- Zoom Factor |
| 34 |
|
| 35 |
private readonly mapWindowIdToZoomFactor = new Map<number, number>(); |
| 36 |
|
| 37 |
getZoomFactor(targetWindow: Window): number { |
| 38 |
return this.mapWindowIdToZoomFactor.get(this.getWindowId(targetWindow)) ?? 1; |
| 39 |
} |
| 40 |
setZoomFactor(zoomFactor: number, targetWindow: Window): void { |
| 41 |
this.mapWindowIdToZoomFactor.set(this.getWindowId(targetWindow), zoomFactor); |
| 42 |
} |
| 43 |
|
| 44 |
// --- Fullscreen |
| 45 |
|
| 46 |
private readonly _onDidChangeFullscreen = new Emitter<number>(); |
| 47 |
readonly onDidChangeFullscreen = this._onDidChangeFullscreen.event; |
| 48 |
|
| 49 |
private readonly mapWindowIdToFullScreen = new Map<number, boolean>(); |
| 50 |
|
| 51 |
setFullscreen(fullscreen: boolean, targetWindow: Window): void { |
| 52 |
if (this.isFullscreen(targetWindow) === fullscreen) { |
| 53 |
return; |
| 54 |
} |
| 55 |
|
| 56 |
const windowId = this.getWindowId(targetWindow); |
| 57 |
this.mapWindowIdToFullScreen.set(windowId, fullscreen); |
| 58 |
this._onDidChangeFullscreen.fire(windowId); |
| 59 |
} |
| 60 |
isFullscreen(targetWindow: Window): boolean { |
| 61 |
return !!this.mapWindowIdToFullScreen.get(this.getWindowId(targetWindow)); |
| 62 |
} |
| 63 |
|
| 64 |
private getWindowId(targetWindow: Window): number { |
| 65 |
return (targetWindow as CodeWindow).vscodeWindowId; |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
export function addMatchMediaChangeListener(targetWindow: Window, query: string | MediaQueryList, callback: (this: MediaQueryList, ev: MediaQueryListEvent) => any): void { |
| 70 |
if (typeof query === 'string') { |
| 71 |
query = targetWindow.matchMedia(query); |
| 72 |
} |
| 73 |
query.addEventListener('change', callback); |
| 74 |
} |
| 75 |
|
| 76 |
/** A zoom index, e.g. 1, 2, 3 */ |
| 77 |
export function setZoomLevel(zoomLevel: number, targetWindow: Window): void { |
| 78 |
WindowManager.INSTANCE.setZoomLevel(zoomLevel, targetWindow); |
| 79 |
} |
| 80 |
export function getZoomLevel(targetWindow: Window): number { |
| 81 |
return WindowManager.INSTANCE.getZoomLevel(targetWindow); |
| 82 |
} |
| 83 |
export const onDidChangeZoomLevel = WindowManager.INSTANCE.onDidChangeZoomLevel; |
| 84 |
|
| 85 |
/** The zoom scale for an index, e.g. 1, 1.2, 1.4 */ |
| 86 |
export function getZoomFactor(targetWindow: Window): number { |
| 87 |
return WindowManager.INSTANCE.getZoomFactor(targetWindow); |
| 88 |
} |
| 89 |
export function setZoomFactor(zoomFactor: number, targetWindow: Window): void { |
| 90 |
WindowManager.INSTANCE.setZoomFactor(zoomFactor, targetWindow); |
| 91 |
} |
| 92 |
|
| 93 |
export function setFullscreen(fullscreen: boolean, targetWindow: Window): void { |
| 94 |
WindowManager.INSTANCE.setFullscreen(fullscreen, targetWindow); |
| 95 |
} |
| 96 |
export function isFullscreen(targetWindow: Window): boolean { |
| 97 |
return WindowManager.INSTANCE.isFullscreen(targetWindow); |
| 98 |
} |
| 99 |
export const onDidChangeFullscreen = WindowManager.INSTANCE.onDidChangeFullscreen; |
| 100 |
|
| 101 |
const userAgent = typeof navigator === 'object' ? navigator.userAgent : ''; |
| 102 |
|
| 103 |
export const isFirefox = (userAgent.indexOf('Firefox') >= 0); |
| 104 |
export const isWebKit = (userAgent.indexOf('AppleWebKit') >= 0); |
| 105 |
export const isChrome = (userAgent.indexOf('Chrome') >= 0); |
| 106 |
export const isSafari = (!isChrome && (userAgent.indexOf('Safari') >= 0)); |
| 107 |
export const isWebkitWebView = (!isChrome && !isSafari && isWebKit); |
| 108 |
export const isElectron = (userAgent.indexOf('Electron/') >= 0); |
| 109 |
export const isAndroid = (userAgent.indexOf('Android') >= 0); |
| 110 |
|
| 111 |
let standalone = false; |
| 112 |
if (typeof mainWindow.matchMedia === 'function') { |
| 113 |
const standaloneMatchMedia = mainWindow.matchMedia('(display-mode: standalone) or (display-mode: window-controls-overlay)'); |
| 114 |
const fullScreenMatchMedia = mainWindow.matchMedia('(display-mode: fullscreen)'); |
| 115 |
standalone = standaloneMatchMedia.matches; |
| 116 |
addMatchMediaChangeListener(mainWindow, standaloneMatchMedia, ({ matches }) => { |
| 117 |
// entering fullscreen would change standaloneMatchMedia.matches to false |
| 118 |
// if standalone is true (running as PWA) and entering fullscreen, skip this change |
| 119 |
if (standalone && fullScreenMatchMedia.matches) { |
| 120 |
return; |
| 121 |
} |
| 122 |
// otherwise update standalone (browser to PWA or PWA to browser) |
| 123 |
standalone = matches; |
| 124 |
}); |
| 125 |
} |
| 126 |
export function isStandalone(): boolean { |
| 127 |
return standalone; |
| 128 |
} |
| 129 |
|
| 130 |
// Visible means that the feature is enabled, not necessarily being rendered |
| 131 |
// e.g. visible is true even in fullscreen mode where the controls are hidden |
| 132 |
// See docs at https://developer.mozilla.org/en-US/docs/Web/API/WindowControlsOverlay/visible |
| 133 |
export function isWCOEnabled(): boolean { |
| 134 |
return (navigator as any)?.windowControlsOverlay?.visible; |
| 135 |
} |
| 136 |
|
| 137 |
// Returns the bounding rect of the titlebar area if it is supported and defined |
| 138 |
// See docs at https://developer.mozilla.org/en-US/docs/Web/API/WindowControlsOverlay/getTitlebarAreaRect |
| 139 |
export function getWCOBoundingRect(): DOMRect | undefined { |
| 140 |
return (navigator as any)?.windowControlsOverlay?.getTitlebarAreaRect(); |
| 141 |
} |