Skip to content

Commit 5ec550c

Browse files
committed
Refactor code for improved readability and consistency across multiple files
1 parent 3c5f205 commit 5ec550c

11 files changed

Lines changed: 128 additions & 118 deletions

.pubignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@
55
/doc/hero.png
66
/build/
77
.git/
8-
.github/
8+
.github/
9+
/doc/

example/lib/gallery_main.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,8 @@ class _GalleryPageState extends State<GalleryPage> {
138138
/// midpoint, using the shared vertex as the curve's control point, so the
139139
/// silhouette bulges through every vertex instead of having sharp corners.
140140
static LayerPathBuilder _blob(List<Offset> vertices) {
141-
Offset mid(Offset a, Offset b) => Offset((a.dx + b.dx) / 2, (a.dy + b.dy) / 2);
141+
Offset mid(Offset a, Offset b) =>
142+
Offset((a.dx + b.dx) / 2, (a.dy + b.dy) / 2);
142143

143144
final builder = LayerPathBuilder()..moveTo(mid(vertices.last, vertices[0]));
144145
for (var i = 0; i < vertices.length; i++) {

example/lib/main.dart

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,7 @@ class _DemoPageState extends State<DemoPage> {
6262
Scene? _restoredScene;
6363
String? _exportStatus;
6464

65-
Scene _buildScene(
66-
Size logicalSize,
67-
double pixelRatio, {
68-
String? fontFamily,
69-
}) {
65+
Scene _buildScene(Size logicalSize, double pixelRatio, {String? fontFamily}) {
7066
final physicalSize = logicalSize * pixelRatio;
7167
return Scenes.of(
7268
width: physicalSize.width,
@@ -484,10 +480,7 @@ class _DemoPageState extends State<DemoPage> {
484480
const SizedBox(height: 8),
485481
const Text('Restored from JSON:'),
486482
const SizedBox(height: 8),
487-
SizedBox(
488-
height: 160,
489-
child: LayerCanvas(scene: _restoredScene),
490-
),
483+
SizedBox(height: 160, child: LayerCanvas(scene: _restoredScene)),
491484
],
492485
],
493486
),

lib/src/rendering/isolate_render.dart

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,5 @@ Future<void> renderToFileOffMainIsolate(
3838
String path, {
3939
OutputFormat format = OutputFormat.png,
4040
}) {
41-
return Isolate.run(
42-
() => renderer.renderToFile(scene, path, format: format),
43-
);
41+
return Isolate.run(() => renderer.renderToFile(scene, path, format: format));
4442
}

lib/src/widgets/layer_canvas_widget.dart

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,10 @@ class _LayerCanvasState extends State<LayerCanvas> {
125125
if (_cacheKey != cacheKey) {
126126
_cacheKey = cacheKey;
127127
final bundle = DefaultAssetBundle.of(context);
128-
_future = resolveSceneAssetSources(scene, bundle).then(
129-
(resolved) => renderOffMainIsolate(widget.renderer, resolved),
130-
);
128+
_future = resolveSceneAssetSources(
129+
scene,
130+
bundle,
131+
).then((resolved) => renderOffMainIsolate(widget.renderer, resolved));
131132
}
132133

133134
Widget content = SizedBox(

test/adapters_test.dart

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -60,17 +60,14 @@ void main() {
6060
expect(FontWeight.w900.toTextWeight().value, TextWeight.black.value);
6161
});
6262

63-
test(
64-
'intermediate FontWeight maps exactly, not to the closest named '
65-
'TextWeight',
66-
() {
67-
// TextWeight.fromValue is exact (layer_canvas 0.1.0-beta.6+), so a
68-
// FontWeight with no matching named constant round-trips to its own
69-
// raw value instead of snapping to the nearest named one.
70-
expect(FontWeight.w200.toTextWeight().value, 200);
71-
expect(FontWeight.w800.toTextWeight().value, 800);
72-
},
73-
);
63+
test('intermediate FontWeight maps exactly, not to the closest named '
64+
'TextWeight', () {
65+
// TextWeight.fromValue is exact (layer_canvas 0.1.0-beta.6+), so a
66+
// FontWeight with no matching named constant round-trips to its own
67+
// raw value instead of snapping to the nearest named one.
68+
expect(FontWeight.w200.toTextWeight().value, 200);
69+
expect(FontWeight.w800.toTextWeight().value, 800);
70+
});
7471

7572
test('TextAlign maps to TextAlignment', () {
7673
expect(TextAlign.left.toTextAlignment(), TextAlignment.left);
@@ -608,25 +605,28 @@ void main() {
608605
expect(decoded.bundleKey, 'packages/brand_kit/images/logo.png');
609606
});
610607

611-
test('registers itself with LayerRegistry so Scene.fromJson decodes it', () {
612-
final scene = Scene(width: 10, height: 10)
613-
..add(
614-
Layers.image(
615-
source: AssetImageSource('images/logo.png'),
616-
size: const Size(10, 10),
617-
),
608+
test(
609+
'registers itself with LayerRegistry so Scene.fromJson decodes it',
610+
() {
611+
final scene = Scene(width: 10, height: 10)
612+
..add(
613+
Layers.image(
614+
source: AssetImageSource('images/logo.png'),
615+
size: const Size(10, 10),
616+
),
617+
);
618+
619+
final decodedScene = Scene.fromJson(
620+
jsonDecodeRoundTrip(scene.toJson()),
618621
);
619-
620-
final decodedScene = Scene.fromJson(
621-
jsonDecodeRoundTrip(scene.toJson()),
622-
);
623-
final decodedLayer = decodedScene.layers.single as ImageLayer;
624-
expect(decodedLayer.source, isA<AssetImageSource>());
625-
expect(
626-
(decodedLayer.source as AssetImageSource).assetKey,
627-
'images/logo.png',
628-
);
629-
});
622+
final decodedLayer = decodedScene.layers.single as ImageLayer;
623+
expect(decodedLayer.source, isA<AssetImageSource>());
624+
expect(
625+
(decodedLayer.source as AssetImageSource).assetKey,
626+
'images/logo.png',
627+
);
628+
},
629+
);
630630
});
631631
}
632632

test/layer_canvas_widget_test.dart

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ import 'test_utils.dart';
88

99
class _ThrowingRenderer extends Renderer {
1010
@override
11-
Future<Uint8List> render(Scene scene, {OutputFormat format = OutputFormat.png}) =>
12-
Future.error(RenderException('boom'), StackTrace.current);
11+
Future<Uint8List> render(
12+
Scene scene, {
13+
OutputFormat format = OutputFormat.png,
14+
}) => Future.error(RenderException('boom'), StackTrace.current);
1315
}
1416

1517
Widget _wrap(Widget child) {

test/scene_widget_test.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@ void main() {
4848
SceneWidget(
4949
width: 100,
5050
height: 100,
51-
children: [Layers.rectangle(size: const Size(100, 100), color: color)],
51+
children: [
52+
Layers.rectangle(size: const Size(100, 100), color: color),
53+
],
5254
),
5355
);
5456

test/serialization_test.dart

Lines changed: 74 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,21 @@ class _FakeAssetBundle extends AssetBundle {
3333
void main() {
3434
group('Scene JSON round trip', () {
3535
test('round-trips width/height/background/layers', () {
36-
final scene = Scene(
37-
width: 200,
38-
height: 100,
39-
background: MemoryImageSource(Uint8List.fromList([1, 2, 3])),
40-
)..addAll([
41-
Layers.rectangle(size: const Size(50, 50), color: const Color(0xFFFF0000)),
42-
Layers.text(text: 'hello', fontWeight: FontWeight.w600),
43-
Layers.group(
44-
children: [Layers.rectangle(size: const Size(10, 10))],
45-
),
46-
]);
36+
final scene =
37+
Scene(
38+
width: 200,
39+
height: 100,
40+
background: MemoryImageSource(Uint8List.fromList([1, 2, 3])),
41+
)..addAll([
42+
Layers.rectangle(
43+
size: const Size(50, 50),
44+
color: const Color(0xFFFF0000),
45+
),
46+
Layers.text(text: 'hello', fontWeight: FontWeight.w600),
47+
Layers.group(
48+
children: [Layers.rectangle(size: const Size(10, 10))],
49+
),
50+
]);
4751

4852
final decoded = Scene.fromJson(
4953
jsonDecode(jsonEncode(scene.toJson())) as Map<String, Object?>,
@@ -60,60 +64,66 @@ void main() {
6064
});
6165

6266
group('resolveSceneAssetSources', () {
63-
test('returns the same Scene instance when no AssetImageSource exists', () async {
64-
final scene = Scene(width: 10, height: 10)
65-
..add(
66-
Layers.image(
67-
source: MemoryImageSource(Uint8List.fromList([1])),
68-
size: const Size(10, 10),
69-
),
67+
test(
68+
'returns the same Scene instance when no AssetImageSource exists',
69+
() async {
70+
final scene = Scene(width: 10, height: 10)
71+
..add(
72+
Layers.image(
73+
source: MemoryImageSource(Uint8List.fromList([1])),
74+
size: const Size(10, 10),
75+
),
76+
);
77+
final bundle = _FakeAssetBundle(Uint8List.fromList([9, 9, 9]));
78+
79+
final resolved = await resolveSceneAssetSources(scene, bundle);
80+
81+
expect(identical(resolved, scene), isTrue);
82+
expect(bundle.loadedKeys, isEmpty);
83+
},
84+
);
85+
86+
test(
87+
'resolves an AssetImageSource background into MemoryImageSource',
88+
() async {
89+
final scene = Scene(
90+
width: 10,
91+
height: 10,
92+
background: AssetImageSource('images/bg.png'),
7093
);
71-
final bundle = _FakeAssetBundle(Uint8List.fromList([9, 9, 9]));
72-
73-
final resolved = await resolveSceneAssetSources(scene, bundle);
74-
75-
expect(identical(resolved, scene), isTrue);
76-
expect(bundle.loadedKeys, isEmpty);
77-
});
78-
79-
test('resolves an AssetImageSource background into MemoryImageSource', () async {
80-
final scene = Scene(
81-
width: 10,
82-
height: 10,
83-
background: AssetImageSource('images/bg.png'),
84-
);
85-
final bundle = _FakeAssetBundle(Uint8List.fromList([1, 2, 3]));
86-
87-
final resolved = await resolveSceneAssetSources(scene, bundle);
88-
89-
expect(resolved.background, isA<MemoryImageSource>());
90-
expect(
91-
(resolved.background as MemoryImageSource).bytes,
92-
[1, 2, 3],
93-
);
94-
expect(bundle.loadedKeys, ['images/bg.png']);
95-
});
96-
97-
test('resolves an AssetImageSource on an ImageLayer, preserving fields', () async {
98-
final scene = Scene(width: 10, height: 10)
99-
..add(
100-
Layers.image(
101-
source: AssetImageSource('images/logo.png'),
102-
size: const Size(10, 10),
103-
fit: BoxFit.cover,
104-
position: const Offset(1, 2),
105-
),
106-
);
107-
final bundle = _FakeAssetBundle(Uint8List.fromList([4, 5, 6]));
108-
109-
final resolved = await resolveSceneAssetSources(scene, bundle);
110-
111-
final layer = resolved.layers.single as ImageLayer;
112-
expect(layer.source, isA<MemoryImageSource>());
113-
expect(layer.fit, ImageFit.cover);
114-
expect(layer.transform.position, const Point2D(1, 2));
115-
expect(layer.size, const Size2D(10, 10));
116-
});
94+
final bundle = _FakeAssetBundle(Uint8List.fromList([1, 2, 3]));
95+
96+
final resolved = await resolveSceneAssetSources(scene, bundle);
97+
98+
expect(resolved.background, isA<MemoryImageSource>());
99+
expect((resolved.background as MemoryImageSource).bytes, [1, 2, 3]);
100+
expect(bundle.loadedKeys, ['images/bg.png']);
101+
},
102+
);
103+
104+
test(
105+
'resolves an AssetImageSource on an ImageLayer, preserving fields',
106+
() async {
107+
final scene = Scene(width: 10, height: 10)
108+
..add(
109+
Layers.image(
110+
source: AssetImageSource('images/logo.png'),
111+
size: const Size(10, 10),
112+
fit: BoxFit.cover,
113+
position: const Offset(1, 2),
114+
),
115+
);
116+
final bundle = _FakeAssetBundle(Uint8List.fromList([4, 5, 6]));
117+
118+
final resolved = await resolveSceneAssetSources(scene, bundle);
119+
120+
final layer = resolved.layers.single as ImageLayer;
121+
expect(layer.source, isA<MemoryImageSource>());
122+
expect(layer.fit, ImageFit.cover);
123+
expect(layer.transform.position, const Point2D(1, 2));
124+
expect(layer.size, const Size2D(10, 10));
125+
},
126+
);
117127

118128
test('recurses into Group children', () async {
119129
final scene = Scene(width: 10, height: 10)

test/svg_layer_test.dart

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,7 @@ void main() {
5454

5555
// See pumpUntilImageRenders's doc comment for why this needs runAsync.
5656
await tester.runAsync(() async {
57-
await tester.pumpWidget(
58-
_wrap(SvgLayer(document, width: 40, height: 30)),
59-
);
57+
await tester.pumpWidget(_wrap(SvgLayer(document, width: 40, height: 30)));
6058
await pumpUntilImageRenders(tester);
6159
});
6260

0 commit comments

Comments
 (0)