-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsettingsStore.js
More file actions
310 lines (274 loc) · 11.8 KB
/
settingsStore.js
File metadata and controls
310 lines (274 loc) · 11.8 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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
const fs = require("fs");
const path = require("path");
const DEFAULT_SETTINGS = Object.freeze({
selectedTheme: "ambientWave",
ambientWave: Object.freeze({
tone: "blue",
sensitivity: "medium",
edgeMode: "bottom",
glowStrength: "medium"
}),
reactiveBorder: Object.freeze({
colorStyle: "rainbow",
intensity: "medium",
borderThickness: "thin",
glowStrength: "medium"
}),
flowBorder: Object.freeze({
direction: "clockwise",
speedMode: "balanced",
segmentLength: "medium",
glowStrength: "medium",
colorStyle: "rainbow"
}),
sideBars: Object.freeze({
colorStyle: "multicolor",
barThickness: "thick",
sensitivity: "medium",
barDensity: "medium"
}),
flatRipples: Object.freeze({
mode: "sideRipples",
intensity: "medium",
colorStyle: "blue",
speed: "calm"
}),
dotParticles: Object.freeze({
density: "medium",
motionStyle: "balanced",
directionBehavior: "beatReactive",
glowStrength: "medium"
}),
rippleFlow: Object.freeze({
mode: "sideRipples",
intensity: "medium",
sensitivity: "medium",
colorStyle: "blue"
}),
snowBubbleParticles: Object.freeze({
fallArea: "middle",
density: "medium",
motionStyle: "balanced",
glowStrength: "medium",
particleSize: "medium"
}),
edgeCrystals: Object.freeze({
flutterStyle: "balanced",
density: "medium",
glowStrength: "medium",
colorStyle: "blue",
edgeMode: "both"
})
});
const VALID_MAIN_THEMES = new Set(["ambientWave", "reactiveBorder", "flowBorder", "sideBars", "flatRipples", "dotParticles", "rippleFlow", "snowBubbleParticles", "edgeCrystals"]);
const VALID_AMBIENT_TONES = new Set(["blue", "purple", "warm"]);
const VALID_LEVELS = new Set(["low", "medium", "high"]);
const VALID_EDGE_MODES = new Set(["top", "bottom", "both"]);
const VALID_GLOW_STRENGTHS = new Set(["soft", "medium", "strong"]);
const VALID_REACTIVE_COLOR_STYLES = new Set(["rainbow", "neonBlue", "neonPurple", "warmGlow"]);
const VALID_BORDER_THICKNESS = new Set(["thin", "medium", "thick"]);
const VALID_FLOW_DIRECTIONS = new Set(["clockwise", "anticlockwise"]);
const VALID_FLOW_SPEEDS = new Set(["calm", "balanced", "energetic"]);
const VALID_FLOW_SEGMENTS = new Set(["short", "medium", "long"]);
const VALID_FLOW_COLOR_STYLES = new Set(["rainbow", "cool", "warm"]);
const VALID_SIDE_BARS_COLOR_STYLES = new Set(["white", "yellow", "aqua", "multicolor"]);
const VALID_SIDE_BARS_THICKNESS = new Set(["thin", "medium", "thick"]);
const VALID_SIDE_BARS_DENSITY = new Set(["low", "medium", "high"]);
const VALID_FLAT_RIPPLES_MODES = new Set(["sideRipples", "flatRipples"]);
const VALID_FLAT_RIPPLES_COLORS = new Set(["red", "blue", "white", "multicolor"]);
const VALID_FLAT_RIPPLES_SPEEDS = new Set(["calm", "balanced", "energetic"]);
const VALID_DOT_PARTICLES_MOTION_STYLES = new Set(["calm", "balanced", "energetic"]);
const VALID_DOT_PARTICLES_DIRECTIONS = new Set(["mostlyClockwise", "mostlyAnticlockwise", "beatReactive"]);
const VALID_RIPPLE_FLOW_MODES = new Set(["sideRipples", "flatRipples"]);
const VALID_RIPPLE_FLOW_COLORS = new Set(["red", "blue", "white"]);
const VALID_SNOW_FALL_AREAS = new Set(["middle", "fullWidth"]);
const VALID_PARTICLE_SIZES = new Set(["small", "medium", "large"]);
const VALID_EDGE_FLUTTER_STYLES = new Set(["soft", "balanced", "energetic"]);
const VALID_EDGE_FLUTTER_COLORS = new Set(["blue", "purple", "red", "white"]);
const VALID_EDGE_FLUTTER_MODES = new Set(["left", "right", "both"]);
function createDefaultSettings() {
return {
selectedTheme: DEFAULT_SETTINGS.selectedTheme,
ambientWave: { ...DEFAULT_SETTINGS.ambientWave },
reactiveBorder: { ...DEFAULT_SETTINGS.reactiveBorder },
flowBorder: { ...DEFAULT_SETTINGS.flowBorder },
sideBars: { ...DEFAULT_SETTINGS.sideBars },
flatRipples: { ...DEFAULT_SETTINGS.flatRipples },
dotParticles: { ...DEFAULT_SETTINGS.dotParticles },
rippleFlow: { ...DEFAULT_SETTINGS.rippleFlow },
snowBubbleParticles: { ...DEFAULT_SETTINGS.snowBubbleParticles },
edgeCrystals: { ...DEFAULT_SETTINGS.edgeCrystals }
};
}
function pick(value, validValues, fallback) {
return validValues.has(value) ? value : fallback;
}
function legacySensitivityToLevel(value) {
if (!Number.isFinite(value)) {
return "medium";
}
if (value < 2.6) {
return "low";
}
if (value < 4.2) {
return "medium";
}
return "high";
}
function sanitizeAmbientWave(input = {}) {
return {
tone: pick(input.tone, VALID_AMBIENT_TONES, DEFAULT_SETTINGS.ambientWave.tone),
sensitivity: pick(input.sensitivity, VALID_LEVELS, DEFAULT_SETTINGS.ambientWave.sensitivity),
edgeMode: pick(input.edgeMode, VALID_EDGE_MODES, DEFAULT_SETTINGS.ambientWave.edgeMode),
glowStrength: pick(input.glowStrength, VALID_GLOW_STRENGTHS, DEFAULT_SETTINGS.ambientWave.glowStrength)
};
}
function sanitizeReactiveBorder(input = {}) {
return {
colorStyle: pick(input.colorStyle, VALID_REACTIVE_COLOR_STYLES, DEFAULT_SETTINGS.reactiveBorder.colorStyle),
intensity: pick(input.intensity, VALID_LEVELS, DEFAULT_SETTINGS.reactiveBorder.intensity),
borderThickness: pick(input.borderThickness, VALID_BORDER_THICKNESS, DEFAULT_SETTINGS.reactiveBorder.borderThickness),
glowStrength: pick(input.glowStrength, VALID_GLOW_STRENGTHS, DEFAULT_SETTINGS.reactiveBorder.glowStrength)
};
}
function sanitizeFlowBorder(input = {}) {
return {
direction: pick(input.direction, VALID_FLOW_DIRECTIONS, DEFAULT_SETTINGS.flowBorder.direction),
speedMode: pick(input.speedMode, VALID_FLOW_SPEEDS, DEFAULT_SETTINGS.flowBorder.speedMode),
segmentLength: pick(input.segmentLength, VALID_FLOW_SEGMENTS, DEFAULT_SETTINGS.flowBorder.segmentLength),
glowStrength: pick(input.glowStrength, VALID_GLOW_STRENGTHS, DEFAULT_SETTINGS.flowBorder.glowStrength),
colorStyle: pick(input.colorStyle, VALID_FLOW_COLOR_STYLES, DEFAULT_SETTINGS.flowBorder.colorStyle)
};
}
function sanitizeSideBars(input = {}) {
return {
colorStyle: pick(input.colorStyle, VALID_SIDE_BARS_COLOR_STYLES, DEFAULT_SETTINGS.sideBars.colorStyle),
barThickness: pick(input.barThickness, VALID_SIDE_BARS_THICKNESS, DEFAULT_SETTINGS.sideBars.barThickness),
sensitivity: pick(input.sensitivity, VALID_LEVELS, DEFAULT_SETTINGS.sideBars.sensitivity),
barDensity: pick(input.barDensity, VALID_SIDE_BARS_DENSITY, DEFAULT_SETTINGS.sideBars.barDensity)
};
}
function sanitizeFlatRipples(input = {}) {
return {
mode: pick(input.mode, VALID_FLAT_RIPPLES_MODES, DEFAULT_SETTINGS.flatRipples.mode),
intensity: pick(input.intensity, VALID_LEVELS, DEFAULT_SETTINGS.flatRipples.intensity),
colorStyle: pick(input.colorStyle, VALID_FLAT_RIPPLES_COLORS, DEFAULT_SETTINGS.flatRipples.colorStyle),
speed: pick(input.speed, VALID_FLAT_RIPPLES_SPEEDS, DEFAULT_SETTINGS.flatRipples.speed)
};
}
function sanitizeDotParticles(input = {}) {
return {
density: pick(input.density, VALID_LEVELS, DEFAULT_SETTINGS.dotParticles.density),
motionStyle: pick(input.motionStyle, VALID_DOT_PARTICLES_MOTION_STYLES, DEFAULT_SETTINGS.dotParticles.motionStyle),
directionBehavior: pick(input.directionBehavior, VALID_DOT_PARTICLES_DIRECTIONS, DEFAULT_SETTINGS.dotParticles.directionBehavior),
glowStrength: pick(input.glowStrength, VALID_GLOW_STRENGTHS, DEFAULT_SETTINGS.dotParticles.glowStrength)
};
}
function sanitizeRippleFlow(input = {}) {
return {
mode: pick(input.mode, VALID_RIPPLE_FLOW_MODES, DEFAULT_SETTINGS.rippleFlow.mode),
intensity: pick(input.intensity, VALID_LEVELS, DEFAULT_SETTINGS.rippleFlow.intensity),
sensitivity: pick(input.sensitivity, VALID_LEVELS, DEFAULT_SETTINGS.rippleFlow.sensitivity),
colorStyle: pick(input.colorStyle, VALID_RIPPLE_FLOW_COLORS, DEFAULT_SETTINGS.rippleFlow.colorStyle)
};
}
function sanitizeSnowBubbleParticles(input = {}) {
return {
fallArea: pick(input.fallArea, VALID_SNOW_FALL_AREAS, DEFAULT_SETTINGS.snowBubbleParticles.fallArea),
density: pick(input.density, VALID_LEVELS, DEFAULT_SETTINGS.snowBubbleParticles.density),
motionStyle: pick(input.motionStyle, VALID_DOT_PARTICLES_MOTION_STYLES, DEFAULT_SETTINGS.snowBubbleParticles.motionStyle),
glowStrength: pick(input.glowStrength, VALID_GLOW_STRENGTHS, DEFAULT_SETTINGS.snowBubbleParticles.glowStrength),
particleSize: pick(input.particleSize, VALID_PARTICLE_SIZES, DEFAULT_SETTINGS.snowBubbleParticles.particleSize)
};
}
function sanitizeEdgeCrystals(input = {}) {
return {
flutterStyle: pick(input.flutterStyle, VALID_EDGE_FLUTTER_STYLES, DEFAULT_SETTINGS.edgeCrystals.flutterStyle),
density: pick(input.density, VALID_LEVELS, DEFAULT_SETTINGS.edgeCrystals.density),
glowStrength: pick(input.glowStrength, VALID_GLOW_STRENGTHS, DEFAULT_SETTINGS.edgeCrystals.glowStrength),
colorStyle: pick(input.colorStyle, VALID_EDGE_FLUTTER_COLORS, DEFAULT_SETTINGS.edgeCrystals.colorStyle),
edgeMode: pick(input.edgeMode, VALID_EDGE_FLUTTER_MODES, DEFAULT_SETTINGS.edgeCrystals.edgeMode)
};
}
function migrateLegacySettings(input = {}) {
if (VALID_MAIN_THEMES.has(input.selectedTheme) && !input.edgeFlutter) {
return input;
}
const normalizedInput = { ...input };
if (normalizedInput.selectedTheme === "edgeFlutter") {
normalizedInput.selectedTheme = "edgeCrystals";
}
if (normalizedInput.edgeFlutter && !normalizedInput.edgeCrystals) {
normalizedInput.edgeCrystals = normalizedInput.edgeFlutter;
}
if (VALID_MAIN_THEMES.has(normalizedInput.selectedTheme)) {
return normalizedInput;
}
const migrated = createDefaultSettings();
const legacyTheme = normalizedInput.theme;
const legacyLevel = legacySensitivityToLevel(normalizedInput.sensitivity);
migrated.ambientWave.sensitivity = legacyLevel;
if (VALID_EDGE_MODES.has(normalizedInput.edgeMode)) {
migrated.ambientWave.edgeMode = normalizedInput.edgeMode;
}
if (legacyTheme === "purple" || legacyTheme === "warm") {
migrated.ambientWave.tone = legacyTheme;
}
if (legacyTheme === "rainbow") {
migrated.selectedTheme = "reactiveBorder";
migrated.reactiveBorder.intensity = legacyLevel;
} else if (legacyTheme === "flow") {
migrated.selectedTheme = "flowBorder";
} else {
migrated.selectedTheme = "ambientWave";
if (legacyTheme === "blue" || legacyTheme === "purple" || legacyTheme === "warm") {
migrated.ambientWave.tone = legacyTheme;
}
}
return migrated;
}
function sanitizeSettings(input = {}) {
const source = migrateLegacySettings(input);
return {
selectedTheme: pick(source.selectedTheme, VALID_MAIN_THEMES, DEFAULT_SETTINGS.selectedTheme),
ambientWave: sanitizeAmbientWave(source.ambientWave),
reactiveBorder: sanitizeReactiveBorder(source.reactiveBorder),
flowBorder: sanitizeFlowBorder(source.flowBorder),
sideBars: sanitizeSideBars(source.sideBars),
flatRipples: sanitizeFlatRipples(source.flatRipples),
dotParticles: sanitizeDotParticles(source.dotParticles),
rippleFlow: sanitizeRippleFlow(source.rippleFlow),
snowBubbleParticles: sanitizeSnowBubbleParticles(source.snowBubbleParticles),
edgeCrystals: sanitizeEdgeCrystals(source.edgeCrystals)
};
}
function createSettingsStore(userDataPath) {
const settingsPath = path.join(userDataPath, "settings.json");
function load() {
try {
if (!fs.existsSync(settingsPath)) {
return createDefaultSettings();
}
const fileContent = fs.readFileSync(settingsPath, "utf8");
const parsed = JSON.parse(fileContent);
return sanitizeSettings(parsed);
} catch (_error) {
return createDefaultSettings();
}
}
function save(settings) {
const cleanSettings = sanitizeSettings(settings);
fs.mkdirSync(path.dirname(settingsPath), { recursive: true });
fs.writeFileSync(settingsPath, JSON.stringify(cleanSettings, null, 2));
return cleanSettings;
}
return {
load,
save,
path: settingsPath
};
}
module.exports = {
DEFAULT_SETTINGS,
createSettingsStore
};