Skip to content

Commit

Permalink
Web - try to detect native fullscreen (#106479)
Browse files Browse the repository at this point in the history
* web - try to detect native fullscreen

* add logging for debugging

* throttle listener

* adjust

* .

* add check for windows
  • Loading branch information
bpasero authored Sep 15, 2020
1 parent e13875b commit fcf3346
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 12 deletions.
63 changes: 63 additions & 0 deletions src/vs/base/browser/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1277,3 +1277,66 @@ export function triggerDownload(dataOrUri: Uint8Array | URI, name: string): void
// Ensure to remove the element from DOM eventually
setTimeout(() => document.body.removeChild(anchor));
}

export enum DetectedFullscreenMode {

/**
* The document is fullscreen, e.g. because an element
* in the document requested to be fullscreen.
*/
DOCUMENT = 1,

/**
* The browser is fullsreen, e.g. because the user enabled
* native window fullscreen for it.
*/
BROWSER
}

export interface IDetectedFullscreen {

/**
* Figure out if the document is fullscreen or the browser.
*/
mode: DetectedFullscreenMode;

/**
* Wether we know for sure that we are in fullscreen mode or
* it is a guess.
*/
guess: boolean;
}

export function detectFullscreen(): IDetectedFullscreen | null {

// Browser fullscreen: use DOM APIs to detect
if (document.fullscreenElement || (<any>document).webkitFullscreenElement || (<any>document).webkitIsFullScreen) {
return { mode: DetectedFullscreenMode.DOCUMENT, guess: false };
}

// There is no standard way to figure out if the browser
// is using native fullscreen. Via checking on screen
// height and comparing that to window height, we can guess
// it though.

if (window.innerHeight === screen.height) {
// if the height of the window matches the screen height, we can
// safely assume that the browser is fullscreen because no browser
// chrome is taking height away (e.g. like toolbars).
return { mode: DetectedFullscreenMode.BROWSER, guess: false };
}

if (platform.isMacintosh || platform.isLinux) {
// macOS and Linux do not properly report `innerHeight`, only Windows does
if (window.outerHeight === screen.height && window.outerWidth === screen.width) {
// if the height of the browser matches the screen height, we can
// only guess that we are in fullscreen. It is also possible that
// the user has turned off taskbars in the OS and the browser is
// simply able to span the entire size of the screen.
return { mode: DetectedFullscreenMode.BROWSER, guess: true };
}
}

// Not in fullscreen
return null;
}
31 changes: 19 additions & 12 deletions src/vs/workbench/browser/web.main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*--------------------------------------------------------------------------------------------*/

import { mark } from 'vs/base/common/performance';
import { domContentLoaded, addDisposableListener, EventType, EventHelper } from 'vs/base/browser/dom';
import { domContentLoaded, addDisposableListener, EventType, EventHelper, detectFullscreen, addDisposableThrottledListener } from 'vs/base/browser/dom';
import { ServiceCollection } from 'vs/platform/instantiation/common/serviceCollection';
import { ILogService, ConsoleLogService, MultiplexLogService } from 'vs/platform/log/common/log';
import { ConsoleLogInAutomationService } from 'vs/platform/log/browser/log';
Expand All @@ -25,8 +25,8 @@ import { Schemas } from 'vs/base/common/network';
import { IWorkspaceContextService } from 'vs/platform/workspace/common/workspace';
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
import { onUnexpectedError } from 'vs/base/common/errors';
import * as browser from 'vs/base/browser/browser';
import * as platform from 'vs/base/common/platform';
import { setFullscreen } from 'vs/base/browser/browser';
import { isIOS, isMacintosh } from 'vs/base/common/platform';
import { URI } from 'vs/base/common/uri';
import { IWorkspaceInitializationPayload } from 'vs/platform/workspaces/common/workspaces';
import { WorkspaceService } from 'vs/workbench/services/configuration/browser/configurationService';
Expand Down Expand Up @@ -58,6 +58,14 @@ class BrowserMain extends Disposable {
private readonly configuration: IWorkbenchConstructionOptions
) {
super();

this.init();
}

private init(): void {

// Browser config
setFullscreen(!!detectFullscreen());
}

async open(): Promise<IWorkbench> {
Expand Down Expand Up @@ -99,7 +107,7 @@ class BrowserMain extends Disposable {
private registerListeners(workbench: Workbench, storageService: BrowserStorageService): void {

// Layout
const viewport = platform.isIOS && (<any>window).visualViewport ? (<any>window).visualViewport /** Visual viewport */ : window /** Layout viewport */;
const viewport = isIOS && window.visualViewport ? window.visualViewport /** Visual viewport */ : window /** Layout viewport */;
this._register(addDisposableListener(viewport, EventType.RESIZE, () => workbench.layout()));

// Prevent the back/forward gestures in macOS
Expand All @@ -123,16 +131,15 @@ class BrowserMain extends Disposable {
}));
this._register(workbench.onShutdown(() => this.dispose()));

// Fullscreen
// Fullscreen (Browser)
[EventType.FULLSCREEN_CHANGE, EventType.WK_FULLSCREEN_CHANGE].forEach(event => {
this._register(addDisposableListener(document, event, () => {
if (document.fullscreenElement || (<any>document).webkitFullscreenElement || (<any>document).webkitIsFullScreen) {
browser.setFullscreen(true);
} else {
browser.setFullscreen(false);
}
}));
this._register(addDisposableListener(document, event, () => setFullscreen(!!detectFullscreen())));
});

// Fullscreen (Native)
this._register(addDisposableThrottledListener(viewport, EventType.RESIZE, () => {
setFullscreen(!!detectFullscreen());
}, undefined, isMacintosh ? 2000 /* adjust for macOS animation */ : 800 /* can be throttled */));
}

private async initServices(): Promise<{ serviceCollection: ServiceCollection, logService: ILogService, storageService: BrowserStorageService }> {
Expand Down

0 comments on commit fcf3346

Please sign in to comment.