From 8c535ad7d72bdaa8a7f0ca4081142f4b7902aef8 Mon Sep 17 00:00:00 2001 From: Adam Barth Date: Fri, 1 Jul 2016 10:06:40 -0700 Subject: [PATCH] Update use of `@protected` (#4805) Now that protected can be accessed from inside the same library, we can use protected in a number of new places and we can remove some trampolines we were using to work around its previous semantics. --- packages/flutter/lib/src/rendering/box.dart | 1 + packages/flutter/lib/src/rendering/node.dart | 3 +++ .../flutter/lib/src/rendering/object.dart | 2 +- .../lib/src/services/image_stream.dart | 5 +--- .../flutter/lib/src/widgets/framework.dart | 27 +++++++++++-------- .../lib/src/widgets/gesture_detector.dart | 12 ++++++--- 6 files changed, 30 insertions(+), 20 deletions(-) diff --git a/packages/flutter/lib/src/rendering/box.dart b/packages/flutter/lib/src/rendering/box.dart index 268e1a034a..7df2485ff6 100644 --- a/packages/flutter/lib/src/rendering/box.dart +++ b/packages/flutter/lib/src/rendering/box.dart @@ -932,6 +932,7 @@ abstract class RenderBox extends RenderObject { /// This function must only be called from [getDistanceToBaseline] and /// [computeDistanceToActualBaseline]. Do not call this function directly from /// outside those two methods. + @protected @mustCallSuper double getDistanceToActualBaseline(TextBaseline baseline) { assert(_debugDoingBaseline); diff --git a/packages/flutter/lib/src/rendering/node.dart b/packages/flutter/lib/src/rendering/node.dart index 2d52e90fb0..b9b4785822 100644 --- a/packages/flutter/lib/src/rendering/node.dart +++ b/packages/flutter/lib/src/rendering/node.dart @@ -52,6 +52,7 @@ class AbstractNode { int get depth => _depth; /// Call only from overrides of [redepthChildren] + @protected void redepthChild(AbstractNode child) { assert(child.owner == owner); if (child._depth <= _depth) { @@ -97,6 +98,7 @@ class AbstractNode { AbstractNode get parent => _parent; /// Subclasses should call this function when they acquire a new child. + @protected @mustCallSuper void adoptChild(AbstractNode child) { assert(child != null); @@ -115,6 +117,7 @@ class AbstractNode { } /// Subclasses should call this function when they lose a child. + @protected @mustCallSuper void dropChild(AbstractNode child) { assert(child != null); diff --git a/packages/flutter/lib/src/rendering/object.dart b/packages/flutter/lib/src/rendering/object.dart index 55d94f408b..e8d7264992 100644 --- a/packages/flutter/lib/src/rendering/object.dart +++ b/packages/flutter/lib/src/rendering/object.dart @@ -30,6 +30,7 @@ export 'package:flutter/foundation.dart' show FlutterError, InformationCollector /// to other children. class ParentData { /// Called when the RenderObject is removed from the tree. + @protected @mustCallSuper void detach() { } @@ -1139,7 +1140,6 @@ abstract class RenderObject extends AbstractNode implements HitTestTarget { @override PipelineOwner get owner => super.owner; - // Workaround for https://github.com/dart-lang/sdk/issues/25232 @override void attach(PipelineOwner owner) { super.attach(owner); diff --git a/packages/flutter/lib/src/services/image_stream.dart b/packages/flutter/lib/src/services/image_stream.dart index 50c0ac1ef3..9857b2cab0 100644 --- a/packages/flutter/lib/src/services/image_stream.dart +++ b/packages/flutter/lib/src/services/image_stream.dart @@ -133,7 +133,7 @@ class ImageStream { } else { result.write('${_completer.runtimeType}; '); final List description = []; - _completer._debugFillDescription(description); + _completer.debugFillDescription(description); result.write(description.join('; ')); } result.write(')'); @@ -213,9 +213,6 @@ class ImageStreamCompleter { description.add('$_current'); description.add('${_listeners.length} listener${_listeners.length == 1 ? "" : "s" }'); } - - // TODO(ianh): remove once @protected allows in-file references - void _debugFillDescription(List description) => debugFillDescription(description); } /// Manages the loading of [ui.Image] objects for static [ImageStream]s (those diff --git a/packages/flutter/lib/src/widgets/framework.dart b/packages/flutter/lib/src/widgets/framework.dart index 540a3a210b..0161291a08 100644 --- a/packages/flutter/lib/src/widgets/framework.dart +++ b/packages/flutter/lib/src/widgets/framework.dart @@ -368,6 +368,7 @@ abstract class Widget { /// is placed in the tree, it is inflated into an [Element], which means a /// widget that is incorporated into the tree multiple times will be inflated /// multiple times. + @protected Element createElement(); /// A short, textual description of this widget. @@ -394,13 +395,6 @@ abstract class Widget { @mustCallSuper void debugFillDescription(List description) { } - /// Lets [Element.debugFillDescription] call the @protected method - /// [debugFillDescription]. This pattern prevents other unwanted callers - /// outside this library. - void _debugFillDescription(List description) { - debugFillDescription(description); - } - /// Whether the `newWidget` can be used to update an [Element] that currently /// has the `oldWidget` as its configuration. /// @@ -540,6 +534,7 @@ abstract class StatefulWidget extends Widget { /// later inserted into the tree again, the framework will call [createState] /// again to create a fresh [State] object, simplifying the lifecycle of /// [State] objects. + @protected State createState(); } @@ -706,6 +701,7 @@ abstract class State { /// /// If you override this, make sure your method starts with a call to /// super.initState(). + @protected @mustCallSuper void initState() { assert(_debugLifecycleState == _StateLifecycle.created); @@ -729,6 +725,7 @@ abstract class State { /// If you override this, make sure your method starts with a call to /// super.didUpdateConfig(oldConfig). // TODO(abarth): Add @mustCallSuper. + @protected void didUpdateConfig(T oldConfig) { } /// Notify the framework that the internal state of this object has changed. @@ -813,6 +810,7 @@ abstract class State { /// /// If you override this, make sure to end your method with a call to /// super.deactivate(). + @protected @mustCallSuper void deactivate() { } @@ -829,6 +827,7 @@ abstract class State { /// /// If you override this, make sure to end your method with a call to /// super.dispose(). + @protected @mustCallSuper void dispose() { assert(_debugLifecycleState == _StateLifecycle.ready); @@ -878,6 +877,7 @@ abstract class State { /// this method because they need to do some expensive work (e.g., network /// fetches) when their dependencies change, and that work would be too /// expensive to do for every build. + @protected @mustCallSuper void dependenciesChanged() { } @@ -965,6 +965,7 @@ abstract class ParentDataWidget extends _ProxyWidg return result; } + @protected void applyParentData(RenderObject renderObject); } @@ -975,6 +976,7 @@ abstract class InheritedWidget extends _ProxyWidget { @override InheritedElement createElement() => new InheritedElement(this); + @protected bool updateShouldNotify(InheritedWidget oldWidget); } @@ -991,13 +993,16 @@ abstract class RenderObjectWidget extends Widget { /// Creates an instance of the [RenderObject] class that this /// [RenderObjectWidget] represents, using the configuration described by this /// [RenderObjectWidget]. + @protected RenderObject createRenderObject(BuildContext context); /// Copies the configuration described by this [RenderObjectWidget] to the /// given [RenderObject], which will be of the same type as returned by this /// object's [createRenderObject]. + @protected void updateRenderObject(BuildContext context, RenderObject renderObject) { } + @protected void didUnmountRenderObject(RenderObject renderObject) { } } @@ -1730,7 +1735,7 @@ abstract class Element implements BuildContext { } else { if (widget.key != null) description.add('${widget.key}'); - widget._debugFillDescription(description); + widget.debugFillDescription(description); } } @@ -2212,7 +2217,7 @@ class ParentDataElement extends _ProxyElement { void notifyClients(ParentDataWidget oldWidget) { void notifyChildren(Element child) { if (child is RenderObjectElement) { - child.updateParentData(widget); + child._updateParentData(widget); } else { assert(child is! ParentDataElement); child.visitChildren(notifyChildren); @@ -2514,7 +2519,7 @@ abstract class RenderObjectElement extends BuildableElement { widget.didUnmountRenderObject(renderObject); } - void updateParentData(ParentDataWidget parentData) { + void _updateParentData(ParentDataWidget parentData) { parentData.applyParentData(renderObject); } @@ -2534,7 +2539,7 @@ abstract class RenderObjectElement extends BuildableElement { _ancestorRenderObjectElement?.insertChildRenderObject(renderObject, newSlot); ParentDataElement parentDataElement = _findAncestorParentDataElement(); if (parentDataElement != null) - updateParentData(parentDataElement.widget); + _updateParentData(parentDataElement.widget); } @override diff --git a/packages/flutter/lib/src/widgets/gesture_detector.dart b/packages/flutter/lib/src/widgets/gesture_detector.dart index a35756d746..bc579a095a 100644 --- a/packages/flutter/lib/src/widgets/gesture_detector.dart +++ b/packages/flutter/lib/src/widgets/gesture_detector.dart @@ -393,7 +393,8 @@ class RawGestureDetectorState extends State { if (!config.excludeFromSemantics) { RenderSemanticsGestureHandler semanticsGestureHandler = context.findRenderObject(); context.visitChildElements((RenderObjectElement element) { - element.widget.updateRenderObject(context, semanticsGestureHandler); + _GestureSemantics widget = element.widget; + widget._updateHandlers(semanticsGestureHandler, _recognizers); }); } } @@ -558,9 +559,7 @@ class _GestureSemantics extends SingleChildRenderObjectWidget { return result; } - @override - void updateRenderObject(BuildContext context, RenderSemanticsGestureHandler renderObject) { - Map recognizers = owner._recognizers; + void _updateHandlers(RenderSemanticsGestureHandler renderObject, Map recognizers) { renderObject ..onTap = recognizers.containsKey(TapGestureRecognizer) ? _handleTap : null ..onLongPress = recognizers.containsKey(LongPressGestureRecognizer) ? _handleLongPress : null @@ -569,4 +568,9 @@ class _GestureSemantics extends SingleChildRenderObjectWidget { ..onVerticalDragUpdate = recognizers.containsKey(VerticalDragGestureRecognizer) || recognizers.containsKey(PanGestureRecognizer) ? _handleVerticalDragUpdate : null; } + + @override + void updateRenderObject(BuildContext context, RenderSemanticsGestureHandler renderObject) { + _updateHandlers(renderObject, owner._recognizers); + } }