Merge pull request #235 from Hixie/focus

Track global keys globally.
This commit is contained in:
Ian Hickson 2015-07-23 12:29:59 -07:00
commit d219e6d331
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;