@@ -42,6 +42,11 @@ function getSettingValue(settingName) {
4242 return null ;
4343}
4444
45+ function isUsingSystemTheme ( ) {
46+ const current = getSettingValue ( "theme" ) ;
47+ return current === null || current === "system-preference" ;
48+ }
49+
4550const localStoredTheme = getSettingValue ( "theme" ) ;
4651
4752const savedHref = [ ] ;
@@ -157,22 +162,6 @@ function switchTheme(styleElem, mainStyleElem, newTheme, saveTheme) {
157162 }
158163}
159164
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-
176165const updateSystemTheme = ( function ( ) {
177166 if ( ! window . matchMedia ) {
178167 // fallback to the CSS computed value
@@ -193,25 +182,25 @@ const updateSystemTheme = (function() {
193182 const mql = window . matchMedia ( "(prefers-color-scheme: dark)" ) ;
194183
195184 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 ) ;
198187 } ;
199188 // maybe the user has disabled the setting in the meantime!
200- if ( getSettingValue ( "use-system-theme" ) !== "false" ) {
189+ if ( isUsingSystemTheme ( ) ) {
201190 const lightTheme = getSettingValue ( "preferred-light-theme" ) || "light" ;
202191 const darkTheme = getSettingValue ( "preferred-dark-theme" ) || "dark" ;
203192
204193 if ( mql . matches ) {
205- use ( darkTheme ) ;
194+ use ( darkTheme , false ) ;
206195 } else {
207196 // prefers a light theme, or has no preference
208- use ( lightTheme ) ;
197+ use ( lightTheme , false ) ;
209198 }
210199 // note: we save the theme so that it doesn't suddenly change when
211200 // the user disables "use-system-theme" and reloads the page or
212201 // navigates to another page
213202 } else {
214- use ( getSettingValue ( "theme" ) ) ;
203+ use ( getSettingValue ( "theme" ) , true ) ;
215204 }
216205 }
217206
@@ -231,10 +220,10 @@ function switchToSavedTheme() {
231220 ) ;
232221}
233222
234- if ( getSettingValue ( "use-system-theme" ) !== "false" && window . matchMedia ) {
223+ if ( isUsingSystemTheme ( ) && window . matchMedia ) {
235224 // update the preferred dark theme if the user is already using a dark theme
236225 // See https://github.com/rust-lang/rust/pull/77809#issuecomment-707875732
237- if ( getSettingValue ( "use-system- theme" ) === null
226+ if ( getSettingValue ( "theme" ) === null
238227 && getSettingValue ( "preferred-dark-theme" ) === null
239228 && darkThemes . indexOf ( localStoredTheme ) >= 0 ) {
240229 updateLocalStorage ( "preferred-dark-theme" , localStoredTheme ) ;
0 commit comments