-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy paththemebox.js
More file actions
285 lines (261 loc) · 8.78 KB
/
themebox.js
File metadata and controls
285 lines (261 loc) · 8.78 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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
// Copyright (c) 2025 Arjun Jayan
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the The GNU Affero General Public
// License as published by the Free Software Foundation; either
// version 3 of the License, or (at your option) any later version.
//
// You should have received a copy of the GNU Affero General Public
// License along with this library; if not, write to the Free Software
// Foundation, 51 Franklin Street, Suite 500 Boston, MA 02110-1335 USA
//A dropdown for selecting theme
/*
global _, platformColor
*/
/* exported ThemeBox, themeConfigs */
const themeConfigs = {
dark: {
textColor: "#E2E2E2",
blockText: "#E2E2E2",
dialogueBox: "#1C1C1C",
strokeColor: "#E2E2E2",
fillColor: "#F9F9F9",
blueButton: "#0066FF",
blueButtonHover: "#023a76",
cancelButton: "#f1f1f1",
cancelButtonHover: "#afafaf",
hoverColor: "#808080",
widgetButton: "#225A91",
widgetButtonSelect: "#979797",
widgetBackground: "#454545",
disconnected: "#5C5C5C",
header: "#1E88E5",
aux: "#1976D2",
sub: "#64B5F6",
rule: "#303030",
ruleColor: "#303030",
trashColor: "#757575",
trashBorder: "#424242",
trashActive: "#E53935",
background: "#303030",
paletteSelected: "#1E1E1E",
paletteBackground: "#1C1C1C",
paletteLabelBackground: "#022363",
paletteLabelSelected: "#01143b",
paletteText: "#BDBDBD",
rulerHighlight: "#FFEB3B",
selectorBackground: "#64B5F6",
selectorSelected: "#1E88E5",
labelColor: "#BDBDBD",
rhythmcellcolor: "#303030",
stopIconcolor: "#D50000",
hitAreaGraphicsBeginFill: "#121212",
orange: "#FB8C00"
},
light: {
textColor: "black",
blockText: "#282828",
dialogueBox: "#fff",
strokeColor: "#E2E2E2",
fillColor: "#F9F9F9",
blueButton: "#0066FF",
blueButtonHover: "#023a76",
cancelButton: "#f1f1f1",
cancelButtonHover: "#afafaf",
hoverColor: "#E0E0E0",
widgetBackground: "#ccc",
widgetButton: "#8cc6ff",
widgetButtonSelect: "#C8C8C8",
disconnected: "#C4C4C4",
header: "#4DA6FF",
aux: "#1A8CFF",
sub: "#8CC6FF",
rule: "#E2E2E2",
ruleColor: "#E2E2E2",
trashColor: "#C0C0C0",
trashBorder: "#808080",
trashActive: "#FF0000",
background: "#F9F9F9",
paletteSelected: "#F3F3F3",
paletteBackground: "#FFFFFF",
paletteLabelBackground: "#8CC6FF",
paletteLabelSelected: "#1A8CFF",
paletteText: "#666666",
rulerHighlight: "#FFBF00",
selectorBackground: "#8CC6FF",
selectorSelected: "#1A8CFF",
labelColor: "#a0a0a0",
rhythmcellcolor: "#c8c8c8",
stopIconcolor: "#ea174c",
hitAreaGraphicsBeginFill: "#FFF",
orange: "#e37a00"
}
};
class ThemeBox {
/**
* @constructor
*/
constructor(activity) {
this.activity = activity;
// Use stored preference if exists, otherwise detect system preference
this._theme = activity.storage.themePreference || getSystemThemePreference();
this._themes = ["light", "dark"];
}
/**
* @public
* @returns {void}
*/
light_onclick() {
this._theme = "light";
this.setPreference();
}
/**
* @public
* @returns {void}
*/
dark_onclick() {
this._theme = "dark";
this.setPreference();
}
/**
* Apply theme instantly without page reload
* @private
* @returns {void}
*/
applyThemeInstantly() {
const body = document.body;
// Update body classes
this._themes.forEach(theme => {
if (theme === this._theme) {
body.classList.add(theme);
} else {
body.classList.remove(theme);
}
});
// Update platformColor globally
if (themeConfigs[this._theme]) {
Object.assign(window.platformColor, themeConfigs[this._theme]);
}
// Update theme-color meta tag
const themeColorMeta = document.querySelector("meta[name=theme-color]");
if (themeColorMeta && window.platformColor) {
themeColorMeta.content = window.platformColor.header;
}
// Update canvas background using theme config
const canvas = document.getElementById("canvas");
if (canvas) {
canvas.style.backgroundColor = window.platformColor.background;
}
// Update the turtles background color (this redraws the canvas background)
if (this.activity.turtles) {
// Unlock to allow makeBackground to redraw
this.activity.turtles._locked = false;
this.activity.turtles._backgroundColor = window.platformColor.background;
this.activity.turtles.makeBackground();
this.activity.refreshCanvas();
}
// Update toolbar icon for current theme
this.updateThemeIcon();
// Refresh UI components that depend on platformColor
this.refreshUIComponents();
// Notify user
this.activity.textMsg(_("Theme switched to " + this._theme + " mode."), 2000);
}
/**
* Update theme selector icon to reflect current theme
* @private
* @returns {void}
*/
updateThemeIcon() {
const themeSelectIcon = document.getElementById("themeSelectIcon");
if (themeSelectIcon) {
const currentThemeElement = document.getElementById(this._theme);
if (currentThemeElement) {
themeSelectIcon.innerHTML = currentThemeElement.innerHTML;
}
}
}
/**
* Refresh UI components that depend on theme colors
* @private
* @returns {void}
*/
refreshUIComponents() {
// Refresh palette if it exists
if (this.activity.palettes) {
try {
// Update palette selector border color
const paletteElement = document.getElementById("palette");
if (paletteElement && paletteElement.childNodes[0]) {
paletteElement.childNodes[0].style.border = `1px solid ${window.platformColor.selectorSelected}`;
}
} catch (e) {
console.debug("Could not refresh palette:", e);
}
}
// Refresh floating windows
const floatingWindows = document.querySelectorAll("#floatingWindows > .windowFrame");
floatingWindows.forEach(win => {
if (this._theme === "dark") {
win.style.backgroundColor = "#454545";
win.style.borderColor = "#000000";
} else {
win.style.backgroundColor = "";
win.style.borderColor = "";
}
});
// Refresh the activity canvas if available
if (this.activity.refreshCanvas) {
this.activity.refreshCanvas();
}
// Update planet iframe theme if it exists
const planetIframe = document.getElementById("planet-iframe");
if (planetIframe && planetIframe.contentDocument) {
try {
const planetBody = planetIframe.contentDocument.body;
if (planetBody) {
this._themes.forEach(theme => {
if (theme === this._theme) {
planetBody.classList.add(theme);
} else {
planetBody.classList.remove(theme);
}
});
}
} catch (e) {
// Cross-origin restriction may prevent this
console.debug("Could not update planet iframe theme:", e);
}
}
}
/**
* @public
* @returns {void}
*/
reload() {
// Keep for backward compatibility, but prefer instant switching
window.location.reload();
}
/**
* @public
* @returns {void}
*/
setPreference() {
if (localStorage.getItem("themePreference") === this._theme) {
this.activity.textMsg(_("Music Blocks is already set to this theme."));
} else {
// Save preference to localStorage
this.activity.storage.themePreference = this._theme;
try {
localStorage.setItem("themePreference", this._theme);
} catch (e) {
console.warn("Could not save theme preference:", e);
}
// Apply theme instantly instead of reloading
this.applyThemeInstantly();
}
}
}
if (typeof module !== "undefined" && module.exports) {
module.exports = ThemeBox;
}