FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Merge pull request #1416 from NativeScript/feature/proxy-view-container · NativeScript/NativeScript@a2fd4d1 · GitHub

Commit a2fd4d1

Browse files
committed
Merge pull request #1416 from NativeScript/feature/proxy-view-container
Feature/proxy view container
2 parents 3cadbb8 + d1676ca commit a2fd4d1

17 files changed

Lines changed: 429 additions & 121 deletions

File tree

‎CrossPlatformModules.csproj‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@
8989
<DependentUpon>modal-page.xml</DependentUpon>
9090
</TypeScriptCompile>
9191
<TypeScriptCompile Include="apps\tests\ui\placeholder\placeholder-tests.ts" />
92+
<TypeScriptCompile Include="apps\tests\ui\proxy-view-container\proxy-view-container-tests.ts" />
9293
<TypeScriptCompile Include="apps\tests\ui\tab-view\tab-view-navigation-tests.ts" />
9394
<TypeScriptCompile Include="apps\tests\xml-declaration\custom-code-file.ts" />
9495
<TypeScriptCompile Include="apps\transforms\app.ts" />
@@ -726,6 +727,8 @@
726727
<TypeScriptCompile Include="ui\builder\binding-builder.d.ts" />
727728
<TypeScriptCompile Include="ui\builder\special-properties.d.ts" />
728729
<TypeScriptCompile Include="ui\builder\special-properties.ts" />
730+
<TypeScriptCompile Include="ui\proxy-view-container\proxy-view-container.d.ts" />
731+
<TypeScriptCompile Include="ui\proxy-view-container\proxy-view-container.ts" />
729732
<TypeScriptCompile Include="ui\styling\background.android.d.ts" />
730733
<TypeScriptCompile Include="ui\styling\style-scope.d.ts" />
731734
<TypeScriptCompile Include="ui\styling\style.d.ts" />
@@ -2087,6 +2090,7 @@
20872090
</Content>
20882091
<Content Include="ui\package.json" />
20892092
<Content Include="tsconfig.json" />
2093+
<Content Include="ui\proxy-view-container\package.json" />
20902094
</ItemGroup>
20912095
<ItemGroup>
20922096
<None Include="build\tslint.json" />
@@ -2149,7 +2153,7 @@
21492153
<SaveServerSettingsInUserFile>False</SaveServerSettingsInUserFile>
21502154
</WebProjectProperties>
21512155
</FlavorProperties>
2152-
<UserProperties ui_2layouts_2wrap-layout_2package_1json__JSONSchema="http://json.schemastore.org/package" ui_2layouts_2grid-layout_2package_1json__JSONSchema="" ui_2layouts_2dock-layout_2package_1json__JSONSchema="" ui_2layouts_2absolute-layout_2package_1json__JSONSchema="http://json.schemastore.org/package" ui_2web-view_2package_1json__JSONSchema="http://json.schemastore.org/package" ui_2content-view_2package_1json__JSONSchema="http://json.schemastore.org/package" apps_2gallery-app_2package_1json__JSONSchema="http://json.schemastore.org/package" apps_2absolute-layout-demo_2package_1json__JSONSchema="http://json.schemastore.org/package" apps_2editable-text-demo_2package_1json__JSONSchema="http://json.schemastore.org/package" ui_2scroll-view_2package_1json__JSONSchema="http://json.schemastore.org/package" />
2156+
<UserProperties ui_2scroll-view_2package_1json__JSONSchema="http://json.schemastore.org/package" apps_2editable-text-demo_2package_1json__JSONSchema="http://json.schemastore.org/package" apps_2absolute-layout-demo_2package_1json__JSONSchema="http://json.schemastore.org/package" apps_2gallery-app_2package_1json__JSONSchema="http://json.schemastore.org/package" ui_2content-view_2package_1json__JSONSchema="http://json.schemastore.org/package" ui_2web-view_2package_1json__JSONSchema="http://json.schemastore.org/package" ui_2layouts_2absolute-layout_2package_1json__JSONSchema="http://json.schemastore.org/package" ui_2layouts_2dock-layout_2package_1json__JSONSchema="" ui_2layouts_2grid-layout_2package_1json__JSONSchema="" ui_2layouts_2wrap-layout_2package_1json__JSONSchema="http://json.schemastore.org/package" />
21532157
</VisualStudio>
21542158
</ProjectExtensions>
21552159
</Project>

‎apps/tests/testRunner.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export function isRunningOnEmulator(): boolean {
2525
}
2626

2727
export var allTests = {};
28+
allTests["PROXY-VIEW-CONTAINER"] = require("./ui/proxy-view-container/proxy-view-container-tests")
2829
allTests["SCROLL-VIEW"] = require("./ui/scroll-view/scroll-view-tests");
2930
allTests["ACTION-BAR"] = require("./ui/action-bar/action-bar-tests");
3031
allTests["XML-DECLARATION"] = require("./xml-declaration/xml-declaration-tests");
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
import TKUnit = require("../../TKUnit");
2+
import helper = require("../helper");
3+
import viewModule = require("ui/core/view");
4+
import observable = require("data/observable");
5+
import color = require("color");
6+
import platform = require("platform");
7+
8+
import {ProxyViewContainer} from "ui/proxy-view-container";
9+
import {View, Button, StackLayout} from "ui";
10+
11+
export function test_add_children_to_attached_proxy() {
12+
var outer = new StackLayout();
13+
var proxy = new ProxyViewContainer();
14+
15+
function testAction(views: Array<viewModule.View>) {
16+
outer.addChild(createBtn("1"));
17+
outer.addChild(proxy);
18+
proxy.addChild(createBtn("2"));
19+
proxy.addChild(createBtn("3"));
20+
proxy.addChild(createBtn("4"));
21+
22+
outer.addChild(createBtn("5"));
23+
24+
assertNativeChildren(outer, ["1", "2", "3", "4", "5"]);
25+
};
26+
27+
helper.buildUIAndRunTest(outer, testAction);
28+
}
29+
30+
export function test_add_children_to_detached_proxy() {
31+
var outer = new StackLayout();
32+
var proxy = new ProxyViewContainer();
33+
34+
function testAction(views: Array<viewModule.View>) {
35+
outer.addChild(createBtn("1"));
36+
37+
proxy.addChild(createBtn("2"));
38+
proxy.addChild(createBtn("3"));
39+
proxy.addChild(createBtn("4"));
40+
outer.addChild(proxy);
41+
42+
outer.addChild(createBtn("5"));
43+
44+
assertNativeChildren(outer, ["1", "2", "3", "4", "5"]);
45+
};
46+
47+
helper.buildUIAndRunTest(outer, testAction);
48+
}
49+
50+
export function test_remove_proxy() {
51+
var outer = new StackLayout();
52+
var proxy = new ProxyViewContainer();
53+
54+
outer.addChild(createBtn("1"));
55+
56+
outer.addChild(proxy);
57+
proxy.addChild(createBtn("2"));
58+
proxy.addChild(createBtn("3"));
59+
proxy.addChild(createBtn("4"));
60+
61+
outer.addChild(createBtn("5"));
62+
63+
function testAction(views: Array<viewModule.View>) {
64+
assertNativeChildren(outer, ["1", "2", "3", "4", "5"]);
65+
outer.removeChild(proxy);
66+
assertNativeChildren(outer, ["1", "5"]);
67+
};
68+
69+
helper.buildUIAndRunTest(outer, testAction);
70+
}
71+
72+
export function test_remove_child_of_attached_proxy() {
73+
var outer = new StackLayout();
74+
var proxy = new ProxyViewContainer();
75+
76+
outer.addChild(createBtn("1"));
77+
78+
outer.addChild(proxy);
79+
proxy.addChild(createBtn("2"));
80+
var testBtn = createBtn("3")
81+
proxy.addChild(testBtn);
82+
proxy.addChild(createBtn("4"));
83+
84+
outer.addChild(createBtn("5"));
85+
86+
function testAction(views: Array<viewModule.View>) {
87+
assertNativeChildren(outer, ["1", "2", "3", "4", "5"]);
88+
proxy.removeChild(testBtn);
89+
assertNativeChildren(outer, ["1", "2", "4", "5"]);
90+
};
91+
92+
helper.buildUIAndRunTest(outer, testAction);
93+
}
94+
95+
export function test_insert_inside_porxy() {
96+
var outer = new StackLayout();
97+
var proxy = new ProxyViewContainer();
98+
99+
outer.addChild(createBtn("1"));
100+
101+
outer.addChild(proxy);
102+
proxy.addChild(createBtn("2"));
103+
proxy.addChild(createBtn("4"));
104+
105+
outer.addChild(createBtn("5"));
106+
107+
function testAction(views: Array<viewModule.View>) {
108+
assertNativeChildren(outer, ["1", "2", "4", "5"]);
109+
proxy.insertChild(createBtn("3"), 1);
110+
assertNativeChildren(outer, ["1", "2", "3", "4", "5"]);
111+
};
112+
113+
helper.buildUIAndRunTest(outer, testAction);
114+
}
115+
116+
export function test_insert_after_porxy() {
117+
var outer = new StackLayout();
118+
var proxy = new ProxyViewContainer();
119+
120+
outer.addChild(createBtn("1"));
121+
122+
outer.addChild(proxy);
123+
proxy.addChild(createBtn("2"));
124+
proxy.addChild(createBtn("3"));
125+
proxy.addChild(createBtn("4"));
126+
127+
function testAction(views: Array<viewModule.View>) {
128+
assertNativeChildren(outer, ["1", "2", "3", "4"]);
129+
outer.insertChild(createBtn("5"), 2);
130+
assertNativeChildren(outer, ["1", "2", "3", "4", "5"]);
131+
};
132+
133+
helper.buildUIAndRunTest(outer, testAction);
134+
}
135+
136+
function createBtn(text: string): Button {
137+
var b = new Button();
138+
b.text = text;
139+
return b;
140+
}
141+
142+
function assertNativeChildren(stack: StackLayout, arr: Array<string>) {
143+
if (stack.android) {
144+
let android: org.nativescript.widgets.StackLayout = stack.android;
145+
TKUnit.assertEqual(android.getChildCount(), arr.length, "Native children");
146+
for (let i = 0; i < arr.length; i++) {
147+
let nativeBtn = <android.widget.Button>android.getChildAt(i);
148+
TKUnit.assertEqual(nativeBtn.getText(), arr[i]);
149+
}
150+
} else if (stack.ios) {
151+
let ios: UIView = stack.ios;
152+
TKUnit.assertEqual(ios.subviews.count, arr.length, "Native children");
153+
for (let i = 0; i < arr.length; i++) {
154+
let nativeBtn = <UIButton>ios.subviews[i];
155+
TKUnit.assertEqual(nativeBtn.titleLabel.text, arr[i]);
156+
}
157+
} else {
158+
TKUnit.assert(false, "No native view to assert");
159+
}
160+
}

‎tsconfig.json‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,7 @@
265265
"apps/tests/ui/page/test-page-module.ts",
266266
"apps/tests/ui/placeholder/placeholder-tests.ts",
267267
"apps/tests/ui/progress/progress-tests.ts",
268+
"apps/tests/ui/proxy-view-container/proxy-view-container-tests.ts",
268269
"apps/tests/ui/repeater/repeater-tests.ts",
269270
"apps/tests/ui/repeater/repeaterItems-bindingToGestures.ts",
270271
"apps/tests/ui/scroll-view/scroll-view-tests.ts",
@@ -594,6 +595,8 @@
594595
"ui/progress/progress.android.ts",
595596
"ui/progress/progress.d.ts",
596597
"ui/progress/progress.ios.ts",
598+
"ui/proxy-view-container/proxy-view-container.d.ts",
599+
"ui/proxy-view-container/proxy-view-container.ts",
597600
"ui/repeater/repeater.d.ts",
598601
"ui/repeater/repeater.ts",
599602
"ui/scroll-view/scroll-view-common.ts",

‎ui/core/view-common.ts‎

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ registerSpecialProperty("class", (instance: definition.View, propertyValue: stri
2222
instance.className = propertyValue;
2323
});
2424

25-
function getEventOrGestureName(name: string) : string {
25+
function getEventOrGestureName(name: string): string {
2626
return name.indexOf("on") === 0 ? name.substr(2, name.length - 2) : name;
2727
}
2828

@@ -190,7 +190,7 @@ export class View extends ProxyObject implements definition.View {
190190
observe(type: gestures.GestureTypes, callback: (args: gestures.GestureEventData) => void, thisArg?: any): void {
191191
if (!this._gestureObservers[type]) {
192192
this._gestureObservers[type] = [];
193-
}
193+
}
194194

195195
this._gestureObservers[type].push(gestures.observe(this, type, callback, thisArg));
196196
}
@@ -926,6 +926,26 @@ export class View extends ProxyObject implements definition.View {
926926
//
927927
}
928928

929+
_childIndexToNativeChildIndex(index?: number): number {
930+
return index;
931+
}
932+
933+
_getNativeViewsCount(): number {
934+
return this._isAddedToNativeVisualTree ? 1 : 0;
935+
}
936+
937+
_eachLayoutView(callback: (View) => void): void {
938+
return callback(this);
939+
}
940+
941+
_addToSuperview(superview: any, index?: number): boolean {
942+
// IOS specific
943+
return false;
944+
}
945+
_removeFromSuperview(): void {
946+
// IOS specific
947+
}
948+
929949
/**
930950
* Core logic for adding a child view to this instance. Used by the framework to handle lifecycle events more centralized. Do not outside the UI Stack implementation.
931951
* // TODO: Think whether we need the base Layout routine.
@@ -954,7 +974,8 @@ export class View extends ProxyObject implements definition.View {
954974
view.style._inheritStyleProperties();
955975

956976
if (!view._isAddedToNativeVisualTree) {
957-
view._isAddedToNativeVisualTree = this._addViewToNativeVisualTree(view, atIndex);
977+
var nativeIndex = this._childIndexToNativeChildIndex(atIndex);
978+
view._isAddedToNativeVisualTree = this._addViewToNativeVisualTree(view, nativeIndex);
958979
}
959980

960981
// TODO: Discuss this.

‎ui/core/view.d.ts‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@ declare module "ui/core/view" {
445445
* Sets in-line CSS string as style.
446446
* @param style - In-line CSS string.
447447
*/
448-
public setInlineStyle(style: string) : void;
448+
public setInlineStyle(style: string): void;
449449

450450
public getGestureObservers(type: gestures.GestureTypes): Array<gestures.GesturesObserver>;
451451

@@ -497,6 +497,14 @@ declare module "ui/core/view" {
497497
_removeView(view: View);
498498
_context: any /* android.content.Context */;
499499

500+
_childIndexToNativeChildIndex(index?: number): number;
501+
_getNativeViewsCount(): number;
502+
503+
_eachLayoutView(callback: (View) => void): void;
504+
505+
_addToSuperview(superview: any, index?: number): boolean;
506+
_removeFromSuperview();
507+
500508
public _applyXmlAttribute(attribute: string, value: any): boolean;
501509

502510
// TODO: Implement logic for stripping these lines out

‎ui/core/view.ios.ts‎

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import viewCommon = require("./view-common");
1+
import types = require("utils/types");
2+
import viewCommon = require("./view-common");
23
import trace = require("trace");
34
import utils = require("utils/utils");
45
import dependencyObservable = require("ui/core/dependency-observable");
@@ -265,6 +266,26 @@ export class View extends viewCommon.View {
265266
if (this._cachedFrame) {
266267
this._setNativeViewFrame(this._nativeView, this._cachedFrame);
267268
}
269+
}
270+
271+
public _addToSuperview(superview: any, atIndex?: number): boolean {
272+
if (superview && this._nativeView) {
273+
if (types.isNullOrUndefined(atIndex) || atIndex >= superview.subviews.count) {
274+
superview.addSubview(this._nativeView);
275+
} else {
276+
superview.insertSubviewAtIndex(this._nativeView, atIndex);
277+
}
278+
279+
return true;
280+
}
281+
282+
return false;
283+
}
284+
285+
public _removeFromSuperview() {
286+
if (this._nativeView) {
287+
this._nativeView.removeFromSuperview();
288+
}
268289
}
269290
}
270291

@@ -290,30 +311,15 @@ export class CustomLayoutView extends View {
290311
}
291312

292313
public _addViewToNativeVisualTree(child: View, atIndex: number): boolean {
293-
super._addViewToNativeVisualTree(child);
314+
super._addViewToNativeVisualTree(child, atIndex);
294315

295-
if (this._nativeView && child._nativeView) {
296-
var types = require("utils/types");
297-
298-
if (types.isNullOrUndefined(atIndex) || atIndex >= this._nativeView.subviews.count) {
299-
this._nativeView.addSubview(child._nativeView);
300-
}
301-
else {
302-
this._nativeView.insertSubviewAtIndex(child._nativeView, atIndex);
303-
}
304-
305-
return true;
306-
}
307-
308-
return false;
316+
return child._addToSuperview(this._nativeView, atIndex);
309317
}
310318

311319
public _removeViewFromNativeVisualTree(child: View): void {
312320
super._removeViewFromNativeVisualTree(child);
313321

314-
if (child._nativeView) {
315-
child._nativeView.removeFromSuperview();
316-
}
322+
child._removeFromSuperview();
317323
}
318324
}
319325

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL