Files
NativeScript/packages/core/accessibility/font-scale.android.ts
Igor Randjelovic f64355ba7a refactor: improved core barrel exports and Application class (#10286)
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);
```
2023-05-25 07:45:39 -07:00

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();
}