UNPKG

3.07 kBJavaScriptView Raw
1import { Application } from '../application';
2import { FontScaleCategory, getClosestValidFontScale } from './font-scale-common';
3export * from './font-scale-common';
4let currentFontScale = null;
5function fontScaleChanged(origFontScale) {
6 const oldValue = currentFontScale;
7 currentFontScale = getClosestValidFontScale(origFontScale);
8 if (oldValue !== currentFontScale) {
9 Application.notify({
10 eventName: Application.fontScaleChangedEvent,
11 object: Application,
12 newValue: currentFontScale,
13 });
14 }
15}
16export function getCurrentFontScale() {
17 setupConfigListener();
18 return currentFontScale;
19}
20export function getFontScaleCategory() {
21 if (currentFontScale < 0.85) {
22 return FontScaleCategory.ExtraSmall;
23 }
24 if (currentFontScale > 1.5) {
25 return FontScaleCategory.ExtraLarge;
26 }
27 return FontScaleCategory.Medium;
28}
29const sizeMap = new Map([
30 [UIContentSizeCategoryExtraSmall, 0.5],
31 [UIContentSizeCategorySmall, 0.7],
32 [UIContentSizeCategoryMedium, 0.85],
33 [UIContentSizeCategoryLarge, 1],
34 [UIContentSizeCategoryExtraLarge, 1.15],
35 [UIContentSizeCategoryExtraExtraLarge, 1.3],
36 [UIContentSizeCategoryExtraExtraExtraLarge, 1.5],
37 [UIContentSizeCategoryAccessibilityMedium, 2],
38 [UIContentSizeCategoryAccessibilityLarge, 2.5],
39 [UIContentSizeCategoryAccessibilityExtraLarge, 3],
40 [UIContentSizeCategoryAccessibilityExtraExtraLarge, 3.5],
41 [UIContentSizeCategoryAccessibilityExtraExtraExtraLarge, 4],
42]);
43function contentSizeUpdated(fontSize) {
44 if (sizeMap.has(fontSize)) {
45 fontScaleChanged(sizeMap.get(fontSize));
46 return;
47 }
48 fontScaleChanged(1);
49}
50function useIOSFontScale() {
51 if (Application.ios.nativeApp) {
52 contentSizeUpdated(Application.ios.nativeApp.preferredContentSizeCategory);
53 }
54 else {
55 fontScaleChanged(1);
56 }
57}
58let fontSizeObserver;
59function setupConfigListener(attempt = 0) {
60 if (fontSizeObserver) {
61 return;
62 }
63 if (!Application.ios.nativeApp) {
64 if (attempt > 100) {
65 fontScaleChanged(1);
66 return;
67 }
68 // Couldn't get launchEvent to trigger.
69 setTimeout(() => setupConfigListener(attempt + 1), 1);
70 return;
71 }
72 fontSizeObserver = Application.ios.addNotificationObserver(UIContentSizeCategoryDidChangeNotification, (args) => {
73 const fontSize = args.userInfo.valueForKey(UIContentSizeCategoryNewValueKey);
74 contentSizeUpdated(fontSize);
75 });
76 Application.on(Application.exitEvent, () => {
77 if (fontSizeObserver) {
78 Application.ios.removeNotificationObserver(fontSizeObserver, UIContentSizeCategoryDidChangeNotification);
79 fontSizeObserver = null;
80 }
81 Application.off(Application.resumeEvent, useIOSFontScale);
82 });
83 Application.on(Application.resumeEvent, useIOSFontScale);
84 useIOSFontScale();
85}
86export function initAccessibilityFontScale() {
87 setupConfigListener();
88}
89//# sourceMappingURL=font-scale.ios.js.map
\No newline at end of file