Skip to content

Commit

Permalink
migrate part of painting to nullsafety (flutter#62696)
Browse files Browse the repository at this point in the history
* migrate part of painting to nullsafety

* address review comments

* address review comment
  • Loading branch information
a14n committed Aug 4, 2020
1 parent fbd6dd6 commit 4518a72
Show file tree
Hide file tree
Showing 8 changed files with 157 additions and 174 deletions.
43 changes: 20 additions & 23 deletions packages/flutter/lib/src/painting/alignment.dart
Expand Up @@ -2,7 +2,6 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// @dart = 2.8

import 'dart:ui' as ui show lerpDouble;

Expand Down Expand Up @@ -88,22 +87,22 @@ abstract class AlignmentGeometry {
/// into a concrete [Alignment] using [resolve].
///
/// {@macro dart.ui.shadow.lerp}
static AlignmentGeometry lerp(AlignmentGeometry a, AlignmentGeometry b, double t) {
static AlignmentGeometry? lerp(AlignmentGeometry? a, AlignmentGeometry? b, double t) {
assert(t != null);
if (a == null && b == null)
return null;
if (a == null)
return b * t;
return b! * t;
if (b == null)
return a * (1.0 - t);
if (a is Alignment && b is Alignment)
return Alignment.lerp(a, b, t);
if (a is AlignmentDirectional && b is AlignmentDirectional)
return AlignmentDirectional.lerp(a, b, t);
return _MixedAlignment(
ui.lerpDouble(a._x, b._x, t),
ui.lerpDouble(a._start, b._start, t),
ui.lerpDouble(a._y, b._y, t),
ui.lerpDouble(a._x, b._x, t)!,
ui.lerpDouble(a._start, b._start, t)!,
ui.lerpDouble(a._y, b._y, t)!,
);
}

Expand All @@ -116,7 +115,7 @@ abstract class AlignmentGeometry {
/// * [Alignment], for which this is a no-op (returns itself).
/// * [AlignmentDirectional], which flips the horizontal direction
/// based on the `direction` argument.
Alignment resolve(TextDirection direction);
Alignment resolve(TextDirection? direction);

@override
String toString() {
Expand Down Expand Up @@ -333,19 +332,19 @@ class Alignment extends AlignmentGeometry {
/// If either is null, this function interpolates from [Alignment.center].
///
/// {@macro dart.ui.shadow.lerp}
static Alignment lerp(Alignment a, Alignment b, double t) {
static Alignment? lerp(Alignment? a, Alignment? b, double t) {
assert(t != null);
if (a == null && b == null)
return null;
if (a == null)
return Alignment(ui.lerpDouble(0.0, b.x, t), ui.lerpDouble(0.0, b.y, t));
return Alignment(ui.lerpDouble(0.0, b!.x, t)!, ui.lerpDouble(0.0, b.y, t)!);
if (b == null)
return Alignment(ui.lerpDouble(a.x, 0.0, t), ui.lerpDouble(a.y, 0.0, t));
return Alignment(ui.lerpDouble(a.x, b.x, t), ui.lerpDouble(a.y, b.y, t));
return Alignment(ui.lerpDouble(a.x, 0.0, t)!, ui.lerpDouble(a.y, 0.0, t)!);
return Alignment(ui.lerpDouble(a.x, b.x, t)!, ui.lerpDouble(a.y, b.y, t)!);
}

@override
Alignment resolve(TextDirection direction) => this;
Alignment resolve(TextDirection? direction) => this;

static String _stringify(double x, double y) {
if (x == -1.0 && y == -1.0)
Expand Down Expand Up @@ -514,27 +513,26 @@ class AlignmentDirectional extends AlignmentGeometry {
/// If either is null, this function interpolates from [AlignmentDirectional.center].
///
/// {@macro dart.ui.shadow.lerp}
static AlignmentDirectional lerp(AlignmentDirectional a, AlignmentDirectional b, double t) {
static AlignmentDirectional? lerp(AlignmentDirectional? a, AlignmentDirectional? b, double t) {
assert(t != null);
if (a == null && b == null)
return null;
if (a == null)
return AlignmentDirectional(ui.lerpDouble(0.0, b.start, t), ui.lerpDouble(0.0, b.y, t));
return AlignmentDirectional(ui.lerpDouble(0.0, b!.start, t)!, ui.lerpDouble(0.0, b.y, t)!);
if (b == null)
return AlignmentDirectional(ui.lerpDouble(a.start, 0.0, t), ui.lerpDouble(a.y, 0.0, t));
return AlignmentDirectional(ui.lerpDouble(a.start, b.start, t), ui.lerpDouble(a.y, b.y, t));
return AlignmentDirectional(ui.lerpDouble(a.start, 0.0, t)!, ui.lerpDouble(a.y, 0.0, t)!);
return AlignmentDirectional(ui.lerpDouble(a.start, b.start, t)!, ui.lerpDouble(a.y, b.y, t)!);
}

@override
Alignment resolve(TextDirection direction) {
Alignment resolve(TextDirection? direction) {
assert(direction != null, 'Cannot resolve $runtimeType without a TextDirection.');
switch (direction) {
switch (direction!) {
case TextDirection.rtl:
return Alignment(-start, y);
case TextDirection.ltr:
return Alignment(start, y);
}
return null;
}

static String _stringify(double start, double y) {
Expand Down Expand Up @@ -622,15 +620,14 @@ class _MixedAlignment extends AlignmentGeometry {
}

@override
Alignment resolve(TextDirection direction) {
Alignment resolve(TextDirection? direction) {
assert(direction != null, 'Cannot resolve $runtimeType without a TextDirection.');
switch (direction) {
switch (direction!) {
case TextDirection.rtl:
return Alignment(_x - _start, _y);
case TextDirection.ltr:
return Alignment(_x + _start, _y);
}
return null;
}
}

Expand All @@ -652,7 +649,7 @@ class _MixedAlignment extends AlignmentGeometry {
class TextAlignVertical {
/// Creates a TextAlignVertical from any y value between -1.0 and 1.0.
const TextAlignVertical({
@required this.y,
required this.y,
}) : assert(y != null),
assert(y >= -1.0 && y <= 1.0);

Expand Down
6 changes: 0 additions & 6 deletions packages/flutter/lib/src/painting/basic_types.dart
Expand Up @@ -2,7 +2,6 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// @dart = 2.8

import 'dart:ui' show TextDirection;

Expand Down Expand Up @@ -143,7 +142,6 @@ Axis flipAxis(Axis direction) {
case Axis.vertical:
return Axis.horizontal;
}
return null;
}

/// A direction in which boxes flow vertically.
Expand Down Expand Up @@ -214,7 +212,6 @@ Axis axisDirectionToAxis(AxisDirection axisDirection) {
case AxisDirection.right:
return Axis.horizontal;
}
return null;
}

/// Returns the [AxisDirection] in which reading occurs in the given [TextDirection].
Expand All @@ -229,7 +226,6 @@ AxisDirection textDirectionToAxisDirection(TextDirection textDirection) {
case TextDirection.ltr:
return AxisDirection.right;
}
return null;
}

/// Returns the opposite of the given [AxisDirection].
Expand All @@ -253,7 +249,6 @@ AxisDirection flipAxisDirection(AxisDirection axisDirection) {
case AxisDirection.left:
return AxisDirection.right;
}
return null;
}

/// Returns whether traveling along the given axis direction visits coordinates
Expand All @@ -271,5 +266,4 @@ bool axisDirectionIsReversed(AxisDirection axisDirection) {
case AxisDirection.right:
return false;
}
return null;
}
25 changes: 11 additions & 14 deletions packages/flutter/lib/src/painting/binding.dart
Expand Up @@ -2,7 +2,6 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// @dart = 2.8

import 'dart:typed_data' show Uint8List;
import 'dart:ui' as ui show instantiateImageCodec, Codec;
Expand All @@ -23,14 +22,12 @@ mixin PaintingBinding on BindingBase, ServicesBinding {
super.initInstances();
_instance = this;
_imageCache = createImageCache();
if (shaderWarmUp != null) {
shaderWarmUp.execute();
}
shaderWarmUp?.execute();
}

/// The current [PaintingBinding], if one has been created.
static PaintingBinding get instance => _instance;
static PaintingBinding _instance;
static PaintingBinding? get instance => _instance;
static PaintingBinding? _instance;

/// [ShaderWarmUp] to be executed during [initInstances].
///
Expand All @@ -53,7 +50,7 @@ mixin PaintingBinding on BindingBase, ServicesBinding {
/// See also:
///
/// * [ShaderWarmUp], the interface of how this warm up works.
static ShaderWarmUp shaderWarmUp = const DefaultShaderWarmUp();
static ShaderWarmUp? shaderWarmUp = const DefaultShaderWarmUp();

/// The singleton that implements the Flutter framework's image cache.
///
Expand All @@ -62,8 +59,8 @@ mixin PaintingBinding on BindingBase, ServicesBinding {
///
/// The image cache is created during startup by the [createImageCache]
/// method.
ImageCache get imageCache => _imageCache;
ImageCache _imageCache;
ImageCache? get imageCache => _imageCache;
ImageCache? _imageCache;

/// Creates the [ImageCache] singleton (accessible via [imageCache]).
///
Expand All @@ -90,8 +87,8 @@ mixin PaintingBinding on BindingBase, ServicesBinding {
/// above its native resolution should prefer scaling the canvas the image is
/// drawn into.
Future<ui.Codec> instantiateImageCodec(Uint8List bytes, {
int cacheWidth,
int cacheHeight,
int? cacheWidth,
int? cacheHeight,
bool allowUpscaling = false,
}) {
assert(cacheWidth == null || cacheWidth > 0);
Expand All @@ -108,8 +105,8 @@ mixin PaintingBinding on BindingBase, ServicesBinding {
@override
void evict(String asset) {
super.evict(asset);
imageCache.clear();
imageCache.clearLiveImages();
imageCache!.clear();
imageCache!.clearLiveImages();
}

@override
Expand Down Expand Up @@ -170,4 +167,4 @@ class _SystemFontsNotifier extends Listenable {
///
/// The image cache is created during startup by the [PaintingBinding]'s
/// [PaintingBinding.createImageCache] method.
ImageCache get imageCache => PaintingBinding.instance.imageCache;
ImageCache? get imageCache => PaintingBinding.instance!.imageCache;
45 changes: 22 additions & 23 deletions packages/flutter/lib/src/painting/colors.dart
Expand Up @@ -2,15 +2,14 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// @dart = 2.8

import 'dart:math' as math;
import 'dart:ui' show Color, lerpDouble, hashValues;

import 'package:flutter/foundation.dart';

double _getHue(double red, double green, double blue, double max, double delta) {
double hue;
late double hue;
if (max == 0.0) {
hue = 0.0;
} else if (max == red) {
Expand Down Expand Up @@ -199,19 +198,19 @@ class HSVColor {
/// {@macro dart.ui.shadow.lerp}
///
/// Values outside of the valid range for each channel will be clamped.
static HSVColor lerp(HSVColor a, HSVColor b, double t) {
static HSVColor? lerp(HSVColor? a, HSVColor? b, double t) {
assert(t != null);
if (a == null && b == null)
return null;
if (a == null)
return b._scaleAlpha(t);
return b!._scaleAlpha(t);
if (b == null)
return a._scaleAlpha(1.0 - t);
return HSVColor.fromAHSV(
lerpDouble(a.alpha, b.alpha, t).clamp(0.0, 1.0) as double,
lerpDouble(a.hue, b.hue, t) % 360.0,
lerpDouble(a.saturation, b.saturation, t).clamp(0.0, 1.0) as double,
lerpDouble(a.value, b.value, t).clamp(0.0, 1.0) as double,
lerpDouble(a.alpha, b.alpha, t)!.clamp(0.0, 1.0) as double,
lerpDouble(a.hue, b.hue, t)! % 360.0,
lerpDouble(a.saturation, b.saturation, t)!.clamp(0.0, 1.0) as double,
lerpDouble(a.value, b.value, t)!.clamp(0.0, 1.0) as double,
);
}

Expand Down Expand Up @@ -383,19 +382,19 @@ class HSLColor {
///
/// Values for `t` are usually obtained from an [Animation<double>], such as
/// an [AnimationController].
static HSLColor lerp(HSLColor a, HSLColor b, double t) {
static HSLColor? lerp(HSLColor? a, HSLColor? b, double t) {
assert(t != null);
if (a == null && b == null)
return null;
if (a == null)
return b._scaleAlpha(t);
return b!._scaleAlpha(t);
if (b == null)
return a._scaleAlpha(1.0 - t);
return HSLColor.fromAHSL(
lerpDouble(a.alpha, b.alpha, t).clamp(0.0, 1.0) as double,
lerpDouble(a.hue, b.hue, t) % 360.0,
lerpDouble(a.saturation, b.saturation, t).clamp(0.0, 1.0) as double,
lerpDouble(a.lightness, b.lightness, t).clamp(0.0, 1.0) as double,
lerpDouble(a.alpha, b.alpha, t)!.clamp(0.0, 1.0) as double,
lerpDouble(a.hue, b.hue, t)! % 360.0,
lerpDouble(a.saturation, b.saturation, t)!.clamp(0.0, 1.0) as double,
lerpDouble(a.lightness, b.lightness, t)!.clamp(0.0, 1.0) as double,
);
}

Expand Down Expand Up @@ -441,7 +440,7 @@ class ColorSwatch<T> extends Color {
final Map<T, Color> _swatch;

/// Returns an element of the swatch table.
Color operator [](T index) => _swatch[index];
Color? operator [](T index) => _swatch[index];

@override
bool operator ==(Object other) {
Expand All @@ -468,9 +467,9 @@ class ColorProperty extends DiagnosticsProperty<Color> {
/// The [showName], [style], and [level] arguments must not be null.
ColorProperty(
String name,
Color value, {
Color? value, {
bool showName = true,
Object defaultValue = kNoDefaultValue,
Object? defaultValue = kNoDefaultValue,
DiagnosticsTreeStyle style = DiagnosticsTreeStyle.singleLine,
DiagnosticLevel level = DiagnosticLevel.info,
}) : assert(showName != null),
Expand All @@ -484,14 +483,14 @@ class ColorProperty extends DiagnosticsProperty<Color> {
);

@override
Map<String, Object> toJsonMap(DiagnosticsSerializationDelegate delegate) {
final Map<String, Object> json = super.toJsonMap(delegate);
Map<String, Object?> toJsonMap(DiagnosticsSerializationDelegate delegate) {
final Map<String, Object?> json = super.toJsonMap(delegate);
if (value != null) {
json['valueProperties'] = <String, Object>{
'red': value.red,
'green': value.green,
'blue': value.blue,
'alpha': value.alpha,
'red': value!.red,
'green': value!.green,
'blue': value!.blue,
'alpha': value!.alpha,
};
}
return json;
Expand Down

0 comments on commit 4518a72

Please sign in to comment.