Skip to content

Commit 76646f3

Browse files
authored
Implement theme settings and iframe styling
Added theme setting functionality and updated iframe styles.
1 parent 1a23f49 commit 76646f3

File tree

1 file changed

+55
-2
lines changed

1 file changed

+55
-2
lines changed

js/index.js

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const DEFAULTSETTINGS = {
1414
alwaysAllowUpdate : false, // Always show "reinstall app" button regardless of the version
1515
autoReload: false, // Automatically reload watch after app App Loader actions (removes "Hold button" prompt)
1616
noPackets: false, // Enable File Upload Compatibility mode (disables binary packet upload)
17+
theme: "device" // App Loader theme: light, dark, device
1718
};
1819
let SETTINGS = JSON.parse(JSON.stringify(DEFAULTSETTINGS)); // clone
1920

@@ -24,6 +25,10 @@ let device = {
2425
connected : false, // are we connected via BLE right now?
2526
appsInstalled : [] // list of app {id,version} of installed apps
2627
};
28+
29+
30+
31+
2732
// FOR TESTING ONLY
2833
/*let LANGUAGE = {
2934
"//":"German language translations",
@@ -312,6 +317,15 @@ function iframeSetup(options) {
312317
// when iframe is loaded, call 'onInit' with info about the device
313318
iframe.addEventListener("load", function() {
314319
console.log("IFRAME loaded");
320+
// Style custom apps for dark mode
321+
const iframeDoc = iframe.contentDocument || iframe.contentWindow.document;
322+
if (iframeDoc && iframeDoc.body) {
323+
const bgColor = window.matchMedia('(prefers-color-scheme: dark)').matches ? '#2c2c2c' : '#ffffff';
324+
const textColor = window.matchMedia('(prefers-color-scheme: dark)').matches ? '#FFFFFF' : '#000000';
325+
iframeDoc.body.style.backgroundColor = bgColor;
326+
iframeDoc.body.style.color = textColor;
327+
}
328+
315329
/* if we get a message from the iframe (eg asking to send data to Puck), handle it
316330
otherwise pass to messageHandler because handleCustomApp may want to handle it */
317331
iframe.contentWindow.addEventListener("message",function(event) {
@@ -431,7 +445,7 @@ function handleCustomApp(appTemplate) {
431445
</div>
432446
<div class="modal-body" style="height:100%">
433447
<div class="content" style="height:100%">
434-
<iframe src="apps/${appTemplate.id}/${appTemplate.custom}" style="width:100%;height:100%;border:0px;">
448+
<iframe class="modal-iframe" src="apps/${appTemplate.id}/${appTemplate.custom}" style="width:100%;height:100%;border:0px;">
435449
</div>
436450
</div>
437451
</div>
@@ -1399,7 +1413,8 @@ function loadSettings() {
13991413
console.error("Invalid settings");
14001414
}
14011415
// upgrade old settings
1402-
if(!SETTINGS.appsfavouritedThisSession) SETTINGS.appsfavouritedThisSession = [];
1416+
if(!SETTINGS.appsfavouritedThisSession) SETTINGS.appsfavouritedThisSession = DEFAULTSETTINGS.appsfavouritedThisSession;
1417+
if(!SETTINGS.theme) SETTINGS.theme = SETTINGS.theme=DEFAULTSETTINGS.theme;
14031418
}
14041419
/// Save settings
14051420
function saveSettings() {
@@ -1420,6 +1435,25 @@ function settingsCheckbox(id, name) {
14201435
saveSettings();
14211436
});
14221437
}
1438+
function applyTheme(theme){
1439+
1440+
if(theme=="dark") document.documentElement.style.colorScheme = 'dark';
1441+
else if(theme=="light") document.documentElement.style.colorScheme = 'light';
1442+
else{
1443+
1444+
const prefersDark = window.matchMedia('(prefers-color-scheme: dark)').matches;
1445+
console.log(prefersDark)
1446+
const theme = prefersDark ? 'dark' : 'light';
1447+
document.documentElement.style.colorScheme = theme;
1448+
}
1449+
}
1450+
function repaintSite(){
1451+
//tricks browser into repainting the site to apply theme changes
1452+
const body = document.body;
1453+
body.style.display = 'none';
1454+
body.offsetHeight;
1455+
body.style.display = '';
1456+
}
14231457
settingsCheckbox("settings-pretokenise", "pretokenise");
14241458
settingsCheckbox("settings-minify", "minify");
14251459
settingsCheckbox("settings-settime", "settime");
@@ -1428,6 +1462,25 @@ settingsCheckbox("settings-autoReload", "autoReload");
14281462
settingsCheckbox("settings-nopacket", "noPackets");
14291463
loadSettings();
14301464

1465+
selectTheme = document.getElementById("settings-theme");
1466+
// Update theme selector
1467+
selectTheme.value = SETTINGS.theme;
1468+
selectTheme.addEventListener("change",event=>{
1469+
SETTINGS.theme = event.target.value;
1470+
saveSettings();
1471+
applyTheme(event.target.value);
1472+
});
1473+
1474+
//apply theme on startup
1475+
applyTheme(SETTINGS.theme);
1476+
//in case system theme changes, add a listener to update site theme if in device mode
1477+
window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', (e) => {
1478+
if(SETTINGS.theme=="device"){
1479+
const newTheme = e.matches ? 'dark' : 'light';
1480+
document.documentElement.style.colorScheme = newTheme;
1481+
}
1482+
});
1483+
14311484
let btn;
14321485

14331486
btn = document.getElementById("defaultsettings");

0 commit comments

Comments
 (0)