useThemeMode.uts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // @ts-nocheck
  2. // #ifndef UNI-APP-X
  3. import { type ComputedRef, ref, watch, getCurrentScope, onScopeDispose, computed, reactive } from './vue';
  4. // #endif
  5. type ThemeMode = 'light' | 'dark' | 'auto'
  6. const THEME_LOCAL_STORAGE_KEY = 'app-theme-preference'
  7. let limeThemeId = -1
  8. const limeThemeMode = ref<ThemeMode>('auto')
  9. const limeTheme = ref(getTheme())
  10. function setWeb (theme: ThemeMode) {
  11. // #ifdef WEB
  12. const pageHead = document.querySelector(".uni-page-head");
  13. if(!pageHead) return
  14. if (theme == 'dark') {
  15. pageHead.style.backgroundColor = '#181818'
  16. pageHead.style.color = ''
  17. } else {
  18. pageHead.style.backgroundColor = 'rgb(245, 245, 245)'
  19. pageHead.style.color = 'rgb(0, 0, 0)'
  20. }
  21. // #endif
  22. }
  23. export function getTheme(): 'dark' | 'light' {
  24. // #ifndef UNI-APP-X && APP
  25. let mode = uni.getStorageSync(THEME_LOCAL_STORAGE_KEY)
  26. limeThemeMode.value = (mode || 'auto')
  27. const hostTheme = uni.getAppBaseInfo().hostTheme;
  28. return limeThemeMode.value === 'auto' ? (hostTheme || 'light') : limeThemeMode.value
  29. // #endif
  30. // #ifdef UNI-APP-X && APP
  31. let { osTheme, appTheme } = uni.getSystemInfoSync();
  32. return appTheme == "auto" ? osTheme! : appTheme!
  33. // #endif
  34. }
  35. // #ifndef UNI-APP-X
  36. let limeThemeCallBack = (result) => {
  37. limeTheme.value = result.theme
  38. };
  39. // #endif
  40. export function stop() {
  41. // #ifndef UNI-APP-X
  42. uni.offThemeChange(limeThemeCallBack)
  43. // #endif
  44. // #ifdef UNI-APP-X
  45. // #ifndef UNI-APP-X && APP
  46. uni.offHostThemeChange(limeThemeId)
  47. // #endif
  48. // #ifdef UNI-APP-X && APP
  49. uni.offAppThemeChange(limeThemeId)
  50. // #endif
  51. // #endif
  52. }
  53. // 检查系统主题
  54. export const checkSystemTheme = () => {
  55. stop()
  56. limeTheme.value = getTheme()
  57. // #ifndef UNI-APP-X
  58. uni.onThemeChange(limeThemeCallBack);
  59. // #endif
  60. // #ifdef UNI-APP-X
  61. // #ifndef UNI-APP-X && APP
  62. limeThemeId = uni.onHostThemeChange((result) => {
  63. limeTheme.value = result.hostTheme
  64. });
  65. // #endif
  66. // #ifdef UNI-APP-X && APP
  67. limeThemeId = uni.onAppThemeChange((res : AppThemeChangeResult) => {
  68. limeTheme.value = res.appTheme.trim()
  69. })
  70. // #endif
  71. // #endif
  72. }
  73. export const isDarkMode = computed(() => {
  74. return limeTheme.value == "dark";
  75. });
  76. export function setThemeMode(theme: 'dark' | 'light' | 'auto') {
  77. // #ifdef UNI-APP-X && APP
  78. if (limeTheme.value == theme) return;
  79. uni.setAppTheme({
  80. theme,
  81. success: function (result: SetAppThemeSuccessResult) {
  82. console.log("设置appTheme为"+ result.theme +"成功")
  83. limeTheme.value = result.theme
  84. },
  85. fail: function (e : IAppThemeFail) {
  86. console.log("设置appTheme为 auto 失败,原因:", e.errMsg)
  87. }
  88. })
  89. // #endif
  90. // #ifndef UNI-APP-X && APP
  91. limeThemeMode.value = theme
  92. uni.setStorageSync(THEME_LOCAL_STORAGE_KEY, theme)
  93. limeTheme.value = getTheme()
  94. // #endif
  95. // #ifdef WEB
  96. setWeb(limeTheme.value )
  97. // #endif
  98. }
  99. export function toggleTheme() {
  100. setThemeMode(isDarkMode.value ? 'light' : 'dark')
  101. }
  102. // #ifdef WEB
  103. watch(isDarkMode, ()=>{
  104. setWeb(limeTheme.value)
  105. })
  106. // #endif