refactor: circular deps part 13

This commit is contained in:
Nathan Walker
2025-07-09 20:07:56 -07:00
parent ee03774ec0
commit 579a25d583
31 changed files with 552 additions and 387 deletions

View File

@ -1,25 +1,15 @@
import { EventData, Observable } from '../data/observable';
import { Screen } from '../platform';
import { Application, ApplicationEventData } from '../application';
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);
}
}
}