Track global keys globally.

Assert that there are no duplicates.
Export GlobalKey from basic.dart, so that people don't have to import widgets.dart just for that.
Fix the "initialFocus" feature which actually didn't work.
This commit is contained in:
Hixie
2015-07-23 10:55:00 -07:00
parent 7224089c9f
commit eb90899aab
4 changed files with 39 additions and 6 deletions

View File

@@ -19,7 +19,7 @@ const EdgeDims _kTextfieldPadding = const EdgeDims.symmetric(vertical: 8.0);
class Input extends StatefulComponent {
Input({
Key key,
GlobalKey key,
this.placeholder,
this.onChanged
}): super(key: key);

View File

@@ -22,7 +22,7 @@ import 'package:sky/widgets/widget.dart';
export 'package:sky/rendering/box.dart' show BackgroundImage, BoxConstraints, BoxDecoration, Border, BorderSide, EdgeDims;
export 'package:sky/rendering/flex.dart' show FlexDirection, FlexJustifyContent, FlexAlignItems;
export 'package:sky/rendering/object.dart' show Point, Offset, Size, Rect, Color, Paint, Path;
export 'package:sky/widgets/widget.dart' show Key, Widget, Component, StatefulComponent, App, runApp, Listener, ParentDataNode;
export 'package:sky/widgets/widget.dart' show Key, GlobalKey, Widget, Component, StatefulComponent, App, runApp, Listener, ParentDataNode;
// PAINTING NODES

View File

@@ -14,11 +14,11 @@ class Focus extends Inherited {
Focus({
GlobalKey key,
GlobalKey initialFocus,
this.initialFocus,
Widget child
}) : super(key: key, child: child);
GlobalKey initialFocus;
final GlobalKey initialFocus;
GlobalKey _currentlyFocusedKey;
GlobalKey get currentlyFocusedKey {

View File

@@ -140,13 +140,46 @@ abstract class Widget {
_notifyingMountStatus = false;
sky.tracing.end("Widget._notifyMountStatusChanged");
}
assert(_debugDuplicateGlobalKeys.isEmpty);
}
static final Map<GlobalKey, Widget> _globalKeys = new Map<GlobalKey, Widget>();
static final Map<GlobalKey, int> _debugDuplicateGlobalKeys = new Map<GlobalKey, int>();
/// Override this function to learn when this [Widget] enters the widget tree.
void didMount() { }
void didMount() {
if (key is GlobalKey) {
assert(() {
if (_globalKeys.containsKey(key)) {
int oldCount = _debugDuplicateGlobalKeys.putIfAbsent(key, () => 1);
assert(oldCount >= 1);
_debugDuplicateGlobalKeys[key] = oldCount + 1;
}
return true;
});
_globalKeys[key] = this;
}
}
/// Override this function to learn when this [Widget] leaves the widget tree.
void didUnmount() { }
void didUnmount() {
if (key is GlobalKey) {
assert(() {
if (_globalKeys.containsKey(key) && _debugDuplicateGlobalKeys.containsKey(key)) {
int oldCount = _debugDuplicateGlobalKeys[key];
assert(oldCount >= 2);
if (oldCount == 2) {
_debugDuplicateGlobalKeys.remove(key);
} else {
_debugDuplicateGlobalKeys[key] = oldCount - 1;
}
}
return true;
});
if (_globalKeys[key] == this)
_globalKeys.remove(key);
}
}
RenderObject _root;