blob: 1b6c751dd515d777c68392dce95e9314f5494139 [file] [log] [blame]
avm999632485a3e2021-09-08 22:18:38 +02001import {MDCTooltip} from '@material/tooltip';
2
3import {createPlainTooltip} from '../../common/tooltip.js';
4
Adrià Vilanova Martínez27c69962021-07-17 23:32:51 +02005import {createExtBadge} from './utils/common.js';
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +02006
7export 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ínez3465e772021-07-11 19:18:41 +020011
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
avm999632485a3e2021-09-08 22:18:38 +020034 let badgeContent, badgeTooltip;
35 [badgeContent, badgeTooltip] = createExtBadge();
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020036
37 darkThemeSwitch.appendChild(badgeContent);
38
39 rightControl.style.width =
40 (parseInt(window.getComputedStyle(rightControl).width) + 58) + 'px';
41 rightControl.insertAdjacentElement('afterbegin', darkThemeSwitch);
avm999632485a3e2021-09-08 22:18:38 +020042
43 createPlainTooltip(
44 switchContent, chrome.i18n.getMessage('inject_ccdarktheme_helper'));
45 new MDCTooltip(badgeTooltip);
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020046}
47
48export 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}