Commit d8668606 authored by Hixie's avatar Hixie

Factor out GlobalKeyWatcher

This also moves the logic that tracks what Widget is being watched into
GlobalKeyWatcher, and much simplifies Mimic based on this.
parent ebf71f59
// 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 @@
import 'package:sky/src/widgets/basic.dart';
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;
_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);
}
}
import 'package:sky/src/widgets/global_key_watcher.dart';
typedef MimicReadyCallback();
......@@ -66,48 +22,24 @@ class Mimic extends GlobalKeyWatcher {
super.syncConstructorArguments(source);
}
Mimicable _mimicable;
void didMount() {
super.didMount();
if (_mimicable == null)
_setMimicable(GlobalKey.getWidget(watchedKey));
bool debugValidateWatchedWidget(Widget candidate) {
return candidate is Mimicable;
}
void didUnmount() {
super.didUnmount();
_stopMimic();
}
Mimicable get _mimicable => watchedWidget;
void didSyncWatchedKey(GlobalKey key, Widget widget) {
super.didSyncWatchedKey(key, widget);
_setMimicable(widget);
}
void didRemoveWatchedKey(GlobalKey key) {
super.didRemoveWatchedKey(key);
_setMimicable(null);
super.didSyncWatchedKey(key, widget); // calls startWatching()
if (onMimicReady != null && _mimicable._didBuildPlaceholder)
onMimicReady();
}
void _stopMimic() {
if (_mimicable != null) {
_mimicable.stopMimic();
_mimicable = null;
}
void startWatching() {
_mimicable.startMimic();
}
void _setMimicable(widget) {
if (_mimicable != widget) {
_stopMimic();
if (widget != null) {
widget.startMimic();
}
}
setState(() {
_mimicable = widget;
});
if (onMimicReady != null && _mimicable != null && _mimicable._didBuildPlaceholder)
onMimicReady();
void stopWatching() {
_mimicable.stopMimic();
}
Widget build() {
......
......@@ -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/framework.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/icon.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