Skip to content

Commit 6d504a9

Browse files
committed
♻️ some more refactor
1 parent 711d53c commit 6d504a9

31 files changed

+1109
-1260
lines changed

packages/box_transform/lib/src/geometry.dart

Lines changed: 57 additions & 166 deletions
Original file line numberDiff line numberDiff line change
@@ -59,19 +59,19 @@ class Constraints {
5959
);
6060
}
6161

62-
/// Constrains a given [box] by the [minWidth], [maxWidth], [minHeight], and
62+
/// Constrains a given [rect] by the [minWidth], [maxWidth], [minHeight], and
6363
/// [maxHeight] values.
6464
///
65-
/// If [absolute] is true, the [box] will be constrained by the absolute
65+
/// If [absolute] is true, the [rect] will be constrained by the absolute
6666
/// values of its width and height.
67-
Box constrainBox(Box box, {bool absolute = false}) {
68-
if (isUnconstrained) return box;
67+
Box constrainRect(Box rect, {bool absolute = false}) {
68+
if (isUnconstrained) return rect;
6969

70-
final double width = absolute ? box.width.abs() : box.width;
71-
final double height = absolute ? box.height.abs() : box.height;
70+
final double width = absolute ? rect.width.abs() : rect.width;
71+
final double height = absolute ? rect.height.abs() : rect.height;
7272
return Box.fromLTWH(
73-
box.left,
74-
box.top,
73+
rect.left,
74+
rect.top,
7575
_clamp(width, minWidth, maxWidth).toDouble(),
7676
_clamp(height, minHeight, maxHeight).toDouble(),
7777
);
@@ -98,9 +98,7 @@ class Constraints {
9898
/// Linearly interpolate between two doubles.
9999
///
100100
/// Same as [lerpDouble] but specialized for non-null `double` type.
101-
double _lerpDouble(double a, double b, double t) {
102-
return a * (1.0 - t) + b * t;
103-
}
101+
double _lerpDouble(double a, double b, double t) => a * (1.0 - t) + b * t;
104102

105103
/// A 2D size with [width] and [height].
106104
class Dimension {
@@ -153,25 +151,12 @@ class Dimension {
153151
/// If the [width] is zero, the result will be zero. If the [height] is zero
154152
/// (and the [width] is not), the result will be [double.infinity] or
155153
/// [double.negativeInfinity] as determined by the sign of [width].
156-
///
157-
/// See also:
158-
///
159-
/// * [AspectRatio], a widget for giving a child widget a specific aspect
160-
/// ratio.
161-
/// * [FittedBox], a widget that (in most modes) attempts to maintain a
162-
/// child widget's aspect ratio while changing its size.
163-
double get aspectRatio {
164-
if (height != 0.0) {
165-
return width / height;
166-
}
167-
if (width > 0.0) {
168-
return double.infinity;
169-
}
170-
if (width < 0.0) {
171-
return double.negativeInfinity;
172-
}
173-
return 0.0;
174-
}
154+
double get aspectRatio => switch (this) {
155+
_ when height != 0 => width / height,
156+
_ when width > 0 => double.infinity,
157+
_ when width < 0 => double.negativeInfinity,
158+
_ => 0,
159+
};
175160

176161
/// An empty size, one with a zero width and a zero height.
177162
static const Dimension zero = Dimension(0.0, 0.0);
@@ -305,14 +290,12 @@ class Dimension {
305290
///
306291
/// Boxes include their top and left edges but exclude their bottom and
307292
/// right edges.
308-
bool contains(Vector2 vec) {
309-
return vec.x >= 0.0 && vec.x < width && vec.y >= 0.0 && vec.y < height;
310-
}
293+
bool contains(Vector2 vec) =>
294+
vec.x >= 0.0 && vec.x < width && vec.y >= 0.0 && vec.y < height;
311295

312296
/// Constrains this with given constraints.
313-
Dimension constrainBy(Constraints constraints) {
314-
return constraints.constrainDimension(this);
315-
}
297+
Dimension constrainBy(Constraints constraints) =>
298+
constraints.constrainDimension(this);
316299

317300
/// A [Dimension] with the [width] and [height] swapped.
318301
Dimension get flipped => Dimension(height, width);
@@ -493,12 +476,11 @@ class Box {
493476

494477
/// Whether any of the coordinates of this rectangle are equal to positive infinity.
495478
// included for consistency with Vector2 and Dimension
496-
bool get isInfinite {
497-
return left >= double.infinity ||
498-
top >= double.infinity ||
499-
right >= double.infinity ||
500-
bottom >= double.infinity;
501-
}
479+
bool get isInfinite =>
480+
left >= double.infinity ||
481+
top >= double.infinity ||
482+
right >= double.infinity ||
483+
bottom >= double.infinity;
502484

503485
/// Whether all coordinates of this rectangle are finite.
504486
bool get isFinite =>
@@ -512,30 +494,24 @@ class Box {
512494
///
513495
/// To translate a rectangle by separate x and y components rather than by an
514496
/// [Vector2], consider [translate].
515-
Box shift(Vector2 vec) {
516-
return Box.fromLTRB(
517-
left + vec.x, top + vec.y, right + vec.x, bottom + vec.y);
518-
}
497+
Box shift(Vector2 vec) =>
498+
Box.fromLTRB(left + vec.x, top + vec.y, right + vec.x, bottom + vec.y);
519499

520500
/// Returns a new rectangle with translateX added to the x components and
521501
/// translateY added to the y components.
522502
///
523503
/// To translate a rectangle by an [Vector2] rather than by separate x and y
524504
/// components, consider [shift].
525-
Box translate(double translateX, double translateY) {
526-
return Box.fromLTRB(
527-
left + translateX,
528-
top + translateY,
529-
right + translateX,
530-
bottom + translateY,
531-
);
532-
}
505+
Box translate(double translateX, double translateY) => Box.fromLTRB(
506+
left + translateX,
507+
top + translateY,
508+
right + translateX,
509+
bottom + translateY,
510+
);
533511

534512
/// Returns a new rectangle with edges moved outwards by the given delta.
535-
Box inflate(double delta) {
536-
return Box.fromLTRB(
537-
left - delta, top - delta, right + delta, bottom + delta);
538-
}
513+
Box inflate(double delta) =>
514+
Box.fromLTRB(left - delta, top - delta, right + delta, bottom + delta);
539515

540516
/// Returns a new rectangle with edges moved inwards by the given delta.
541517
Box deflate(double delta) => inflate(-delta);
@@ -544,30 +520,25 @@ class Box {
544520
/// rectangle and this rectangle. The two rectangles must overlap
545521
/// for this to be meaningful. If the two rectangles do not overlap,
546522
/// then the resulting Box will have a negative width or height.
547-
Box intersect(Box other) {
548-
return Box.fromLTRB(math.max(left, other.left), math.max(top, other.top),
549-
math.min(right, other.right), math.min(bottom, other.bottom));
550-
}
523+
Box intersect(Box other) => Box.fromLTRB(
524+
math.max(left, other.left),
525+
math.max(top, other.top),
526+
math.min(right, other.right),
527+
math.min(bottom, other.bottom));
551528

552529
/// Returns a new rectangle which is the bounding box containing this
553530
/// rectangle and the given rectangle.
554-
Box expandToInclude(Box other) {
555-
return Box.fromLTRB(
556-
math.min(left, other.left),
557-
math.min(top, other.top),
558-
math.max(right, other.right),
559-
math.max(bottom, other.bottom),
560-
);
561-
}
531+
Box expandToInclude(Box other) => Box.fromLTRB(
532+
math.min(left, other.left),
533+
math.min(top, other.top),
534+
math.max(right, other.right),
535+
math.max(bottom, other.bottom),
536+
);
562537

563538
/// Whether `other` has a nonzero area of overlap with this rectangle.
564539
bool overlaps(Box other) {
565-
if (right <= other.left || other.right <= left) {
566-
return false;
567-
}
568-
if (bottom <= other.top || other.bottom <= top) {
569-
return false;
570-
}
540+
if (right <= other.left || other.right <= left) return false;
541+
if (bottom <= other.top || other.bottom <= top) return false;
571542
return true;
572543
}
573544

@@ -630,96 +601,34 @@ class Box {
630601

631602
/// Returns aspect ratio of this rectangle. Unlike [aspectRatio], it has a
632603
/// safe mechanism where if the width or height is zero, returns 1.
633-
double get safeAspectRatio {
634-
if (size.width == 0 || size.height == 0) {
635-
return 1.0;
636-
}
637-
return size.aspectRatio;
638-
}
604+
double get safeAspectRatio => switch (size) {
605+
_ when width == 0 || height == 0 => 1,
606+
_ => size.aspectRatio,
607+
};
639608

640609
/// Whether the point specified by the given Vector2 (which is assumed to be
641610
/// relative to the origin) lies between the left and right and the top and
642611
/// bottom edges of this rectangle.
643612
///
644613
/// Boxes include their top and left edges but exclude their bottom and
645614
/// right edges.
646-
bool contains(Vector2 vec) {
647-
return vec.x >= left && vec.x < right && vec.y >= top && vec.y < bottom;
648-
}
615+
bool contains(Vector2 vec) =>
616+
vec.x >= left && vec.x < right && vec.y >= top && vec.y < bottom;
649617

650618
/// Constrains this box instance to the given [constraints].
651619
///
652620
/// [constraints] the constraints to apply to this box.
653621
///
654622
/// [returns] a new box instance.
655-
Box constrainBy(Constraints constraints) {
656-
return constraints.constrainBox(this);
657-
}
658-
659-
/// Constrains this box instance to the given [parent] box.
660-
///
661-
/// [parent] the parent box to clamp this box inside.
662-
///
663-
/// [returns] a new box instance.
664-
Box clampThisInsideParent(Box parent) {
665-
return Box.fromLTRB(
666-
math.max(parent.left, left),
667-
math.max(parent.top, top),
668-
math.min(parent.right, right),
669-
math.min(parent.bottom, bottom),
670-
);
671-
}
672-
673-
/// Whether this box is overflowing the given [child] box. Returns true if
674-
/// any of the child's edges are outside of this box.
675-
bool isOverflowing(Box child) {
676-
return child.left < left ||
677-
child.top < top ||
678-
child.right > right ||
679-
child.bottom > bottom ||
680-
child.width > width ||
681-
child.height > height;
682-
}
623+
Box constrainBy(Constraints constraints) => constraints.constrainRect(this);
683624

684625
/// Constrains the given [child] box instance within the bounds of this box.
685626
/// This function will preserve the sign of the child's width and height.
686627
/// It will also maintain the aspect ratio of the child if the [aspectRatio]
687628
/// is specified.
688629
///
689630
/// [child] the child box to clamp inside this box.
690-
///
691-
/// [resizeMode] defines how to contain the child, whether it should keep its
692-
/// aspect ratio or not, or if it should be resized to fit.
693-
///
694-
/// [allowResizeOverflow] decides whether to allow the box to overflow the
695-
/// resize operation to its opposite side to continue the resize operation
696-
/// until its constrained on both sides.
697-
///
698-
/// If this is set to false, the box will cease the resize operation the
699-
/// instant it hits an edge of the [clampingRect].
700-
///
701-
/// If this is set to true, the box will continue the resize operation until
702-
/// it is constrained to both sides of the [clampingRect].
703-
///
704-
/// [handle] defines the handle that is being dragged to resize the box if
705-
/// available. This only matters if [allowResizeOverflow] is set to false.
706-
///
707-
/// [currentFlip] defines the current flip of the box if available.
708-
///
709-
/// [aspectRatio] will allow the box to maintain its aspect ratio if
710-
/// it overflows outside its clamping box and needs to be re-adjusted to
711-
/// fit back inside. This will ensure that the correction operation will
712-
/// maintain the aspect ratio of the box.
713-
///
714-
/// [returns] a new box instance.
715-
Box containOther(
716-
Box child, {
717-
ResizeMode resizeMode = ResizeMode.freeform,
718-
required HandlePosition handle,
719-
Flip currentFlip = Flip.none,
720-
double? aspectRatio,
721-
bool allowResizeOverflow = true,
722-
}) {
631+
Box containOther(Box child) {
723632
final double xSign = child.width.sign;
724633
final double ySign = child.height.sign;
725634
final double childWidth = child.width.abs();
@@ -735,20 +644,6 @@ class Box {
735644
double newWidth = math.min(width, childWidth);
736645
double newHeight = math.min(height, childHeight);
737646

738-
if (resizeMode.isScalable && aspectRatio != null) {
739-
final double newAspectRatio = newWidth / newHeight;
740-
final double widthAdjustment = newHeight * aspectRatio;
741-
final double heightAdjustment = newWidth / aspectRatio;
742-
743-
if (aspectRatio < newAspectRatio) {
744-
newHeight = math.min(height, heightAdjustment);
745-
newWidth = newHeight * aspectRatio;
746-
} else if (aspectRatio > newAspectRatio) {
747-
newWidth = math.min(width, widthAdjustment);
748-
newHeight = newWidth / aspectRatio;
749-
}
750-
}
751-
752647
return Box.fromLTWH(
753648
newLeft,
754649
newTop,
@@ -812,12 +707,8 @@ class Box {
812707

813708
@override
814709
bool operator ==(Object other) {
815-
if (identical(this, other)) {
816-
return true;
817-
}
818-
if (runtimeType != other.runtimeType) {
819-
return false;
820-
}
710+
if (identical(this, other)) return true;
711+
if (runtimeType != other.runtimeType) return false;
821712
return other is Box &&
822713
other.left == left &&
823714
other.top == top &&

0 commit comments

Comments
 (0)