Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 18f72b5

Browse files
authored
Move viewConfiguration parsing from PlatformDispatcher to _hooks (#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 #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
1 parent 48558a3 commit 18f72b5

File tree

2 files changed

+96
-95
lines changed

2 files changed

+96
-95
lines changed

lib/ui/hooks.dart

Lines changed: 92 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,98 @@ void _updateDisplays(
3030
PlatformDispatcher.instance._updateDisplays(displays);
3131
}
3232

33+
List<DisplayFeature> _decodeDisplayFeatures({
34+
required List<double> bounds,
35+
required List<int> type,
36+
required List<int> state,
37+
required double devicePixelRatio,
38+
}) {
39+
assert(bounds.length / 4 == type.length, 'Bounds are rectangles, requiring 4 measurements each');
40+
assert(type.length == state.length);
41+
final List<DisplayFeature> result = <DisplayFeature>[];
42+
for(int i = 0; i < type.length; i++) {
43+
final int rectOffset = i * 4;
44+
result.add(DisplayFeature(
45+
bounds: Rect.fromLTRB(
46+
bounds[rectOffset] / devicePixelRatio,
47+
bounds[rectOffset + 1] / devicePixelRatio,
48+
bounds[rectOffset + 2] / devicePixelRatio,
49+
bounds[rectOffset + 3] / devicePixelRatio,
50+
),
51+
type: DisplayFeatureType.values[type[i]],
52+
state: state[i] < DisplayFeatureState.values.length
53+
? DisplayFeatureState.values[state[i]]
54+
: DisplayFeatureState.unknown,
55+
));
56+
}
57+
return result;
58+
}
59+
60+
_ViewConfiguration _buildViewConfiguration(
61+
double devicePixelRatio,
62+
double width,
63+
double height,
64+
double viewPaddingTop,
65+
double viewPaddingRight,
66+
double viewPaddingBottom,
67+
double viewPaddingLeft,
68+
double viewInsetTop,
69+
double viewInsetRight,
70+
double viewInsetBottom,
71+
double viewInsetLeft,
72+
double systemGestureInsetTop,
73+
double systemGestureInsetRight,
74+
double systemGestureInsetBottom,
75+
double systemGestureInsetLeft,
76+
double physicalTouchSlop,
77+
List<double> displayFeaturesBounds,
78+
List<int> displayFeaturesType,
79+
List<int> displayFeaturesState,
80+
int displayId,
81+
) {
82+
return _ViewConfiguration(
83+
devicePixelRatio: devicePixelRatio,
84+
geometry: Rect.fromLTWH(0.0, 0.0, width, height),
85+
viewPadding: ViewPadding._(
86+
top: viewPaddingTop,
87+
right: viewPaddingRight,
88+
bottom: viewPaddingBottom,
89+
left: viewPaddingLeft,
90+
),
91+
viewInsets: ViewPadding._(
92+
top: viewInsetTop,
93+
right: viewInsetRight,
94+
bottom: viewInsetBottom,
95+
left: viewInsetLeft,
96+
),
97+
padding: ViewPadding._(
98+
top: math.max(0.0, viewPaddingTop - viewInsetTop),
99+
right: math.max(0.0, viewPaddingRight - viewInsetRight),
100+
bottom: math.max(0.0, viewPaddingBottom - viewInsetBottom),
101+
left: math.max(0.0, viewPaddingLeft - viewInsetLeft),
102+
),
103+
systemGestureInsets: ViewPadding._(
104+
top: math.max(0.0, systemGestureInsetTop),
105+
right: math.max(0.0, systemGestureInsetRight),
106+
bottom: math.max(0.0, systemGestureInsetBottom),
107+
left: math.max(0.0, systemGestureInsetLeft),
108+
),
109+
gestureSettings: GestureSettings(
110+
physicalTouchSlop: physicalTouchSlop == _kUnsetGestureSetting ? null : physicalTouchSlop,
111+
),
112+
displayFeatures: _decodeDisplayFeatures(
113+
bounds: displayFeaturesBounds,
114+
type: displayFeaturesType,
115+
state: displayFeaturesState,
116+
devicePixelRatio: devicePixelRatio,
117+
),
118+
displayId: displayId,
119+
);
120+
}
121+
33122
@pragma('vm:entry-point')
34123
void _updateWindowMetrics(
35-
int id,
124+
int viewId,
36125
double devicePixelRatio,
37126
double width,
38127
double height,
@@ -54,8 +143,7 @@ void _updateWindowMetrics(
54143
List<int> displayFeaturesState,
55144
int displayId,
56145
) {
57-
PlatformDispatcher.instance._updateWindowMetrics(
58-
id,
146+
final _ViewConfiguration viewConfiguration = _buildViewConfiguration(
59147
devicePixelRatio,
60148
width,
61149
height,
@@ -77,6 +165,7 @@ void _updateWindowMetrics(
77165
displayFeaturesState,
78166
displayId,
79167
);
168+
PlatformDispatcher.instance._updateWindowMetrics(viewId, viewConfiguration);
80169
}
81170

82171
typedef _LocaleClosure = String Function();

lib/ui/platform_dispatcher.dart

Lines changed: 4 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -263,107 +263,19 @@ class PlatformDispatcher {
263263
// Called from the engine, via hooks.dart
264264
//
265265
// Updates the metrics of the window with the given id.
266-
void _updateWindowMetrics(
267-
int id,
268-
double devicePixelRatio,
269-
double width,
270-
double height,
271-
double viewPaddingTop,
272-
double viewPaddingRight,
273-
double viewPaddingBottom,
274-
double viewPaddingLeft,
275-
double viewInsetTop,
276-
double viewInsetRight,
277-
double viewInsetBottom,
278-
double viewInsetLeft,
279-
double systemGestureInsetTop,
280-
double systemGestureInsetRight,
281-
double systemGestureInsetBottom,
282-
double systemGestureInsetLeft,
283-
double physicalTouchSlop,
284-
List<double> displayFeaturesBounds,
285-
List<int> displayFeaturesType,
286-
List<int> displayFeaturesState,
287-
int displayId,
288-
) {
289-
final _ViewConfiguration viewConfiguration = _ViewConfiguration(
290-
devicePixelRatio: devicePixelRatio,
291-
geometry: Rect.fromLTWH(0.0, 0.0, width, height),
292-
viewPadding: ViewPadding._(
293-
top: viewPaddingTop,
294-
right: viewPaddingRight,
295-
bottom: viewPaddingBottom,
296-
left: viewPaddingLeft,
297-
),
298-
viewInsets: ViewPadding._(
299-
top: viewInsetTop,
300-
right: viewInsetRight,
301-
bottom: viewInsetBottom,
302-
left: viewInsetLeft,
303-
),
304-
padding: ViewPadding._(
305-
top: math.max(0.0, viewPaddingTop - viewInsetTop),
306-
right: math.max(0.0, viewPaddingRight - viewInsetRight),
307-
bottom: math.max(0.0, viewPaddingBottom - viewInsetBottom),
308-
left: math.max(0.0, viewPaddingLeft - viewInsetLeft),
309-
),
310-
systemGestureInsets: ViewPadding._(
311-
top: math.max(0.0, systemGestureInsetTop),
312-
right: math.max(0.0, systemGestureInsetRight),
313-
bottom: math.max(0.0, systemGestureInsetBottom),
314-
left: math.max(0.0, systemGestureInsetLeft),
315-
),
316-
gestureSettings: GestureSettings(
317-
physicalTouchSlop: physicalTouchSlop == _kUnsetGestureSetting ? null : physicalTouchSlop,
318-
),
319-
displayFeatures: _decodeDisplayFeatures(
320-
bounds: displayFeaturesBounds,
321-
type: displayFeaturesType,
322-
state: displayFeaturesState,
323-
devicePixelRatio: devicePixelRatio,
324-
),
325-
displayId: displayId,
326-
);
327-
328-
final FlutterView? view = _views[id];
329-
if (id == _kImplicitViewId && view == null) {
266+
void _updateWindowMetrics(int viewId, _ViewConfiguration viewConfiguration) {
267+
final FlutterView? view = _views[viewId];
268+
if (viewId == _kImplicitViewId && view == null) {
330269
// TODO(goderbauer): Remove the implicit creation of the implicit view
331270
// when we have an addView API and the implicit view is added via that.
332-
_views[id] = FlutterView._(id, this, viewConfiguration);
271+
_views[viewId] = FlutterView._(viewId, this, viewConfiguration);
333272
} else {
334273
assert(view != null);
335274
view!._viewConfiguration = viewConfiguration;
336275
}
337-
338276
_invoke(onMetricsChanged, _onMetricsChangedZone);
339277
}
340278

341-
List<DisplayFeature> _decodeDisplayFeatures({
342-
required List<double> bounds,
343-
required List<int> type,
344-
required List<int> state,
345-
required double devicePixelRatio,
346-
}) {
347-
assert(bounds.length / 4 == type.length, 'Bounds are rectangles, requiring 4 measurements each');
348-
assert(type.length == state.length);
349-
final List<DisplayFeature> result = <DisplayFeature>[];
350-
for(int i = 0; i < type.length; i++) {
351-
final int rectOffset = i * 4;
352-
result.add(DisplayFeature(
353-
bounds: Rect.fromLTRB(
354-
bounds[rectOffset] / devicePixelRatio,
355-
bounds[rectOffset + 1] / devicePixelRatio,
356-
bounds[rectOffset + 2] / devicePixelRatio,
357-
bounds[rectOffset + 3] / devicePixelRatio,
358-
),
359-
type: DisplayFeatureType.values[type[i]],
360-
state: state[i] < DisplayFeatureState.values.length
361-
? DisplayFeatureState.values[state[i]]
362-
: DisplayFeatureState.unknown,
363-
));
364-
}
365-
return result;
366-
}
367279

368280
/// A callback invoked when any view begins a frame.
369281
///

0 commit comments

Comments
 (0)