55 lines
1.5 KiB
TypeScript
55 lines
1.5 KiB
TypeScript
/**
|
|
* System info utilities — replaces deprecated uni.getSystemInfoSync()
|
|
* with the recommended granular APIs.
|
|
*/
|
|
|
|
interface SystemLayout {
|
|
/** Status bar height in px */
|
|
readonly statusBarHeight: number
|
|
/** Window width in px */
|
|
readonly windowWidth: number
|
|
/** Custom nav bar height in px (status bar + title bar) */
|
|
readonly navBarHeight: number
|
|
}
|
|
|
|
let cached: SystemLayout | null = null
|
|
|
|
/**
|
|
* Returns layout dimensions using the new granular APIs.
|
|
* Falls back to getSystemInfoSync only if the new APIs are unavailable.
|
|
* Results are cached since these values never change during a session.
|
|
*/
|
|
export function getSystemLayout(): SystemLayout {
|
|
if (cached) return cached
|
|
|
|
let statusBarHeight = 20
|
|
let windowWidth = 375
|
|
|
|
try {
|
|
// New recommended APIs (WeChat base lib >= 2.25.3)
|
|
const windowInfo = uni.getWindowInfo()
|
|
const deviceInfo = uni.getDeviceInfo()
|
|
|
|
statusBarHeight = windowInfo.statusBarHeight ?? 20
|
|
windowWidth = windowInfo.windowWidth ?? 375
|
|
|
|
// Silence unused var — deviceInfo is here for future use
|
|
void deviceInfo
|
|
} catch {
|
|
// Fallback for older base lib versions
|
|
try {
|
|
const sysInfo = uni.getSystemInfoSync()
|
|
statusBarHeight = sysInfo.statusBarHeight ?? 20
|
|
windowWidth = sysInfo.windowWidth ?? 375
|
|
} catch {
|
|
// Use defaults
|
|
}
|
|
}
|
|
|
|
const navTitlePx = 88 * (windowWidth / 750)
|
|
const navBarHeight = Math.round(statusBarHeight + navTitlePx)
|
|
|
|
cached = { statusBarHeight, windowWidth, navBarHeight }
|
|
return cached
|
|
}
|