Skip to content

Commit beea3a4

Browse files
committed
♻️ improve usage of records and resize handlers.
1 parent 5337339 commit beea3a4

File tree

8 files changed

+63
-90
lines changed

8 files changed

+63
-90
lines changed

packages/box_transform/lib/src/resize_handlers/freeform_resizing.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ final class FreeformResizeHandler extends ResizeHandler {
66
const FreeformResizeHandler();
77

88
@override
9-
(Box rect, Box largest, bool hasValidFlip) resize({
9+
({Box rect, Box largest, bool hasValidFlip}) resize({
1010
required Box initialRect,
1111
required Box explodedRect,
1212
required Box clampingRect,
@@ -60,6 +60,6 @@ final class FreeformResizeHandler extends ResizeHandler {
6060
clampingRect: clampingRect,
6161
);
6262

63-
return (newRect, area, isValid);
63+
return (rect: newRect, largest: area, hasValidFlip: isValid);
6464
}
6565
}

packages/box_transform/lib/src/resize_handlers/resize_handler.dart

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,26 @@ import 'dart:math';
55
import '../../box_transform.dart';
66

77
part 'freeform_resizing.dart';
8+
89
part 'scale_resizing.dart';
10+
911
part 'symmetric_resizing.dart';
12+
1013
part 'symmetric_scale_resizing.dart';
1114

1215
/// An abstract class the provides a common interface for all resize modes.
1316
sealed class ResizeHandler {
1417
/// A default constructor for [ResizeHandler].
1518
const ResizeHandler();
1619

20+
/// Creates a [ResizeHandler] from the given [mode].
21+
factory ResizeHandler.from(ResizeMode mode) => switch (mode) {
22+
ResizeMode.freeform => const FreeformResizeHandler(),
23+
ResizeMode.scale => const ScaleResizeHandler(),
24+
ResizeMode.symmetric => const SymmetricResizeHandler(),
25+
ResizeMode.symmetricScale => const SymmetricScaleResizeHandler(),
26+
};
27+
1728
/// Resizes the given [explodedRect] to fit within the [clampingRect].
1829
///
1930
/// Specifying the [handle] will determine how the [explodedRect] will be
@@ -26,7 +37,7 @@ sealed class ResizeHandler {
2637
///
2738
/// The [constraints] is the constraints that the [explodedRect] is not
2839
/// allowed to shrink or grow beyond.
29-
(Box, Box, bool) resize({
40+
({Box rect, Box largest, bool hasValidFlip}) resize({
3041
required Box initialRect,
3142
required Box explodedRect,
3243
required Box clampingRect,

packages/box_transform/lib/src/resize_handlers/scale_resizing.dart

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ final class ScaleResizeHandler extends ResizeHandler {
66
const ScaleResizeHandler();
77

88
@override
9-
(Box, Box, bool) resize({
9+
({Box rect, Box largest, bool hasValidFlip}) resize({
1010
required Box initialRect,
1111
required Box explodedRect,
1212
required Box clampingRect,
@@ -17,7 +17,7 @@ final class ScaleResizeHandler extends ResizeHandler {
1717
final flippedHandle = handle.flip(flip);
1818
final effectiveInitialRect = flipBox(initialRect, flip, handle);
1919

20-
(Box, Box, bool) result;
20+
({Box rect, Box largest, bool hasValidFlip}) result;
2121

2222
switch (flippedHandle) {
2323
case HandlePosition.none:
@@ -56,15 +56,13 @@ final class ScaleResizeHandler extends ResizeHandler {
5656
break;
5757
}
5858

59-
final bool hasValidFlip = result.$3;
60-
if (!hasValidFlip) {
59+
if (!result.hasValidFlip) {
6160
// Since we can't flip the box, explodedRect (which is a raw rect with delta applied)
6261
// would be flipped so we can't use that because it would make the size
6362
// calculations wrong. Instead we use box from the result which is the
6463
// flipped box but with correct constraints applied. (min rect always).
65-
final Box explodedRect = result.$1;
6664
return resize(
67-
explodedRect: explodedRect,
65+
explodedRect: result.rect,
6866
initialRect: initialRect,
6967
clampingRect: clampingRect,
7068
handle: handle,
@@ -78,7 +76,7 @@ final class ScaleResizeHandler extends ResizeHandler {
7876

7977
/// Handle resizing for [HandlePosition.bottomRight] handle
8078
/// for [ResizeMode.scale].
81-
(Box, Box, bool) handleBottomRight(
79+
({Box rect, Box largest, bool hasValidFlip}) handleBottomRight(
8280
Box rect,
8381
Box initialRect,
8482
Box clampingRect,
@@ -143,11 +141,11 @@ final class ScaleResizeHandler extends ResizeHandler {
143141

144142
final isValid = isValidBox(rect, constraints, clampingRect);
145143

146-
return (rect, largest, isValid);
144+
return (rect: rect, largest: largest, hasValidFlip: isValid);
147145
}
148146

149147
/// Handle resizing for the right handle.
150-
(Box, Box, bool) handleRight(
148+
({Box rect, Box largest, bool hasValidFlip}) handleRight(
151149
Box rect,
152150
Box initialRect,
153151
Box clampingRect,
@@ -219,11 +217,11 @@ final class ScaleResizeHandler extends ResizeHandler {
219217

220218
final isValid = isValidBox(rect, constraints, clampingRect);
221219

222-
return (rect, largest, isValid);
220+
return (rect: rect, largest: largest, hasValidFlip: isValid);
223221
}
224222

225223
/// handle resizing for the left handle
226-
(Box, Box, bool) handleLeft(
224+
({Box rect, Box largest, bool hasValidFlip}) handleLeft(
227225
Box rect,
228226
Box initialRect,
229227
Box clampingRect,
@@ -295,11 +293,11 @@ final class ScaleResizeHandler extends ResizeHandler {
295293

296294
final isValid = isValidBox(rect, constraints, clampingRect);
297295

298-
return (rect, largest, isValid);
296+
return (rect: rect, largest: largest, hasValidFlip: isValid);
299297
}
300298

301299
/// handle resizing for the bottom handle.
302-
(Box, Box, bool) handleBottom(
300+
({Box rect, Box largest, bool hasValidFlip}) handleBottom(
303301
Box rect,
304302
Box initialRect,
305303
Box clampingRect,
@@ -371,11 +369,11 @@ final class ScaleResizeHandler extends ResizeHandler {
371369

372370
final isValid = isValidBox(rect, constraints, clampingRect);
373371

374-
return (rect, largest, isValid);
372+
return (rect: rect, largest: largest, hasValidFlip: isValid);
375373
}
376374

377375
/// handle resizing for the top handle.
378-
(Box, Box, bool) handleTop(
376+
({Box rect, Box largest, bool hasValidFlip}) handleTop(
379377
Box rect,
380378
Box initialRect,
381379
Box clampingRect,
@@ -447,12 +445,12 @@ final class ScaleResizeHandler extends ResizeHandler {
447445

448446
final isValid = isValidBox(rect, constraints, clampingRect);
449447

450-
return (rect, largest, isValid);
448+
return (rect: rect, largest: largest, hasValidFlip: isValid);
451449
}
452450

453451
/// Handle resizing for [HandlePosition.topLeft] handle
454452
/// for [ResizeMode.scale].
455-
(Box, Box, bool) handleTopLeft(
453+
({Box rect, Box largest, bool hasValidFlip}) handleTopLeft(
456454
Box rect,
457455
Box initialRect,
458456
Box clampingRect,
@@ -517,12 +515,12 @@ final class ScaleResizeHandler extends ResizeHandler {
517515

518516
final isValid = isValidBox(rect, constraints, clampingRect);
519517

520-
return (rect, largest, isValid);
518+
return (rect: rect, largest: largest, hasValidFlip: isValid);
521519
}
522520

523521
/// Handle resizing for [HandlePosition.bottomLeft] handle
524522
/// for [ResizeMode.scale].
525-
(Box, Box, bool) handleBottomLeft(
523+
({Box rect, Box largest, bool hasValidFlip}) handleBottomLeft(
526524
Box rect,
527525
Box initialRect,
528526
Box clampingRect,
@@ -587,12 +585,12 @@ final class ScaleResizeHandler extends ResizeHandler {
587585

588586
final isValid = isValidBox(rect, constraints, clampingRect);
589587

590-
return (rect, largest, isValid);
588+
return (rect: rect, largest: largest, hasValidFlip: isValid);
591589
}
592590

593591
/// Handle resizing for [HandlePosition.topRight] handle
594592
/// for [ResizeMode.scale].
595-
(Box, Box, bool) handleTopRight(
593+
({Box rect, Box largest, bool hasValidFlip}) handleTopRight(
596594
Box rect,
597595
Box initialRect,
598596
Box clampingRect,
@@ -657,6 +655,6 @@ final class ScaleResizeHandler extends ResizeHandler {
657655

658656
final isValid = isValidBox(rect, constraints, clampingRect);
659657

660-
return (rect, largest, isValid);
658+
return (rect: rect, largest: largest, hasValidFlip: isValid);
661659
}
662660
}

packages/box_transform/lib/src/resize_handlers/symmetric_resizing.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ final class SymmetricResizeHandler extends ResizeHandler {
66
const SymmetricResizeHandler();
77

88
@override
9-
(Box, Box, bool) resize({
9+
({Box rect, Box largest, bool hasValidFlip}) resize({
1010
required Box initialRect,
1111
required Box explodedRect,
1212
required Box clampingRect,
@@ -55,6 +55,6 @@ final class SymmetricResizeHandler extends ResizeHandler {
5555
height: min(area.height, max(explodedRect.height, minRect.height)),
5656
);
5757

58-
return (newRect, area, true);
58+
return (rect: newRect, largest: area, hasValidFlip: true);
5959
}
6060
}

packages/box_transform/lib/src/resize_handlers/symmetric_scale_resizing.dart

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ final class SymmetricScaleResizeHandler extends ResizeHandler {
66
const SymmetricScaleResizeHandler();
77

88
@override
9-
(Box, Box, bool) resize({
9+
({Box rect, Box largest, bool hasValidFlip}) resize({
1010
required Box initialRect,
1111
required Box explodedRect,
1212
required Box clampingRect,
@@ -43,7 +43,7 @@ final class SymmetricScaleResizeHandler extends ResizeHandler {
4343
}
4444

4545
/// Handles the symmetric resize mode for corner handles.
46-
(Box, Box, bool) handleScaleSymmetricCorner(
46+
({Box rect, Box largest, bool hasValidFlip}) handleScaleSymmetricCorner(
4747
Box rect,
4848
Box initialRect,
4949
Box clampingRect,
@@ -115,16 +115,16 @@ final class SymmetricScaleResizeHandler extends ResizeHandler {
115115

116116
if (rect.width > maxRect.width || rect.height > maxRect.height) {
117117
rect = maxRect;
118-
return (maxRect, maxRect, true);
118+
return (rect: maxRect, largest: maxRect, hasValidFlip: true);
119119
} else if (rect.width < minRect.width || rect.height < minRect.height) {
120-
return (minRect, maxRect, true);
120+
return (rect: minRect, largest: maxRect, hasValidFlip: true);
121121
}
122122

123-
return (rect, maxRect, true);
123+
return (rect: rect, largest: maxRect, hasValidFlip: true);
124124
}
125125

126126
/// Handles the symmetric resize mode for side handles.
127-
(Box, Box, bool) handleScaleSymmetricSide(
127+
({Box rect, Box largest, bool hasValidFlip}) handleScaleSymmetricSide(
128128
Box rect,
129129
Box initialRect,
130130
Box clampingRect,
@@ -194,11 +194,11 @@ final class SymmetricScaleResizeHandler extends ResizeHandler {
194194

195195
if (rect.width > maxRect.width || rect.height > maxRect.height) {
196196
rect = maxRect;
197-
return (maxRect, maxRect, true);
197+
return (rect: maxRect, largest: maxRect, hasValidFlip: true);
198198
} else if (rect.width < minRect.width || rect.height < minRect.height) {
199-
return (minRect, maxRect, true);
199+
return (rect: minRect, largest: maxRect, hasValidFlip: true);
200200
}
201201

202-
return (rect, maxRect, true);
202+
return (rect: rect, largest: maxRect, hasValidFlip: true);
203203
}
204204
}

packages/box_transform/lib/src/transformer.dart

Lines changed: 13 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ class BoxTransformer {
119119

120120
if (resizeMode.hasSymmetry) delta = Vector2(delta.x * 2, delta.y * 2);
121121

122-
(Box, Box, bool) result = _calculateNewBox(
122+
({Box rect, Box largest, bool hasValidFlip}) result = _calculateNewBox(
123123
initialBox: initialBox,
124124
handle: handle,
125125
delta: delta,
@@ -132,8 +132,8 @@ class BoxTransformer {
132132
flipRect: allowBoxFlipping,
133133
);
134134

135-
final Box newRect = result.$1;
136-
final Box largestRect = result.$2;
135+
final Box newRect = result.rect;
136+
final Box largestRect = result.largest;
137137

138138
// Detect terminal resizing, where the resizing reached a hard limit.
139139
bool minWidthReached = false;
@@ -240,7 +240,7 @@ class BoxTransformer {
240240
);
241241
}
242242

243-
static (Box, Box, bool) _calculateNewBox({
243+
static ({Box rect, Box largest, bool hasValidFlip}) _calculateNewBox({
244244
required Box initialBox,
245245
required HandlePosition handle,
246246
required Vector2 delta,
@@ -262,50 +262,14 @@ class BoxTransformer {
262262
flipRect: flipRect,
263263
);
264264

265-
(Box rect, Box largestRect, bool isValid) result;
266-
switch (resizeMode) {
267-
case ResizeMode.freeform:
268-
result = const FreeformResizeHandler().resize(
269-
explodedRect: explodedRect,
270-
clampingRect: clampingRect,
271-
handle: handle,
272-
constraints: constraints,
273-
initialRect: initialBox,
274-
flip: flip,
275-
);
276-
break;
277-
case ResizeMode.symmetric:
278-
result = const SymmetricResizeHandler().resize(
279-
explodedRect: explodedRect,
280-
clampingRect: clampingRect,
281-
handle: handle,
282-
constraints: constraints,
283-
initialRect: initialBox,
284-
flip: flip,
285-
);
286-
break;
287-
case ResizeMode.scale:
288-
result = const ScaleResizeHandler().resize(
289-
explodedRect: explodedRect,
290-
clampingRect: clampingRect,
291-
handle: handle,
292-
constraints: constraints,
293-
initialRect: initialBox,
294-
flip: flip,
295-
);
296-
break;
297-
case ResizeMode.symmetricScale:
298-
result = const SymmetricScaleResizeHandler().resize(
299-
explodedRect: explodedRect,
300-
clampingRect: clampingRect,
301-
handle: handle,
302-
constraints: constraints,
303-
initialRect: initialBox,
304-
flip: flip,
305-
);
306-
break;
307-
}
308-
309-
return result;
265+
final ResizeHandler resizer = ResizeHandler.from(resizeMode);
266+
return resizer.resize(
267+
explodedRect: explodedRect,
268+
clampingRect: clampingRect,
269+
handle: handle,
270+
constraints: constraints,
271+
initialRect: initialBox,
272+
flip: flip,
273+
);
310274
}
311275
}

packages/box_transform/pubspec.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ environment:
1313

1414
dependencies:
1515
vector_math: ^2.1.4
16-
meta: ^1.8.0
16+
meta: ^1.9.1
1717

1818
dev_dependencies:
19-
lints: ^2.0.1
20-
test: ^1.22.2
19+
lints: ^2.1.0
20+
test: ^1.24.3
2121
coverage: ^1.6.3

pubspec.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name: box_transform
22

33
environment:
4-
sdk: '>=2.18.0 <3.0.0'
4+
sdk: '>=3.0.0 <4.0.0'
55

66
dev_dependencies:
7-
melos: ^3.0.0
7+
melos: ^3.1.0

0 commit comments

Comments
 (0)