每日运势小程序
This commit is contained in:
52
DailyFortuneGuide/utils/safeArea.js
Normal file
52
DailyFortuneGuide/utils/safeArea.js
Normal file
@@ -0,0 +1,52 @@
|
||||
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
|
||||
};
|
||||
Reference in New Issue
Block a user