edge_insets.dart 5.32 KB
Newer Older
1 2 3 4
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
import 'dart:ui' as ui show lerpDouble, WindowPadding;
6 7

import 'basic_types.dart';
8 9 10 11 12

/// An immutable set of offsets in each of the four cardinal directions.
///
/// Typically used for an offset from each of the four sides of a box. For
/// example, the padding inside a box can be represented using this class.
13
class EdgeInsets {
14
  /// Creates insets from offsets from the left, top, right, and bottom.
15 16
  const EdgeInsets.fromLTRB(this.left, this.top, this.right, this.bottom);

17
  /// Creates insets where all the offsets are value.
18
  const EdgeInsets.all(double value)
19
      : left = value, top = value, right = value, bottom = value;
20

21
  /// Creates insets with only the given values non-zero.
22
  const EdgeInsets.only({
23
    this.left: 0.0,
24 25
    this.top: 0.0,
    this.right: 0.0,
26
    this.bottom: 0.0
27
  });
28

29
  /// Creates insets with symmetrical vertical and horizontal offsets.
30
  const EdgeInsets.symmetric({ double vertical: 0.0,
31
                             double horizontal: 0.0 })
32 33
    : left = horizontal, top = vertical, right = horizontal, bottom = vertical;

34
  /// Creates insets that match the given window padding.
35 36 37 38 39
  EdgeInsets.fromWindowPadding(ui.WindowPadding padding)
    : left = padding.left, top = padding.top, right = padding.right, bottom = padding.bottom;

  /// The offset from the left.
  final double left;
40 41 42 43 44 45 46 47 48 49 50

  /// The offset from the top.
  final double top;

  /// The offset from the right.
  final double right;

  /// The offset from the bottom.
  final double bottom;

  /// Whether every dimension is non-negative.
51
  bool get isNonNegative => left >= 0.0 && top >= 0.0 && right >= 0.0 && bottom >= 0.0;
52

Adam Barth's avatar
Adam Barth committed
53 54 55 56 57 58
  /// The total offset in the vertical direction.
  double get horizontal => left + right;

  /// The total offset in the horizontal direction.
  double get vertical => top + bottom;

59
  /// The size that this EdgeInsets would occupy with an empty interior.
60
  Size get collapsedSize => new Size(horizontal, vertical);
Adam Barth's avatar
Adam Barth committed
61

62
  /// An EdgeInsets with top and bottom as well as left and right flipped.
63
  EdgeInsets get flipped => new EdgeInsets.fromLTRB(left, top, right, bottom);
64

65 66 67 68 69
  /// Returns a new rect that is bigger than the given rect in each direction by
  /// the amount of inset in each direction. Specifically, the left edge of the
  /// rect is moved left by [left], the top edge of the rect is moved up by
  /// [top], the right edge of the rect is moved right by [right], and the
  /// bottom edge of the rect is moved down by [bottom].
70 71
  Rect inflateRect(Rect rect) {
    return new Rect.fromLTRB(rect.left - left, rect.top - top, rect.right + right, rect.bottom + bottom);
72 73
  }

74
  /// Returns the difference between two EdgeInsets.
75
  EdgeInsets operator -(EdgeInsets other) {
76 77
    return new EdgeInsets.fromLTRB(
      left - other.left,
78 79
      top - other.top,
      right - other.right,
80
      bottom - other.bottom
81 82 83
    );
  }

84
  /// Returns the sum of two EdgeInsets.
85
  EdgeInsets operator +(EdgeInsets other) {
86 87
    return new EdgeInsets.fromLTRB(
      left + other.left,
88 89
      top + other.top,
      right + other.right,
90
      bottom + other.bottom
91 92 93
    );
  }

94
  /// Scales the EdgeInsets in each dimension by the given factor.
95
  EdgeInsets operator *(double other) {
96 97
    return new EdgeInsets.fromLTRB(
      left * other,
98 99
      top * other,
      right * other,
100
      bottom * other
101 102 103
    );
  }

104
  /// Divides the EdgeInsets in each dimension by the given factor.
105
  EdgeInsets operator /(double other) {
106 107
    return new EdgeInsets.fromLTRB(
      left / other,
108 109
      top / other,
      right / other,
110
      bottom / other
111 112 113
    );
  }

114
  /// Integer divides the EdgeInsets in each dimension by the given factor.
115
  EdgeInsets operator ~/(double other) {
116 117
    return new EdgeInsets.fromLTRB(
      (left ~/ other).toDouble(),
118 119
      (top ~/ other).toDouble(),
      (right ~/ other).toDouble(),
120
      (bottom ~/ other).toDouble()
121 122 123
    );
  }

124
  /// Computes the remainder in each dimension by the given factor.
125
  EdgeInsets operator %(double other) {
126 127
    return new EdgeInsets.fromLTRB(
      left % other,
128 129
      top % other,
      right % other,
130
      bottom % other
131 132 133
    );
  }

134
  /// Linearly interpolate between two EdgeInsets.
135
  ///
136 137
  /// If either is null, this function interpolates from [EdgeInsets.zero].
  static EdgeInsets lerp(EdgeInsets a, EdgeInsets b, double t) {
138 139 140 141 142 143
    if (a == null && b == null)
      return null;
    if (a == null)
      return b * t;
    if (b == null)
      return a * (1.0 - t);
144 145
    return new EdgeInsets.fromLTRB(
      ui.lerpDouble(a.left, b.left, t),
146 147
      ui.lerpDouble(a.top, b.top, t),
      ui.lerpDouble(a.right, b.right, t),
148
      ui.lerpDouble(a.bottom, b.bottom, t)
149 150 151
    );
  }

152
  /// An EdgeInsets with zero offsets in each direction.
153
  static const EdgeInsets zero = const EdgeInsets.all(0.0);
154

155
  @override
156 157 158
  bool operator ==(dynamic other) {
    if (identical(this, other))
      return true;
159
    if (other is! EdgeInsets)
160
      return false;
161
    final EdgeInsets typedOther = other;
162 163
    return left == typedOther.left &&
           top == typedOther.top &&
164
           right == typedOther.right &&
165
           bottom == typedOther.bottom;
166 167
  }

168
  @override
169
  int get hashCode => hashValues(left, top, right, bottom);
170

171
  @override
172
  String toString() => "EdgeInsets($left, $top, $right, $bottom)";
173
}