Commit 0913fb71 authored by Ian Hickson's avatar Ian Hickson

Merge pull request #1197 from Hixie/mimic

Factor out GlobalKeyWatcher
parents f3655f34 d8668606
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:sky/src/widgets/framework.dart';
abstract class GlobalKeyWatcher extends StatefulComponent {
GlobalKeyWatcher({
Key key,
this.watchedKey
});
GlobalKey watchedKey;
void syncConstructorArguments(GlobalKeyWatcher source) {
if (source != source.watchedKey) {
_removeListeners();
watchedKey = source.watchedKey;
if (mounted)
_setWatchedWidget(GlobalKey.getWidget(watchedKey));
_addListeners();
}
}
Widget get watchedWidget => _watchedWidget;
Widget _watchedWidget;
void _setWatchedWidget(Widget value) {
assert(mounted || value == null);
if (watchedWidget != value) {
if (watchedWidget != null)
stopWatching();
assert(debugValidateWatchedWidget(value));
setState(() {
_watchedWidget = value;
});
if (watchedWidget != null)
startWatching();
}
}
bool debugValidateWatchedWidget(Widget candidate) => true;
void didMount() {
super.didMount();
_setWatchedWidget(GlobalKey.getWidget(watchedKey));
_addListeners();
}
void didUnmount() {
super.didUnmount();
_removeListeners();
_setWatchedWidget(null);
}
void _addListeners() {
GlobalKey.registerSyncListener(watchedKey, didSyncWatchedKey);
GlobalKey.registerRemoveListener(watchedKey, didRemoveWatchedKey);
}
void _removeListeners() {
GlobalKey.unregisterSyncListener(watchedKey, didSyncWatchedKey);
GlobalKey.unregisterRemoveListener(watchedKey, didRemoveWatchedKey);
}
void didSyncWatchedKey(GlobalKey key, Widget widget) {
assert(key == watchedKey);
_setWatchedWidget(widget);
}
void didRemoveWatchedKey(GlobalKey key) {
assert(key == watchedKey);
_setWatchedWidget(null);
}
void stopWatching() { }
void startWatching() { }
}
...@@ -4,51 +4,7 @@ ...@@ -4,51 +4,7 @@
import 'package:sky/src/widgets/basic.dart'; import 'package:sky/src/widgets/basic.dart';
import 'package:sky/src/widgets/framework.dart'; import 'package:sky/src/widgets/framework.dart';
import 'package:sky/src/widgets/global_key_watcher.dart';
abstract class GlobalKeyWatcher extends StatefulComponent {
GlobalKeyWatcher({
Key key,
this.watchedKey
});
GlobalKey watchedKey;
void syncConstructorArguments(GlobalKeyWatcher source) {
if (source != source.watchedKey) {
_removeListeners();
watchedKey = source.watchedKey;
_addListeners();
}
}
void didMount() {
super.didMount();
_addListeners();
}
void didUnmount() {
super.didUnmount();
_removeListeners();
}
void didSyncWatchedKey(GlobalKey key, Widget widget) {
assert(key == watchedKey);
}
void didRemoveWatchedKey(GlobalKey key) {
assert(key == watchedKey);
}
void _addListeners() {
GlobalKey.registerSyncListener(watchedKey, didSyncWatchedKey);
GlobalKey.registerRemoveListener(watchedKey, didRemoveWatchedKey);
}
void _removeListeners() {
GlobalKey.unregisterSyncListener(watchedKey, didSyncWatchedKey);
GlobalKey.unregisterRemoveListener(watchedKey, didRemoveWatchedKey);
}
}
typedef MimicReadyCallback(); typedef MimicReadyCallback();
...@@ -66,48 +22,24 @@ class Mimic extends GlobalKeyWatcher { ...@@ -66,48 +22,24 @@ class Mimic extends GlobalKeyWatcher {
super.syncConstructorArguments(source); super.syncConstructorArguments(source);
} }
Mimicable _mimicable; bool debugValidateWatchedWidget(Widget candidate) {
return candidate is Mimicable;
void didMount() {
super.didMount();
if (_mimicable == null)
_setMimicable(GlobalKey.getWidget(watchedKey));
} }
void didUnmount() { Mimicable get _mimicable => watchedWidget;
super.didUnmount();
_stopMimic();
}
void didSyncWatchedKey(GlobalKey key, Widget widget) { void didSyncWatchedKey(GlobalKey key, Widget widget) {
super.didSyncWatchedKey(key, widget); super.didSyncWatchedKey(key, widget); // calls startWatching()
_setMimicable(widget); if (onMimicReady != null && _mimicable._didBuildPlaceholder)
} onMimicReady();
void didRemoveWatchedKey(GlobalKey key) {
super.didRemoveWatchedKey(key);
_setMimicable(null);
} }
void _stopMimic() { void startWatching() {
if (_mimicable != null) { _mimicable.startMimic();
_mimicable.stopMimic();
_mimicable = null;
}
} }
void _setMimicable(widget) { void stopWatching() {
if (_mimicable != widget) { _mimicable.stopMimic();
_stopMimic();
if (widget != null) {
widget.startMimic();
}
}
setState(() {
_mimicable = widget;
});
if (onMimicReady != null && _mimicable != null && _mimicable._didBuildPlaceholder)
onMimicReady();
} }
Widget build() { Widget build() {
......
...@@ -28,6 +28,7 @@ export 'package:sky/src/widgets/floating_action_button.dart'; ...@@ -28,6 +28,7 @@ export 'package:sky/src/widgets/floating_action_button.dart';
export 'package:sky/src/widgets/focus.dart'; export 'package:sky/src/widgets/focus.dart';
export 'package:sky/src/widgets/framework.dart'; export 'package:sky/src/widgets/framework.dart';
export 'package:sky/src/widgets/gesture_detector.dart'; export 'package:sky/src/widgets/gesture_detector.dart';
export 'package:sky/src/widgets/global_key_watcher.dart';
export 'package:sky/src/widgets/homogeneous_viewport.dart'; export 'package:sky/src/widgets/homogeneous_viewport.dart';
export 'package:sky/src/widgets/icon.dart'; export 'package:sky/src/widgets/icon.dart';
export 'package:sky/src/widgets/icon_button.dart'; export 'package:sky/src/widgets/icon_button.dart';
......
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