UNPKG

@nativescript/core

Version:

A JavaScript library providing an easy to use api for interacting with iOS and Android platform APIs.

89 lines 3.07 kB
import { Application } from '../application'; import { FontScaleCategory, getClosestValidFontScale } from './font-scale-common'; export * from './font-scale-common'; let currentFontScale = null; function fontScaleChanged(origFontScale) { const oldValue = currentFontScale; currentFontScale = getClosestValidFontScale(origFontScale); if (oldValue !== currentFontScale) { Application.notify({ eventName: Application.fontScaleChangedEvent, object: Application, newValue: currentFontScale, }); } } export function getCurrentFontScale() { setupConfigListener(); return currentFontScale; } export function getFontScaleCategory() { if (currentFontScale < 0.85) { return FontScaleCategory.ExtraSmall; } if (currentFontScale > 1.5) { return FontScaleCategory.ExtraLarge; } return FontScaleCategory.Medium; } const sizeMap = new Map([ [UIContentSizeCategoryExtraSmall, 0.5], [UIContentSizeCategorySmall, 0.7], [UIContentSizeCategoryMedium, 0.85], [UIContentSizeCategoryLarge, 1], [UIContentSizeCategoryExtraLarge, 1.15], [UIContentSizeCategoryExtraExtraLarge, 1.3], [UIContentSizeCategoryExtraExtraExtraLarge, 1.5], [UIContentSizeCategoryAccessibilityMedium, 2], [UIContentSizeCategoryAccessibilityLarge, 2.5], [UIContentSizeCategoryAccessibilityExtraLarge, 3], [UIContentSizeCategoryAccessibilityExtraExtraLarge, 3.5], [UIContentSizeCategoryAccessibilityExtraExtraExtraLarge, 4], ]); function contentSizeUpdated(fontSize) { if (sizeMap.has(fontSize)) { fontScaleChanged(sizeMap.get(fontSize)); return; } fontScaleChanged(1); } function useIOSFontScale() { if (Application.ios.nativeApp) { contentSizeUpdated(Application.ios.nativeApp.preferredContentSizeCategory); } else { fontScaleChanged(1); } } let fontSizeObserver; function setupConfigListener(attempt = 0) { if (fontSizeObserver) { return; } if (!Application.ios.nativeApp) { if (attempt > 100) { fontScaleChanged(1); return; } // Couldn't get launchEvent to trigger. setTimeout(() => setupConfigListener(attempt + 1), 1); return; } fontSizeObserver = Application.ios.addNotificationObserver(UIContentSizeCategoryDidChangeNotification, (args) => { const fontSize = args.userInfo.valueForKey(UIContentSizeCategoryNewValueKey); contentSizeUpdated(fontSize); }); Application.on(Application.exitEvent, () => { if (fontSizeObserver) { Application.ios.removeNotificationObserver(fontSizeObserver, UIContentSizeCategoryDidChangeNotification); fontSizeObserver = null; } Application.off(Application.resumeEvent, useIOSFontScale); }); Application.on(Application.resumeEvent, useIOSFontScale); useIOSFontScale(); } export function initAccessibilityFontScale() { setupConfigListener(); } //# sourceMappingURL=font-scale.ios.js.map