-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsignal-lm-theme.js
More file actions
114 lines (102 loc) · 4.17 KB
/
Copy pathsignal-lm-theme.js
File metadata and controls
114 lines (102 loc) · 4.17 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
(function () {
const SETTINGS_KEY = 'lmStudioLite.settings.v1';
const media = window.matchMedia ? window.matchMedia('(prefers-color-scheme: dark)') : null;
const THEME_OPTIONS = [
['system', 'System'],
['light', 'Light'],
['dark', 'Midnight'],
['classic-dark', 'Classic Dark'],
['matrix', 'Matrix']
];
function readSettings() {
try { return JSON.parse(localStorage.getItem(SETTINGS_KEY) || '{}') || {}; }
catch { return {}; }
}
function writeSettings(next) {
localStorage.setItem(SETTINGS_KEY, JSON.stringify(next || {}));
}
function normalizePreference(preference) {
const pref = preference || readSettings().theme || 'system';
return pref === 'midnight' ? 'dark' : pref;
}
function resolveTheme(preference) {
const pref = normalizePreference(preference);
if (pref === 'dark' || pref === 'classic-dark' || pref === 'matrix') return 'dark';
if (pref === 'light') return 'light';
return media && media.matches ? 'dark' : 'light';
}
function resolveVariant(preference, resolved) {
const pref = normalizePreference(preference);
if (resolved !== 'dark') return '';
if (pref === 'classic-dark') return 'classic-dark';
if (pref === 'matrix') return 'matrix';
return 'midnight';
}
function getThemeLabel(resolved, variant) {
if (resolved !== 'dark') return 'Light';
if (variant === 'classic-dark') return 'Classic Dark';
if (variant === 'matrix') return 'Matrix';
return 'Midnight';
}
function getThemeColor(resolved, variant) {
if (resolved !== 'dark') return '#f5f3ef';
if (variant === 'classic-dark') return '#0f0f11';
if (variant === 'matrix') return '#001106';
return '#090a0d';
}
function ensureThemeOptions() {
document.querySelectorAll('[data-theme-select]').forEach(select => {
const current = select.value || normalizePreference();
select.innerHTML = '';
THEME_OPTIONS.forEach(([value, label]) => {
const option = document.createElement('option');
option.value = value;
option.textContent = label;
select.appendChild(option);
});
select.value = current === 'midnight' ? 'dark' : current;
});
}
function applyTheme(preference) {
const settings = readSettings();
const pref = normalizePreference(preference || settings.theme);
const resolved = resolveTheme(pref);
const variant = resolveVariant(pref, resolved);
document.documentElement.dataset.themePreference = pref;
document.documentElement.dataset.theme = resolved;
if (variant) document.documentElement.dataset.themeVariant = variant;
else delete document.documentElement.dataset.themeVariant;
const themeColor = getThemeColor(resolved, variant);
document.documentElement.style.colorScheme = resolved === 'dark' ? 'dark' : 'light';
document.documentElement.style.backgroundColor = resolved === 'dark' ? themeColor : '';
ensureThemeOptions();
document.querySelectorAll('[data-theme-select]').forEach(select => { select.value = pref; });
document.querySelectorAll('[data-theme-label]').forEach(label => {
label.textContent = getThemeLabel(resolved, variant);
});
let meta = document.querySelector('meta[name="theme-color"]');
if (!meta) {
meta = document.createElement('meta');
meta.name = 'theme-color';
document.head.appendChild(meta);
}
meta.content = themeColor;
return resolved;
}
function setTheme(preference) {
const settings = readSettings();
settings.theme = normalizePreference(preference || 'system');
writeSettings(settings);
applyTheme(settings.theme);
}
function toggleTheme() {
const current = applyTheme();
setTheme(current === 'dark' ? 'light' : 'dark');
}
window.LmStudioLiteTheme = { readSettings, writeSettings, resolveTheme, resolveVariant, applyTheme, setTheme, toggleTheme };
applyTheme();
if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', () => applyTheme());
else applyTheme();
if (media && media.addEventListener) media.addEventListener('change', () => applyTheme());
window.addEventListener('storage', event => { if (event.key === SETTINGS_KEY) applyTheme(); });
})();