Skip to content

Commit a015c35

Browse files
committed
Resizing refactor #8
- ✨ Implement constraints for all resizing modes.
1 parent abd13d2 commit a015c35

File tree

4 files changed

+428
-108
lines changed

4 files changed

+428
-108
lines changed

packages/box_transform/lib/src/geometry.dart

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,7 @@ class Constraints {
2424
this.maxWidth = double.infinity,
2525
this.minHeight = 0.0,
2626
this.maxHeight = double.infinity,
27-
}) : assert(minWidth >= 0),
28-
assert(maxWidth >= 0),
29-
assert(minHeight >= 0),
30-
assert(maxHeight >= 0),
31-
assert(minWidth <= maxWidth),
27+
}) : assert(minWidth <= maxWidth),
3228
assert(minHeight <= maxHeight);
3329

3430
/// Creates a new unconstrained [Constraints] object.

packages/box_transform/lib/src/helpers.dart

Lines changed: 69 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -384,22 +384,27 @@ Box getAvailableAreaForHandle({
384384
required Box rect,
385385
required Box clampingRect,
386386
required HandlePosition handle,
387+
Constraints constraints = const Constraints.unconstrained(),
387388
}) {
389+
Box box;
388390
if (handle.isSide) {
389391
final opposite = handle.opposite;
390-
return Box.fromLTRB(
392+
box = Box.fromLTRB(
391393
opposite.influencesLeft ? rect.left : clampingRect.left,
392394
opposite.influencesTop ? rect.top : clampingRect.top,
393395
opposite.influencesRight ? rect.right : clampingRect.right,
394396
opposite.influencesBottom ? rect.bottom : clampingRect.bottom,
395397
);
398+
} else {
399+
box = Box.fromLTRB(
400+
handle.influencesLeft ? clampingRect.left : rect.left,
401+
handle.influencesTop ? clampingRect.top : rect.top,
402+
handle.influencesRight ? clampingRect.right : rect.right,
403+
handle.influencesBottom ? clampingRect.bottom : rect.bottom,
404+
);
396405
}
397-
return Box.fromLTRB(
398-
handle.influencesLeft ? clampingRect.left : rect.left,
399-
handle.influencesTop ? clampingRect.top : rect.top,
400-
handle.influencesRight ? clampingRect.right : rect.right,
401-
handle.influencesBottom ? clampingRect.bottom : rect.bottom,
402-
);
406+
407+
return box;
403408
}
404409

405410
/// Returns the clamping rect for the given handle for [ResizeMode.scale].
@@ -486,3 +491,60 @@ Box getClampingRectForCornerHandle({
486491
maxHeight,
487492
);
488493
}
494+
495+
/// Constrains available area for [ResizeMode.scale].
496+
Box constrainAvailableAreaForScaling({
497+
required Box area,
498+
required Box initialRect,
499+
required HandlePosition handle,
500+
required Constraints constraints,
501+
}) {
502+
if (constraints.isUnconstrained) return area;
503+
504+
final maxWidth = min(constraints.maxWidth, area.width);
505+
final maxHeight = min(constraints.maxHeight, area.height);
506+
507+
final constrainedBox = Box.fromHandle(
508+
handle.anchor(initialRect),
509+
handle,
510+
maxWidth,
511+
maxHeight,
512+
);
513+
514+
return Box.fromLTRB(
515+
max(constrainedBox.left, area.left),
516+
max(constrainedBox.top, area.top),
517+
min(constrainedBox.right, area.right),
518+
min(constrainedBox.bottom, area.bottom),
519+
);
520+
}
521+
522+
/// Returns a minimum Rect for given constraints when [ResizeMode.scale].
523+
Box getMinRectForScaling({
524+
required Box initialRect,
525+
required HandlePosition handle,
526+
required Constraints constraints,
527+
}) {
528+
final double minWidth;
529+
final double minHeight;
530+
531+
if (!constraints.isUnconstrained) {
532+
if (initialRect.aspectRatio < 1) {
533+
minWidth = constraints.minWidth;
534+
minHeight = minWidth / initialRect.aspectRatio;
535+
} else {
536+
minHeight = constraints.minHeight;
537+
minWidth = minHeight * initialRect.aspectRatio;
538+
}
539+
} else {
540+
minWidth = 0;
541+
minHeight = 0;
542+
}
543+
544+
return Box.fromHandle(
545+
handle.anchor(initialRect),
546+
handle,
547+
minWidth,
548+
minHeight,
549+
);
550+
}

0 commit comments

Comments
 (0)