mirror of
				https://github.com/NativeScript/NativeScript.git
				synced 2025-11-04 04:18:52 +08:00 
			
		
		
		
	refactor(core): zero circulars + esm ready (#10770)
This commit is contained in:
		@ -1,25 +1,15 @@
 | 
			
		||||
import { EventData, Observable } from '../data/observable';
 | 
			
		||||
import { Screen } from '../platform';
 | 
			
		||||
import { Application, ApplicationEventData } from '../application';
 | 
			
		||||
import { Screen } from '../platform/screen';
 | 
			
		||||
import { getApplicationProperties, toggleApplicationEventListeners } from '../application/helpers-common';
 | 
			
		||||
import type { ApplicationEventData } from '../application/application-interfaces';
 | 
			
		||||
import { matchQuery, MediaQueryType } from '../css-mediaquery';
 | 
			
		||||
import { Trace } from '../trace';
 | 
			
		||||
 | 
			
		||||
const mediaQueryLists: MediaQueryListImpl[] = [];
 | 
			
		||||
const applicationEvents: string[] = [Application.orientationChangedEvent, Application.systemAppearanceChangedEvent];
 | 
			
		||||
 | 
			
		||||
// In browser, developers cannot create MediaQueryList instances without calling matchMedia
 | 
			
		||||
let isMediaInitializationEnabled: boolean = false;
 | 
			
		||||
 | 
			
		||||
function toggleApplicationEventListeners(toAdd: boolean) {
 | 
			
		||||
	for (const eventName of applicationEvents) {
 | 
			
		||||
		if (toAdd) {
 | 
			
		||||
			Application.on(eventName, onDeviceChange);
 | 
			
		||||
		} else {
 | 
			
		||||
			Application.off(eventName, onDeviceChange);
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function onDeviceChange(args: ApplicationEventData) {
 | 
			
		||||
	for (const mql of mediaQueryLists) {
 | 
			
		||||
		const matches = checkIfMediaQueryMatches(mql.media);
 | 
			
		||||
@ -42,14 +32,15 @@ function checkIfMediaQueryMatches(mediaQueryString: string): boolean {
 | 
			
		||||
	let matches: boolean;
 | 
			
		||||
 | 
			
		||||
	try {
 | 
			
		||||
		const appProperties = getApplicationProperties();
 | 
			
		||||
		matches = matchQuery(mediaQueryString, {
 | 
			
		||||
			type: MediaQueryType.screen,
 | 
			
		||||
			width: widthPixels,
 | 
			
		||||
			height: heightPixels,
 | 
			
		||||
			'device-width': widthPixels,
 | 
			
		||||
			'device-height': heightPixels,
 | 
			
		||||
			orientation: Application.orientation(),
 | 
			
		||||
			'prefers-color-scheme': Application.systemAppearance(),
 | 
			
		||||
			orientation: appProperties.orientation,
 | 
			
		||||
			'prefers-color-scheme': appProperties.systemAppearance,
 | 
			
		||||
		});
 | 
			
		||||
	} catch (err) {
 | 
			
		||||
		matches = false;
 | 
			
		||||
@ -130,7 +121,7 @@ class MediaQueryListImpl extends Observable implements MediaQueryList {
 | 
			
		||||
			mediaQueryLists.push(this);
 | 
			
		||||
 | 
			
		||||
			if (mediaQueryLists.length === 1) {
 | 
			
		||||
				toggleApplicationEventListeners(true);
 | 
			
		||||
				toggleApplicationEventListeners(true, onDeviceChange);
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
@ -151,7 +142,7 @@ class MediaQueryListImpl extends Observable implements MediaQueryList {
 | 
			
		||||
					mediaQueryLists.splice(index, 1);
 | 
			
		||||
 | 
			
		||||
					if (!mediaQueryLists.length) {
 | 
			
		||||
						toggleApplicationEventListeners(false);
 | 
			
		||||
						toggleApplicationEventListeners(false, onDeviceChange);
 | 
			
		||||
					}
 | 
			
		||||
				}
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user