Skip to content

Commit

Permalink
fix: Use enum instead of const int internally in length.dart
Browse files Browse the repository at this point in the history
  • Loading branch information
Sub6Resources committed Sep 17, 2022
1 parent a62449a commit 9dc7f08
Showing 1 changed file with 27 additions and 20 deletions.
47 changes: 27 additions & 20 deletions lib/src/style/length.dart
Original file line number Diff line number Diff line change
@@ -1,55 +1,62 @@
/// Increase new base unit types' values by a factor of 2 each time.
const int _percent = 0x1;
const int _length = 0x2;
const int _auto = 0x4;
/// These are the base unit types
enum _UnitType {
percent,
length,
auto,
lengthPercent(children: [_UnitType.length, _UnitType.percent]),
lengthPercentAuto(children: [_UnitType.length, _UnitType.percent, _UnitType.auto]);

/// These values are combinations of the base unit-types
const int _lengthPercent = _length | _percent;
const int _lengthPercentAuto = _lengthPercent | _auto;
final List<_UnitType> children;

const _UnitType({this.children = const []});

bool matches(_UnitType other) {
return this == other || children.contains(other);
}
}

/// A Unit represents a CSS unit
enum Unit {
//ch,
em(_length),
em(_UnitType.length),
//ex,
percent(_percent),
px(_length),
rem(_length),
percent(_UnitType.percent),
px(_UnitType.length),
rem(_UnitType.length),
//Q,
//vh,
//vw,
auto(_auto);
auto(_UnitType.auto);

const Unit(this.unitType);
final int unitType;
final _UnitType unitType;
}

/// Represents a CSS dimension https://drafts.csswg.org/css-values/#dimensions
abstract class Dimension {
double value;
Unit unit;

Dimension(this.value, this.unit, int _dimensionUnitType)
: assert(
identical((unit.unitType | _dimensionUnitType), _dimensionUnitType),
"This dimension was given a Unit that isn't specified.");
Dimension(this.value, this.unit, _UnitType _dimensionUnitType)
: assert(_dimensionUnitType.matches(unit.unitType),
"This Dimension was given a Unit that isn't specified.");
}

/// This dimension takes a value with a length unit such as px or em. Note that
/// these can be fixed or relative (but they must not be a percent)
class Length extends Dimension {
Length(double value, [Unit unit = Unit.px]) : super(value, unit, _length);
Length(double value, [Unit unit = Unit.px]) : super(value, unit, _UnitType.length);
}

/// This dimension takes a value with a length-percent unit such as px or em
/// or %. Note that these can be fixed or relative (but they must not be a
/// percent)
class LengthOrPercent extends Dimension {
LengthOrPercent(double value, [Unit unit = Unit.px])
: super(value, unit, _lengthPercent);
: super(value, unit, _UnitType.lengthPercent);
}

class AutoOrLengthOrPercent extends Dimension {
AutoOrLengthOrPercent(double value, [Unit unit = Unit.px])
: super(value, unit, _lengthPercentAuto);
: super(value, unit, _UnitType.lengthPercentAuto);
}

0 comments on commit 9dc7f08

Please sign in to comment.