diff --git a/lib/web_ui/lib/src/engine/window.dart b/lib/web_ui/lib/src/engine/window.dart index 557dbf21b85f6..9949e7488d0bd 100644 --- a/lib/web_ui/lib/src/engine/window.dart +++ b/lib/web_ui/lib/src/engine/window.dart @@ -47,11 +47,24 @@ class EngineWindow extends ui.Window { @override ui.Size get physicalSize { + if (_physicalSize?.value == null) { + _computePhysicalSize(); + } + assert(_physicalSize != null); + assert(_physicalSize.value != null); + return _physicalSize.value; + } + + /// Computes the physical size of the screen from [html.window]. + /// + /// This function is expensive. It triggers browser layout if there are + /// pending DOM writes. + void _computePhysicalSize() { bool override = false; assert(() { if (webOnlyDebugPhysicalSizeOverride != null) { - _physicalSize = webOnlyDebugPhysicalSizeOverride; + _physicalSize = FrameReference(webOnlyDebugPhysicalSizeOverride); override = true; } return true; @@ -68,23 +81,15 @@ class EngineWindow extends ui.Window { windowInnerWidth = html.window.innerWidth * devicePixelRatio; windowInnerHeight = html.window.innerHeight * devicePixelRatio; } - if (windowInnerWidth != _lastKnownWindowInnerWidth || - windowInnerHeight != _lastKnownWindowInnerHeight) { - _lastKnownWindowInnerWidth = windowInnerWidth; - _lastKnownWindowInnerHeight = windowInnerHeight; - _physicalSize = ui.Size( - windowInnerWidth, - windowInnerHeight, - ); - } + _physicalSize = FrameReference(ui.Size( + windowInnerWidth, + windowInnerHeight, + )); } - - return _physicalSize; } - ui.Size _physicalSize = ui.Size.zero; - double _lastKnownWindowInnerWidth = -1; - double _lastKnownWindowInnerHeight = -1; + /// Lazily populated and cleared at the end of the frame. + FrameReference _physicalSize; /// Overrides the value of [physicalSize] in tests. ui.Size webOnlyDebugPhysicalSizeOverride;