| 
9 | 9 |     const isSettingsPage = window.location.pathname.endsWith("/settings.html");  | 
10 | 10 | 
 
  | 
11 | 11 |     function changeSetting(settingName, value) {  | 
 | 12 | +        if (settingName === "theme") {  | 
 | 13 | +            const useSystem = value === "system preference" ? "true" : "false";  | 
 | 14 | +            updateLocalStorage("use-system-theme", useSystem);  | 
 | 15 | +        }  | 
12 | 16 |         updateLocalStorage(settingName, value);  | 
13 | 17 | 
 
  | 
14 | 18 |         switch (settingName) {  | 
15 | 19 |             case "theme":  | 
16 | 20 |             case "preferred-dark-theme":  | 
17 | 21 |             case "preferred-light-theme":  | 
18 |  | -            case "use-system-theme":  | 
19 | 22 |                 updateSystemTheme();  | 
20 | 23 |                 updateLightAndDark();  | 
21 | 24 |                 break;  | 
 | 
45 | 48 |     }  | 
46 | 49 | 
 
  | 
47 | 50 |     function showLightAndDark() {  | 
48 |  | -        addClass(document.getElementById("theme").parentElement, "hidden");  | 
49 | 51 |         removeClass(document.getElementById("preferred-light-theme").parentElement, "hidden");  | 
50 | 52 |         removeClass(document.getElementById("preferred-dark-theme").parentElement, "hidden");  | 
51 | 53 |     }  | 
52 | 54 | 
 
  | 
53 | 55 |     function hideLightAndDark() {  | 
54 | 56 |         addClass(document.getElementById("preferred-light-theme").parentElement, "hidden");  | 
55 | 57 |         addClass(document.getElementById("preferred-dark-theme").parentElement, "hidden");  | 
56 |  | -        removeClass(document.getElementById("theme").parentElement, "hidden");  | 
57 | 58 |     }  | 
58 | 59 | 
 
  | 
59 | 60 |     function updateLightAndDark() {  | 
60 |  | -        if (getSettingValue("use-system-theme") !== "false") {  | 
 | 61 | +        const useSystem = getSettingValue("use-system-theme");  | 
 | 62 | +        if (useSystem === "true" || (useSystem === null && getSettingValue("theme") === null)) {  | 
61 | 63 |             showLightAndDark();  | 
62 | 64 |         } else {  | 
63 | 65 |             hideLightAndDark();  | 
 | 
91 | 93 |         });  | 
92 | 94 |         onEachLazy(settingsElement.querySelectorAll("input[type=\"radio\"]"), elem => {  | 
93 | 95 |             const settingId = elem.name;  | 
94 |  | -            const settingValue = getSettingValue(settingId);  | 
 | 96 | +            let settingValue = getSettingValue(settingId);  | 
 | 97 | +            if (settingId === "theme") {  | 
 | 98 | +                const useSystem = getSettingValue("use-system-theme");  | 
 | 99 | +                if (useSystem === "true" || settingValue === null) {  | 
 | 100 | +                    if (useSystem !== "false") {  | 
 | 101 | +                        settingValue = "system preference";  | 
 | 102 | +                    } else {  | 
 | 103 | +                        // This is the default theme.  | 
 | 104 | +                        settingValue = "light";  | 
 | 105 | +                    }  | 
 | 106 | +                }  | 
 | 107 | +            }  | 
95 | 108 |             if (settingValue !== null && settingValue !== "null") {  | 
96 | 109 |                 elem.checked = settingValue === elem.value;  | 
97 | 110 |             }  | 
 | 
120 | 133 | 
 
  | 
121 | 134 |             if (setting["options"] !== undefined) {  | 
122 | 135 |                 // This is a select setting.  | 
123 |  | -                output += `<div class="radio-line" id="${js_data_name}">\  | 
124 |  | -                        <span class="setting-name">${setting_name}</span>\  | 
125 |  | -                        <div class="choices">`;  | 
 | 136 | +                output += `\  | 
 | 137 | +<div class="radio-line" id="${js_data_name}">  | 
 | 138 | +    <span class="setting-name">${setting_name}</span>  | 
 | 139 | +<div class="choices">`;  | 
126 | 140 |                 onEach(setting["options"], option => {  | 
127 | 141 |                     const checked = option === setting["default"] ? " checked" : "";  | 
 | 142 | +                    const full = `${js_data_name}-${option.replace(/ /g,"-")}`;  | 
128 | 143 | 
 
  | 
129 |  | -                    output += `<label for="${js_data_name}-${option}" class="choice">\  | 
130 |  | -                           <input type="radio" name="${js_data_name}" \  | 
131 |  | -                                id="${js_data_name}-${option}" value="${option}"${checked}>\  | 
132 |  | -                           <span>${option}</span>\  | 
133 |  | -                         </label>`;  | 
 | 144 | +                    output += `\  | 
 | 145 | +<label for="${full}" class="choice">  | 
 | 146 | +    <input type="radio" name="${js_data_name}"  | 
 | 147 | +        id="${full}" value="${option}"${checked}>  | 
 | 148 | +    <span>${option}</span>  | 
 | 149 | +</label>`;  | 
134 | 150 |                 });  | 
135 | 151 |                 output += "</div></div>";  | 
136 | 152 |             } else {  | 
137 | 153 |                 // This is a toggle.  | 
138 | 154 |                 const checked = setting["default"] === true ? " checked" : "";  | 
139 |  | -                output += `<label class="toggle">\  | 
140 |  | -                        <input type="checkbox" id="${js_data_name}"${checked}>\  | 
141 |  | -                        <span class="label">${setting_name}</span>\  | 
142 |  | -                    </label>`;  | 
 | 155 | +                output += `\  | 
 | 156 | +<label class="toggle">\  | 
 | 157 | +    <input type="checkbox" id="${js_data_name}"${checked}>\  | 
 | 158 | +    <span class="label">${setting_name}</span>\  | 
 | 159 | +</label>`;  | 
143 | 160 |             }  | 
144 | 161 |             output += "</div>";  | 
145 | 162 |         }  | 
 | 
156 | 173 |         theme_names.push("light", "dark", "ayu");  | 
157 | 174 | 
 
  | 
158 | 175 |         const settings = [  | 
159 |  | -            {  | 
160 |  | -                "name": "Use system theme",  | 
161 |  | -                "js_name": "use-system-theme",  | 
162 |  | -                "default": true,  | 
163 |  | -            },  | 
164 | 176 |             {  | 
165 | 177 |                 "name": "Theme",  | 
166 | 178 |                 "js_name": "theme",  | 
167 |  | -                "default": "light",  | 
168 |  | -                "options": theme_names,  | 
 | 179 | +                "default": "system preference",  | 
 | 180 | +                "options": theme_names.concat("system preference"),  | 
169 | 181 |             },  | 
170 | 182 |             {  | 
171 | 183 |                 "name": "Preferred light theme",  | 
 | 
0 commit comments