-
-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathindex.js
More file actions
225 lines (172 loc) · 7.21 KB
/
index.js
File metadata and controls
225 lines (172 loc) · 7.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
// This is kinda Hacky, but to retain NS 6 & NS 7 Compatibility we have to use different locations
// For some of the items.
const Core = require("@nativescript/core");
const isNS6 = Core.CSSUtils == null;
const Platform = require("@nativescript/core/platform");
// We have to pull these directly to be compatible with NS 6 & NS 7
const Application = isNS6 ? require("@nativescript/core/application") : Core.Application;
const removeClass = isNS6 ? require("@nativescript/core/css/system-classes").removeFromRootViewCssClasses : Core.CSSUtils.removeFromRootViewCssClasses;
const View = isNS6 ? require("@nativescript/core/ui/core/view").View : Core.View;
const Frame = isNS6 ? require("@nativescript/core/ui/frame").Frame : Core.Frame;
const display = Platform.Screen ? Platform.Screen.mainScreen : Platform.screen.mainScreen;
const Device = Platform.Device ? Platform.Device : Platform.device;
const whiteSpaceRegExp = /\s+/;
const platformClass = `ns-${Platform.isAndroid ? "android" : "ios"}`;
const sdkVersionClass = Device.sdkVersion.replace(".", "-");
let started = false;
if (Platform.isAndroid) {
// Force disabling the system Overriding Theme if on Android
// this will then allow our Theme.Dark, Theme.Auto, and Theme.Light to work correctly...
Application.on("launch", () => {
androidx.appcompat.app.AppCompatDelegate.setDefaultNightMode(androidx.appcompat.app.AppCompatDelegate.MODE_NIGHT_NO);
});
}
export class ClassList {
constructor(className) {
this.list = new Set();
(className || "").split(whiteSpaceRegExp).forEach((v) => v && this.list.add(v));
}
add(...classes) {
classes.forEach((v) => this.list.add(v));
return this;
}
remove(...classes) {
classes.forEach((v) => this.list.delete(v));
return this;
}
get() {
return Array.from(this.list).join(" ");
}
}
export class Theme {
static setMode(mode, root = null) {
if (mode !== Theme.Auto && mode !== Theme.Light && mode !== Theme.Dark) {
console.error("Invalid Theme in setMode", mode);
return;
}
// Set the current Mode
Theme.currentMode = mode;
// If we come into setMode to change, while something else is waiting to change, ignore the prior change...
if (Theme._timer != null) {
clearTimeout(Theme._timer);
Theme._timer = null;
}
// If we have no root view, then we need to get it -- however there is the possibility the root view
// Won't be ready, so we need to throw this off until it is...
if (root == null) {
root = Application.getRootView();
if (root == null) {
Theme._timer = setTimeout( () => { Theme.setMode(mode); Theme._timer = null; });
return;
}
}
Theme.rootView = root;
const classList = new ClassList(Theme.rootView.className);
classList.remove(Theme.Light, Theme.Dark);
if (Theme.currentMode !== Theme.Auto) {
removeClass(Theme.Light);
removeClass(Theme.Dark);
classList.add(Theme.currentMode);
} else {
// Reset to Auto system theme
setTimeout( Application.systemAppearanceChanged.bind(this, Theme.rootView, Application.systemAppearance()));
}
Theme.rootView.className = classList.get();
}
static toggleMode(isDark) {
if (isDark === undefined) {
const mode = Theme.currentMode === Theme.Auto && Application.systemAppearance ? `ns-${Application.systemAppearance()}` : Theme.getMode();
Theme.setMode(mode === Theme.Light ? Theme.Dark : Theme.Light);
return;
}
Theme.setMode(isDark ? Theme.Dark : Theme.Light);
}
static getMode() {
const root = Application.getRootView();
return Theme.currentMode || (((root && root.className) || "").indexOf(Theme.Dark) !== -1 ? Theme.Dark : Theme.Light);
}
}
Theme.Light = "ns-light";
Theme.Dark = "ns-dark";
Theme.Auto = "auto";
Theme.currentMode = Theme.Auto;
export default Theme;
// Where the magic happens
const oldSetupAsRootView = View._setupAsRootView;
View._setupAsRootView = function() {
oldSetupAsRootView.call(this, ...arguments);
Theme.setMode(Theme.currentMode, this);
};
// Disable SystemAppearance changes if Theme.currentMode is not auto
const oldSystemAppearanceChanged = Application.systemAppearanceChanged;
if (oldSystemAppearanceChanged) {
Application.systemAppearanceChanged = function () {
if (Theme.currentMode === Theme.Auto) {
oldSystemAppearanceChanged.call(this, ...arguments);
}
};
}
// Make sure to substitute systemAppearance method too, as some plugins call it directly
const oldSystemAppearance = Application.systemAppearance;
if (oldSystemAppearance) {
Application.systemAppearance = function () {
if (Theme.currentMode === Theme.Auto) {
return oldSystemAppearance.call(this, ...arguments);
}
return Theme.currentMode.substr(3);
};
}
/* Deprecated root class setters, now available in core modules */
function updateRootClasses(orientation, root = Application.getRootView(), classes = []) {
const classList = new ClassList(root.className);
classList
.remove("ns-portrait", "ns-landscape", "ns-unknown", ...classes)
.add(`ns-${orientation}`, ...classes);
root.className = classList.get();
}
function handleOrientation({ newValue: orientation }) {
updateRootClasses(orientation);
let modalViews = View._getRootModalViews();
if (modalViews && modalViews.length) {
const classList = new ClassList(Application.getRootView().className);
modalViews.forEach((view) => updateRootClasses(orientation, view, classList.add("ns-modal").list));
}
}
function getOrientation() {
return display.heightDIPs > display.widthDIPs ? "portrait" : "landscape";
}
const rootModalTrap = {
defineProperty(target, key, desc) {
if (desc && "value" in desc) {
target[key] = desc.value;
if (desc.value instanceof Frame) {
const classList = new ClassList(Application.getRootView().className);
updateRootClasses(getOrientation(), desc.value, classList.add("ns-modal").list);
}
}
return target;
}
};
Application.on(Application.displayedEvent, () => {
const root = Application.getRootView();
// Bail out if no root view or root classes already set (pre 6.1).
if (!root || root.cssClasses.has("ns-root")) {
// Add ns-{platform}-{sdkVersion} classes
if (root) {
root.className = new ClassList(root.className)
.add(`${platformClass}__${sdkVersionClass}`)
.get();
}
return;
}
// Get notified when a modal is created.
View._rootModalViews = new Proxy(View._rootModalViews, rootModalTrap);
root.className = new ClassList(root.className)
.add("ns-root", platformClass, `ns-${Device.deviceType.toLowerCase()}`)
.get();
if (!started) {
handleOrientation({ newValue: getOrientation() });
Application.on(Application.orientationChangedEvent, handleOrientation);
started = true;
}
});