forked from betaflight/betaflight-configurator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DarkTheme.js
79 lines (66 loc) · 2.05 KB
/
DarkTheme.js
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
import GUI from "./gui";
import windowWatcherUtil from "./utils/window_watchers";
import { checkSetupAnalytics } from "./Analytics";
import $ from 'jquery';
const css_dark = [
'./css/dark-theme.css',
];
const DarkTheme = {
configEnabled: undefined,
};
DarkTheme.isDarkThemeEnabled = function (callback) {
if (this.configEnabled === 0) {
callback(true);
} else if (this.configEnabled === 2) {
if (GUI.isCordova()) {
cordova.plugins.ThemeDetection.isDarkModeEnabled(function(success) {
callback(success.value);
}, function(error) {
console.log(`cordova-plugin-theme-detection: ${error}`);
callback(false);
});
} else {
const isEnabled = window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches;
callback(isEnabled);
}
} else {
callback(false);
}
};
DarkTheme.apply = function() {
const self = this;
this.isDarkThemeEnabled(function(isEnabled) {
if (isEnabled) {
self.applyDark();
} else {
self.applyNormal();
}
if (chrome.app.window !== undefined) {
windowWatcherUtil.passValue(chrome.app.window.get("receiver_msp"), 'darkTheme', isEnabled);
}
});
};
DarkTheme.autoSet = function() {
if (this.configEnabled === 2) {
this.apply();
}
};
DarkTheme.setConfig = function (result) {
if (this.configEnabled !== result) {
this.configEnabled = result;
this.apply();
}
};
DarkTheme.applyDark = function () {
css_dark.forEach((el) => $(`link[href="${el}"]`).prop('disabled', false));
};
DarkTheme.applyNormal = function () {
css_dark.forEach((el) => $(`link[href="${el}"]`).prop('disabled', true));
};
export function setDarkTheme(enabled) {
DarkTheme.setConfig(enabled);
checkSetupAnalytics(function (analyticsService) {
analyticsService.sendEvent(analyticsService.EVENT_CATEGORIES.APPLICATION, 'DarkTheme', enabled);
});
}
export default DarkTheme;