Files
MiniProgram/DailyFortuneGuide/utils/safeArea.js
2025-11-09 18:41:07 +08:00

53 lines
1.4 KiB
JavaScript

const DEFAULT_PADDING = 64;
const EXTRA_SPACING = 12;
function getMenuPadding(extraSpacing) {
if (typeof tt === "undefined" || typeof tt.getMenuButtonBoundingClientRect !== "function") {
return 0;
}
try {
const rect = tt.getMenuButtonBoundingClientRect();
if (!rect) {
return 0;
}
if (typeof rect.bottom === "number") {
return rect.bottom + extraSpacing;
}
if (typeof rect.top === "number" && typeof rect.height === "number") {
return rect.top + rect.height + extraSpacing;
}
} catch (error) {
console.warn("safe-area: menu rect unavailable", error);
}
return 0;
}
function getStatusBarPadding(extraSpacing) {
if (typeof tt === "undefined" || typeof tt.getSystemInfoSync !== "function") {
return 0;
}
try {
const info = tt.getSystemInfoSync();
return (info && info.statusBarHeight ? info.statusBarHeight : 0) + extraSpacing;
} catch (error) {
console.warn("safe-area: system info unavailable", error);
}
return 0;
}
function getPageSafeTop(extraSpacing = EXTRA_SPACING, fallback = DEFAULT_PADDING) {
const menuPadding = getMenuPadding(extraSpacing);
if (menuPadding) {
return Math.round(menuPadding);
}
const statusPadding = getStatusBarPadding(extraSpacing);
if (statusPadding) {
return Math.round(statusPadding);
}
return fallback;
}
module.exports = {
getPageSafeTop
};