Skip to content

Commit

Permalink
Add match media change listener in browser ts (#143287)
Browse files Browse the repository at this point in the history
* Replace the isStandalone const with a method of the same name

* Adding the matchMediaChangeListener into browser.ts so it can be used there

* Fix formatting

* Use browser.ts for all usages of addMatchMediaChangeListener

Co-authored-by: Anthony Stewart <anthonystewart@google.com>
  • Loading branch information
a-stewart and a-stewart committed Feb 26, 2022
1 parent 0a22534 commit 12e6b0b
Show file tree
Hide file tree
Showing 9 changed files with 29 additions and 15 deletions.
20 changes: 19 additions & 1 deletion src/vs/base/browser/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,13 @@ class PixelRatioFacade {
}
}

export function addMatchMediaChangeListener(query: string | MediaQueryList, callback: (this: MediaQueryList, ev: MediaQueryListEvent) => any): void {
if (typeof query === 'string') {
query = window.matchMedia(query);
}
query.addEventListener('change', callback);
}

/**
* Returns the pixel ratio.
*
Expand Down Expand Up @@ -186,4 +193,15 @@ export const isSafari = (!isChrome && (userAgent.indexOf('Safari') >= 0));
export const isWebkitWebView = (!isChrome && !isSafari && isWebKit);
export const isElectron = (userAgent.indexOf('Electron/') >= 0);
export const isAndroid = (userAgent.indexOf('Android') >= 0);
export const isStandalone = (window.matchMedia && window.matchMedia('(display-mode: standalone)').matches);

let standalone = false;
if (window.matchMedia) {
const matchMedia = window.matchMedia('(display-mode: standalone)');
standalone = matchMedia.matches;
addMatchMediaChangeListener(matchMedia, ({ matches }) => {
standalone = matches;
});
}
export function isStandalone(): boolean {
return standalone;
}
2 changes: 1 addition & 1 deletion src/vs/base/browser/canIUse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export const BrowserFeatures = {
)
},
keyboard: (() => {
if (platform.isNative || browser.isStandalone) {
if (platform.isNative || browser.isStandalone()) {
return KeyboardSupport.Always;
}

Expand Down
5 changes: 0 additions & 5 deletions src/vs/base/browser/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1638,11 +1638,6 @@ export function getCookieValue(name: string): string | undefined {
return match ? match.pop() : undefined;
}

export function addMatchMediaChangeListener(query: string, callback: () => void): void {
const mediaQueryList = window.matchMedia(query);
mediaQueryList.addEventListener('change', callback);
}

export const enum ZIndex {
SASH = 35,
SuggestWidget = 40,
Expand Down
2 changes: 1 addition & 1 deletion src/vs/code/browser/workbench/workbench.ts
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ class WorkspaceProvider implements IWorkspaceProvider {
return true;
} else {
let result;
if (isStandalone) {
if (isStandalone()) {
result = window.open(targetHref, '_blank', 'toolbar=no'); // ensures to open another 'standalone' window!
} else {
result = window.open(targetHref);
Expand Down
2 changes: 1 addition & 1 deletion src/vs/editor/contrib/gotoSymbol/browser/goToCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ export class DefinitionAction extends SymbolNavigationAction {
}
}

const goToDefinitionKb = isWeb && !isStandalone
const goToDefinitionKb = isWeb && !isStandalone()
? KeyMod.CtrlCmd | KeyCode.F12
: KeyCode.F12;

Expand Down
3 changes: 2 additions & 1 deletion src/vs/editor/standalone/browser/standaloneThemeService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*--------------------------------------------------------------------------------------------*/

import * as dom from 'vs/base/browser/dom';
import { addMatchMediaChangeListener } from 'vs/base/browser/browser';
import { Color } from 'vs/base/common/color';
import { Emitter } from 'vs/base/common/event';
import { FontStyle, TokenizationRegistry, TokenMetadata } from 'vs/editor/common/languages';
Expand Down Expand Up @@ -252,7 +253,7 @@ export class StandaloneThemeService extends Disposable implements IStandaloneThe
this._updateCSS();
});

dom.addMatchMediaChangeListener('(forced-colors: active)', () => {
addMatchMediaChangeListener('(forced-colors: active)', () => {
this._updateActualTheme();
});
}
Expand Down
2 changes: 1 addition & 1 deletion src/vs/workbench/browser/style.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ registerThemingParticipant((theme, collector) => {
}

// Update body background color to ensure the home indicator area looks similar to the workbench
if (isIOS && isStandalone) {
if (isIOS && isStandalone()) {
collector.addRule(`body { background-color: ${workbenchBackground}; }`);
}

Expand Down
2 changes: 1 addition & 1 deletion src/vs/workbench/browser/workbench.contribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,7 @@ const registry = Registry.as<IConfigurationRegistry>(ConfigurationExtensions.Con
localize('window.confirmBeforeClose.keyboardOnly', "Only ask for confirmation if a keybinding was detected. Note that detection may not be possible in some cases."),
localize('window.confirmBeforeClose.never', "Never explicitly ask for confirmation unless data loss is imminent.")
],
'default': isWeb && !isStandalone ? 'keyboardOnly' : 'never', // on by default in web, unless PWA
'default': isWeb && !isStandalone() ? 'keyboardOnly' : 'never', // on by default in web, unless PWA
'description': localize('confirmBeforeCloseWeb', "Controls whether to show a confirmation dialog before closing the browser tab or window. Note that even if enabled, browsers may still decide to close a tab or window without confirmation and that this setting is only a hint that may not work in all cases."),
'scope': ConfigurationScope.APPLICATION,
'included': isWeb
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*--------------------------------------------------------------------------------------------*/

import { Emitter, Event } from 'vs/base/common/event';
import * as dom from 'vs/base/browser/dom';
import { addMatchMediaChangeListener } from 'vs/base/browser/browser';
import { registerSingleton } from 'vs/platform/instantiation/common/extensions';
import { Disposable } from 'vs/base/common/lifecycle';
import { IHostColorSchemeService } from 'vs/workbench/services/themes/common/hostColorSchemeService';
Expand All @@ -24,10 +24,10 @@ export class BrowserHostColorSchemeService extends Disposable implements IHostCo

private registerListeners(): void {

dom.addMatchMediaChangeListener('(prefers-color-scheme: dark)', () => {
addMatchMediaChangeListener('(prefers-color-scheme: dark)', () => {
this._onDidSchemeChangeEvent.fire();
});
dom.addMatchMediaChangeListener('(forced-colors: active)', () => {
addMatchMediaChangeListener('(forced-colors: active)', () => {
this._onDidSchemeChangeEvent.fire();
});
}
Expand Down

0 comments on commit 12e6b0b

Please sign in to comment.