diff --git a/libretranslate/static/css/main.css b/libretranslate/static/css/main.css index 75f01dc..1e11ad6 100644 --- a/libretranslate/static/css/main.css +++ b/libretranslate/static/css/main.css @@ -162,7 +162,7 @@ h3.header { left: 2px; } -.locale-panel a:hover{ +.locale-panel a:hovselecter{ background-color: transparent !important; } @@ -186,10 +186,7 @@ h3.header { select { color: var(--fg-color); -} - -select option { - color: var(--pri-bg-color); + background-color: var(--pri-bg-color); } .language-select { @@ -206,7 +203,6 @@ select option { text-decoration: underline; -webkit-appearance: none; appearance: none; - background-color: transparent; padding: 0; text-indent: 0.01px; text-overflow: ""; diff --git a/libretranslate/templates/app.js.template b/libretranslate/templates/app.js.template index adf234f..c01523b 100644 --- a/libretranslate/templates/app.js.template +++ b/libretranslate/templates/app.js.template @@ -525,4 +525,83 @@ function setApiKey(){ localStorage.setItem("api_key", newKey); } +// Color scheme handling +function getPreferredColorScheme(){ + var systemScheme = 'light'; + if(window.matchMedia('(prefers-color-scheme: dark)').matches){ + systemScheme = 'dark'; + } + var chosenScheme = systemScheme; + + if(localStorage.getItem("scheme")){ + chosenScheme = localStorage.getItem("scheme"); + } + + if(systemScheme === chosenScheme){ + localStorage.removeItem("scheme"); + } + + return chosenScheme; +} + +// Write chosen color scheme to local storage +// Unless the system scheme matches the the stored scheme, in which case... remove from local storage +function savePreferredColorScheme(scheme){ + var systemScheme = 'light'; + + if(window.matchMedia('(prefers-color-scheme: dark)').matches){ + systemScheme = 'dark'; + } + + if(systemScheme === scheme){ + localStorage.removeItem("scheme"); + } else { + localStorage.setItem("scheme", scheme); + } + +} + +// Get the current scheme, and apply the opposite +function toggleColorScheme(){ + let newScheme = "light"; + let scheme = getPreferredColorScheme(); + if (scheme === "light"){ + newScheme = "dark"; + } + + applyPreferredColorScheme(newScheme); +} + +// Apply the chosen color scheme by traversing stylesheet rules, and applying a medium. +function applyPreferredColorScheme(scheme) { + for (var s = 0; s < document.styleSheets.length; s++) { + for (var i = 0; i < document.styleSheets[s].cssRules.length; i++) { + rule = document.styleSheets[s].cssRules[i]; + if (rule && rule.media && rule.media.mediaText.includes("prefers-color-scheme")) { + switch (scheme) { + case "light": + rule.media.appendMedium("original-prefers-color-scheme"); + if (rule.media.mediaText.includes("light")) rule.media.deleteMedium("(prefers-color-scheme: light)"); + if (rule.media.mediaText.includes("dark")) rule.media.deleteMedium("(prefers-color-scheme: dark)"); + break; + case "dark": + rule.media.appendMedium("(prefers-color-scheme: light)"); + rule.media.appendMedium("(prefers-color-scheme: dark)"); + if (rule.media.mediaText.includes("original")) rule.media.deleteMedium("original-prefers-color-scheme"); + break; + default: + rule.media.appendMedium("(prefers-color-scheme: dark)"); + if (rule.media.mediaText.includes("light")) rule.media.deleteMedium("(prefers-color-scheme: light)"); + if (rule.media.mediaText.includes("original")) rule.media.deleteMedium("original-prefers-color-scheme"); + break; + } + } + } + } + + savePreferredColorScheme(scheme); +} + +applyPreferredColorScheme(getPreferredColorScheme()); + // @license-end diff --git a/libretranslate/templates/index.html b/libretranslate/templates/index.html index 9b66f9d..33726b5 100644 --- a/libretranslate/templates/index.html +++ b/libretranslate/templates/index.html @@ -78,6 +78,8 @@ {{ _h("Edit") }}create +
  • lightbulb_outline +
  • {% endset %} {{ menulinks }}