-
Notifications
You must be signed in to change notification settings - Fork 958
Expand file tree
/
Copy pathlist.js
More file actions
161 lines (144 loc) · 3.44 KB
/
list.js
File metadata and controls
161 lines (144 loc) · 3.44 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
import fsOperation from "fileSystem";
import { isDeviceDarkTheme } from "lib/systemConfiguration";
import Url from "utils/Url";
import color from "utils/color";
import fonts from "../lib/fonts";
import settings from "../lib/settings";
import ThemeBuilder from "./builder";
import themes, { updateSystemTheme } from "./preInstalled";
/** @type {Map<string, ThemeBuilder>} */
const appThemes = new Map();
let themeApplied = false;
function init() {
themes.forEach((theme) => add(theme));
(async () => {
updateSystemTheme(isDeviceDarkTheme());
})();
}
/**
* @typedef {object} Theme
* @property {string} id
* @property {string} name
* @property {string} type
* @property {string} version
* @property {string} primaryColor
*/
/**
* Returns a list of all themes
* @returns {Theme[]}
*/
function list() {
return Array.from(appThemes.keys()).map((name) => {
const { id, type, primaryColor, version } = appThemes.get(name);
return {
id,
type,
version,
primaryColor,
name: name.capitalize(),
};
});
}
/**
*
* @param {string} name
* @returns {ThemeBuilder}
*/
function get(name) {
return appThemes.get(name.toLowerCase());
}
/**
*
* @param {ThemeBuilder} theme
* @returns
*/
function add(theme) {
if (!(theme instanceof ThemeBuilder)) return;
if (appThemes.has(theme.id)) return;
appThemes.set(theme.id, theme);
if (settings.value.appTheme === theme.id) {
apply(theme.id);
}
}
/**
* Apply a theme
* @param {string} id The name of the theme to apply
* @param {boolean} init Whether or not this is the first time the theme is being applied
*/
export async function apply(id, init) {
if (!DOES_SUPPORT_THEME) {
id = "default";
}
themeApplied = true;
const loaderFile = Url.join(ASSETS_DIRECTORY, "res/tail-spin.svg");
const svgName = "__tail-spin__.svg";
const img = Url.join(DATA_STORAGE, svgName);
const theme = get(id);
const $style = document.head.get("style#app-theme") ?? (
<style id="app-theme"></style>
);
const update = {
appTheme: id,
};
if (id === "custom") {
update.customTheme = theme.toJSON();
}
if (init && theme.preferredEditorTheme) {
update.editorTheme = theme.preferredEditorTheme;
if (editorManager != null && editorManager.editor != null) {
editorManager.editor.setTheme(theme.preferredEditorTheme);
}
}
if (init && theme.preferredFont) {
update.editorFont = theme.preferredFont;
fonts.setFont(theme.preferredFont);
}
settings.update(update, false);
localStorage.__primary_color = theme.primaryColor;
document.body.setAttribute("theme-type", theme.type);
$style.textContent = theme.css;
document.head.append($style);
// Set status bar and navigation bar color
system.setUiTheme(
color(theme.primaryColor).hex.toString(),
theme.toJSON("hex"),
);
try {
let fs = fsOperation(loaderFile);
const svg = await fs.readFile("utf8");
fs = fsOperation(img);
if (!(await fs.exists())) {
await fsOperation(DATA_STORAGE).createFile(svgName);
}
await fs.writeFile(svg.replace(/#fff/g, theme.primaryColor));
} catch (error) {
window.log("error", error);
}
}
/**
* Update a theme
* @param {ThemeBuilder} theme
*/
export function update(theme) {
if (!(theme instanceof ThemeBuilder)) return;
const oldTheme = get(theme.id);
if (!oldTheme) {
add(theme);
return;
}
const json = theme.toJSON();
Object.keys(json).forEach((key) => {
oldTheme[key] = json[key];
});
}
export default {
get applied() {
return themeApplied;
},
init,
list,
get,
add,
apply,
update,
};