feat: add System (auto) theme following OS prefers-color-scheme (#504)
Synthesized from PRs #506, #509, #514 (all by armorbreak001 and cloudyun888). Implementation: - static/index.html: flicker-prevention head script resolves 'system' to 'dark'/'light' via matchMedia before first paint. Adds 'System (auto)' as first option in theme picker. onchange calls _applyTheme(). - static/boot.js: new _applyTheme(name) helper — resolves 'system' via matchMedia, sets data-theme, registers a MQ change listener so the UI tracks OS switches live. loadSettings() now calls _applyTheme() instead of direct data-theme assignment. - static/commands.js: adds 'system' to valid /theme command names, delegates apply to _applyTheme(). - static/panels.js: _settingsThemeOnOpen reads from localStorage (preserves 'system' string, not the resolved 'dark'/'light'). _revertSettingsPreview calls _applyTheme() so reverting to 'system' correctly re-enables OS tracking. - static/i18n.js: cmd_theme description now lists 'system' first in all 5 locales (en, es, de, zh-Hans, zh-Hant). Design choices vs submitted PRs: - No separate system-theme.js file (unnecessary indirection). - matchMedia listener does NOT POST to /api/settings (OS can change rapidly; persisting on every OS switch would hammer the server). Co-authored-by: armorbreak001 <armorbreak001@users.noreply.github.com> Co-authored-by: cloudyun888 <cloudyun888@users.noreply.github.com>
This commit is contained in:
@@ -121,14 +121,14 @@ async function cmdUsage(){
|
||||
}
|
||||
|
||||
async function cmdTheme(args){
|
||||
const themes=['dark','light','slate','solarized','monokai','nord','oled'];
|
||||
const themes=['system','dark','light','slate','solarized','monokai','nord','oled'];
|
||||
if(!args||!themes.includes(args.toLowerCase())){
|
||||
showToast(t('theme_usage')+themes.join('|'));
|
||||
return;
|
||||
}
|
||||
const themeName=args.toLowerCase();
|
||||
document.documentElement.dataset.theme=themeName;
|
||||
localStorage.setItem('hermes-theme',themeName);
|
||||
_applyTheme(themeName);
|
||||
try{await api('/api/settings',{method:'POST',body:JSON.stringify({theme:themeName})});}catch(e){}
|
||||
// Update settings dropdown if panel is open
|
||||
const sel=$('settingsTheme');
|
||||
|
||||
Reference in New Issue
Block a user