This repository was archived by the owner on Feb 25, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6k
Reland "[web] Fix Scene clip bounds. Trigger resize on DPR Change." #50457
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
lib/web_ui/lib/src/engine/view_embedder/display_dpr_stream.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| // Copyright 2013 The Flutter Authors. All rights reserved. | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| import 'dart:async'; | ||
| import 'dart:js_interop'; | ||
|
|
||
| import 'package:meta/meta.dart'; | ||
| import 'package:ui/src/engine/display.dart'; | ||
| import 'package:ui/src/engine/dom.dart'; | ||
| import 'package:ui/ui.dart' as ui show Display; | ||
|
|
||
| /// Provides a stream of `devicePixelRatio` changes for the given display. | ||
| /// | ||
| /// Note that until the Window Management API is generally available, this class | ||
| /// only monitors the global `devicePixelRatio` property, provided by the default | ||
| /// [EngineFlutterDisplay.instance]. | ||
| /// | ||
| /// See: https://developer.mozilla.org/en-US/docs/Web/API/Window_Management_API | ||
| class DisplayDprStream { | ||
| DisplayDprStream( | ||
| this._display, { | ||
| @visibleForTesting DebugDisplayDprStreamOverrides? overrides, | ||
| }) : _currentDpr = _display.devicePixelRatio, | ||
| _debugOverrides = overrides { | ||
| // Start listening to DPR changes. | ||
| _subscribeToMediaQuery(); | ||
| } | ||
|
|
||
| /// A singleton instance of DisplayDprStream. | ||
| static DisplayDprStream instance = | ||
| DisplayDprStream(EngineFlutterDisplay.instance); | ||
|
|
||
| // The display object that will provide the DPR information. | ||
| final ui.Display _display; | ||
|
|
||
| // Last reported value of DPR. | ||
| double _currentDpr; | ||
|
|
||
| // Controls the [dprChanged] broadcast Stream. | ||
| final StreamController<double> _dprStreamController = | ||
| StreamController<double>.broadcast(); | ||
|
|
||
| // Object that fires a `change` event for the `_currentDpr`. | ||
| late DomEventTarget _dprMediaQuery; | ||
|
|
||
| // Creates the media query for the latest known DPR value, and adds a change listener to it. | ||
| void _subscribeToMediaQuery() { | ||
| if (_debugOverrides?.getMediaQuery != null) { | ||
| _dprMediaQuery = _debugOverrides!.getMediaQuery!(_currentDpr); | ||
| } else { | ||
| _dprMediaQuery = domWindow.matchMedia('(resolution: ${_currentDpr}dppx)'); | ||
| } | ||
| _dprMediaQuery.addEventListenerWithOptions( | ||
| 'change', | ||
| createDomEventListener(_onDprMediaQueryChange), | ||
| <String, Object>{ | ||
| // We only listen `once` because this event only triggers once when the | ||
| // DPR changes from `_currentDpr`. Once that happens, we need a new | ||
| // `_dprMediaQuery` that is watching the new `_currentDpr`. | ||
| // | ||
| // By using `once`, we don't need to worry about detaching the event | ||
| // listener from the old mediaQuery after we're done with it. | ||
| 'once': true, | ||
| 'passive': true, | ||
| }, | ||
| ); | ||
| } | ||
|
|
||
| // Handler of the _dprMediaQuery 'change' event. | ||
| // | ||
| // This calls subscribe again because events are listened to with `once: true`. | ||
| JSVoid _onDprMediaQueryChange(DomEvent _) { | ||
| _currentDpr = _display.devicePixelRatio; | ||
| _dprStreamController.add(_currentDpr); | ||
| // Re-subscribe... | ||
| _subscribeToMediaQuery(); | ||
| } | ||
|
|
||
| /// A stream that emits the latest value of [EngineFlutterDisplay.instance.devicePixelRatio]. | ||
| Stream<double> get dprChanged => _dprStreamController.stream; | ||
|
|
||
| // The overrides object that is used for testing. | ||
| final DebugDisplayDprStreamOverrides? _debugOverrides; | ||
| } | ||
|
|
||
| @visibleForTesting | ||
| class DebugDisplayDprStreamOverrides { | ||
| DebugDisplayDprStreamOverrides({ | ||
| this.getMediaQuery, | ||
| }); | ||
| final DomEventTarget Function(double currentValue)? getMediaQuery; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: why capital "L"?