All files / utils screenHelper.ts

100% Statements 8/8
100% Branches 0/0
100% Functions 2/2
100% Lines 8/8

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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      4x   4x           3x                                   6x   6x 6x   6x   6x            
import { Dimensions, Platform, StatusBar } from 'react-native';
import { EdgeInsets } from 'react-native-safe-area-context';
 
const { width: SCREEN_WIDTH, height: SCREEN_HEIGHT } = Dimensions.get('window');
 
export const ScreenHelper = {
  width: SCREEN_WIDTH,
  height: SCREEN_HEIGHT,
 
  // 获取安全区域布局
  getSafeAreaLayout: (insets: EdgeInsets) => {
    return {
      top: insets.top,
      bottom: insets.bottom,
      left: insets.left,
      right: insets.right,
      usableHeight: SCREEN_HEIGHT - insets.top - insets.bottom,
      usableWidth: SCREEN_WIDTH - insets.left - insets.right,
    };
  },
 
  // 限制坐标在屏幕范围内
  clampPosition: (
    x: number,
    y: number,
    itemWidth: number,
    itemHeight: number,
    insets: EdgeInsets
  ) => {
    const minX = insets.left;
    // Ensure maxX is at least minX if item is wider than screen
    const maxX = Math.max(minX, SCREEN_WIDTH - itemWidth - insets.right);
    const minY = insets.top;
    // Ensure maxY is at least minY if item is taller than screen
    const maxY = Math.max(minY, SCREEN_HEIGHT - itemHeight - insets.bottom);
 
    return {
      x: Math.min(Math.max(x, minX), maxX),
      y: Math.min(Math.max(y, minY), maxY),
    };
  },
};