|
1 | | -/// Increase new base unit types' values by a factor of 2 each time. |
2 | | -const int _percent = 0x1; |
3 | | -const int _length = 0x2; |
4 | | -const int _auto = 0x4; |
| 1 | +/// These are the base unit types |
| 2 | +enum _UnitType { |
| 3 | + percent, |
| 4 | + length, |
| 5 | + auto, |
| 6 | + lengthPercent(children: [_UnitType.length, _UnitType.percent]), |
| 7 | + lengthPercentAuto(children: [_UnitType.length, _UnitType.percent, _UnitType.auto]); |
5 | 8 |
|
6 | | -/// These values are combinations of the base unit-types |
7 | | -const int _lengthPercent = _length | _percent; |
8 | | -const int _lengthPercentAuto = _lengthPercent | _auto; |
| 9 | + final List<_UnitType> children; |
| 10 | + |
| 11 | + const _UnitType({this.children = const []}); |
| 12 | + |
| 13 | + bool matches(_UnitType other) { |
| 14 | + return this == other || children.contains(other); |
| 15 | + } |
| 16 | +} |
9 | 17 |
|
10 | 18 | /// A Unit represents a CSS unit |
11 | 19 | enum Unit { |
12 | 20 | //ch, |
13 | | - em(_length), |
| 21 | + em(_UnitType.length), |
14 | 22 | //ex, |
15 | | - percent(_percent), |
16 | | - px(_length), |
17 | | - rem(_length), |
| 23 | + percent(_UnitType.percent), |
| 24 | + px(_UnitType.length), |
| 25 | + rem(_UnitType.length), |
18 | 26 | //Q, |
19 | 27 | //vh, |
20 | 28 | //vw, |
21 | | - auto(_auto); |
| 29 | + auto(_UnitType.auto); |
22 | 30 |
|
23 | 31 | const Unit(this.unitType); |
24 | | - final int unitType; |
| 32 | + final _UnitType unitType; |
25 | 33 | } |
26 | 34 |
|
27 | 35 | /// Represents a CSS dimension https://drafts.csswg.org/css-values/#dimensions |
28 | 36 | abstract class Dimension { |
29 | 37 | double value; |
30 | 38 | Unit unit; |
31 | 39 |
|
32 | | - Dimension(this.value, this.unit, int _dimensionUnitType) |
33 | | - : assert( |
34 | | - identical((unit.unitType | _dimensionUnitType), _dimensionUnitType), |
35 | | - "This dimension was given a Unit that isn't specified."); |
| 40 | + Dimension(this.value, this.unit, _UnitType _dimensionUnitType) |
| 41 | + : assert(_dimensionUnitType.matches(unit.unitType), |
| 42 | + "This Dimension was given a Unit that isn't specified."); |
36 | 43 | } |
37 | 44 |
|
38 | 45 | /// This dimension takes a value with a length unit such as px or em. Note that |
39 | 46 | /// these can be fixed or relative (but they must not be a percent) |
40 | 47 | class Length extends Dimension { |
41 | | - Length(double value, [Unit unit = Unit.px]) : super(value, unit, _length); |
| 48 | + Length(double value, [Unit unit = Unit.px]) : super(value, unit, _UnitType.length); |
42 | 49 | } |
43 | 50 |
|
44 | 51 | /// This dimension takes a value with a length-percent unit such as px or em |
45 | 52 | /// or %. Note that these can be fixed or relative (but they must not be a |
46 | 53 | /// percent) |
47 | 54 | class LengthOrPercent extends Dimension { |
48 | 55 | LengthOrPercent(double value, [Unit unit = Unit.px]) |
49 | | - : super(value, unit, _lengthPercent); |
| 56 | + : super(value, unit, _UnitType.lengthPercent); |
50 | 57 | } |
51 | 58 |
|
52 | 59 | class AutoOrLengthOrPercent extends Dimension { |
53 | 60 | AutoOrLengthOrPercent(double value, [Unit unit = Unit.px]) |
54 | | - : super(value, unit, _lengthPercentAuto); |
| 61 | + : super(value, unit, _UnitType.lengthPercentAuto); |
55 | 62 | } |
0 commit comments