avm99963 | 2485a3e | 2021-09-08 22:18:38 +0200 | [diff] [blame] | 1 | import {MDCTooltip} from '@material/tooltip'; |
| 2 | |
Adrià Vilanova Martínez | 968e93c | 2024-03-11 23:18:16 +0100 | [diff] [blame] | 3 | import {createPlainTooltip} from '../../../common/tooltip.js'; |
| 4 | import {createExtBadge} from '../utils/common.js'; |
avm99963 | 2485a3e | 2021-09-08 22:18:38 +0200 | [diff] [blame] | 5 | |
Adrià Vilanova Martínez | f472d49 | 2024-03-05 21:26:53 +0100 | [diff] [blame] | 6 | export const kColorThemeMode = Object.freeze({ |
| 7 | Auto: Symbol('auto'), |
| 8 | Light: Symbol('light'), |
| 9 | Dark: Symbol('dark'), |
| 10 | }); |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 11 | |
Adrià Vilanova Martínez | 968e93c | 2024-03-11 23:18:16 +0100 | [diff] [blame] | 12 | export function injectDarkThemeButton(rightControl, previousDarkThemeOption) { |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 13 | var darkThemeSwitch = document.createElement('material-button'); |
| 14 | darkThemeSwitch.classList.add('TWPT-dark-theme', 'TWPT-btn--with-badge'); |
| 15 | darkThemeSwitch.setAttribute('button', ''); |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 16 | |
Adrià Vilanova Martínez | 968e93c | 2024-03-11 23:18:16 +0100 | [diff] [blame] | 17 | darkThemeSwitch.addEventListener('click', () => { |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 18 | chrome.storage.sync.get(null, currentOptions => { |
Adrià Vilanova Martínez | 968e93c | 2024-03-11 23:18:16 +0100 | [diff] [blame] | 19 | currentOptions.ccdarktheme_switch_status = !previousDarkThemeOption; |
| 20 | chrome.storage.sync.set(currentOptions, () => { |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 21 | location.reload(); |
| 22 | }); |
| 23 | }); |
| 24 | }); |
| 25 | |
| 26 | var switchContent = document.createElement('div'); |
| 27 | switchContent.classList.add('content'); |
| 28 | |
| 29 | var icon = document.createElement('material-icon'); |
| 30 | |
| 31 | var i = document.createElement('i'); |
| 32 | i.classList.add('material-icon-i', 'material-icons-extended'); |
| 33 | i.textContent = 'brightness_4'; |
| 34 | |
| 35 | icon.appendChild(i); |
| 36 | switchContent.appendChild(icon); |
| 37 | darkThemeSwitch.appendChild(switchContent); |
| 38 | |
avm99963 | 2485a3e | 2021-09-08 22:18:38 +0200 | [diff] [blame] | 39 | let badgeContent, badgeTooltip; |
| 40 | [badgeContent, badgeTooltip] = createExtBadge(); |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 41 | |
| 42 | darkThemeSwitch.appendChild(badgeContent); |
| 43 | |
| 44 | rightControl.style.width = |
| 45 | (parseInt(window.getComputedStyle(rightControl).width) + 58) + 'px'; |
| 46 | rightControl.insertAdjacentElement('afterbegin', darkThemeSwitch); |
avm99963 | 2485a3e | 2021-09-08 22:18:38 +0200 | [diff] [blame] | 47 | |
| 48 | createPlainTooltip( |
| 49 | switchContent, chrome.i18n.getMessage('inject_ccdarktheme_helper')); |
| 50 | new MDCTooltip(badgeTooltip); |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 51 | } |
| 52 | |
Adrià Vilanova Martínez | f472d49 | 2024-03-05 21:26:53 +0100 | [diff] [blame] | 53 | export function getCurrentColorTheme(options) { |
| 54 | if (!options.ccdarktheme) { |
| 55 | return kColorThemeMode.Light; |
| 56 | } else { |
| 57 | if (options.ccdarktheme_mode == 'switch') { |
| 58 | return options.ccdarktheme_switch_status ? kColorThemeMode.Dark : |
| 59 | kColorThemeMode.Light; |
| 60 | } else { |
| 61 | return kColorThemeMode.Auto; |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 66 | export function isDarkThemeOn(options) { |
Adrià Vilanova Martínez | f472d49 | 2024-03-05 21:26:53 +0100 | [diff] [blame] | 67 | const activeColorTheme = getCurrentColorTheme(options); |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 68 | |
Adrià Vilanova Martínez | f472d49 | 2024-03-05 21:26:53 +0100 | [diff] [blame] | 69 | switch (activeColorTheme) { |
| 70 | case kColorThemeMode.Auto: |
| 71 | return window.matchMedia && |
| 72 | window.matchMedia('(prefers-color-scheme: dark)').matches; |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 73 | |
Adrià Vilanova Martínez | f472d49 | 2024-03-05 21:26:53 +0100 | [diff] [blame] | 74 | case kColorThemeMode.Light: |
| 75 | return false; |
| 76 | |
| 77 | case kColorThemeMode.Dark: |
| 78 | return true; |
| 79 | } |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 80 | } |