ResponsiveData.fromMediaQuery constructor

ResponsiveData.fromMediaQuery(
  1. MediaQueryData? media,
  2. ScalifyConfig cfg
)

Implementation

factory ResponsiveData.fromMediaQuery(
    MediaQueryData? media, ScalifyConfig cfg) {
  if (media == null) return ResponsiveData.identity;

  final width = media.size.width;
  final height = media.size.height;

  if (width == 0 || height == 0) return ResponsiveData.identity;

  // ScreenType Logic
  ScreenType type;
  if (width < cfg.mobileBreakpoint) {
    type =
        (width < cfg.watchBreakpoint) ? ScreenType.watch : ScreenType.mobile;
  } else if (width < cfg.smallDesktopBreakpoint) {
    type = (width < cfg.tabletBreakpoint)
        ? ScreenType.tablet
        : ScreenType.smallDesktop;
  } else {
    type = (width < cfg.desktopBreakpoint)
        ? ScreenType.desktop
        : ScreenType.largeDesktop;
  }

  // Scale Logic
  double calculatedScaleWidth = width / cfg.designWidth;
  if (width > cfg.memoryProtectionThreshold) {
    final thresholdScale = cfg.memoryProtectionThreshold / cfg.designWidth;
    final excessWidth = width - cfg.memoryProtectionThreshold;
    calculatedScaleWidth = thresholdScale +
        ((excessWidth / cfg.designWidth) * cfg.highResScaleFactor);
  }

  final double calculatedScaleHeight = height / cfg.designHeight;

  final double finalScaleWidth =
      calculatedScaleWidth.clamp(cfg.minScale, cfg.maxScale);
  final double finalScaleHeight =
      calculatedScaleHeight.clamp(cfg.minScale, cfg.maxScale);
  final double finalCombined = finalScaleWidth;

  // FIX: Use textScaler instead of deprecated textScaleFactor
  // .scale(1.0) returns the raw scaling factor equivalent to the old property
  final double systemTextScaleFactor = media.textScaler.scale(1.0);

  return ResponsiveData._(
    size: Size(width, height),
    textScaleFactor: systemTextScaleFactor,
    screenType: type,
    config: cfg,
    scaleWidth: finalScaleWidth,
    scaleHeight: finalScaleHeight,
    scaleFactor: finalCombined,
    // Computing IDs (x1000 for precision retention up to 3 decimals)
    scaleWidthId: (finalScaleWidth * 1000).round(),
    scaleHeightId: (finalScaleHeight * 1000).round(),
    scaleFactorId: (finalCombined * 1000).round(),
    textScaleFactorId: (systemTextScaleFactor * 100).round(),
  );
}