| 5 | const CHANNEL_NAME: &str = "dx-theme"; |
| 6 | |
| 7 | pub fn theme_seed() { |
| 8 | _ = document::eval(&format!( |
| 9 | r#" |
| 10 | (function () {{ |
| 11 | if (window.__dx_theme_seeded) return; |
| 12 | window.__dx_theme_seeded = true; |
| 13 | |
| 14 | const COOKIE_NAME = '{COOKIE_NAME}'; |
| 15 | const CHANNEL_NAME = '{CHANNEL_NAME}'; |
| 16 | |
| 17 | function getCookie(name) {{ |
| 18 | const prefix = name + '='; |
| 19 | const parts = document.cookie.split(';'); |
| 20 | for (let p of parts) {{ |
| 21 | p = p.trim(); |
| 22 | if (p.startsWith(prefix)) return decodeURIComponent(p.slice(prefix.length)); |
| 23 | }} |
| 24 | return null; |
| 25 | }} |
| 26 | |
| 27 | function apply(theme) {{ |
| 28 | if (theme === 'dark' || theme === 'light') {{ |
| 29 | document.documentElement.setAttribute('data-theme', theme); |
| 30 | }} else {{ |
| 31 | document.documentElement.removeAttribute('data-theme'); |
| 32 | }} |
| 33 | }} |
| 34 | |
| 35 | apply(getCookie(COOKIE_NAME)); |
| 36 | |
| 37 | try {{ |
| 38 | const ch = new BroadcastChannel(CHANNEL_NAME); |
| 39 | ch.addEventListener('message', (event) => {{ |
| 40 | const data = event.data; |
| 41 | apply(data && data.theme); |
| 42 | }}); |
| 43 | window.__dx_theme_channel = ch; |
| 44 | }} catch (_) {{}} |
| 45 | }})(); |
| 46 | "#, |
| 47 | )); |
| 48 | } |
| 49 | |
| 50 | pub fn set_theme(dark_mode: bool) { |
| 51 | let theme = if dark_mode { "dark" } else { "light" }; |