The code below is based on the Doxygen Awesome project with some minor modifications https://github.com/jothepro/doxygen-awesome-css MIT License Copyright (c) 2021 - 2022 jothepro Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated document
| 28 | */ |
| 29 | |
| 30 | class DarkModeToggle extends HTMLElement { |
| 31 | static icon = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" height="1.2em" width="1.2em"><g fill="none" fill-rule="evenodd"><path d="M0 0h24v24H0z"></path><rect width="1" height="3" x="12" fill="currentColor" rx=".5"></rect><rect width="1" height="3" x="12" y="21" fill="currentColor" rx=".5"></rect><rect width="1" height="3" x="22" y="10.5" fill="currentColor" rx=".5" transform="rotate(90 22.5 12)"></rect><rect width="1" height="3" x="1" y="10.5" fill="currentColor" rx=".5" transform="rotate(90 1.5 12)"></rect><rect width="1" height="3" x="19" y="3" fill="currentColor" rx=".5" transform="rotate(-135 19.5 4.5)"></rect><rect width="1" height="3" x="19" y="18" fill="currentColor" rx=".5" transform="rotate(135 19.5 19.5)"></rect><rect width="1" height="3" x="4" y="3" fill="currentColor" rx=".5" transform="scale(1 -1) rotate(45 15.37 0)"></rect><rect width="1" height="3" x="4" y="18" fill="currentColor" rx=".5" transform="scale(1 -1) rotate(-45 -42.57 0)"></rect><circle cx="12" cy="12" r="6.5" stroke="currentColor"></circle></g></svg>' |
| 32 | static icond = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" height="1.2em" width="1.2em"><g fill="none" fill-rule="evenodd"><path d="M0 0h24v24H0z"></path><rect width="1" height="3" x="12" fill="currentColor" rx=".5"></rect><rect width="1" height="3" x="12" y="21" fill="currentColor" rx=".5"></rect><rect width="1" height="3" x="22" y="10.5" fill="currentColor" rx=".5" transform="rotate(90 22.5 12)"></rect><rect width="1" height="3" x="1" y="10.5" fill="currentColor" rx=".5" transform="rotate(90 1.5 12)"></rect><rect width="1" height="3" x="19" y="3" fill="currentColor" rx=".5" transform="rotate(-135 19.5 4.5)"></rect><rect width="1" height="3" x="19" y="18" fill="currentColor" rx=".5" transform="rotate(135 19.5 19.5)"></rect><rect width="1" height="3" x="4" y="3" fill="currentColor" rx=".5" transform="scale(1 -1) rotate(45 15.37 0)"></rect><rect width="1" height="3" x="4" y="18" fill="currentColor" rx=".5" transform="scale(1 -1) rotate(-45 -42.57 0)"></rect><circle cx="12" cy="12" r="6.5" stroke="currentColor" fill="currentColor"></circle></g></svg>' |
| 33 | static title = "Toggle Light/Dark Mode" |
| 34 | |
| 35 | static prefersLightModeInDarkModeKey = "prefers-light-mode-in-dark-mode" |
| 36 | static prefersDarkModeInLightModeKey = "prefers-dark-mode-in-light-mode" |
| 37 | |
| 38 | static _staticConstructor = function() { |
| 39 | DarkModeToggle.enableDarkMode(DarkModeToggle.userPreference) |
| 40 | // Update the color scheme when the browsers preference changes |
| 41 | // without user interaction on the website. |
| 42 | window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', event => { |
| 43 | DarkModeToggle.onSystemPreferenceChanged() |
| 44 | }) |
| 45 | // Update the color scheme when the tab is made visible again. |
| 46 | // It is possible that the appearance was changed in another tab |
| 47 | // while this tab was in the background. |
| 48 | document.addEventListener("visibilitychange", visibilityState => { |
| 49 | if (document.visibilityState === 'visible') { |
| 50 | DarkModeToggle.onSystemPreferenceChanged() |
| 51 | } |
| 52 | }); |
| 53 | }() |
| 54 | |
| 55 | static addButton() { |
| 56 | |
| 57 | var tbuttons = document.getElementsByTagName("dark-mode-toggle"); |
| 58 | var toggleButton; |
| 59 | var titleArea = document.getElementById("titlearea"); |
| 60 | var searchBox = document.getElementById("MSearchBox"); |
| 61 | var mainMenu = document.getElementById("main-menu"); |
| 62 | var navRow1 = document.getElementById("navrow1"); |
| 63 | var mainMenuVisible = false; |
| 64 | if (!tbuttons.length){ |
| 65 | toggleButton = document.createElement('dark-mode-toggle') |
| 66 | toggleButton.title = DarkModeToggle.title |
| 67 | } else {toggleButton=tbuttons[0]} |
| 68 | |
| 69 | |
| 70 | if (DarkModeToggle.darkModeEnabled){ |
| 71 | toggleButton.innerHTML=DarkModeToggle.icond |
| 72 | } else { |
| 73 | toggleButton.innerHTML=DarkModeToggle.icon |
| 74 | } |
| 75 | |
| 76 | if (mainMenu) { |
| 77 | var menuStyle = window.getComputedStyle(mainMenu); |
| 78 | mainMenuVisible = menuStyle.display!=='none' |
| 79 | } |
| 80 | var searchBoxPos1 = document.getElementById("searchBoxPos1"); |
| 81 | if (searchBox) { // (1) search box visible |
| 82 | searchBox.parentNode.appendChild(toggleButton) |
| 83 | } else if (navRow1) { // (2) no search box, static menu bar |
| 84 | var li = document.createElement('li'); |
| 85 | li.style = 'float: right;' |
| 86 | li.appendChild(toggleButton); |
| 87 | toggleButton.style = 'width: 24px; height: 25px; padding-top: 11px; float: right;'; |
nothing calls this directly
no test coverage detected