Skip to content

Commit

Permalink
Use PlatformDispatcher.instance over window where possible (#99496)
Browse files Browse the repository at this point in the history
  • Loading branch information
goderbauer authored Mar 3, 2022
1 parent a4aeb77 commit 7f2c1cd
Show file tree
Hide file tree
Showing 38 changed files with 175 additions and 197 deletions.
10 changes: 5 additions & 5 deletions dev/benchmarks/macrobenchmarks/lib/src/web/recorder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ abstract class SceneBuilderRecorder extends Recorder {
final Completer<Profile> profileCompleter = Completer<Profile>();
_profile = Profile(name: name);

window.onBeginFrame = (_) {
PlatformDispatcher.instance.onBeginFrame = (_) {
try {
startMeasureFrame(profile!);
onBeginFrame();
Expand All @@ -271,7 +271,7 @@ abstract class SceneBuilderRecorder extends Recorder {
rethrow;
}
};
window.onDrawFrame = () {
PlatformDispatcher.instance.onDrawFrame = () {
try {
_profile!.record('drawFrameDuration', () {
final SceneBuilder sceneBuilder = SceneBuilder();
Expand All @@ -286,7 +286,7 @@ abstract class SceneBuilderRecorder extends Recorder {
endMeasureFrame();

if (shouldContinue()) {
window.scheduleFrame();
PlatformDispatcher.instance.scheduleFrame();
} else {
profileCompleter.complete(_profile);
}
Expand All @@ -295,7 +295,7 @@ abstract class SceneBuilderRecorder extends Recorder {
rethrow;
}
};
window.scheduleFrame();
PlatformDispatcher.instance.scheduleFrame();
return profileCompleter.future;
}
}
Expand Down Expand Up @@ -403,7 +403,7 @@ abstract class WidgetRecorder extends Recorder implements FrameRecorder {
profile!.addDataPoint('drawFrameDuration', _drawFrameStopwatch.elapsed, reported: true);

if (shouldContinue()) {
window.scheduleFrame();
PlatformDispatcher.instance.scheduleFrame();
} else {
for (final VoidCallback fn in _didStopCallbacks)
fn();
Expand Down
6 changes: 2 additions & 4 deletions dev/benchmarks/test_apps/stocks/test/icon_color_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:ui' as ui show window;

import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:stocks/main.dart' as stocks;
Expand Down Expand Up @@ -61,8 +59,8 @@ void main() {
expect(find.text('Account Balance'), findsNothing);

// drag the drawer out
final Offset left = Offset(0.0, (ui.window.physicalSize / ui.window.devicePixelRatio).height / 2.0);
final Offset right = Offset((ui.window.physicalSize / ui.window.devicePixelRatio).width, left.dy);
final Offset left = Offset(0.0, (WidgetsBinding.instance.window.physicalSize / WidgetsBinding.instance.window.devicePixelRatio).height / 2.0);
final Offset right = Offset((WidgetsBinding.instance.window.physicalSize / WidgetsBinding.instance.window.devicePixelRatio).width, left.dy);
final TestGesture gesture = await tester.startGesture(left);
await tester.pump();
await gesture.moveTo(right);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:ui' as ui;

import 'package:collection/collection.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
Expand All @@ -14,8 +12,8 @@ VoidCallback? originalSemanticsListener;
void main() {
WidgetsFlutterBinding.ensureInitialized();
// Disconnects semantics listener for testing purposes.
originalSemanticsListener = ui.window.onSemanticsEnabledChanged;
ui.window.onSemanticsEnabledChanged = null;
originalSemanticsListener = WidgetsBinding.instance.platformDispatcher.onSemanticsEnabledChanged;
RendererBinding.instance.platformDispatcher.onSemanticsEnabledChanged = null;
RendererBinding.instance.setSemanticsEnabled(false);
// If the test passes, LifeCycleSpy will rewire the semantics listener back.
runApp(const LifeCycleSpy());
Expand Down Expand Up @@ -71,7 +69,7 @@ class _LifeCycleSpyState extends State<LifeCycleSpy> with WidgetsBindingObserver
if (const ListEquality<AppLifecycleState?>().equals(_actualLifeCycleSequence, _expectedLifeCycleSequence)) {
// Rewires the semantics harness if test passes.
RendererBinding.instance.setSemanticsEnabled(true);
ui.window.onSemanticsEnabledChanged = originalSemanticsListener;
RendererBinding.instance.platformDispatcher.onSemanticsEnabledChanged = originalSemanticsListener;
}
return const MaterialApp(
title: 'Flutter View',
Expand Down
2 changes: 1 addition & 1 deletion dev/integration_tests/ui/lib/route.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ import 'package:flutter_driver/driver_extension.dart';

void main() {
enableFlutterDriverExtension(handler: (String? message) async {
return ui.window.defaultRouteName;
return ui.PlatformDispatcher.instance.defaultRouteName;
});
}
5 changes: 3 additions & 2 deletions examples/layers/raw/canvas.dart
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ void beginFrame(Duration timeStamp) {
}

void main() {
ui.window.onBeginFrame = beginFrame;
ui.window.scheduleFrame();
ui.PlatformDispatcher.instance
..onBeginFrame = beginFrame
..scheduleFrame();
}
4 changes: 2 additions & 2 deletions examples/layers/raw/hello_world.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ void beginFrame(Duration timeStamp) {
// calls main() as soon as it has loaded your code.
void main() {
// The engine calls onBeginFrame whenever it wants us to produce a frame.
ui.window.onBeginFrame = beginFrame;
ui.PlatformDispatcher.instance.onBeginFrame = beginFrame;
// Here we kick off the whole process by asking the engine to schedule a new
// frame. The engine will eventually call onBeginFrame when it is time for us
// to actually produce the frame.
ui.window.scheduleFrame();
ui.PlatformDispatcher.instance.scheduleFrame();
}
7 changes: 4 additions & 3 deletions examples/layers/raw/spinning_square.dart
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,11 @@ void beginFrame(Duration timeStamp) {
// After rendering the current frame of the animation, we ask the engine to
// schedule another frame. The engine will call beginFrame again when its time
// to produce the next frame.
ui.window.scheduleFrame();
ui.PlatformDispatcher.instance.scheduleFrame();
}

void main() {
ui.window.onBeginFrame = beginFrame;
ui.window.scheduleFrame();
ui.PlatformDispatcher.instance
..onBeginFrame = beginFrame
..scheduleFrame();
}
5 changes: 3 additions & 2 deletions examples/layers/raw/text.dart
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ void main() {
..layout(const ui.ParagraphConstraints(width: 180.0));

// Finally, we register our beginFrame callback and kick off the first frame.
ui.window.onBeginFrame = beginFrame;
ui.window.scheduleFrame();
ui.PlatformDispatcher.instance
..onBeginFrame = beginFrame
..scheduleFrame();
}
8 changes: 4 additions & 4 deletions examples/layers/raw/touch_input.dart
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ void handlePointerDataPacket(ui.PointerDataPacket packet) {
// point where the engine calls onBeginFrame, which signals the boundary
// between one frame and another.
color = const ui.Color(0xFF00FF00);
ui.window.scheduleFrame();
ui.PlatformDispatcher.instance.scheduleFrame();
}
}
}
Expand All @@ -104,12 +104,12 @@ void handlePointerDataPacket(ui.PointerDataPacket packet) {
void main() {
color = const ui.Color(0xFF00FF00);
// The engine calls onBeginFrame whenever it wants us to produce a frame.
ui.window.onBeginFrame = beginFrame;
ui.PlatformDispatcher.instance.onBeginFrame = beginFrame;
// The engine calls onPointerDataPacket whenever it had updated information
// about the pointers directed at our app.
ui.window.onPointerDataPacket = handlePointerDataPacket;
ui.PlatformDispatcher.instance.onPointerDataPacket = handlePointerDataPacket;
// Here we kick off the whole process by asking the engine to schedule a new
// frame. The engine will eventually call onBeginFrame when it is time for us
// to actually produce the frame.
ui.window.scheduleFrame();
ui.PlatformDispatcher.instance.scheduleFrame();
}
3 changes: 1 addition & 2 deletions packages/flutter/lib/src/material/dropdown.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// found in the LICENSE file.

import 'dart:math' as math;
import 'dart:ui' show window;

import 'package:flutter/foundation.dart';
import 'package:flutter/rendering.dart';
Expand Down Expand Up @@ -1360,7 +1359,7 @@ class _DropdownButtonState<T> extends State<DropdownButton<T>> with WidgetsBindi
if (result == null) {
// If there's no MediaQuery, then use the window aspect to determine
// orientation.
final Size size = window.physicalSize;
final Size size = WidgetsBinding.instance.window.physicalSize;
result = size.width > size.height ? Orientation.landscape : Orientation.portrait;
}
return result;
Expand Down
3 changes: 1 addition & 2 deletions packages/flutter/lib/src/material/time_picker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import 'dart:async';
import 'dart:math' as math;
import 'dart:ui' as ui;

import 'package:flutter/rendering.dart';
import 'package:flutter/services.dart';
Expand Down Expand Up @@ -1746,7 +1745,7 @@ class _HourMinuteTextFieldState extends State<_HourMinuteTextField> with Restora
//
// TODO(rami-a): Once https://github.com/flutter/flutter/issues/67571 is
// resolved, remove the window check for semantics being enabled on web.
final String? hintText = MediaQuery.of(context).accessibleNavigation || ui.window.semanticsEnabled
final String? hintText = MediaQuery.of(context).accessibleNavigation || WidgetsBinding.instance.window.semanticsEnabled
? widget.semanticHintText
: (focusNode.hasFocus ? null : _formattedValue);
inputDecoration = inputDecoration.copyWith(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:ui' as ui;

import 'package:flutter/foundation.dart';
import 'package:flutter/physics.dart';
import 'package:flutter/scheduler.dart';
Expand All @@ -16,9 +14,10 @@ import '../scheduler/scheduler_tester.dart';
void main() {
setUp(() {
WidgetsFlutterBinding.ensureInitialized();
WidgetsBinding.instance.resetEpoch();
ui.window.onBeginFrame = null;
ui.window.onDrawFrame = null;
WidgetsBinding.instance
..resetEpoch()
..platformDispatcher.onBeginFrame = null
..platformDispatcher.onDrawFrame = null;
});

test('Can set value during status callback', () {
Expand Down
9 changes: 4 additions & 5 deletions packages/flutter/test/animation/animations_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:ui' as ui;

import 'package:flutter/widgets.dart';
import 'package:flutter_test/flutter_test.dart';

Expand All @@ -17,9 +15,10 @@ class BogusCurve extends Curve {
void main() {
setUp(() {
WidgetsFlutterBinding.ensureInitialized();
WidgetsBinding.instance.resetEpoch();
ui.window.onBeginFrame = null;
ui.window.onDrawFrame = null;
WidgetsBinding.instance
..resetEpoch()
..platformDispatcher.onBeginFrame = null
..platformDispatcher.onDrawFrame = null;
});

test('toString control test', () {
Expand Down
6 changes: 3 additions & 3 deletions packages/flutter/test/foundation/service_extensions_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,10 @@ class TestServiceExtensionsBinding extends BindingBase
}
Future<void> doFrame() async {
frameScheduled = false;
ui.window.onBeginFrame?.call(Duration.zero);
binding.platformDispatcher.onBeginFrame?.call(Duration.zero);
await flushMicrotasks();
ui.window.onDrawFrame?.call();
ui.window.onReportTimings?.call(<ui.FrameTiming>[]);
binding.platformDispatcher.onDrawFrame?.call();
binding.platformDispatcher.onReportTimings?.call(<ui.FrameTiming>[]);
}

@override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,33 +93,33 @@ void main() {
GestureBinding.instance.resamplingEnabled = true;
const Duration kSamplingOffset = Duration(milliseconds: -5);
GestureBinding.instance.samplingOffset = kSamplingOffset;
ui.window.onPointerDataPacket!(packet);
GestureBinding.instance.platformDispatcher.onPointerDataPacket!(packet);
expect(events.length, 0);

requestFrame();
await tester.pump(const Duration(milliseconds: 10));
expect(events.length, 1);
expect(events[0], isA<PointerDownEvent>());
expect(events[0].timeStamp, currentTestFrameTime() + kSamplingOffset);
expect(events[0].position, Offset(7.5 / ui.window.devicePixelRatio, 0.0));
expect(events[0].position, Offset(7.5 / GestureBinding.instance.window.devicePixelRatio, 0.0));

// Now the system time is epoch + 20ms
requestFrame();
await tester.pump(const Duration(milliseconds: 10));
expect(events.length, 2);
expect(events[1].timeStamp, currentTestFrameTime() + kSamplingOffset);
expect(events[1], isA<PointerMoveEvent>());
expect(events[1].position, Offset(22.5 / ui.window.devicePixelRatio, 0.0));
expect(events[1].delta, Offset(15.0 / ui.window.devicePixelRatio, 0.0));
expect(events[1].position, Offset(22.5 / GestureBinding.instance.window.devicePixelRatio, 0.0));
expect(events[1].delta, Offset(15.0 / GestureBinding.instance.window.devicePixelRatio, 0.0));

// Now the system time is epoch + 30ms
requestFrame();
await tester.pump(const Duration(milliseconds: 10));
expect(events.length, 4);
expect(events[2].timeStamp, currentTestFrameTime() + kSamplingOffset);
expect(events[2], isA<PointerMoveEvent>());
expect(events[2].position, Offset(37.5 / ui.window.devicePixelRatio, 0.0));
expect(events[2].delta, Offset(15.0 / ui.window.devicePixelRatio, 0.0));
expect(events[2].position, Offset(37.5 / GestureBinding.instance.window.devicePixelRatio, 0.0));
expect(events[2].delta, Offset(15.0 / GestureBinding.instance.window.devicePixelRatio, 0.0));
expect(events[3].timeStamp, currentTestFrameTime() + kSamplingOffset);
expect(events[3], isA<PointerUpEvent>());
});
Expand All @@ -134,7 +134,7 @@ void main() {
],
);
GestureBinding.instance.resamplingEnabled = true;
ui.window.onPointerDataPacket!(packet);
GestureBinding.instance.platformDispatcher.onPointerDataPacket!(packet);

// Expected to stop resampling, but the timer keeps active if _timer?.cancel() not be called.
GestureBinding.instance.resamplingEnabled = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ void main() {
final List<PointerEvent> events = <PointerEvent>[];
binding.callback = events.add;

ui.window.onPointerDataPacket?.call(packet);
GestureBinding.instance.platformDispatcher.onPointerDataPacket?.call(packet);

// No pointer events should have been dispatched yet.
expect(events.length, 0);
Expand All @@ -139,7 +139,7 @@ void main() {
expect(events.length, 1);
expect(events[0], isA<PointerDownEvent>());
expect(events[0].timeStamp, binding.frameTime! + samplingOffset);
expect(events[0].position, Offset(0.0 / ui.window.devicePixelRatio, 0.0));
expect(events[0].position, Offset(0.0 / GestureBinding.instance.window.devicePixelRatio, 0.0));

// Second frame callback should have been requested.
callback = binding.postFrameCallback;
Expand All @@ -154,8 +154,8 @@ void main() {
expect(events.length, 2);
expect(events[1], isA<PointerMoveEvent>());
expect(events[1].timeStamp, binding.frameTime! + samplingOffset);
expect(events[1].position, Offset(10.0 / ui.window.devicePixelRatio, 0.0));
expect(events[1].delta, Offset(10.0 / ui.window.devicePixelRatio, 0.0));
expect(events[1].position, Offset(10.0 / GestureBinding.instance.window.devicePixelRatio, 0.0));
expect(events[1].delta, Offset(10.0 / GestureBinding.instance.window.devicePixelRatio, 0.0));

// Verify that resampling continues without a frame callback.
async.elapse(frameInterval * 1.5);
Expand Down
Loading

0 comments on commit 7f2c1cd

Please sign in to comment.