avm99963 | 2485a3e | 2021-09-08 22:18:38 +0200 | [diff] [blame] | 1 | import {MDCTooltip} from '@material/tooltip'; |
| 2 | |
| 3 | import {createPlainTooltip} from '../../common/tooltip.js'; |
| 4 | |
Adrià Vilanova Martínez | 27c6996 | 2021-07-17 23:32:51 +0200 | [diff] [blame] | 5 | import {createExtBadge} from './utils/common.js'; |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 6 | |
| 7 | export function injectDarkModeButton(rightControl, previousDarkModeOption) { |
| 8 | var darkThemeSwitch = document.createElement('material-button'); |
| 9 | darkThemeSwitch.classList.add('TWPT-dark-theme', 'TWPT-btn--with-badge'); |
| 10 | darkThemeSwitch.setAttribute('button', ''); |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 11 | |
| 12 | darkThemeSwitch.addEventListener('click', e => { |
| 13 | chrome.storage.sync.get(null, currentOptions => { |
| 14 | currentOptions.ccdarktheme_switch_status = !previousDarkModeOption; |
| 15 | chrome.storage.sync.set(currentOptions, _ => { |
| 16 | location.reload(); |
| 17 | }); |
| 18 | }); |
| 19 | }); |
| 20 | |
| 21 | var switchContent = document.createElement('div'); |
| 22 | switchContent.classList.add('content'); |
| 23 | |
| 24 | var icon = document.createElement('material-icon'); |
| 25 | |
| 26 | var i = document.createElement('i'); |
| 27 | i.classList.add('material-icon-i', 'material-icons-extended'); |
| 28 | i.textContent = 'brightness_4'; |
| 29 | |
| 30 | icon.appendChild(i); |
| 31 | switchContent.appendChild(icon); |
| 32 | darkThemeSwitch.appendChild(switchContent); |
| 33 | |
avm99963 | 2485a3e | 2021-09-08 22:18:38 +0200 | [diff] [blame] | 34 | let badgeContent, badgeTooltip; |
| 35 | [badgeContent, badgeTooltip] = createExtBadge(); |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 36 | |
| 37 | darkThemeSwitch.appendChild(badgeContent); |
| 38 | |
| 39 | rightControl.style.width = |
| 40 | (parseInt(window.getComputedStyle(rightControl).width) + 58) + 'px'; |
| 41 | rightControl.insertAdjacentElement('afterbegin', darkThemeSwitch); |
avm99963 | 2485a3e | 2021-09-08 22:18:38 +0200 | [diff] [blame] | 42 | |
| 43 | createPlainTooltip( |
| 44 | switchContent, chrome.i18n.getMessage('inject_ccdarktheme_helper')); |
| 45 | new MDCTooltip(badgeTooltip); |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | export function isDarkThemeOn(options) { |
| 49 | if (!options.ccdarktheme) return false; |
| 50 | |
| 51 | if (options.ccdarktheme_mode == 'switch') |
| 52 | return options.ccdarktheme_switch_status; |
| 53 | |
| 54 | return window.matchMedia && |
| 55 | window.matchMedia('(prefers-color-scheme: dark)').matches; |
| 56 | } |