Move viewConfiguration parsing from PlatformDispatcher to _hooks (flutter/engine#44787)

This PR moves the code that parses `viewConfiguration` from
`PlatformDispatcher` to `_hooks`. This makes `PlatformDispatcher`'s API
cleaner by hiding the encoding implementation of `ViewConfiguration` in
`_hooks`, and allows more APIs to pass view configuration, such as the
`addView` that will be introduced in
https://github.com/flutter/engine/pull/42991.

This PR should not need unit tests since it's just a refactor, and the
code path that contains `_updateWindowMetrics` has been tested in
existing unit tests.

## Pre-launch Checklist

- [ ] I read the [Contributor Guide] and followed the process outlined
there for submitting PRs.
- [ ] I read the [Tree Hygiene] wiki page, which explains my
responsibilities.
- [ ] I read and followed the [Flutter Style Guide] and the [C++,
Objective-C, Java style guides].
- [ ] I listed at least one issue that this PR fixes in the description
above.
- [ ] I added new tests to check the change I am making or feature I am
adding, or Hixie said the PR is test-exempt. See [testing the engine]
for instructions on writing and running engine tests.
- [ ] I updated/added relevant documentation (doc comments with `///`).
- [ ] I signed the [CLA].
- [ ] All existing and new tests are passing.

If you need help, consider asking for advice on the #hackers-new channel
on [Discord].

<!-- Links -->
[Contributor Guide]:
https://github.com/flutter/flutter/wiki/Tree-hygiene#overview
[Tree Hygiene]: https://github.com/flutter/flutter/wiki/Tree-hygiene
[Flutter Style Guide]:
https://github.com/flutter/flutter/wiki/Style-guide-for-Flutter-repo
[C++, Objective-C, Java style guides]:
https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style
[testing the engine]:
https://github.com/flutter/flutter/wiki/Testing-the-engine
[CLA]: https://cla.developers.google.com/
[flutter/tests]: https://github.com/flutter/tests
[breaking change policy]:
https://github.com/flutter/flutter/wiki/Tree-hygiene#handling-breaking-changes
[Discord]: https://github.com/flutter/flutter/wiki/Chat
This commit is contained in:
Tong Mu
2023-08-17 09:29:12 -07:00
committed by GitHub
parent a2bf6ed1f2
commit bd3da2c47e
2 changed files with 98 additions and 97 deletions

View File

@@ -30,9 +30,34 @@ void _updateDisplays(
PlatformDispatcher.instance._updateDisplays(displays);
}
@pragma('vm:entry-point')
void _updateWindowMetrics(
int id,
List<DisplayFeature> _decodeDisplayFeatures({
required List<double> bounds,
required List<int> type,
required List<int> state,
required double devicePixelRatio,
}) {
assert(bounds.length / 4 == type.length, 'Bounds are rectangles, requiring 4 measurements each');
assert(type.length == state.length);
final List<DisplayFeature> result = <DisplayFeature>[];
for(int i = 0; i < type.length; i++) {
final int rectOffset = i * 4;
result.add(DisplayFeature(
bounds: Rect.fromLTRB(
bounds[rectOffset] / devicePixelRatio,
bounds[rectOffset + 1] / devicePixelRatio,
bounds[rectOffset + 2] / devicePixelRatio,
bounds[rectOffset + 3] / devicePixelRatio,
),
type: DisplayFeatureType.values[type[i]],
state: state[i] < DisplayFeatureState.values.length
? DisplayFeatureState.values[state[i]]
: DisplayFeatureState.unknown,
));
}
return result;
}
_ViewConfiguration _buildViewConfiguration(
double devicePixelRatio,
double width,
double height,
@@ -54,8 +79,71 @@ void _updateWindowMetrics(
List<int> displayFeaturesState,
int displayId,
) {
PlatformDispatcher.instance._updateWindowMetrics(
id,
return _ViewConfiguration(
devicePixelRatio: devicePixelRatio,
geometry: Rect.fromLTWH(0.0, 0.0, width, height),
viewPadding: ViewPadding._(
top: viewPaddingTop,
right: viewPaddingRight,
bottom: viewPaddingBottom,
left: viewPaddingLeft,
),
viewInsets: ViewPadding._(
top: viewInsetTop,
right: viewInsetRight,
bottom: viewInsetBottom,
left: viewInsetLeft,
),
padding: ViewPadding._(
top: math.max(0.0, viewPaddingTop - viewInsetTop),
right: math.max(0.0, viewPaddingRight - viewInsetRight),
bottom: math.max(0.0, viewPaddingBottom - viewInsetBottom),
left: math.max(0.0, viewPaddingLeft - viewInsetLeft),
),
systemGestureInsets: ViewPadding._(
top: math.max(0.0, systemGestureInsetTop),
right: math.max(0.0, systemGestureInsetRight),
bottom: math.max(0.0, systemGestureInsetBottom),
left: math.max(0.0, systemGestureInsetLeft),
),
gestureSettings: GestureSettings(
physicalTouchSlop: physicalTouchSlop == _kUnsetGestureSetting ? null : physicalTouchSlop,
),
displayFeatures: _decodeDisplayFeatures(
bounds: displayFeaturesBounds,
type: displayFeaturesType,
state: displayFeaturesState,
devicePixelRatio: devicePixelRatio,
),
displayId: displayId,
);
}
@pragma('vm:entry-point')
void _updateWindowMetrics(
int viewId,
double devicePixelRatio,
double width,
double height,
double viewPaddingTop,
double viewPaddingRight,
double viewPaddingBottom,
double viewPaddingLeft,
double viewInsetTop,
double viewInsetRight,
double viewInsetBottom,
double viewInsetLeft,
double systemGestureInsetTop,
double systemGestureInsetRight,
double systemGestureInsetBottom,
double systemGestureInsetLeft,
double physicalTouchSlop,
List<double> displayFeaturesBounds,
List<int> displayFeaturesType,
List<int> displayFeaturesState,
int displayId,
) {
final _ViewConfiguration viewConfiguration = _buildViewConfiguration(
devicePixelRatio,
width,
height,
@@ -77,6 +165,7 @@ void _updateWindowMetrics(
displayFeaturesState,
displayId,
);
PlatformDispatcher.instance._updateWindowMetrics(viewId, viewConfiguration);
}
typedef _LocaleClosure = String Function();

View File

@@ -263,107 +263,19 @@ class PlatformDispatcher {
// Called from the engine, via hooks.dart
//
// Updates the metrics of the window with the given id.
void _updateWindowMetrics(
int id,
double devicePixelRatio,
double width,
double height,
double viewPaddingTop,
double viewPaddingRight,
double viewPaddingBottom,
double viewPaddingLeft,
double viewInsetTop,
double viewInsetRight,
double viewInsetBottom,
double viewInsetLeft,
double systemGestureInsetTop,
double systemGestureInsetRight,
double systemGestureInsetBottom,
double systemGestureInsetLeft,
double physicalTouchSlop,
List<double> displayFeaturesBounds,
List<int> displayFeaturesType,
List<int> displayFeaturesState,
int displayId,
) {
final _ViewConfiguration viewConfiguration = _ViewConfiguration(
devicePixelRatio: devicePixelRatio,
geometry: Rect.fromLTWH(0.0, 0.0, width, height),
viewPadding: ViewPadding._(
top: viewPaddingTop,
right: viewPaddingRight,
bottom: viewPaddingBottom,
left: viewPaddingLeft,
),
viewInsets: ViewPadding._(
top: viewInsetTop,
right: viewInsetRight,
bottom: viewInsetBottom,
left: viewInsetLeft,
),
padding: ViewPadding._(
top: math.max(0.0, viewPaddingTop - viewInsetTop),
right: math.max(0.0, viewPaddingRight - viewInsetRight),
bottom: math.max(0.0, viewPaddingBottom - viewInsetBottom),
left: math.max(0.0, viewPaddingLeft - viewInsetLeft),
),
systemGestureInsets: ViewPadding._(
top: math.max(0.0, systemGestureInsetTop),
right: math.max(0.0, systemGestureInsetRight),
bottom: math.max(0.0, systemGestureInsetBottom),
left: math.max(0.0, systemGestureInsetLeft),
),
gestureSettings: GestureSettings(
physicalTouchSlop: physicalTouchSlop == _kUnsetGestureSetting ? null : physicalTouchSlop,
),
displayFeatures: _decodeDisplayFeatures(
bounds: displayFeaturesBounds,
type: displayFeaturesType,
state: displayFeaturesState,
devicePixelRatio: devicePixelRatio,
),
displayId: displayId,
);
final FlutterView? view = _views[id];
if (id == _kImplicitViewId && view == null) {
void _updateWindowMetrics(int viewId, _ViewConfiguration viewConfiguration) {
final FlutterView? view = _views[viewId];
if (viewId == _kImplicitViewId && view == null) {
// TODO(goderbauer): Remove the implicit creation of the implicit view
// when we have an addView API and the implicit view is added via that.
_views[id] = FlutterView._(id, this, viewConfiguration);
_views[viewId] = FlutterView._(viewId, this, viewConfiguration);
} else {
assert(view != null);
view!._viewConfiguration = viewConfiguration;
}
_invoke(onMetricsChanged, _onMetricsChangedZone);
}
List<DisplayFeature> _decodeDisplayFeatures({
required List<double> bounds,
required List<int> type,
required List<int> state,
required double devicePixelRatio,
}) {
assert(bounds.length / 4 == type.length, 'Bounds are rectangles, requiring 4 measurements each');
assert(type.length == state.length);
final List<DisplayFeature> result = <DisplayFeature>[];
for(int i = 0; i < type.length; i++) {
final int rectOffset = i * 4;
result.add(DisplayFeature(
bounds: Rect.fromLTRB(
bounds[rectOffset] / devicePixelRatio,
bounds[rectOffset + 1] / devicePixelRatio,
bounds[rectOffset + 2] / devicePixelRatio,
bounds[rectOffset + 3] / devicePixelRatio,
),
type: DisplayFeatureType.values[type[i]],
state: state[i] < DisplayFeatureState.values.length
? DisplayFeatureState.values[state[i]]
: DisplayFeatureState.unknown,
));
}
return result;
}
/// A callback invoked when any view begins a frame.
///