mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-15 19:26:42 +08:00

BREAKING CHANGES: `Application.orientation` is no longer a function. Migration: Remove `()` from the `Application.orientation()` call: ```diff import { Application } from "@nativescript/core"; -console.log(Application.orientation()); +console.log(Application.orientation); ``` `Application.systemAppearance` is no longer a function. Migration: Remove `()` from the `Application.systemAppearance()` call: ```diff import { Application } from "@nativescript/core"; -console.log(Application.systemAppearance()); +console.log(Application.systemAppearance); ```
68 lines
1.7 KiB
TypeScript
68 lines
1.7 KiB
TypeScript
import { Application, ApplicationEventData } from '../application';
|
|
import { FontScaleCategory, getClosestValidFontScale } from './font-scale-common';
|
|
export * from './font-scale-common';
|
|
|
|
let currentFontScale: number = null;
|
|
function fontScaleChanged(origFontScale: number) {
|
|
const oldValue = currentFontScale;
|
|
currentFontScale = getClosestValidFontScale(origFontScale);
|
|
|
|
if (oldValue !== currentFontScale) {
|
|
Application.notify({
|
|
eventName: Application.fontScaleChangedEvent,
|
|
object: Application,
|
|
newValue: currentFontScale,
|
|
} as ApplicationEventData);
|
|
}
|
|
}
|
|
|
|
export function getCurrentFontScale(): number {
|
|
setupConfigListener();
|
|
|
|
return currentFontScale;
|
|
}
|
|
|
|
export function getFontScaleCategory(): FontScaleCategory {
|
|
return FontScaleCategory.Medium;
|
|
}
|
|
|
|
function useAndroidFontScale() {
|
|
fontScaleChanged(Number(Application.android.context.getResources().getConfiguration().fontScale));
|
|
}
|
|
|
|
let configChangedCallback: android.content.ComponentCallbacks2;
|
|
function setupConfigListener() {
|
|
if (configChangedCallback) {
|
|
return;
|
|
}
|
|
|
|
Application.off(Application.launchEvent, setupConfigListener);
|
|
const context = Application.android?.context as android.content.Context;
|
|
if (!context) {
|
|
Application.on(Application.launchEvent, setupConfigListener);
|
|
|
|
return;
|
|
}
|
|
|
|
useAndroidFontScale();
|
|
|
|
configChangedCallback = new android.content.ComponentCallbacks2({
|
|
onLowMemory() {
|
|
// Dummy
|
|
},
|
|
onTrimMemory() {
|
|
// Dummy
|
|
},
|
|
onConfigurationChanged(newConfig: android.content.res.Configuration) {
|
|
fontScaleChanged(Number(newConfig.fontScale));
|
|
},
|
|
});
|
|
|
|
context.registerComponentCallbacks(configChangedCallback);
|
|
Application.on(Application.resumeEvent, useAndroidFontScale);
|
|
}
|
|
|
|
export function initAccessibilityFontScale(): void {
|
|
setupConfigListener();
|
|
}
|