1 | import { Application } from '../application';
|
2 | import { AccessibilityServiceEnabledObservable } from './accessibility-service';
|
3 | import { FontScaleCategory, getCurrentFontScale, getFontScaleCategory, VALID_FONT_SCALES } from './font-scale';
|
4 |
|
5 | const fontScaleExtraSmallCategoryClass = `a11y-fontscale-xs`;
|
6 | const fontScaleMediumCategoryClass = `a11y-fontscale-m`;
|
7 | const fontScaleExtraLargeCategoryClass = `a11y-fontscale-xl`;
|
8 | const fontScaleCategoryClasses = [fontScaleExtraSmallCategoryClass, fontScaleMediumCategoryClass, fontScaleExtraLargeCategoryClass];
|
9 | const a11yServiceEnabledClass = `a11y-service-enabled`;
|
10 | const a11yServiceDisabledClass = `a11y-service-disabled`;
|
11 | const a11yServiceClasses = [a11yServiceEnabledClass, a11yServiceDisabledClass];
|
12 | let accessibilityServiceObservable;
|
13 | let fontScaleCssClasses;
|
14 | let currentFontScaleClass = '';
|
15 | let currentFontScaleCategory = '';
|
16 | let currentA11YServiceClass = '';
|
17 | function 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 | }
|
24 | function 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 | }
|
33 | function 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 | }
|
43 | export function initAccessibilityCssHelper() {
|
44 | ensureClasses();
|
45 | Application.on(Application.fontScaleChangedEvent, () => {
|
46 | updateCurrentHelperClasses();
|
47 | applyFontScaleToRootViews();
|
48 | });
|
49 | accessibilityServiceObservable.on(AccessibilityServiceEnabledObservable.propertyChangeEvent, updateCurrentHelperClasses);
|
50 | }
|
51 |
|
52 |
|
53 |
|
54 |
|
55 | function 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 |
|
\ | No newline at end of file |