Commit 50cf609e authored by Ian Hickson's avatar Ian Hickson

Merge pull request #453 from Hixie/yak4-of

Pave the Foo.of() cowpath.
parents 752e341d 65d81451
...@@ -19,7 +19,7 @@ class IconTheme extends InheritedWidget { ...@@ -19,7 +19,7 @@ class IconTheme extends InheritedWidget {
final IconThemeData data; final IconThemeData data;
static IconThemeData of(BuildContext context) { static IconThemeData of(BuildContext context) {
IconTheme result = context.inheritedWidgetOfType(IconTheme); IconTheme result = context.inheritFromWidgetOfType(IconTheme);
return result?.data; return result?.data;
} }
......
...@@ -23,7 +23,7 @@ class ButtonTheme extends InheritedWidget { ...@@ -23,7 +23,7 @@ class ButtonTheme extends InheritedWidget {
final ButtonColor color; final ButtonColor color;
static ButtonColor of(BuildContext context) { static ButtonColor of(BuildContext context) {
ButtonTheme result = context.inheritedWidgetOfType(ButtonTheme); ButtonTheme result = context.inheritFromWidgetOfType(ButtonTheme);
return result?.color ?? ButtonColor.normal; return result?.color ?? ButtonColor.normal;
} }
......
...@@ -23,7 +23,7 @@ class Theme extends InheritedWidget { ...@@ -23,7 +23,7 @@ class Theme extends InheritedWidget {
static final ThemeData _kFallbackTheme = new ThemeData.fallback(); static final ThemeData _kFallbackTheme = new ThemeData.fallback();
static ThemeData of(BuildContext context) { static ThemeData of(BuildContext context) {
Theme theme = context.inheritedWidgetOfType(Theme); Theme theme = context.inheritFromWidgetOfType(Theme);
return theme == null ? _kFallbackTheme : theme.data; return theme == null ? _kFallbackTheme : theme.data;
} }
......
...@@ -972,7 +972,7 @@ class DefaultTextStyle extends InheritedWidget { ...@@ -972,7 +972,7 @@ class DefaultTextStyle extends InheritedWidget {
final TextStyle style; final TextStyle style;
static TextStyle of(BuildContext context) { static TextStyle of(BuildContext context) {
DefaultTextStyle result = context.inheritedWidgetOfType(DefaultTextStyle); DefaultTextStyle result = context.inheritFromWidgetOfType(DefaultTextStyle);
return result?.style; return result?.style;
} }
...@@ -1163,7 +1163,7 @@ class DefaultAssetBundle extends InheritedWidget { ...@@ -1163,7 +1163,7 @@ class DefaultAssetBundle extends InheritedWidget {
final AssetBundle bundle; final AssetBundle bundle;
static AssetBundle of(BuildContext context) { static AssetBundle of(BuildContext context) {
DefaultAssetBundle result = context.inheritedWidgetOfType(DefaultAssetBundle); DefaultAssetBundle result = context.inheritFromWidgetOfType(DefaultAssetBundle);
return result?.bundle; return result?.bundle;
} }
......
...@@ -87,7 +87,7 @@ class Focus extends StatefulComponent { ...@@ -87,7 +87,7 @@ class Focus extends StatefulComponent {
static bool at(BuildContext context, Widget widget, { bool autofocus: true }) { static bool at(BuildContext context, Widget widget, { bool autofocus: true }) {
assert(widget != null); assert(widget != null);
assert(widget.key is GlobalKey); assert(widget.key is GlobalKey);
_FocusScope focusScope = context.inheritedWidgetOfType(_FocusScope); _FocusScope focusScope = context.inheritFromWidgetOfType(_FocusScope);
if (focusScope != null) { if (focusScope != null) {
if (autofocus) if (autofocus)
focusScope._setFocusedWidgetIfUnset(widget.key); focusScope._setFocusedWidgetIfUnset(widget.key);
...@@ -100,7 +100,7 @@ class Focus extends StatefulComponent { ...@@ -100,7 +100,7 @@ class Focus extends StatefulComponent {
static bool _atScope(BuildContext context, Widget widget, { bool autofocus: true }) { static bool _atScope(BuildContext context, Widget widget, { bool autofocus: true }) {
assert(widget != null); assert(widget != null);
_FocusScope focusScope = context.inheritedWidgetOfType(_FocusScope); _FocusScope focusScope = context.inheritFromWidgetOfType(_FocusScope);
if (focusScope != null) { if (focusScope != null) {
if (autofocus) if (autofocus)
focusScope._setFocusedScopeIfUnset(widget.key); focusScope._setFocusedScopeIfUnset(widget.key);
...@@ -118,7 +118,7 @@ class Focus extends StatefulComponent { ...@@ -118,7 +118,7 @@ class Focus extends StatefulComponent {
static void moveTo(BuildContext context, Widget widget) { static void moveTo(BuildContext context, Widget widget) {
assert(widget != null); assert(widget != null);
assert(widget.key is GlobalKey); assert(widget.key is GlobalKey);
_FocusScope focusScope = context.inheritedWidgetOfType(_FocusScope); _FocusScope focusScope = context.inheritFromWidgetOfType(_FocusScope);
if (focusScope != null) if (focusScope != null)
focusScope.focusState._setFocusedWidget(widget.key); focusScope.focusState._setFocusedWidget(widget.key);
} }
...@@ -126,7 +126,7 @@ class Focus extends StatefulComponent { ...@@ -126,7 +126,7 @@ class Focus extends StatefulComponent {
static void moveScopeTo(BuildContext context, Focus component) { static void moveScopeTo(BuildContext context, Focus component) {
assert(component != null); assert(component != null);
assert(component.key != null); assert(component.key != null);
_FocusScope focusScope = context.inheritedWidgetOfType(_FocusScope); _FocusScope focusScope = context.inheritFromWidgetOfType(_FocusScope);
if (focusScope != null) if (focusScope != null)
focusScope.focusState._setFocusedScope(component.key); focusScope.focusState._setFocusedScope(component.key);
} }
......
...@@ -534,7 +534,9 @@ typedef void ElementVisitor(Element element); ...@@ -534,7 +534,9 @@ typedef void ElementVisitor(Element element);
abstract class BuildContext { abstract class BuildContext {
Widget get widget; Widget get widget;
RenderObject findRenderObject(); RenderObject findRenderObject();
InheritedWidget inheritedWidgetOfType(Type targetType); InheritedWidget inheritFromWidgetOfType(Type targetType);
Widget ancestorWidgetOfType(Type targetType);
State ancestorStateOfType(Type targetType);
void visitAncestorElements(bool visitor(Element element)); void visitAncestorElements(bool visitor(Element element));
void visitChildElements(void visitor(Element element)); void visitChildElements(void visitor(Element element));
} }
...@@ -806,18 +808,33 @@ abstract class Element<T extends Widget> implements BuildContext { ...@@ -806,18 +808,33 @@ abstract class Element<T extends Widget> implements BuildContext {
assert(() { _debugLifecycleState = _ElementLifecycle.defunct; return true; }); assert(() { _debugLifecycleState = _ElementLifecycle.defunct; return true; });
} }
RenderObject findRenderObject() => renderObject;
Set<Type> _dependencies; Set<Type> _dependencies;
InheritedWidget inheritedWidgetOfType(Type targetType) { InheritedWidget inheritFromWidgetOfType(Type targetType) {
if (_dependencies == null) if (_dependencies == null)
_dependencies = new Set<Type>(); _dependencies = new Set<Type>();
_dependencies.add(targetType); _dependencies.add(targetType);
return ancestorWidgetOfType(targetType);
}
Widget ancestorWidgetOfType(Type targetType) {
Element ancestor = _parent; Element ancestor = _parent;
while (ancestor != null && ancestor.widget.runtimeType != targetType) while (ancestor != null && ancestor.widget.runtimeType != targetType)
ancestor = ancestor._parent; ancestor = ancestor._parent;
return ancestor?.widget; return ancestor?.widget;
} }
RenderObject findRenderObject() => renderObject; State ancestorStateOfType(Type targetType) {
Element ancestor = _parent;
while (ancestor != null) {
if (ancestor is StatefulComponentElement && ancestor.state.runtimeType == targetType)
break;
ancestor = ancestor._parent;
}
StatefulComponentElement statefulAncestor = ancestor;
return statefulAncestor?.state;
}
void visitAncestorElements(bool visitor(Element element)) { void visitAncestorElements(bool visitor(Element element)) {
Element ancestor = _parent; Element ancestor = _parent;
......
...@@ -42,7 +42,7 @@ class MediaQuery extends InheritedWidget { ...@@ -42,7 +42,7 @@ class MediaQuery extends InheritedWidget {
final MediaQueryData data; final MediaQueryData data;
static MediaQueryData of(BuildContext context) { static MediaQueryData of(BuildContext context) {
MediaQuery query = context.inheritedWidgetOfType(MediaQuery); MediaQuery query = context.inheritFromWidgetOfType(MediaQuery);
return query == null ? null : query.data; return query == null ? null : query.data;
} }
......
...@@ -54,17 +54,7 @@ class Navigator extends StatefulComponent { ...@@ -54,17 +54,7 @@ class Navigator extends StatefulComponent {
static const String defaultRouteName = '/'; static const String defaultRouteName = '/';
static NavigatorState of(BuildContext context) { static NavigatorState of(BuildContext context) => context.ancestorStateOfType(NavigatorState);
NavigatorState result;
context.visitAncestorElements((Element element) {
if (element is StatefulComponentElement && element.state is NavigatorState) {
result = element.state;
return false;
}
return true;
});
return result;
}
NavigatorState createState() => new NavigatorState(); NavigatorState createState() => new NavigatorState();
} }
......
...@@ -86,16 +86,8 @@ class PageStorage extends StatelessComponent { ...@@ -86,16 +86,8 @@ class PageStorage extends StatelessComponent {
/// Might return null if there is no PageStorage in this context. /// Might return null if there is no PageStorage in this context.
static PageStorageBucket of(BuildContext context) { static PageStorageBucket of(BuildContext context) {
PageStorageBucket result; PageStorage widget = context.ancestorWidgetOfType(PageStorage);
context.visitAncestorElements((Element element) { return widget?.bucket;
Widget widget = element.widget;
if (widget is PageStorage) {
result = widget.bucket;
return false;
}
return true;
});
return result;
} }
Widget build(BuildContext context) => child; Widget build(BuildContext context) => child;
......
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