blob: c1888eae7d6332c8af2d16d14f2fc5f60d24f2d8 [file] [log] [blame]
avm999632485a3e2021-09-08 22:18:38 +02001import {MDCTooltip} from '@material/tooltip';
2
Adrià Vilanova Martínez968e93c2024-03-11 23:18:16 +01003import {createPlainTooltip} from '../../../common/tooltip.js';
4import {createExtBadge} from '../utils/common.js';
avm999632485a3e2021-09-08 22:18:38 +02005
Adrià Vilanova Martínezf472d492024-03-05 21:26:53 +01006export const kColorThemeMode = Object.freeze({
7 Auto: Symbol('auto'),
8 Light: Symbol('light'),
9 Dark: Symbol('dark'),
10});
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020011
Adrià Vilanova Martínez968e93c2024-03-11 23:18:16 +010012export function injectDarkThemeButton(rightControl, previousDarkThemeOption) {
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020013 var darkThemeSwitch = document.createElement('material-button');
14 darkThemeSwitch.classList.add('TWPT-dark-theme', 'TWPT-btn--with-badge');
15 darkThemeSwitch.setAttribute('button', '');
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020016
Adrià Vilanova Martínez968e93c2024-03-11 23:18:16 +010017 darkThemeSwitch.addEventListener('click', () => {
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020018 chrome.storage.sync.get(null, currentOptions => {
Adrià Vilanova Martínez968e93c2024-03-11 23:18:16 +010019 currentOptions.ccdarktheme_switch_status = !previousDarkThemeOption;
20 chrome.storage.sync.set(currentOptions, () => {
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020021 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
avm999632485a3e2021-09-08 22:18:38 +020039 let badgeContent, badgeTooltip;
40 [badgeContent, badgeTooltip] = createExtBadge();
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020041
42 darkThemeSwitch.appendChild(badgeContent);
43
44 rightControl.style.width =
45 (parseInt(window.getComputedStyle(rightControl).width) + 58) + 'px';
46 rightControl.insertAdjacentElement('afterbegin', darkThemeSwitch);
avm999632485a3e2021-09-08 22:18:38 +020047
48 createPlainTooltip(
49 switchContent, chrome.i18n.getMessage('inject_ccdarktheme_helper'));
50 new MDCTooltip(badgeTooltip);
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020051}
52
Adrià Vilanova Martínezf472d492024-03-05 21:26:53 +010053export 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ínez3465e772021-07-11 19:18:41 +020066export function isDarkThemeOn(options) {
Adrià Vilanova Martínezf472d492024-03-05 21:26:53 +010067 const activeColorTheme = getCurrentColorTheme(options);
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020068
Adrià Vilanova Martínezf472d492024-03-05 21:26:53 +010069 switch (activeColorTheme) {
70 case kColorThemeMode.Auto:
71 return window.matchMedia &&
72 window.matchMedia('(prefers-color-scheme: dark)').matches;
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020073
Adrià Vilanova Martínezf472d492024-03-05 21:26:53 +010074 case kColorThemeMode.Light:
75 return false;
76
77 case kColorThemeMode.Dark:
78 return true;
79 }
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020080}