Unverified Commit 3d0607b5 authored by LongCatIsLooong's avatar LongCatIsLooong Committed by GitHub

Defer `systemFontsDidChange` to the transientCallbacks phase (#117123)

* Always defer systemFontsDidChange to transientCallbacks phase

* unnecessary import
parent 9102f2fe
...@@ -10,6 +10,7 @@ import 'package:flutter/animation.dart'; ...@@ -10,6 +10,7 @@ import 'package:flutter/animation.dart';
import 'package:flutter/foundation.dart'; import 'package:flutter/foundation.dart';
import 'package:flutter/gestures.dart'; import 'package:flutter/gestures.dart';
import 'package:flutter/painting.dart'; import 'package:flutter/painting.dart';
import 'package:flutter/scheduler.dart';
import 'package:flutter/semantics.dart'; import 'package:flutter/semantics.dart';
import 'debug.dart'; import 'debug.dart';
...@@ -4011,13 +4012,18 @@ mixin ContainerRenderObjectMixin<ChildType extends RenderObject, ParentDataType ...@@ -4011,13 +4012,18 @@ mixin ContainerRenderObjectMixin<ChildType extends RenderObject, ParentDataType
/// Mixin for [RenderObject] that will call [systemFontsDidChange] whenever the /// Mixin for [RenderObject] that will call [systemFontsDidChange] whenever the
/// system fonts change. /// system fonts change.
/// ///
/// System fonts can change when the OS install or remove a font. Use this mixin if /// System fonts can change when the OS installs or removes a font. Use this
/// the [RenderObject] uses [TextPainter] or [Paragraph] to correctly update the /// mixin if the [RenderObject] uses [TextPainter] or [Paragraph] to correctly
/// text when it happens. /// update the text when it happens.
mixin RelayoutWhenSystemFontsChangeMixin on RenderObject { mixin RelayoutWhenSystemFontsChangeMixin on RenderObject {
/// A callback that is called when system fonts have changed. /// A callback that is called when system fonts have changed.
/// ///
/// The framework defers the invocation of the callback to the
/// [SchedulerPhase.transientCallbacks] phase to ensure that the
/// [RenderObject]'s text layout is still valid when user interactions are in
/// progress (which usually take place during the [SchedulerPhase.idle] phase).
///
/// By default, [markNeedsLayout] is called on the [RenderObject] /// By default, [markNeedsLayout] is called on the [RenderObject]
/// implementing this mixin. /// implementing this mixin.
/// ///
...@@ -4029,15 +4035,44 @@ mixin RelayoutWhenSystemFontsChangeMixin on RenderObject { ...@@ -4029,15 +4035,44 @@ mixin RelayoutWhenSystemFontsChangeMixin on RenderObject {
markNeedsLayout(); markNeedsLayout();
} }
bool _hasPendingSystemFontsDidChangeCallBack = false;
void _scheduleSystemFontsUpdate() {
assert(
SchedulerBinding.instance.schedulerPhase == SchedulerPhase.idle,
'${objectRuntimeType(this, "RelayoutWhenSystemFontsChangeMixin")}._scheduleSystemFontsUpdate() '
'called during ${SchedulerBinding.instance.schedulerPhase}.',
);
if (_hasPendingSystemFontsDidChangeCallBack) {
return;
}
_hasPendingSystemFontsDidChangeCallBack = true;
SchedulerBinding.instance.scheduleFrameCallback((Duration timeStamp) {
assert(_hasPendingSystemFontsDidChangeCallBack);
_hasPendingSystemFontsDidChangeCallBack = false;
assert(
attached || (debugDisposed ?? true),
'$this is detached during ${SchedulerBinding.instance.schedulerPhase} but is not disposed.',
);
if (attached) {
systemFontsDidChange();
}
});
}
@override @override
void attach(covariant PipelineOwner owner) { void attach(PipelineOwner owner) {
super.attach(owner); super.attach(owner);
PaintingBinding.instance.systemFonts.addListener(systemFontsDidChange); // If there's a pending callback that would imply this node was detached
// between the idle phase and the next transientCallbacks phase. The tree
// can not be mutated between those two phases so that should never happen.
assert(!_hasPendingSystemFontsDidChangeCallBack);
PaintingBinding.instance.systemFonts.addListener(_scheduleSystemFontsUpdate);
} }
@override @override
void detach() { void detach() {
PaintingBinding.instance.systemFonts.removeListener(systemFontsDidChange); assert(!_hasPendingSystemFontsDidChangeCallBack);
PaintingBinding.instance.systemFonts.removeListener(_scheduleSystemFontsUpdate);
super.detach(); super.detach();
} }
} }
......
...@@ -8,7 +8,6 @@ import 'dart:ui' as ui show BoxHeightStyle, BoxWidthStyle, Gradient, LineMetrics ...@@ -8,7 +8,6 @@ import 'dart:ui' as ui show BoxHeightStyle, BoxWidthStyle, Gradient, LineMetrics
import 'package:flutter/foundation.dart'; import 'package:flutter/foundation.dart';
import 'package:flutter/gestures.dart'; import 'package:flutter/gestures.dart';
import 'package:flutter/scheduler.dart';
import 'package:flutter/semantics.dart'; import 'package:flutter/semantics.dart';
import 'package:flutter/services.dart'; import 'package:flutter/services.dart';
...@@ -654,38 +653,11 @@ class RenderParagraph extends RenderBox ...@@ -654,38 +653,11 @@ class RenderParagraph extends RenderBox
); );
} }
bool _systemFontsChangeScheduled = false;
@override @override
void systemFontsDidChange() { void systemFontsDidChange() {
final SchedulerPhase phase = SchedulerBinding.instance.schedulerPhase;
switch (phase) {
case SchedulerPhase.idle:
case SchedulerPhase.postFrameCallbacks:
if (_systemFontsChangeScheduled) {
return;
}
_systemFontsChangeScheduled = true;
SchedulerBinding.instance.scheduleFrameCallback((Duration timeStamp) {
assert(_systemFontsChangeScheduled);
_systemFontsChangeScheduled = false;
assert(
attached || (debugDisposed ?? true),
'$this is detached during $phase but not disposed.',
);
if (attached) {
super.systemFontsDidChange(); super.systemFontsDidChange();
_textPainter.markNeedsLayout(); _textPainter.markNeedsLayout();
} }
});
break;
case SchedulerPhase.transientCallbacks:
case SchedulerPhase.midFrameMicrotasks:
case SchedulerPhase.persistentCallbacks:
super.systemFontsDidChange();
_textPainter.markNeedsLayout();
break;
}
}
// Placeholder dimensions representing the sizes of child inline widgets. // Placeholder dimensions representing the sizes of child inline widgets.
// //
......
...@@ -11,14 +11,8 @@ import 'package:flutter/rendering.dart'; ...@@ -11,14 +11,8 @@ import 'package:flutter/rendering.dart';
import 'package:flutter/services.dart'; import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart'; import 'package:flutter_test/flutter_test.dart';
void main() { Future<void> verifyMarkedNeedsLayoutDuringTransientCallbacksPhase(WidgetTester tester, RenderObject renderObject) async {
testWidgets('RenderParagraph relayout upon system fonts changes', (WidgetTester tester) async { assert(!renderObject.debugNeedsLayout);
await tester.pumpWidget(
const MaterialApp(
home: Text('text widget'),
),
);
final RenderObject renderObject = tester.renderObject(find.text('text widget'));
const Map<String, dynamic> data = <String, dynamic>{ const Map<String, dynamic> data = <String, dynamic>{
'type': 'fontsChange', 'type': 'fontsChange',
...@@ -33,17 +27,35 @@ void main() { ...@@ -33,17 +27,35 @@ void main() {
tester.binding.scheduleFrameCallback((Duration timeStamp) { tester.binding.scheduleFrameCallback((Duration timeStamp) {
animation.complete(renderObject.debugNeedsLayout); animation.complete(renderObject.debugNeedsLayout);
}); });
// The fonts change does not mark the render object as needing layout
// immediately.
expect(renderObject.debugNeedsLayout, isFalse); expect(renderObject.debugNeedsLayout, isFalse);
await tester.pump(); await tester.pump();
expect(await animation.future, isTrue); expect(await animation.future, isTrue);
}); }
testWidgets('Safe to query RenderParagraph for text layout after system fonts changes', (WidgetTester tester) async { void main() {
testWidgets('RenderParagraph relayout upon system fonts changes', (WidgetTester tester) async {
await tester.pumpWidget( await tester.pumpWidget(
const MaterialApp( const MaterialApp(
home: Text('text widget'), home: Text('text widget'),
), ),
); );
final RenderObject renderObject = tester.renderObject(find.text('text widget'));
await verifyMarkedNeedsLayoutDuringTransientCallbacksPhase(tester, renderObject);
});
testWidgets(
'Safe to query a RelayoutWhenSystemFontsChangeMixin for text layout after system fonts changes',
(WidgetTester tester) async {
final _RenderCustomRelayoutWhenSystemFontsChange child = _RenderCustomRelayoutWhenSystemFontsChange();
await tester.pumpWidget(
Directionality(
textDirection: TextDirection.ltr,
child: WidgetToRenderBoxAdapter(renderBox: child),
),
);
const Map<String, dynamic> data = <String, dynamic>{ const Map<String, dynamic> data = <String, dynamic>{
'type': 'fontsChange', 'type': 'fontsChange',
}; };
...@@ -52,16 +64,9 @@ void main() { ...@@ -52,16 +64,9 @@ void main() {
SystemChannels.system.codec.encodeMessage(data), SystemChannels.system.codec.encodeMessage(data),
(ByteData? data) { }, (ByteData? data) { },
); );
final RenderParagraph paragraph = tester.renderObject<RenderParagraph>(find.text('text widget')); expect(child.hasValidTextLayout, isTrue);
Object? exception; },
try { );
paragraph.getPositionForOffset(Offset.zero);
paragraph.hitTest(BoxHitTestResult(), position: Offset.zero);
} catch (e) {
exception = e;
}
expect(exception, isNull);
});
testWidgets('RenderEditable relayout upon system fonts changes', (WidgetTester tester) async { testWidgets('RenderEditable relayout upon system fonts changes', (WidgetTester tester) async {
await tester.pumpWidget( await tester.pumpWidget(
...@@ -69,16 +74,9 @@ void main() { ...@@ -69,16 +74,9 @@ void main() {
home: SelectableText('text widget'), home: SelectableText('text widget'),
), ),
); );
const Map<String, dynamic> data = <String, dynamic>{
'type': 'fontsChange',
};
await ServicesBinding.instance.defaultBinaryMessenger.handlePlatformMessage(
'flutter/system',
SystemChannels.system.codec.encodeMessage(data),
(ByteData? data) { },
);
final EditableTextState state = tester.state(find.byType(EditableText)); final EditableTextState state = tester.state(find.byType(EditableText));
expect(state.renderEditable.debugNeedsLayout, isTrue); await verifyMarkedNeedsLayoutDuringTransientCallbacksPhase(tester, state.renderEditable);
}); });
testWidgets('Banner repaint upon system fonts changes', (WidgetTester tester) async { testWidgets('Banner repaint upon system fonts changes', (WidgetTester tester) async {
...@@ -210,11 +208,8 @@ void main() { ...@@ -210,11 +208,8 @@ void main() {
SystemChannels.system.codec.encodeMessage(data), SystemChannels.system.codec.encodeMessage(data),
(ByteData? data) { }, (ByteData? data) { },
); );
final RenderObject renderObject = tester.renderObject(find.byType(RangeSlider)); final RenderObject renderObject = tester.renderObject(find.byWidgetPredicate((Widget widget) => widget.runtimeType.toString() == '_RangeSliderRenderObjectWidget'));
await verifyMarkedNeedsLayoutDuringTransientCallbacksPhase(tester, renderObject);
late bool sliderBoxNeedsLayout;
renderObject.visitChildren((RenderObject child) {sliderBoxNeedsLayout = child.debugNeedsLayout;});
expect(sliderBoxNeedsLayout, isTrue);
}); });
testWidgets('Slider relayout upon system fonts changes', (WidgetTester tester) async { testWidgets('Slider relayout upon system fonts changes', (WidgetTester tester) async {
...@@ -228,17 +223,9 @@ void main() { ...@@ -228,17 +223,9 @@ void main() {
), ),
), ),
); );
const Map<String, dynamic> data = <String, dynamic>{
'type': 'fontsChange',
};
await ServicesBinding.instance.defaultBinaryMessenger.handlePlatformMessage(
'flutter/system',
SystemChannels.system.codec.encodeMessage(data),
(ByteData? data) { },
);
// _RenderSlider is the last render object in the tree. // _RenderSlider is the last render object in the tree.
final RenderObject renderObject = tester.allRenderObjects.last; final RenderObject renderObject = tester.allRenderObjects.last;
expect(renderObject.debugNeedsLayout, isTrue); await verifyMarkedNeedsLayoutDuringTransientCallbacksPhase(tester, renderObject);
}); });
testWidgets('TimePicker relayout upon system fonts changes', (WidgetTester tester) async { testWidgets('TimePicker relayout upon system fonts changes', (WidgetTester tester) async {
...@@ -289,3 +276,19 @@ void main() { ...@@ -289,3 +276,19 @@ void main() {
expect(renderObject.debugNeedsPaint, isTrue); expect(renderObject.debugNeedsPaint, isTrue);
}); });
} }
class _RenderCustomRelayoutWhenSystemFontsChange extends RenderBox with RelayoutWhenSystemFontsChangeMixin {
bool hasValidTextLayout = false;
@override
void systemFontsDidChange() {
super.systemFontsDidChange();
hasValidTextLayout = false;
}
@override
void performLayout() {
size = constraints.biggest;
hasValidTextLayout = true;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment