Register ProxyViewContainer children with parent layout.

- Fixes a crash when used in a GridLayout on iOS since the layout needs to be
notified of any layout children changes.
- Adds a _parentChanged hook for all views.
This commit is contained in:
Hristo Deshev
2016-01-28 18:27:31 +02:00
parent e6da4f3377
commit 698345f171
7 changed files with 114 additions and 31 deletions

View File

@@ -1,8 +1,8 @@
import types = require("utils/types");
import view = require("ui/core/view");
import {View} from "ui/core/view";
import definition = require("ui/proxy-view-container");
import trace = require("trace");
import layout = require("ui/layouts/layout-base");
import {LayoutBase} from "ui/layouts/layout-base";
/**
* Proxy view container that adds all its native children directly to the parent.
* To be used as a logical grouping container of views.
@@ -16,7 +16,7 @@ import layout = require("ui/layouts/layout-base");
// * Proxy (with children) is removed form the DOM.
// - IOS: Handled in _removeFromSuperview - when the proxy is removed, it removes all its children from its parent.
// - Android: _onDetached calls _removeViewFromNativeVisualTree recursively when the proxy is removed from its parent.
export class ProxyViewContainer extends layout.LayoutBase implements definition.ProxyViewContainer {
export class ProxyViewContainer extends LayoutBase implements definition.ProxyViewContainer {
// No native view for proxy container.
get ios(): any {
return null;
@@ -51,15 +51,15 @@ export class ProxyViewContainer extends layout.LayoutBase implements definition.
});
}
public _addViewToNativeVisualTree(child: view.View, atIndex?: number): boolean {
trace.write("ViewContainer._addViewToNativeVisualTree for a child " + view + " ViewContainer.parent: " + this.parent, trace.categories.ViewHierarchy);
public _addViewToNativeVisualTree(child: View, atIndex?: number): boolean {
trace.write("ViewContainer._addViewToNativeVisualTree for a child " + child + " ViewContainer.parent: " + this.parent, trace.categories.ViewHierarchy);
super._addViewToNativeVisualTree(child);
var parent = this.parent;
if (parent) {
let baseIndex = 0;
let insideIndex = 0;
if (parent instanceof layout.LayoutBase) {
if (parent instanceof LayoutBase) {
// Get my index in parent and convert it to native index.
baseIndex = parent._childIndexToNativeChildIndex(parent.getChildIndex(this));
}
@@ -78,8 +78,8 @@ export class ProxyViewContainer extends layout.LayoutBase implements definition.
return false;
}
public _removeViewFromNativeVisualTree(child: view.View): void {
trace.write("ProxyViewContainer._removeViewFromNativeVisualTree for a child " + view + " ViewContainer.parent: " + this.parent, trace.categories.ViewHierarchy);
public _removeViewFromNativeVisualTree(child: View): void {
trace.write("ProxyViewContainer._removeViewFromNativeVisualTree for a child " + child + " ViewContainer.parent: " + this.parent, trace.categories.ViewHierarchy);
super._removeViewFromNativeVisualTree(child);
var parent = this.parent;
@@ -108,4 +108,43 @@ export class ProxyViewContainer extends layout.LayoutBase implements definition.
return true;
});
}
/*
* Some layouts (e.g. GridLayout) need to get notified when adding and
* removing children, so that they can update private measure data.
*
* We register our children with the parent to avoid breakage.
*/
public _registerLayoutChild(child: View) {
if (this.parent instanceof LayoutBase) {
(<LayoutBase>this.parent)._registerLayoutChild(child);
}
}
public _unregisterLayoutChild(child: View) {
if (this.parent instanceof LayoutBase) {
(<LayoutBase>this.parent)._unregisterLayoutChild(child);
}
}
/*
* Register/unregister existing children with the parent layout.
*/
public _parentChanged(oldParent: View): void {
const addingToParent = this.parent && !oldParent;
const newLayout = <LayoutBase>this.parent;
const oldLayout = <LayoutBase>oldParent;
if (addingToParent && newLayout instanceof LayoutBase) {
this._eachChildView((child) => {
newLayout._registerLayoutChild(child);
return true;
});
} else if (oldLayout instanceof LayoutBase) {
this._eachChildView((child) => {
oldLayout._unregisterLayoutChild(child);
return true;
});
}
}
}