2016-08-17 06:01:20 +08:00
|
|
|
import ko from 'ko';
|
2023-02-20 22:47:46 +08:00
|
|
|
import { $htmlCL, appEl, elementById, leftPanelDisabled, Settings, SettingsGet } from 'Common/Globals';
|
2022-11-08 17:53:14 +08:00
|
|
|
import { isArray, arrayLength } from 'Common/Utils';
|
2023-01-19 23:46:39 +08:00
|
|
|
import { cssLink, serverRequestRaw } from 'Common/Links';
|
2022-11-08 17:53:14 +08:00
|
|
|
import { SaveSettingStatus } from 'Common/Enums';
|
2023-02-21 19:59:56 +08:00
|
|
|
import { addSubscribablesTo } from 'External/ko';
|
2015-01-27 05:06:00 +08:00
|
|
|
|
2022-11-08 17:53:14 +08:00
|
|
|
let __themeTimer = 0;
|
|
|
|
|
|
|
|
export const
|
2023-02-23 20:54:32 +08:00
|
|
|
// Also see Styles/_Values.less @maxMobileWidth
|
|
|
|
isMobile = matchMedia('(max-width: 799px)'),
|
2023-08-28 16:11:34 +08:00
|
|
|
// https://github.com/the-djmaze/snappymail/issues/1150
|
|
|
|
// isSmall = matchMedia('(max-width: 1400px)'),
|
2023-02-23 20:54:32 +08:00
|
|
|
|
2022-11-08 17:53:14 +08:00
|
|
|
ThemeStore = {
|
|
|
|
theme: ko.observable(''),
|
|
|
|
themes: ko.observableArray(),
|
|
|
|
userBackgroundName: ko.observable(''),
|
|
|
|
userBackgroundHash: ko.observable(''),
|
|
|
|
fontSansSerif: ko.observable(''),
|
|
|
|
fontSerif: ko.observable(''),
|
|
|
|
fontMono: ko.observable(''),
|
2023-03-18 06:20:42 +08:00
|
|
|
isMobile: ko.observable(false)
|
|
|
|
},
|
2022-11-08 17:53:14 +08:00
|
|
|
|
2023-03-18 06:20:42 +08:00
|
|
|
initThemes = () => {
|
|
|
|
const theme = SettingsGet('Theme'),
|
|
|
|
themes = Settings.app('themes');
|
2022-11-08 17:53:14 +08:00
|
|
|
|
2023-03-18 06:20:42 +08:00
|
|
|
ThemeStore.themes(isArray(themes) ? themes : []);
|
|
|
|
ThemeStore.theme(theme);
|
|
|
|
changeTheme(theme);
|
|
|
|
if (!ThemeStore.isMobile()) {
|
|
|
|
ThemeStore.userBackgroundName(SettingsGet('userBackgroundName'));
|
|
|
|
ThemeStore.userBackgroundHash(SettingsGet('userBackgroundHash'));
|
2021-02-15 22:20:22 +08:00
|
|
|
}
|
2023-03-18 06:20:42 +08:00
|
|
|
ThemeStore.fontSansSerif(SettingsGet('fontSansSerif'));
|
|
|
|
ThemeStore.fontSerif(SettingsGet('fontSerif'));
|
|
|
|
ThemeStore.fontMono(SettingsGet('fontMono'));
|
|
|
|
|
|
|
|
leftPanelDisabled(ThemeStore.isMobile());
|
2022-11-08 17:53:14 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
changeTheme = (value, themeTrigger = ()=>0) => {
|
|
|
|
const themeStyle = elementById('app-theme-style'),
|
|
|
|
clearTimer = () => {
|
|
|
|
__themeTimer = setTimeout(() => themeTrigger(SaveSettingStatus.Idle), 1000);
|
|
|
|
},
|
2023-01-19 23:46:39 +08:00
|
|
|
url = cssLink(value);
|
2022-11-08 17:53:14 +08:00
|
|
|
|
2023-02-01 18:09:18 +08:00
|
|
|
if (themeStyle.dataset.name != value) {
|
|
|
|
clearTimeout(__themeTimer);
|
|
|
|
|
|
|
|
themeTrigger(SaveSettingStatus.Saving);
|
|
|
|
|
|
|
|
rl.app.Remote.abort('theme').get('theme', url)
|
|
|
|
.then(data => {
|
|
|
|
if (2 === arrayLength(data)) {
|
|
|
|
themeStyle.textContent = data[1];
|
|
|
|
themeStyle.dataset.name = value;
|
|
|
|
themeTrigger(SaveSettingStatus.Success);
|
|
|
|
}
|
|
|
|
clearTimer();
|
|
|
|
}, clearTimer);
|
|
|
|
}
|
2022-11-08 17:53:14 +08:00
|
|
|
},
|
2021-03-05 23:46:40 +08:00
|
|
|
|
2023-01-27 04:42:14 +08:00
|
|
|
convertThemeName = theme => theme.replace(/@[a-z]+$/, '').replace(/([A-Z])/g, ' $1').trim();
|
2015-01-27 05:06:00 +08:00
|
|
|
|
2023-02-21 19:59:56 +08:00
|
|
|
addSubscribablesTo(ThemeStore, {
|
|
|
|
fontSansSerif: value => {
|
|
|
|
if (null != value) {
|
|
|
|
let cl = appEl.classList;
|
|
|
|
cl.forEach(name => {
|
|
|
|
if (name.startsWith('font') && !/font(Serif|Mono)/.test(name)) {
|
|
|
|
cl.remove(name);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
value && cl.add('font'+value);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
fontSerif: value => {
|
|
|
|
if (null != value) {
|
|
|
|
let cl = appEl.classList;
|
|
|
|
cl.forEach(name => name.startsWith('fontSerif') && cl.remove(name));
|
|
|
|
value && cl.add('fontSerif'+value);
|
|
|
|
}
|
|
|
|
},
|
2022-10-20 15:33:44 +08:00
|
|
|
|
2023-02-21 19:59:56 +08:00
|
|
|
fontMono: value => {
|
|
|
|
if (null != value) {
|
|
|
|
let cl = appEl.classList;
|
|
|
|
cl.forEach(name => name.startsWith('fontMono') && cl.remove(name));
|
|
|
|
value && cl.add('fontMono'+value);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
userBackgroundHash: value => {
|
|
|
|
appEl.classList.toggle('UserBackground', !!value);
|
|
|
|
appEl.style.backgroundImage = value ? "url("+serverRequestRaw('UserBackground', value)+")" : null;
|
|
|
|
}
|
2021-07-20 22:49:03 +08:00
|
|
|
});
|
2023-02-23 20:54:32 +08:00
|
|
|
|
|
|
|
isMobile.onchange = e => {
|
|
|
|
ThemeStore.isMobile(e.matches);
|
|
|
|
$htmlCL.toggle('rl-mobile', e.matches);
|
2023-08-28 16:11:34 +08:00
|
|
|
/*$htmlCL.contains('sm-msgView-side') || */leftPanelDisabled(e.matches);
|
2023-02-23 20:54:32 +08:00
|
|
|
};
|
|
|
|
isMobile.onchange(isMobile);
|
2023-08-28 16:11:34 +08:00
|
|
|
|
|
|
|
//isSmall.onchange = e => $htmlCL.contains('sm-msgView-side') && leftPanelDisabled(e.matches);
|
|
|
|
//isSmall.onchange(isSmall);
|