@@ -42,6 +42,11 @@ function getSettingValue(settingName) {
42
42
return null ;
43
43
}
44
44
45
+ function isUsingSystemTheme ( ) {
46
+ const current = getSettingValue ( "theme" ) ;
47
+ return current === null || current === "system-preference" ;
48
+ }
49
+
45
50
const localStoredTheme = getSettingValue ( "theme" ) ;
46
51
47
52
const savedHref = [ ] ;
@@ -157,22 +162,6 @@ function switchTheme(styleElem, mainStyleElem, newTheme, saveTheme) {
157
162
}
158
163
}
159
164
160
- // This function is called from "main.js".
161
- // eslint-disable-next-line no-unused-vars
162
- function useSystemTheme ( value ) {
163
- if ( value === undefined ) {
164
- value = true ;
165
- }
166
-
167
- updateLocalStorage ( "use-system-theme" , value ) ;
168
-
169
- // update the toggle if we're on the settings page
170
- const toggle = document . getElementById ( "use-system-theme" ) ;
171
- if ( toggle && toggle instanceof HTMLInputElement ) {
172
- toggle . checked = value ;
173
- }
174
- }
175
-
176
165
const updateSystemTheme = ( function ( ) {
177
166
if ( ! window . matchMedia ) {
178
167
// fallback to the CSS computed value
@@ -193,25 +182,25 @@ const updateSystemTheme = (function() {
193
182
const mql = window . matchMedia ( "(prefers-color-scheme: dark)" ) ;
194
183
195
184
function handlePreferenceChange ( mql ) {
196
- const use = theme => {
197
- switchTheme ( window . currentTheme , window . mainTheme , theme , true ) ;
185
+ const use = ( theme , saveIt ) => {
186
+ switchTheme ( window . currentTheme , window . mainTheme , theme , saveIt ) ;
198
187
} ;
199
188
// maybe the user has disabled the setting in the meantime!
200
- if ( getSettingValue ( "use-system-theme" ) !== "false" ) {
189
+ if ( isUsingSystemTheme ( ) ) {
201
190
const lightTheme = getSettingValue ( "preferred-light-theme" ) || "light" ;
202
191
const darkTheme = getSettingValue ( "preferred-dark-theme" ) || "dark" ;
203
192
204
193
if ( mql . matches ) {
205
- use ( darkTheme ) ;
194
+ use ( darkTheme , false ) ;
206
195
} else {
207
196
// prefers a light theme, or has no preference
208
- use ( lightTheme ) ;
197
+ use ( lightTheme , false ) ;
209
198
}
210
199
// note: we save the theme so that it doesn't suddenly change when
211
200
// the user disables "use-system-theme" and reloads the page or
212
201
// navigates to another page
213
202
} else {
214
- use ( getSettingValue ( "theme" ) ) ;
203
+ use ( getSettingValue ( "theme" ) , true ) ;
215
204
}
216
205
}
217
206
@@ -231,10 +220,10 @@ function switchToSavedTheme() {
231
220
) ;
232
221
}
233
222
234
- if ( getSettingValue ( "use-system-theme" ) !== "false" && window . matchMedia ) {
223
+ if ( isUsingSystemTheme ( ) && window . matchMedia ) {
235
224
// update the preferred dark theme if the user is already using a dark theme
236
225
// See https://github.com/rust-lang/rust/pull/77809#issuecomment-707875732
237
- if ( getSettingValue ( "use-system- theme" ) === null
226
+ if ( getSettingValue ( "theme" ) === null
238
227
&& getSettingValue ( "preferred-dark-theme" ) === null
239
228
&& darkThemes . indexOf ( localStoredTheme ) >= 0 ) {
240
229
updateLocalStorage ( "preferred-dark-theme" , localStoredTheme ) ;
0 commit comments