From 02eab150ed445873cdaac26bbf3cf139f965aaa4 Mon Sep 17 00:00:00 2001 From: zhuyifei Date: Wed, 10 Jan 2024 16:16:00 +0800 Subject: [PATCH] fix: devtools display error when debugger with the real machine --- webf/lib/src/devtools/modules/page.dart | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/webf/lib/src/devtools/modules/page.dart b/webf/lib/src/devtools/modules/page.dart index ac5b9308c3..c892c34e3d 100644 --- a/webf/lib/src/devtools/modules/page.dart +++ b/webf/lib/src/devtools/modules/page.dart @@ -4,6 +4,7 @@ */ import 'dart:convert'; +import 'dart:math' as math; import 'dart:typed_data'; import 'package:meta/meta.dart'; @@ -188,6 +189,8 @@ class InspectPageModule extends UIInspectorModule { switch (method) { case 'startScreencast': sendToFrontend(id, null); + _devToolsMaxWidth = params?['maxWidth'] ?? 0; + _devToolsMaxHeight = params?['maxHeight'] ?? 0; startScreenCast(); break; case 'stopScreencast': @@ -222,10 +225,20 @@ class InspectPageModule extends UIInspectorModule { int? _lastSentSessionID; bool _isFramingScreenCast = false; + int _devToolsMaxWidth = 0; + int _devToolsMaxHeight = 0; void _frameScreenCast(Duration timeStamp) { Element root = document.documentElement!; - root.toBlob().then((Uint8List screenShot) { + // the devtools of some pc do not automatically scale. so modify devicePixelRatio for it + double? devicePixelRatio; + double viewportWidth = document.viewport!.viewportSize.width; + double viewportHeight = document.viewport!.viewportSize.height; + if (_devToolsMaxWidth > 0 && _devToolsMaxHeight > 0 && viewportWidth > 0 && viewportHeight > 0) { + devicePixelRatio = math.min(_devToolsMaxHeight / viewportHeight, _devToolsMaxHeight / viewportHeight); + devicePixelRatio = math.min(devicePixelRatio, document.controller.ownerFlutterView.devicePixelRatio); + } + root.toBlob(devicePixelRatio: devicePixelRatio).then((Uint8List screenShot) { String encodedImage = base64Encode(screenShot); _lastSentSessionID = timeStamp.inMilliseconds; InspectorEvent event = PageScreenCastFrameEvent(ScreenCastFrame(