Skip to content

Commit 26f049d

Browse files
committed
fix(simulator): implement theme toggling, Monaco theme sync, and localStorage persistence
1 parent 43aa1ab commit 26f049d

1 file changed

Lines changed: 34 additions & 5 deletions

File tree

simulator/simulator.js

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -482,13 +482,43 @@ if "display_driver" in sys.modules:
482482
// Theme Switching
483483
// =========================================================================
484484

485-
function updateEditorTheme() {
486-
const isLight = document.documentElement.getAttribute("data-theme") === "light";
485+
const THEME_STORAGE_KEY = "pydevices-theme";
486+
487+
function currentTheme() {
488+
return document.documentElement.getAttribute("data-theme") === "light" ? "light" : "dark";
489+
}
490+
491+
function updateThemeUI(theme) {
492+
const isLight = theme === "light";
493+
const themeBtn = document.getElementById("theme-toggle");
494+
if (themeBtn) {
495+
themeBtn.innerHTML = isLight
496+
? '<svg viewBox="0 0 24 24" width="16" height="16" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M21 12.79A9 9 0 1 1 11.21 3 7 7 0 0 0 21 12.79z"/></svg>'
497+
: '<svg viewBox="0 0 24 24" width="16" height="16" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="4"/><path d="M12 2v2M12 20v2M4.9 4.9l1.4 1.4M17.7 17.7l1.4 1.4M2 12h2M20 12h2M4.9 19.1l1.4-1.4M17.7 6.3l1.4-1.4"/></svg>';
498+
themeBtn.title = isLight ? "Switch to Dark Theme" : "Switch to Light Theme";
499+
}
487500
if (monacoEditor && typeof monaco !== "undefined") {
488501
monaco.editor.setTheme(isLight ? "vs" : "vs-dark");
489502
}
490503
}
491504

505+
function applyTheme(theme) {
506+
if (theme === "light") {
507+
document.documentElement.setAttribute("data-theme", "light");
508+
} else {
509+
document.documentElement.removeAttribute("data-theme");
510+
}
511+
try {
512+
localStorage.setItem(THEME_STORAGE_KEY, theme);
513+
} catch (e) {}
514+
updateThemeUI(theme);
515+
}
516+
517+
function toggleTheme() {
518+
const next = currentTheme() === "light" ? "dark" : "light";
519+
applyTheme(next);
520+
}
521+
492522
// =========================================================================
493523
// DOM Event Bindings
494524
// =========================================================================
@@ -511,10 +541,9 @@ if "display_driver" in sys.modules:
511541

512542
const themeToggle = document.getElementById("theme-toggle");
513543
if (themeToggle) {
514-
themeToggle.addEventListener("click", () => {
515-
setTimeout(updateEditorTheme, 50);
516-
});
544+
themeToggle.addEventListener("click", toggleTheme);
517545
}
546+
updateThemeUI(currentTheme());
518547

519548
window.addEventListener("resize", () => {
520549
if (monacoEditor) monacoEditor.layout();

0 commit comments

Comments
 (0)