'a'
mh-two-thousand-and-two
2024-04-12 44d2c92345cd156a59fc327b3060292a282d2893
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import { normallizeStyles } from 'uni-shared'
import { weexGetSystemInfoSync } from '../api/device/system'
import { setStatusBarStyle } from '../bridge'
import { getCurrentPages } from './page'
 
const ON_THEME_CHANGE = 'api.onThemeChange'
 
function onThemeChange (callback = () => { }) {
  UniServiceJSBridge.on(ON_THEME_CHANGE, callback)
}
 
function offThemeChange (callback = () => { }) {
  UniServiceJSBridge.off(ON_THEME_CHANGE, callback)
}
 
function getNavigatorStyle () {
  return plus.navigator.getUIStyle() === 'dark' ? 'light' : 'dark'
}
 
export function changePagesNavigatorStyle () {
  if (__uniConfig.darkmode) {
    const theme = getNavigatorStyle()
 
    setStatusBarStyle(theme)
 
    const pages = getCurrentPages(true)
    pages.forEach((page) => {
      page.$page.meta.statusBarStyle = theme
    })
  }
}
 
export function parseTheme (pageStyle) {
  if (__uniConfig.darkmode) {
    let parsedStyle = {}
    let theme = plus.navigator.getUIStyle()
 
    const systemInfo = weexGetSystemInfoSync()
    // 小程序 SDK
    if (systemInfo && systemInfo.hostTheme) {
      theme = systemInfo.hostTheme
    }
 
    parsedStyle = normallizeStyles(pageStyle, __uniConfig.themeConfig, theme)
    return parsedStyle
  }
  return pageStyle
}
 
export function useTabBarThemeChange (tabBar, options) {
  if (__uniConfig.darkmode) {
    const fn = () => {
      const {
        list = [], color, selectedColor,
        backgroundColor, borderStyle
      } = parseTheme(options, false)
      const tabbarStyle = {
        color,
        selectedColor,
        backgroundColor,
        borderStyle
      }
 
      tabBar && tabBar.setTabBarStyle(tabbarStyle)
      tabBar && tabBar.setTabBarItems({
        list: list.map((item) => ({
          iconPath: item.iconPath,
          selectedIconPath: item.selectedIconPath,
          visible: item.visible
        }))
      })
      // TODO 暂未实现
      // tabBar && tabBar.setAnimationAlphaBGColor(parseTheme((__uniConfig.window || {}).backgroundColor, false))
    }
 
    fn()
 
    onThemeChange(fn)
  }
}
 
export function useWebviewThemeChange (webview, getWebviewStyle) {
  if (__uniConfig.darkmode) {
    const fn = () => {
      const webviewStyle = getWebviewStyle()
      const style = {
        animationAlphaBGColor: webviewStyle.animationAlphaBGColor,
        background: webviewStyle.background,
        backgroundColorBottom: webviewStyle.backgroundColorBottom,
        backgroundColorTop: webviewStyle.backgroundColorTop
      }
      var titleNView = webviewStyle.titleNView
      if (typeof titleNView !== 'undefined') {
        style.titleNView = typeof titleNView === 'object' ? {
          backgroundColor: titleNView.backgroundColor,
          titleColor: titleNView.titleColor
        } : titleNView
      }
      webview && webview.setStyle(webviewStyle)
    }
    onThemeChange(fn)
    webview.addEventListener('close', () => offThemeChange(fn))
  }
}