updateBackground.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import { colorIsDark, lighten, darken } from '/@/utils/color';
  2. import { useAppStore } from '/@/store/modules/app';
  3. import { ThemeEnum } from '/@/enums/appEnum';
  4. import { setCssVar } from './util';
  5. import { SIDE_BAR_BG_COLOR_LIST, SIDER_LOGO_BG_COLOR_LIST } from '/@/settings/designSetting';
  6. const HEADER_BG_COLOR_VAR = '--vent-header-bg-color';
  7. const HEADER_BG_HOVER_COLOR_VAR = '--header-bg-hover-color';
  8. const HEADER_MENU_ACTIVE_BG_COLOR_VAR = '--header-active-menu-bg-color';
  9. const SIDER_LOGO_BG_COLOR = '--sider-logo-bg-color';
  10. const SIDER_DARK_BG_COLOR = '--sider-dark-bg-color';
  11. const SIDER_DARK_DARKEN_BG_COLOR = '--sider-dark-darken-bg-color';
  12. const SIDER_LIGHTEN_BG_COLOR = '--sider-dark-lighten-bg-color';
  13. /**
  14. * Change the background color of the top header
  15. * @param color
  16. */
  17. export function updateHeaderBgColor(color?: string) {
  18. const appStore = useAppStore();
  19. const darkMode = appStore.getDarkMode === ThemeEnum.DARK;
  20. if (!color) {
  21. if (darkMode) {
  22. color = '#151515';
  23. } else {
  24. color = appStore.getHeaderSetting.bgColor;
  25. }
  26. }
  27. // bg color
  28. setCssVar(HEADER_BG_COLOR_VAR, color);
  29. // hover color
  30. const hoverColor = lighten(color!, 6);
  31. setCssVar(HEADER_BG_HOVER_COLOR_VAR, hoverColor);
  32. setCssVar(HEADER_MENU_ACTIVE_BG_COLOR_VAR, hoverColor);
  33. // Determine the depth of the color value and automatically switch the theme
  34. const isDark = colorIsDark(color!);
  35. appStore.setProjectConfig({
  36. headerSetting: {
  37. theme: isDark || darkMode ? ThemeEnum.DARK : ThemeEnum.LIGHT,
  38. },
  39. });
  40. }
  41. /**
  42. * Change the background color of the left menu
  43. * @param color bg color
  44. */
  45. export function updateSidebarBgColor(color?: string) {
  46. const appStore = useAppStore();
  47. // if (!isHexColor(color)) return;
  48. const darkMode = appStore.getDarkMode === ThemeEnum.DARK;
  49. if (!color) {
  50. if (darkMode) {
  51. color = '#212121';
  52. } else {
  53. color = appStore.getMenuSetting.bgColor;
  54. }
  55. }
  56. // update-begin--author:liaozhiyang---date:20230811---for:【QQYUN-5922】logo背景色渐变
  57. let findIndex = SIDE_BAR_BG_COLOR_LIST.findIndex((item) => item === color);
  58. setCssVar(SIDER_LOGO_BG_COLOR, findIndex == -1 ? 'linear-gradient(180deg, #000000, #282828)' : SIDER_LOGO_BG_COLOR_LIST[findIndex]);
  59. // update-end--author:liaozhiyang---date:20230811---for:【QQYUN-5922】llogo背景色渐变
  60. setCssVar(SIDER_DARK_BG_COLOR, color);
  61. setCssVar(SIDER_DARK_DARKEN_BG_COLOR, darken(color!, 6));
  62. setCssVar(SIDER_LIGHTEN_BG_COLOR, lighten(color!, 5));
  63. // only #ffffff is light
  64. // Only when the background color is #fff, the theme of the menu will be changed to light
  65. const isLight = ['#fff', '#ffffff'].includes(color!.toLowerCase());
  66. appStore.setProjectConfig({
  67. menuSetting: {
  68. theme: isLight && !darkMode ? ThemeEnum.LIGHT : ThemeEnum.DARK,
  69. },
  70. });
  71. }