-
-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathindex.js
More file actions
153 lines (113 loc) · 4.14 KB
/
index.js
File metadata and controls
153 lines (113 loc) · 4.14 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
import * as app from "tns-core-modules/application";
import { device, isAndroid, screen } from "tns-core-modules/platform";
import * as view from "tns-core-modules/ui/core/view";
import * as frame from "tns-core-modules/ui/frame";
const display = screen.mainScreen;
const whiteSpaceRegExp = /\s+/;
const platformClass = `ns-${isAndroid ? "android" : "ios"}`;
const sdkVersionClass = device.sdkVersion.replace(".", "-");
let started = false;
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 = app.getRootView()) {
Theme.currentMode = mode;
Theme.rootView = root;
if (!root || !mode) {
return;
}
const classList = new ClassList(Theme.rootView.className);
classList
.remove(Theme.Light, Theme.Dark)
.add(Theme.currentMode);
Theme.rootView.className = classList.get();
}
static toggleMode(isDark) {
if (isDark === undefined) {
Theme.setMode(Theme.getMode() === Theme.Light ? Theme.Dark : Theme.Light);
return;
}
Theme.setMode(isDark ? Theme.Dark : Theme.Light);
}
static getMode() {
const root = app.getRootView();
return Theme.currentMode || ((root.className || "").indexOf(Theme.Dark) !== -1 ? Theme.Dark : Theme.Light);
}
}
Theme.Light = "ns-light";
Theme.Dark = "ns-dark";
export default Theme;
// Where the magic happens
const oldSetupAsRootView = view.ViewCommon.prototype._setupAsRootView;
view.ViewCommon.prototype._setupAsRootView = function() {
oldSetupAsRootView.call(this, ...arguments);
Theme.setMode(Theme.currentMode, this);
};
/* Deprecated root class setters, now available in core modules */
function updateRootClasses(orientation, root = app.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);
if (view._rootModalViews.length) {
const classList = new ClassList(app.getRootView().className);
view._rootModalViews.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.Frame) {
const classList = new ClassList(app.getRootView().className);
updateRootClasses(getOrientation(), desc.value, classList.add("ns-modal").list);
}
}
return target;
}
};
app.on(app.displayedEvent, () => {
const root = app.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() });
app.on(app.orientationChangedEvent, handleOrientation);
started = true;
}
});