|
880 | 880 | applyState(state, { changedKeys: [], source: 'init' }); |
881 | 881 | cleanupFns.push(subscribeSettings(applyState, { immediate: false })); |
882 | 882 |
|
| 883 | + var observedThemeDark = isDarkThemeActive(); |
| 884 | + var applyThemeStateIfChanged = function(source) { |
| 885 | + var nextThemeDark = isDarkThemeActive(); |
| 886 | + if (nextThemeDark === observedThemeDark) return; |
| 887 | + observedThemeDark = nextThemeDark; |
| 888 | + applyState(getSettings(), { changedKeys: ['theme'], source: source }); |
| 889 | + }; |
883 | 890 | var onThemeChanged = function() { |
884 | | - applyState(getSettings(), { changedKeys: ['theme'], source: 'subtitle-ui-theme' }); |
| 891 | + applyThemeStateIfChanged('subtitle-ui-theme-event'); |
885 | 892 | }; |
886 | 893 | window.addEventListener('neko-theme-changed', onThemeChanged); |
887 | 894 | cleanupFns.push(function() { |
888 | 895 | window.removeEventListener('neko-theme-changed', onThemeChanged); |
889 | 896 | }); |
| 897 | + if (window.MutationObserver && document.documentElement) { |
| 898 | + var themeObserver = new MutationObserver(function(mutations) { |
| 899 | + for (var i = 0; i < mutations.length; i += 1) { |
| 900 | + if (mutations[i].attributeName === 'data-theme') { |
| 901 | + applyThemeStateIfChanged('subtitle-ui-theme-attribute'); |
| 902 | + break; |
| 903 | + } |
| 904 | + } |
| 905 | + }); |
| 906 | + themeObserver.observe(document.documentElement, { |
| 907 | + attributes: true, |
| 908 | + attributeFilter: ['data-theme'] |
| 909 | + }); |
| 910 | + cleanupFns.push(function() { |
| 911 | + themeObserver.disconnect(); |
| 912 | + }); |
| 913 | + } |
890 | 914 |
|
891 | 915 | if (window.i18next && typeof window.i18next.on === 'function') { |
892 | 916 | var onLanguageChanged = function(nextLocale) { |
|
0 commit comments