use-preferences.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. import { computed } from 'vue';
  2. import { diff } from '@vben-core/toolkit';
  3. import { isDarkTheme, preferencesManager } from './preferences';
  4. function usePreferences() {
  5. const preferences = preferencesManager.getPreferences();
  6. const initialPreferences = preferencesManager.getInitialPreferences();
  7. /**
  8. * @zh_CN 计算偏好设置的变化
  9. */
  10. const diffPreference = computed(() => {
  11. return diff(initialPreferences, preferences);
  12. });
  13. const appPreferences = computed(() => preferences.app);
  14. const shortcutKeysPreferences = computed(() => preferences.shortcutKeys);
  15. /**
  16. * @zh_CN 判断是否为暗黑模式
  17. * @param preferences - 当前偏好设置对象,它的主题值将被用来判断是否为暗黑模式。
  18. * @returns 如果主题为暗黑模式,返回 true,否则返回 false。
  19. */
  20. const isDark = computed(() => {
  21. return isDarkTheme(preferences.theme.mode);
  22. });
  23. const theme = computed(() => {
  24. return isDark.value ? 'dark' : 'light';
  25. });
  26. /**
  27. * @zh_CN 布局方式
  28. */
  29. const layout = computed(() =>
  30. appPreferences.value.isMobile ? 'sidebar-nav' : appPreferences.value.layout,
  31. );
  32. /**
  33. * @zh_CN 是否全屏显示content,不需要侧边、底部、顶部、tab区域
  34. */
  35. const isFullContent = computed(
  36. () => appPreferences.value.layout === 'full-content',
  37. );
  38. /**
  39. * @zh_CN 是否侧边导航模式
  40. */
  41. const isSideNav = computed(
  42. () => appPreferences.value.layout === 'sidebar-nav',
  43. );
  44. /**
  45. * @zh_CN 是否侧边混合模式
  46. */
  47. const isSideMixedNav = computed(
  48. () => appPreferences.value.layout === 'sidebar-mixed-nav',
  49. );
  50. /**
  51. * @zh_CN 是否为头部导航模式
  52. */
  53. const isHeaderNav = computed(
  54. () => appPreferences.value.layout === 'header-nav',
  55. );
  56. /**
  57. * @zh_CN 是否为混合导航模式
  58. */
  59. const isMixedNav = computed(
  60. () => appPreferences.value.layout === 'mixed-nav',
  61. );
  62. /**
  63. * @zh_CN 是否包含侧边导航模式
  64. */
  65. const isSideMode = computed(() => {
  66. return isMixedNav.value || isSideMixedNav.value || isSideNav.value;
  67. });
  68. /**
  69. * @zh_CN 是否开启keep-alive
  70. * 在tabs可见以及开启keep-alive的情况下才开启
  71. */
  72. const keepAlive = computed(
  73. () => preferences.tabbar.enable && preferences.tabbar.keepAlive,
  74. );
  75. /**
  76. * @zh_CN 登录注册页面布局是否为左侧
  77. */
  78. const authPanelLeft = computed(() => {
  79. return appPreferences.value.authPageLayout === 'panel-left';
  80. });
  81. /**
  82. * @zh_CN 登录注册页面布局是否为左侧
  83. */
  84. const authPanelRight = computed(() => {
  85. return appPreferences.value.authPageLayout === 'panel-right';
  86. });
  87. /**
  88. * @zh_CN 登录注册页面布局是否为中间
  89. */
  90. const authPanelCenter = computed(() => {
  91. return appPreferences.value.authPageLayout === 'panel-center';
  92. });
  93. /**
  94. * @zh_CN 是否启用全局搜索快捷键
  95. */
  96. const globalSearchShortcutKey = computed(() => {
  97. const { enable, globalSearch } = shortcutKeysPreferences.value;
  98. return enable && globalSearch;
  99. });
  100. /**
  101. * @zh_CN 是否启用全局注销快捷键
  102. */
  103. const globalLogoutShortcutKey = computed(() => {
  104. const { enable, globalLogout } = shortcutKeysPreferences.value;
  105. return enable && globalLogout;
  106. });
  107. const globalLockScreenShortcutKey = computed(() => {
  108. const { enable, globalLockScreen } = shortcutKeysPreferences.value;
  109. return enable && globalLockScreen;
  110. });
  111. /**
  112. * @zh_CN 是否启用全局偏好设置快捷键
  113. */
  114. const globalPreferencesShortcutKey = computed(() => {
  115. const { enable, globalPreferences } = shortcutKeysPreferences.value;
  116. return enable && globalPreferences;
  117. });
  118. /**
  119. * @zh_CN 内容是否已经最大化
  120. * 排除 full-content模式
  121. */
  122. const contentIsMaximize = computed(() => {
  123. const headerIsHidden = preferences.header.hidden;
  124. const sidebarIsHidden = preferences.sidebar.hidden;
  125. return headerIsHidden && sidebarIsHidden && !isFullContent.value;
  126. });
  127. return {
  128. authPanelCenter,
  129. authPanelLeft,
  130. authPanelRight,
  131. contentIsMaximize,
  132. diffPreference,
  133. globalLockScreenShortcutKey,
  134. globalLogoutShortcutKey,
  135. globalPreferencesShortcutKey,
  136. globalSearchShortcutKey,
  137. isDark,
  138. isFullContent,
  139. isHeaderNav,
  140. isMixedNav,
  141. isSideMixedNav,
  142. isSideMode,
  143. isSideNav,
  144. keepAlive,
  145. layout,
  146. theme,
  147. };
  148. }
  149. export { usePreferences };