UNPKG

4.2 kBJavaScriptView Raw
1import { Application } from '../application';
2import { AccessibilityServiceEnabledObservable } from './accessibility-service';
3import { FontScaleCategory, getCurrentFontScale, getFontScaleCategory, VALID_FONT_SCALES } from './font-scale';
4// CSS-classes
5const fontScaleExtraSmallCategoryClass = `a11y-fontscale-xs`;
6const fontScaleMediumCategoryClass = `a11y-fontscale-m`;
7const fontScaleExtraLargeCategoryClass = `a11y-fontscale-xl`;
8const fontScaleCategoryClasses = [fontScaleExtraSmallCategoryClass, fontScaleMediumCategoryClass, fontScaleExtraLargeCategoryClass];
9const a11yServiceEnabledClass = `a11y-service-enabled`;
10const a11yServiceDisabledClass = `a11y-service-disabled`;
11const a11yServiceClasses = [a11yServiceEnabledClass, a11yServiceDisabledClass];
12let accessibilityServiceObservable;
13let fontScaleCssClasses;
14let currentFontScaleClass = '';
15let currentFontScaleCategory = '';
16let currentA11YServiceClass = '';
17function ensureClasses() {
18 if (accessibilityServiceObservable) {
19 return;
20 }
21 fontScaleCssClasses = new Map(VALID_FONT_SCALES.map((fs) => [fs, `a11y-fontscale-${Number(fs * 100).toFixed(0)}`]));
22 accessibilityServiceObservable = new AccessibilityServiceEnabledObservable();
23}
24function applyRootCssClass(cssClasses, newCssClass) {
25 const rootView = Application.getRootView();
26 if (!rootView) {
27 return;
28 }
29 Application.applyCssClass(rootView, cssClasses, newCssClass);
30 const rootModalViews = rootView._getRootModalViews();
31 rootModalViews.forEach((rootModalView) => Application.applyCssClass(rootModalView, cssClasses, newCssClass));
32}
33function applyFontScaleToRootViews() {
34 const rootView = Application.getRootView();
35 if (!rootView) {
36 return;
37 }
38 const fontScale = getCurrentFontScale();
39 rootView.style.fontScaleInternal = fontScale;
40 const rootModalViews = rootView._getRootModalViews();
41 rootModalViews.forEach((rootModalView) => (rootModalView.style.fontScaleInternal = fontScale));
42}
43export function initAccessibilityCssHelper() {
44 ensureClasses();
45 Application.on(Application.fontScaleChangedEvent, () => {
46 updateCurrentHelperClasses();
47 applyFontScaleToRootViews();
48 });
49 accessibilityServiceObservable.on(AccessibilityServiceEnabledObservable.propertyChangeEvent, updateCurrentHelperClasses);
50}
51/**
52 * Update the helper CSS-classes.
53 * Return true is any changes.
54 */
55function updateCurrentHelperClasses() {
56 const fontScale = getCurrentFontScale();
57 const fontScaleCategory = getFontScaleCategory();
58 const oldFontScaleClass = currentFontScaleClass;
59 if (fontScaleCssClasses.has(fontScale)) {
60 currentFontScaleClass = fontScaleCssClasses.get(fontScale);
61 }
62 else {
63 currentFontScaleClass = fontScaleCssClasses.get(1);
64 }
65 if (oldFontScaleClass !== currentFontScaleClass) {
66 applyRootCssClass([...fontScaleCssClasses.values()], currentFontScaleClass);
67 }
68 const oldActiveFontScaleCategory = currentFontScaleCategory;
69 switch (fontScaleCategory) {
70 case FontScaleCategory.ExtraSmall: {
71 currentFontScaleCategory = fontScaleExtraSmallCategoryClass;
72 break;
73 }
74 case FontScaleCategory.Medium: {
75 currentFontScaleCategory = fontScaleMediumCategoryClass;
76 break;
77 }
78 case FontScaleCategory.ExtraLarge: {
79 currentFontScaleCategory = fontScaleExtraLargeCategoryClass;
80 break;
81 }
82 default: {
83 currentFontScaleCategory = fontScaleMediumCategoryClass;
84 break;
85 }
86 }
87 if (oldActiveFontScaleCategory !== currentFontScaleCategory) {
88 applyRootCssClass(fontScaleCategoryClasses, currentFontScaleCategory);
89 }
90 const oldA11YStatusClass = currentA11YServiceClass;
91 if (accessibilityServiceObservable.accessibilityServiceEnabled) {
92 currentA11YServiceClass = a11yServiceEnabledClass;
93 }
94 else {
95 currentA11YServiceClass = a11yServiceDisabledClass;
96 }
97 if (oldA11YStatusClass !== currentA11YServiceClass) {
98 applyRootCssClass(a11yServiceClasses, currentA11YServiceClass);
99 }
100}
101//# sourceMappingURL=accessibility-css-helper.js.map
\No newline at end of file