From 88d3fc562794eb9365ade0914124daa9aea0ba5a Mon Sep 17 00:00:00 2001 From: Mao Yufeng Date: Thu, 14 Oct 2021 11:20:07 +0900 Subject: [PATCH 1/7] fix: only reizing needed axis --- .../lib/src/snapshot_support.dart | 59 +++++++++++++++---- 1 file changed, 49 insertions(+), 10 deletions(-) diff --git a/playbook_snapshot/lib/src/snapshot_support.dart b/playbook_snapshot/lib/src/snapshot_support.dart index db8c618..31e179e 100644 --- a/playbook_snapshot/lib/src/snapshot_support.dart +++ b/playbook_snapshot/lib/src/snapshot_support.dart @@ -50,8 +50,9 @@ class SnapshotSupport { for (final scrollView in scrollViews) { extendedSize = _extendScrollableSnapshotSize( scrollView: scrollView, - extendedSize: extendedSize, + currentExtendedSize: extendedSize, originSize: lastExtendedSize, + resizingTarget: scenario.layout.compressedResizingTarget, ); } if (extendedSize <= lastExtendedSize) break; @@ -87,30 +88,42 @@ class SnapshotSupport { static Size _extendScrollableSnapshotSize({ required ScrollView scrollView, - required Size extendedSize, + required Size currentExtendedSize, required Size originSize, + required _CompressedResizingTarget resizingTarget, }) { final controller = scrollView.controller; if (controller == null) { return Size( - max(extendedSize.width, originSize.width), - max(extendedSize.height, originSize.height), + resizingTarget.needResizingWidth + ? max(currentExtendedSize.width, originSize.width) + : originSize.width, + resizingTarget.needResizingHeight + ? max(currentExtendedSize.height, originSize.height) + : originSize.height, ); } final scrollAxis = controller.position.axis; final maxScrollExtent = controller.position.maxScrollExtent; + final Size newExtendedSize; switch (scrollAxis) { case Axis.horizontal: - final height = max(originSize.height, extendedSize.height); - final width = max(maxScrollExtent + originSize.width, extendedSize.width); - return Size(width, height); + final height = max(originSize.height, currentExtendedSize.height); + final width = max(maxScrollExtent + originSize.width, currentExtendedSize.width); + newExtendedSize = Size(width, height); + break; case Axis.vertical: - final height = max(maxScrollExtent + originSize.height, extendedSize.height); - final width = max(originSize.width, extendedSize.width); - return Size(width, height); + final height = max(maxScrollExtent + originSize.height, currentExtendedSize.height); + final width = max(originSize.width, currentExtendedSize.width); + newExtendedSize = Size(width, height); + break; } + return Size( + resizingTarget.needResizingWidth ? newExtendedSize.width : originSize.width, + resizingTarget.needResizingHeight ? newExtendedSize.height : originSize.height, + ); } } @@ -123,6 +136,18 @@ extension on ScenarioLayout { return v is ScenarioLayoutCompressed || h is ScenarioLayoutCompressed; } + _CompressedResizingTarget get compressedResizingTarget { + if (v is ScenarioLayoutCompressed && h is ScenarioLayoutCompressed) { + return _CompressedResizingTarget.both; + } else if (v is ScenarioLayoutCompressed) { + return _CompressedResizingTarget.vertical; + } else if (h is ScenarioLayoutCompressed) { + return _CompressedResizingTarget.horizontal; + } else { + throw StateError('No need compressed resizing.'); + } + } + double absoluteWidth(SnapshotDevice device) { switch (h.runtimeType) { case ScenarioLayoutFixed: @@ -147,3 +172,17 @@ extension on ScenarioLayout { return device.size.height; } } + +enum _CompressedResizingTarget { + horizontal, + vertical, + both, +} + +extension on _CompressedResizingTarget { + bool get needResizingWidth => + this == _CompressedResizingTarget.both || this == _CompressedResizingTarget.horizontal; + + bool get needResizingHeight => + this == _CompressedResizingTarget.both || this == _CompressedResizingTarget.vertical; +} From 31ce96f63e29aae24642a798d7d219e4f0b701c9 Mon Sep 17 00:00:00 2001 From: Mao Yufeng Date: Thu, 14 Oct 2021 11:28:48 +0900 Subject: [PATCH 2/7] feat: only retry resizing in a limit count --- playbook_snapshot/lib/src/snapshot_support.dart | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/playbook_snapshot/lib/src/snapshot_support.dart b/playbook_snapshot/lib/src/snapshot_support.dart index 31e179e..876152f 100644 --- a/playbook_snapshot/lib/src/snapshot_support.dart +++ b/playbook_snapshot/lib/src/snapshot_support.dart @@ -7,6 +7,8 @@ import 'package:playbook/playbook.dart'; import 'snapshot_device.dart'; class SnapshotSupport { + static const _maxTryResizeCount = 10; + static Future startDevice( Widget target, WidgetTester tester, @@ -39,6 +41,7 @@ class SnapshotSupport { // However, maxScrollExtent may report incorrectly. // To solve this, we repeatedly calculate size and update size until we can get a stable value. var lastExtendedSize = device.size; + var resize = 0; while (true) { final scrollViews = find .byWidgetPredicate((widget) => widget is ScrollView) @@ -58,6 +61,11 @@ class SnapshotSupport { if (extendedSize <= lastExtendedSize) break; lastExtendedSize = extendedSize; await _setSnapshotSize(tester, lastExtendedSize); + resize++; + if (resize >= _maxTryResizeCount) { + throw StateError( + 'Try resizing too many times. Please try to set your scenario to have a fixed size. Current is ${scenario.layout}.'); + } } snapshotSize = lastExtendedSize; } else { From ee3fad31147c8f74c83b0fd6f5c3f7819a127535 Mon Sep 17 00:00:00 2001 From: Mao Yufeng Date: Thu, 14 Oct 2021 16:29:37 +0900 Subject: [PATCH 3/7] fix: calculate initiala extendedSize --- playbook_snapshot/lib/src/snapshot_support.dart | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/playbook_snapshot/lib/src/snapshot_support.dart b/playbook_snapshot/lib/src/snapshot_support.dart index 876152f..2829ed1 100644 --- a/playbook_snapshot/lib/src/snapshot_support.dart +++ b/playbook_snapshot/lib/src/snapshot_support.dart @@ -40,7 +40,14 @@ class SnapshotSupport { // We use scrollController.maxScrollExtent to calculate the snapshot size. // However, maxScrollExtent may report incorrectly. // To solve this, we repeatedly calculate size and update size until we can get a stable value. - var lastExtendedSize = device.size; + var lastExtendedSize = Size( + scenario.layout.compressedResizingTarget.needResizingWidth + ? device.size.width + : absoluteSize.width, + scenario.layout.compressedResizingTarget.needResizingHeight + ? device.size.height + : absoluteSize.height, + ); var resize = 0; while (true) { final scrollViews = find From 75cc166feed5effc8f3e3c640c15eb23a970ff41 Mon Sep 17 00:00:00 2001 From: Mao Yufeng Date: Thu, 14 Oct 2021 16:43:51 +0900 Subject: [PATCH 4/7] fix: find scrollable instead of scrollView --- playbook_snapshot/lib/src/snapshot_support.dart | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/playbook_snapshot/lib/src/snapshot_support.dart b/playbook_snapshot/lib/src/snapshot_support.dart index 2829ed1..deab782 100644 --- a/playbook_snapshot/lib/src/snapshot_support.dart +++ b/playbook_snapshot/lib/src/snapshot_support.dart @@ -50,16 +50,16 @@ class SnapshotSupport { ); var resize = 0; while (true) { - final scrollViews = find - .byWidgetPredicate((widget) => widget is ScrollView) + final scrollables = find + .byWidgetPredicate((widget) => widget is Scrollable) .evaluate() - .map((e) => e.widget as ScrollView); - if (scrollViews.isEmpty) break; + .map((e) => e.widget as Scrollable); + if (scrollables.isEmpty) break; var extendedSize = device.size; - for (final scrollView in scrollViews) { + for (final scrollable in scrollables) { extendedSize = _extendScrollableSnapshotSize( - scrollView: scrollView, + scrollable: scrollable, currentExtendedSize: extendedSize, originSize: lastExtendedSize, resizingTarget: scenario.layout.compressedResizingTarget, @@ -102,12 +102,12 @@ class SnapshotSupport { } static Size _extendScrollableSnapshotSize({ - required ScrollView scrollView, + required Scrollable scrollable, required Size currentExtendedSize, required Size originSize, required _CompressedResizingTarget resizingTarget, }) { - final controller = scrollView.controller; + final controller = scrollable.controller; if (controller == null) { return Size( resizingTarget.needResizingWidth From 43d93aa5f3245cac983b04fbf6e4fdadd857f915 Mon Sep 17 00:00:00 2001 From: Mao Yufeng Date: Thu, 14 Oct 2021 17:31:54 +0900 Subject: [PATCH 5/7] feat: update error message --- playbook_snapshot/lib/src/snapshot_support.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/playbook_snapshot/lib/src/snapshot_support.dart b/playbook_snapshot/lib/src/snapshot_support.dart index deab782..2fe0448 100644 --- a/playbook_snapshot/lib/src/snapshot_support.dart +++ b/playbook_snapshot/lib/src/snapshot_support.dart @@ -71,7 +71,7 @@ class SnapshotSupport { resize++; if (resize >= _maxTryResizeCount) { throw StateError( - 'Try resizing too many times. Please try to set your scenario to have a fixed size. Current is ${scenario.layout}.'); + 'Try resizing too many times. Please try to set your scenario to have a fixed size.'); } } snapshotSize = lastExtendedSize; From 4d8a658a9ff0ad6103d2722c83b272dbe508ac1b Mon Sep 17 00:00:00 2001 From: Mao Yufeng Date: Thu, 14 Oct 2021 17:48:37 +0900 Subject: [PATCH 6/7] fix: also check resize too large --- playbook_snapshot/lib/src/snapshot_support.dart | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/playbook_snapshot/lib/src/snapshot_support.dart b/playbook_snapshot/lib/src/snapshot_support.dart index 2fe0448..61ab0fc 100644 --- a/playbook_snapshot/lib/src/snapshot_support.dart +++ b/playbook_snapshot/lib/src/snapshot_support.dart @@ -8,6 +8,7 @@ import 'snapshot_device.dart'; class SnapshotSupport { static const _maxTryResizeCount = 10; + static const _maxSnapshotSize = 50000; static Future startDevice( Widget target, @@ -73,6 +74,10 @@ class SnapshotSupport { throw StateError( 'Try resizing too many times. Please try to set your scenario to have a fixed size.'); } + if (extendedSize.width >= _maxSnapshotSize || extendedSize.height >= _maxSnapshotSize) { + throw StateError( + 'Try resizing too large size. Please try to set your scenario to have a fixed size.'); + } } snapshotSize = lastExtendedSize; } else { From 07d9aa7743a5a1500fe1f81101028d59f3fe479c Mon Sep 17 00:00:00 2001 From: Mao Yufeng Date: Thu, 14 Oct 2021 18:49:17 +0900 Subject: [PATCH 7/7] fix: try catch controller position --- playbook_snapshot/lib/src/snapshot_support.dart | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/playbook_snapshot/lib/src/snapshot_support.dart b/playbook_snapshot/lib/src/snapshot_support.dart index 61ab0fc..a21f925 100644 --- a/playbook_snapshot/lib/src/snapshot_support.dart +++ b/playbook_snapshot/lib/src/snapshot_support.dart @@ -76,7 +76,7 @@ class SnapshotSupport { } if (extendedSize.width >= _maxSnapshotSize || extendedSize.height >= _maxSnapshotSize) { throw StateError( - 'Try resizing too large size. Please try to set your scenario to have a fixed size.'); + 'Try resizing too large size ${extendedSize}. Please try to set your scenario to have a fixed size.'); } } snapshotSize = lastExtendedSize; @@ -113,7 +113,11 @@ class SnapshotSupport { required _CompressedResizingTarget resizingTarget, }) { final controller = scrollable.controller; - if (controller == null) { + ScrollPosition? position; + try { + position = controller?.position; + } catch (_) {} + if (position == null) { return Size( resizingTarget.needResizingWidth ? max(currentExtendedSize.width, originSize.width) @@ -124,8 +128,8 @@ class SnapshotSupport { ); } - final scrollAxis = controller.position.axis; - final maxScrollExtent = controller.position.maxScrollExtent; + final scrollAxis = position.axis; + final maxScrollExtent = position.maxScrollExtent; final Size newExtendedSize; switch (scrollAxis) {