All files / utils responsiveLayout.ts

100% Statements 49/49
97.95% Branches 48/49
100% Functions 13/13
100% Lines 49/49

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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347                          12x                                         11x                                             8x   4x 4x   1x     3x                                         8x                                         7x 7x   7x   3x     4x   3x       1x                           4x 2x     2x 1x     1x                             8x   8x   1x   1x     6x                               9x   5x 5x             4x 9x                                 8x 6x   3x     3x       2x                                 12x                                                 885x 54x   831x 637x 637x   194x 5x 5x   189x                                     514x                               321x                               90x                                     245x    
/**
 * Responsive Layout Utility Functions
 * 
 * Helper functions for responsive layout calculations and touch target validation
 * 
 * @module utils/responsiveLayout
 * @category Mobile UI
 * @korean 반응형레이아웃유틸
 */
 
/**
 * Minimum touch target size according to iOS Human Interface Guidelines
 */
export const MIN_TOUCH_TARGET_SIZE = 44;
 
/**
 * Validate if an element meets minimum touch target requirements
 * 
 * @param width - Element width in pixels
 * @param height - Element height in pixels
 * @param minSize - Minimum size (default: 44px iOS guideline)
 * @returns True if element meets minimum requirements
 * 
 * @example
 * ```typescript
 * const isValid = isValidTouchTarget(50, 50); // true
 * const isTooSmall = isValidTouchTarget(30, 30); // false
 * ```
 */
export function isValidTouchTarget(
  width: number,
  height: number,
  minSize: number = MIN_TOUCH_TARGET_SIZE
): boolean {
  return width >= minSize && height >= minSize;
}
 
/**
 * Calculate optimal font size for given viewport width
 * Ensures minimum readable font sizes on mobile
 * 
 * @param viewportWidth - Viewport width in pixels
 * @param baseSize - Base font size for desktop (default: 16)
 * @param minSize - Minimum font size for mobile (default: 14)
 * @returns Calculated font size in pixels
 * 
 * @example
 * ```typescript
 * const fontSize = calculateFontSize(375, 16, 14); // 14px for mobile
 * const fontSize = calculateFontSize(1920, 16, 14); // 16px for desktop
 * ```
 */
export function calculateFontSize(
  viewportWidth: number,
  baseSize: number = 16,
  minSize: number = 14
): number {
  if (viewportWidth < 768) {
    // Mobile: use minimum size or scale down slightly
    return Math.max(minSize, Math.floor(baseSize * 0.875));
  } else if (viewportWidth < 1024) {
    // Tablet: scale proportionally
    return Math.max(minSize, Math.floor(baseSize * 0.9375));
  }
  // Desktop: use base size
  return baseSize;
}
 
/**
 * Calculate element position within safe area
 * Ensures elements don't overlap notch or home indicator
 * 
 * @param value - Position value in pixels
 * @param safeAreaInset - Safe area inset for that edge
 * @returns Adjusted position value
 * 
 * @example
 * ```typescript
 * const top = calculateSafePosition(10, 44); // 54px (10 + 44)
 * const bottom = calculateSafePosition(20, 34); // 54px (20 + 34)
 * ```
 */
export function calculateSafePosition(
  value: number,
  safeAreaInset: number
): number {
  return value + safeAreaInset;
}
 
/**
 * Calculate optimal HUD height for given viewport
 * Scales based on device type and orientation
 * 
 * @param viewportWidth - Viewport width in pixels
 * @param isLandscape - Whether in landscape orientation
 * @returns HUD height in pixels
 * 
 * @example
 * ```typescript
 * const hudHeight = calculateHUDHeight(375, false); // ~80px for mobile portrait
 * const hudHeight = calculateHUDHeight(667, true); // ~60px for mobile landscape
 * ```
 */
export function calculateHUDHeight(
  viewportWidth: number,
  isLandscape: boolean
): number {
  const isMobile = viewportWidth < 768;
  const isSmallMobile = viewportWidth <= 375; // Changed to <= to match 375px iPhone SE
 
  if (isLandscape && isMobile) {
    // Landscape: minimize HUD to maximize gameplay area
    return isSmallMobile ? 60 : 70;
  }
 
  if (isMobile) {
    // Portrait mobile: compact but readable
    return isSmallMobile ? 80 : 95;
  }
 
  // Desktop/tablet: larger HUD with more information
  return 120;
}
 
/**
 * Calculate optimal control bar height for mobile
 * 
 * @param isMobile - Whether on mobile device
 * @param isLandscape - Whether in landscape orientation
 * @returns Control bar height in pixels
 */
export function calculateControlsHeight(
  isMobile: boolean,
  isLandscape: boolean
): number {
  if (!isMobile) {
    return 0; // Desktop uses keyboard
  }
 
  if (isLandscape) {
    return 100; // Minimal controls in landscape
  }
 
  return 130; // Full touch controls in portrait
}
 
/**
 * Calculate spacing between HUD elements
 * Ensures proper touch targets and visual hierarchy
 * 
 * @param isMobile - Whether on mobile device
 * @param density - Spacing density ('compact' | 'normal' | 'spacious')
 * @returns Spacing value in pixels
 */
export function calculateSpacing(
  isMobile: boolean,
  density: 'compact' | 'normal' | 'spacious' = 'normal'
): number {
  const baseSpacing = isMobile ? 8 : 12;
 
  switch (density) {
    case 'compact':
      return Math.floor(baseSpacing * 0.75);
    case 'spacious':
      return Math.ceil(baseSpacing * 1.5);
    case 'normal':
    default:
      return baseSpacing;
  }
}
 
/**
 * Calculate optimal progress bar dimensions
 * Ensures bars are touch-friendly on mobile
 * 
 * @param isMobile - Whether on mobile device
 * @param type - Bar type ('health' | 'ki' | 'stamina')
 * @returns Bar dimensions { width, height }
 */
export function calculateProgressBarSize(
  isMobile: boolean,
  type: 'health' | 'ki' | 'stamina'
): { width: number; height: number } {
  if (isMobile) {
    // Mobile: touch-friendly sizes
    const height = type === 'health' ? 48 : 40; // Health bar larger
    return {
      width: 160,
      height,
    };
  }
 
  // Desktop: more detailed display
  const height = type === 'health' ? 55 : 45;
  return {
    width: 220,
    height,
  };
}
 
/**
 * Get optimal grid layout for trigram stance selector
 * 
 * @param isMobile - Whether on mobile device
 * @param isLandscape - Whether in landscape orientation
 * @returns Grid configuration { columns, rows, gap }
 */
export function getStanceSelectorLayout(
  isMobile: boolean,
  isLandscape: boolean
): { columns: number; rows: number; gap: number } {
  if (isMobile) {
    if (isLandscape) {
      // Landscape: horizontal layout
      return { columns: 4, rows: 2, gap: 8 };
    }
    // Portrait: compact grid
    return { columns: 4, rows: 2, gap: 10 };
  }
 
  // Desktop: spacious layout
  return { columns: 4, rows: 2, gap: 15 };
}
 
/**
 * Resolution breakpoints for responsive design
 * @korean 반응형 중단점
 */
export interface ResolutionBreakpoints {
  readonly mobile: number;    // 768px
  readonly tablet: number;    // 1280px
  readonly desktop: number;   // 1920px
  readonly ultrawide: number; // 2560px
}
 
/**
 * Standard breakpoints for responsive sizing
 */
export const BREAKPOINTS: ResolutionBreakpoints = Object.freeze({
  mobile: 768,
  tablet: 1280,
  desktop: 1920,
  ultrawide: 2560,
});
 
/**
 * Get responsive size based on screen width
 * Scales linearly between breakpoints
 * 
 * @param width - Screen width in pixels
 * @param sizes - Size values at each breakpoint
 * @returns Interpolated size value
 * 
 * @example
 * ```typescript
 * const fontSize = getResponsiveSize(1024, { mobile: 12, tablet: 14, desktop: 16 });
 * // Returns ~13.3 (interpolated between mobile and tablet)
 * ```
 */
export function getResponsiveSize(
  width: number,
  sizes: { mobile: number; tablet: number; desktop: number }
): number {
  if (width < BREAKPOINTS.mobile) {
    return sizes.mobile;
  }
  if (width < BREAKPOINTS.tablet) {
    const ratio = (width - BREAKPOINTS.mobile) / (BREAKPOINTS.tablet - BREAKPOINTS.mobile);
    return sizes.mobile + (sizes.tablet - sizes.mobile) * ratio;
  }
  if (width < BREAKPOINTS.desktop) {
    const ratio = (width - BREAKPOINTS.tablet) / (BREAKPOINTS.desktop - BREAKPOINTS.tablet);
    return sizes.tablet + (sizes.desktop - sizes.tablet) * ratio;
  }
  return sizes.desktop;
}
 
/**
 * Calculate HUD height as percentage of screen height
 * Ensures minimum and maximum bounds for usability
 * 
 * @param height - Screen height in pixels
 * @param percentage - Target percentage (0.0 - 1.0)
 * @returns Calculated HUD height with bounds applied
 * 
 * @example
 * ```typescript
 * const hudHeight = getHUDHeight(1080, 0.08); // ~86px (8% of 1080)
 * const tooSmall = getHUDHeight(400, 0.08);   // 40px (minimum applied)
 * const tooLarge = getHUDHeight(3000, 0.08);  // 120px (maximum applied)
 * ```
 */
export function getHUDHeight(height: number, percentage: number): number {
  return Math.max(40, Math.min(120, height * percentage));
}
 
/**
 * Calculate padding based on resolution
 * 
 * @param width - Screen width in pixels
 * @returns Padding value in pixels
 * 
 * @example
 * ```typescript
 * const padding = getResponsivePadding(375);  // 8px (mobile)
 * const padding = getResponsivePadding(1920); // 16px (desktop)
 * ```
 */
export function getResponsivePadding(width: number): number {
  return getResponsiveSize(width, { mobile: 8, tablet: 12, desktop: 16 });
}
 
/**
 * Calculate font size based on resolution
 * 
 * @param width - Screen width in pixels
 * @returns Font size in pixels
 * 
 * @example
 * ```typescript
 * const fontSize = getResponsiveFontSize(375);  // 12px (mobile)
 * const fontSize = getResponsiveFontSize(1920); // 16px (desktop)
 * ```
 */
export function getResponsiveFontSize(width: number): number {
  return getResponsiveSize(width, { mobile: 12, tablet: 14, desktop: 16 });
}
 
/**
 * Determine if mobile controls should be shown
 * NOTE: This is the ONLY valid use of isMobile prop
 * 
 * @param width - Screen width in pixels
 * @param isMobile - Optional mobile device flag
 * @returns Whether to show mobile controls
 * 
 * @example
 * ```typescript
 * const showControls = shouldShowMobileControls(500, false); // true (narrow screen)
 * const showControls = shouldShowMobileControls(1920, false); // false (wide screen)
 * const showControls = shouldShowMobileControls(1920, true); // true (mobile device)
 * ```
 */
export function shouldShowMobileControls(width: number, isMobile: boolean = false): boolean {
  return isMobile || width < BREAKPOINTS.mobile;
}