| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- import { buildSafeAreaCssVarsStyle } from './safeAreaCss.js'
- export { buildSafeAreaCssVarsStyle }
- const DEFAULT_NAV_CONTENT_HEIGHT = 44
- const DEFAULT_GAP = 8
- const DEFAULT_HORIZONTAL_PADDING = 8
- function getSystemInfo() {
- // 微信基础库 3.x 起 getSystemInfoSync 已弃用,优先拆分 API
- try {
- if (typeof uni.getWindowInfo === 'function') {
- const win = uni.getWindowInfo() || {}
- return {
- statusBarHeight: win.statusBarHeight,
- windowWidth: win.windowWidth
- }
- }
- } catch (_) {
- /* fall through */
- }
- try {
- return uni.getSystemInfoSync() || {}
- } catch (_) {
- return {}
- }
- }
- function getMenuButtonRect() {
- try {
- const rect = uni.getMenuButtonBoundingClientRect?.()
- return rect && rect.width ? rect : null
- } catch (error) {
- return null
- }
- }
- export function computeSafeAreaMetrics() {
- const systemInfo = getSystemInfo()
- const menuRect = getMenuButtonRect()
- const statusBarHeight = Number(systemInfo.statusBarHeight || 20)
- const windowWidth = Number(systemInfo.windowWidth || 375)
- let navContentHeight = DEFAULT_NAV_CONTENT_HEIGHT
- let navTopGap = DEFAULT_GAP
- let capsuleWidth = 0
- let capsuleRightSafeWidth = 0
- if (menuRect) {
- navTopGap = Math.max(Number(menuRect.top || 0) - statusBarHeight, DEFAULT_GAP)
- navContentHeight = Math.max(Number(menuRect.height || 0) + navTopGap * 2, DEFAULT_NAV_CONTENT_HEIGHT)
- capsuleWidth = Number(menuRect.width || 0)
- const rightInset = Math.max(windowWidth - Number(menuRect.right || windowWidth), 0)
- capsuleRightSafeWidth = Math.max(capsuleWidth + rightInset + DEFAULT_HORIZONTAL_PADDING, 96)
- } else {
- // 无胶囊位(H5、无自定义导航栏等):不预留右侧占位;样式里已有 32rpx 水平内边距
- capsuleRightSafeWidth = 0
- }
- const topSafeHeight = statusBarHeight
- const headerHeight = topSafeHeight + navContentHeight
- return {
- statusBarHeight,
- topSafeHeight,
- navContentHeight,
- headerHeight,
- navTopGap,
- capsuleWidth,
- capsuleRightSafeWidth,
- }
- }
- export function applySafeAreaCssVars(metrics) {
- if (typeof document === 'undefined' || !document?.documentElement) return
- const root = document.documentElement.style
- const style = buildSafeAreaCssVarsStyle(metrics)
- Object.entries(style).forEach(([key, value]) => {
- root.setProperty(key, value)
- })
- }
- export function initGlobalSafeArea() {
- const metrics = computeSafeAreaMetrics()
- const app = getApp?.()
- if (app) {
- app.globalData = app.globalData || {}
- app.globalData.safeArea = metrics
- }
- applySafeAreaCssVars(metrics)
- return metrics
- }
- export function getGlobalSafeArea() {
- const app = getApp?.()
- return app?.globalData?.safeArea || computeSafeAreaMetrics()
- }
- export function getSafeAreaStyle() {
- return buildSafeAreaCssVarsStyle(getGlobalSafeArea())
- }
|