Repository: flutter/flutter
File: packages/flutter/lib/src/painting/text_scaler.dart (Dart)

Code before:

  @override
  String toString() => textScaleFactor == 1.0 ? 'no scaling' : 'linear (${textScaleFactor}x)';
}

final class _ClampedTextScaler implements TextScaler {
  const _ClampedTextScaler(this.scaler, this.minScale, this.maxScale) : assert(maxScale > minScale);
  final TextScaler scaler;
  final double minScale;
  final double maxScale;

  @override
  double get textScaleFactor => clampDouble(scaler.textScaleFactor, minScale, maxScale);

  @override
  double scale(double fontSize) {
    assert(fontSize >= 0);
    assert(fontSize.isFinite);
    return clampDouble(scaler.scale(fontSize), minScale * fontSize, maxScale * fontSize);
  }

  @override
  TextScaler clamp({double minScaleFactor = 0, double maxScaleFactor = double.infinity}) {
    final double newMinScale = max(minScaleFactor, minScale);
    final double newMaxScale = min(maxScaleFactor, maxScale);
    return newMinScale == newMaxScale
        ? _LinearTextScaler(newMinScale)
        : _ClampedTextScaler(scaler, newMinScale, newMaxScale);
  }

  @override
  bool operator ==(Object other) {
    if (identical(this, other)) {
      return true;
    }
    return other is _ClampedTextScaler &&
        minScale == other.minScale &&
        maxScale == other.maxScale &&
        scaler == other.scaler;
  }

  @override
  int get hashCode => Object.hash(scaler, minScale, maxScale);

  @override
  String toString() => '$scaler clamped [$minScale, $maxScale]';
}


Review comment (suggestion):
Instead of merging the outer and inner bounds, we should use the provided bounds (`minScaleFactor` and `maxScaleFactor`) to clamp and create a new class instance.

Code after:

  @override
  String toString() => textScaleFactor == 1.0 ? 'no scaling' : 'linear (${textScaleFactor}x)';
}

final class _ClampedTextScaler implements TextScaler {
  const _ClampedTextScaler(this.scaler, this.minScale, this.maxScale) : assert(maxScale > minScale);
  final TextScaler scaler;
  final double minScale;
  final double maxScale;

  @override
  double get textScaleFactor => clampDouble(scaler.textScaleFactor, minScale, maxScale);

  @override
  double scale(double fontSize) {
    assert(fontSize >= 0);
    assert(fontSize.isFinite);
    return clampDouble(scaler.scale(fontSize), minScale * fontSize, maxScale * fontSize);
  }

  @override
  TextScaler clamp({double minScaleFactor = 0, double maxScaleFactor = double.infinity}) {
    assert(maxScaleFactor >= minScaleFactor);
    assert(!maxScaleFactor.isNaN);
    assert(minScaleFactor.isFinite);
    assert(minScaleFactor >= 0);

    final double newMinScale = max(minScale, minScaleFactor);
    final double newMaxScale = min(maxScale, maxScaleFactor);

    if (newMaxScale <= newMinScale) {
      // Ranges don't overlap or collapse to a single point.
      return TextScaler.linear(clampDouble(minScale, minScaleFactor, maxScaleFactor));
    }
    return _ClampedTextScaler(scaler, newMinScale, newMaxScale);
  }

  @override
  bool operator ==(Object other) {
    if (identical(this, other)) {
      return true;
    }
    return other is _ClampedTextScaler &&
        minScale == other.minScale &&
        maxScale == other.maxScale &&
        scaler == other.scaler;
  }

  @override
  int get hashCode => Object.hash(scaler, minScale, maxScale);