index.uts 5.7 KB
Newer Older
DCloud-yyl's avatar
DCloud-yyl 已提交
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
import { getCurrentPage, navigator } from '@dcloudio/uni-runtime';
import { HideNavigationBarLoading, HideNavigationBarLoadingOptions, HideNavigationBarLoadingSuccess, SetNavigationBarColor, SetNavigationBarColorOptions, SetNavigationBarColorSuccess, SetNavigationBarTitle, SetNavigationBarTitleOptions, SetNavigationBarTitleSuccess, ShowNavigationBarLoading, ShowNavigationBarLoadingOptions, ShowNavigationBarLoadingSuccess } from '../interface.uts';
import { API_SET_NAVIGATION_BAR_COLOR, API_SET_NAVIGATION_BAR_TITLE, SetNavigationBarColorProtocol, SetNavigationBarTitleProtocol } from '../protocol.uts';

export { SetNavigationBarColor, SetNavigationBarColorOptions, SetNavigationBarColorSuccess, SetNavigationBarTitle, SetNavigationBarTitleOptions, SetNavigationBarTitleSuccess, ShowNavigationBarLoading, ShowNavigationBarLoadingOptions, ShowNavigationBarLoadingSuccess, HideNavigationBarLoading, HideNavigationBarLoadingOptions, HideNavigationBarLoadingSuccess }

interface TitleNView {
  titleText: string;
  autoBackButton?: boolean
  loading?: boolean
}
interface BackButton {
  color?: string
}
interface PlusWebviewWebviewTitleNViewStyles {
  backgroundColor?: string
  titleColor?: string,
  titleNView?: TitleNView
  backButton?: BackButton
}
interface Webview {
  getStyle: () => PlusWebviewWebviewTitleNViewStyles | null;
  setStyle: (style: PlusWebviewWebviewTitleNViewStyles) => void;
};
interface $page {
  statusBarStyle: 'dark' | 'light'
}
interface Page {
  $getAppWebview: () => Webview | null;
  $page: $page
}

const getWebview = (page: Page): Webview | null => {
  const webview = page.$getAppWebview()
  return webview

}

export const setNavigationBarColor: SetNavigationBarColor =
  defineAsyncApi<SetNavigationBarColorOptions, SetNavigationBarColorSuccess>(
    API_SET_NAVIGATION_BAR_COLOR,
    (
      args: SetNavigationBarColorOptions,
      executor: ApiExecutor<SetNavigationBarColorSuccess>
    ) => {
      const page = getCurrentPage() as Page
      if (!page) {
        return executor.reject(`getCurrentPages is empty`)
      }
      const webview = getWebview(page)
      if (webview) {
        const styles: PlusWebviewWebviewTitleNViewStyles = {}
        if (args.frontColor) {
          styles.titleColor = args.frontColor
        }
        if (args.backgroundColor) {
          styles.backgroundColor = args.backgroundColor
        }
        const statusBarStyle = args.frontColor === '#000000' ? 'dark' : 'light'
DCloud-yyl's avatar
DCloud-yyl 已提交
60
        try { navigator.setStatusBarStyle(statusBarStyle) } catch (error) { }
DCloud-yyl's avatar
DCloud-yyl 已提交
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 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161

        // 用户调用api时同时改变当前页配置,这样在系统调用设置时,可以避免覆盖用户设置
        page.$page.statusBarStyle = statusBarStyle

        const style = webview.getStyle()
        if (style && style.titleNView) {
          if (style.titleNView.autoBackButton) {
            styles.backButton = styles.backButton || {}
            styles.backButton.color = args.frontColor
          }
          webview.setStyle({
            titleNView: styles as TitleNView,
          } as PlusWebviewWebviewTitleNViewStyles)
        }
        executor.resolve()
      } else {
        executor.reject()
      }
    },
    SetNavigationBarColorProtocol
  ) as SetNavigationBarColor


export const setNavigationBarTitle: SetNavigationBarTitle =
  defineAsyncApi<SetNavigationBarTitleOptions, SetNavigationBarTitleSuccess>(
    API_SET_NAVIGATION_BAR_TITLE,
    (args: SetNavigationBarTitleOptions, executor: ApiExecutor<SetNavigationBarTitleSuccess>) => {
      const page = getCurrentPage() as Page
      if (!page) {
        return executor.reject(`getCurrentPages is empty`)
      }

      const webview = getWebview(page)
      if (webview) {
        const style = webview.getStyle()
        if (style && style.titleNView) {
          webview.setStyle({
            titleNView: {
              titleText: args.title,
            } as TitleNView,
          } as PlusWebviewWebviewTitleNViewStyles)
        }
        executor.resolve()
      } else {
        executor.reject()
      }
    },
    SetNavigationBarTitleProtocol
  ) as SetNavigationBarTitle

export const showNavigationBarLoading: ShowNavigationBarLoading =
  defineAsyncApi<ShowNavigationBarLoadingOptions, ShowNavigationBarLoadingSuccess>(
    API_SET_NAVIGATION_BAR_TITLE,
    (args: ShowNavigationBarLoadingOptions, executor: ApiExecutor<ShowNavigationBarLoadingSuccess>) => {
      const page = getCurrentPage() as Page
      if (!page) {
        return executor.reject(`getCurrentPages is empty`)
      }

      const webview = getWebview(page)
      if (webview) {
        const style = webview.getStyle()
        if (style && style.titleNView) {
          webview.setStyle({
            titleNView: {
              loading: true,
            } as TitleNView,
          } as PlusWebviewWebviewTitleNViewStyles)
        }
        executor.resolve()
      } else {
        executor.reject()
      }
    }
  ) as ShowNavigationBarLoading

export const hideNavigationBarLoading: HideNavigationBarLoading =
  defineAsyncApi<HideNavigationBarLoadingOptions, HideNavigationBarLoadingSuccess>(
    API_SET_NAVIGATION_BAR_TITLE,
    (args: HideNavigationBarLoadingOptions, executor: ApiExecutor<HideNavigationBarLoadingSuccess>) => {
      const page = getCurrentPage() as Page
      if (!page) {
        return executor.reject(`getCurrentPages is empty`)
      }

      const webview = getWebview(page)
      if (webview) {
        const style = webview.getStyle()
        if (style && style.titleNView) {
          webview.setStyle({
            titleNView: {
              loading: false,
            } as TitleNView,
          } as PlusWebviewWebviewTitleNViewStyles)
        }
        executor.resolve()
      } else {
        executor.reject()
      }
    }
  ) as HideNavigationBarLoading