clear nativeView field. (#3418)

moved some native setters from TabView to TabViewItem.
shorthand properties converters now accounts for unsetValue.
better formatting for test duration.
This commit is contained in:
Hristo Hristov
2017-01-05 18:08:42 +02:00
committed by GitHub
parent ea92b12419
commit bfab188ba0
8 changed files with 226 additions and 209 deletions

View File

@ -49,7 +49,7 @@ export var write = function write(message: string, type?: number) {
var runTest = function (testInfo: TestInfoEntry) { var runTest = function (testInfo: TestInfoEntry) {
let start = time(); let start = time();
let duration; let duration: number;
try { try {
if (testInfo.instance) { if (testInfo.instance) {
testInfo.testFunc.apply(testInfo.instance); testInfo.testFunc.apply(testInfo.instance);
@ -59,7 +59,7 @@ var runTest = function (testInfo: TestInfoEntry) {
} }
if (testInfo.isTest) { if (testInfo.isTest) {
duration = time() - start; duration = (time() - start).toFixed(2);
testInfo.duration = duration; testInfo.duration = duration;
write(`--- [${testInfo.testName}] OK, duration: ${duration}`, trace.messageType.info); write(`--- [${testInfo.testName}] OK, duration: ${duration}`, trace.messageType.info);
testInfo.isPassed = true; testInfo.isPassed = true;
@ -67,7 +67,7 @@ var runTest = function (testInfo: TestInfoEntry) {
} }
catch (e) { catch (e) {
if (testInfo.isTest) { if (testInfo.isTest) {
duration = time() - start; duration = (time() - start).toFixed(2);
testInfo.duration = duration; testInfo.duration = duration;
write(`--- [${testInfo.testName}] FAILED: ${e.message}, Stack: ${e.stack}, duration: ${duration}`, trace.messageType.error); write(`--- [${testInfo.testName}] FAILED: ${e.message}, Stack: ${e.stack}, duration: ${duration}`, trace.messageType.error);
testInfo.isPassed = false; testInfo.isPassed = false;

View File

@ -210,7 +210,7 @@ function startLog(): void {
function log(): void { function log(): void {
let testsName: string = this.name; let testsName: string = this.name;
let duration = TKUnit.time() - this.start; let duration = TKUnit.time() - this.start;
TKUnit.write(testsName + " COMPLETED for " + duration + " BACKSTACK DEPTH: " + topmost().backStack.length, messageType.info); TKUnit.write(testsName + " COMPLETED for " + duration.toFixed(2) + " BACKSTACK DEPTH: " + topmost().backStack.length, messageType.info);
} }
let testsSelector: string let testsSelector: string
@ -243,7 +243,7 @@ export var runAll = function (testSelector?: string) {
} }
} }
console.log("TESTS: " + singleModuleName + " " + singleTestName); console.log("TESTS: " + singleModuleName ? singleModuleName : "" + " " + singleTestName ? singleTestName : "");
var totalSuccess = 0; var totalSuccess = 0;
var totalFailed: Array<TKUnit.TestFailure> = []; var totalFailed: Array<TKUnit.TestFailure> = [];

View File

@ -492,7 +492,9 @@ export class ViewBase extends Observable implements ViewBaseDefinition {
this.parent._addViewToNativeVisualTree(this, atIndex); this.parent._addViewToNativeVisualTree(this, atIndex);
} }
applyNativeSetters(this); if (this.nativeView) {
applyNativeSetters(this);
}
this.eachChild((child) => { this.eachChild((child) => {
child._setupUI(context); child._setupUI(context);
@ -510,8 +512,10 @@ export class ViewBase extends Observable implements ViewBaseDefinition {
return true; return true;
}); });
// TODO: rename and implement this as resetNativeSetters if (this.nativeView) {
resetStyleProperties(this.style); // TODO: rename and implement this as resetNativeSetters
resetStyleProperties(this.style);
}
if (this.parent) { if (this.parent) {
this.parent._removeViewFromNativeVisualTree(this); this.parent._removeViewFromNativeVisualTree(this);

View File

@ -4,7 +4,7 @@ import { Animation, AnimationPromise } from "ui/animation";
import { Source } from "utils/debug"; import { Source } from "utils/debug";
import { Background } from "ui/styling/background"; import { Background } from "ui/styling/background";
import { import {
ViewBase, getEventOrGestureName, EventData, Style, ViewBase, getEventOrGestureName, EventData, Style, unsetValue,
Property, CssProperty, ShorthandProperty, InheritedCssProperty, Property, CssProperty, ShorthandProperty, InheritedCssProperty,
gestureFromString, isIOS, traceEnabled, traceWrite, traceCategories, printUnregisteredProperties, makeParser, makeValidator gestureFromString, isIOS, traceEnabled, traceWrite, traceCategories, printUnregisteredProperties, makeParser, makeValidator
} from "./view-base"; } from "./view-base";
@ -1135,13 +1135,13 @@ heightProperty.register(Style);
const marginProperty = new ShorthandProperty<Style, string | PercentLength>({ const marginProperty = new ShorthandProperty<Style, string | PercentLength>({
name: "margin", cssName: "margin", name: "margin", cssName: "margin",
getter: function (this: Style) { getter: function (this: Style) {
if (this.marginTop === this.marginRight && if (this.marginTop === this.marginRight &&
this.marginTop === this.marginBottom && this.marginTop === this.marginBottom &&
this.marginTop === this.marginLeft) { this.marginTop === this.marginLeft) {
return this.marginTop; return this.marginTop;
} }
return `${this.marginTop} ${this.marginRight} ${this.marginBottom} ${this.marginLeft}`; return `${this.marginTop} ${this.marginRight} ${this.marginBottom} ${this.marginLeft}`;
}, },
converter: convertToMargins converter: convertToMargins
}); });
@ -1161,13 +1161,13 @@ marginBottomProperty.register(Style);
const paddingProperty = new ShorthandProperty<Style, string | Length>({ const paddingProperty = new ShorthandProperty<Style, string | Length>({
name: "padding", cssName: "padding", name: "padding", cssName: "padding",
getter: function (this: Style) { getter: function (this: Style) {
if (this.paddingTop === this.paddingRight && if (this.paddingTop === this.paddingRight &&
this.paddingTop === this.paddingBottom && this.paddingTop === this.paddingBottom &&
this.paddingTop === this.paddingLeft) { this.paddingTop === this.paddingLeft) {
return this.paddingTop; return this.paddingTop;
} }
return `${this.paddingTop} ${this.paddingRight} ${this.paddingBottom} ${this.paddingLeft}`; return `${this.paddingTop} ${this.paddingRight} ${this.paddingBottom} ${this.paddingLeft}`;
}, },
converter: convertToPaddings converter: convertToPaddings
}); });
@ -1295,7 +1295,7 @@ function parseThickness(value: string): Thickness {
} }
function convertToMargins(this: void, value: string | PercentLength): [CssProperty<any, any>, any][] { function convertToMargins(this: void, value: string | PercentLength): [CssProperty<any, any>, any][] {
if (typeof value === "string" && value !== "auto"){ if (typeof value === "string" && value !== "auto") {
let thickness = parseThickness(value); let thickness = parseThickness(value);
return [ return [
[marginTopProperty, PercentLength.parse(thickness.top)], [marginTopProperty, PercentLength.parse(thickness.top)],
@ -1315,7 +1315,7 @@ function convertToMargins(this: void, value: string | PercentLength): [CssProper
} }
function convertToPaddings(this: void, value: string | Length): [CssProperty<any, any>, any][] { function convertToPaddings(this: void, value: string | Length): [CssProperty<any, any>, any][] {
if (typeof value === "string" && value !== "auto"){ if (typeof value === "string" && value !== "auto") {
let thickness = parseThickness(value); let thickness = parseThickness(value);
return [ return [
[paddingTopProperty, Length.parse(thickness.top)], [paddingTopProperty, Length.parse(thickness.top)],
@ -1406,10 +1406,10 @@ function transformConverter(value: string): Object {
} }
function convertToTransform(value: string): [CssProperty<any, any>, any][] { function convertToTransform(value: string): [CssProperty<any, any>, any][] {
let newTransform = transformConverter(value); let newTransform = value == unsetValue ? { "none": "none" } : transformConverter(value);
let array = []; let array = [];
let values: Array<string>; let values: Array<string>;
for (var transform in newTransform) { for (let transform in newTransform) {
switch (transform) { switch (transform) {
case "scaleX": case "scaleX":
array.push([scaleXProperty, parseFloat(newTransform[transform])]); array.push([scaleXProperty, parseFloat(newTransform[transform])]);
@ -1625,7 +1625,7 @@ const borderColorProperty = new ShorthandProperty<Style, string | Color>({
Color.equals(this.borderTopColor, this.borderBottomColor) && Color.equals(this.borderTopColor, this.borderBottomColor) &&
Color.equals(this.borderTopColor, this.borderLeftColor)) { Color.equals(this.borderTopColor, this.borderLeftColor)) {
return this.borderTopColor ? this.borderTopColor.toString() : ""; return this.borderTopColor ? this.borderTopColor.toString() : "";
} }
else { else {
return `${this.borderTopColor} ${this.borderRightColor} ${this.borderBottomColor} ${this.borderLeftColor}`; return `${this.borderTopColor} ${this.borderRightColor} ${this.borderBottomColor} ${this.borderLeftColor}`;
} }
@ -1690,15 +1690,15 @@ const borderWidthProperty = new ShorthandProperty<Style, string | Length>({
getter: function (this: Style) { getter: function (this: Style) {
if (this.borderTopWidth === this.borderRightWidth && if (this.borderTopWidth === this.borderRightWidth &&
this.borderTopWidth === this.borderBottomWidth && this.borderTopWidth === this.borderBottomWidth &&
this.borderTopWidth === this.borderLeftWidth){ this.borderTopWidth === this.borderLeftWidth) {
return this.borderTopWidth; return this.borderTopWidth;
} }
else { else {
return `${this.borderTopWidth} ${this.borderRightWidth} ${this.borderBottomWidth} ${this.borderLeftWidth}`; return `${this.borderTopWidth} ${this.borderRightWidth} ${this.borderBottomWidth} ${this.borderLeftWidth}`;
} }
}, },
converter: function (value) { converter: function (value) {
if (typeof value === "string" && value !== "auto"){ if (typeof value === "string" && value !== "auto") {
let borderWidths = parseThickness(value); let borderWidths = parseThickness(value);
return [ return [
[borderTopWidthProperty, borderWidths.top], [borderTopWidthProperty, borderWidths.top],
@ -1795,7 +1795,7 @@ const borderRadiusProperty = new ShorthandProperty<Style, string | number>({
return `${this.borderTopLeftRadius} ${this.borderTopRightRadius} ${this.borderBottomRightRadius} ${this.borderBottomLeftRadius}`; return `${this.borderTopLeftRadius} ${this.borderTopRightRadius} ${this.borderBottomRightRadius} ${this.borderBottomLeftRadius}`;
}, },
converter: function (value) { converter: function (value) {
if (typeof value === "string"){ if (typeof value === "string") {
let borderRadius = parseThickness(value); let borderRadius = parseThickness(value);
return [ return [
[borderTopLeftRadiusProperty, borderRadius.top], [borderTopLeftRadiusProperty, borderRadius.top],
@ -1950,15 +1950,24 @@ const fontProperty = new ShorthandProperty<Style, string>({
return `${this.fontStyle} ${this.fontWeight} ${this.fontSize} ${this.fontFamily}`; return `${this.fontStyle} ${this.fontWeight} ${this.fontSize} ${this.fontFamily}`;
}, },
converter: function (value) { converter: function (value) {
let font = parseFont(value); if (value == unsetValue) {
let fontSize = fontSizeConverter(font.fontSize); return [
[fontStyleProperty, unsetValue],
[fontWeightProperty, unsetValue],
[fontSizeProperty, unsetValue],
[fontFamilyProperty, unsetValue]
];
} else {
let font = parseFont(value);
let fontSize = fontSizeConverter(font.fontSize);
return [ return [
[fontStyleProperty, font.fontStyle], [fontStyleProperty, font.fontStyle],
[fontWeightProperty, font.fontWeight], [fontWeightProperty, font.fontWeight],
[fontSizeProperty, fontSize], [fontSizeProperty, fontSize],
[fontFamilyProperty, font.fontFamily] [fontFamilyProperty, font.fontFamily]
] ];
}
} }
}) })
fontProperty.register(Style); fontProperty.register(Style);

View File

@ -120,6 +120,7 @@ export class View extends ViewCommon {
} }
this[ANDROID] = undefined; this[ANDROID] = undefined;
this.nativeView = undefined;
} }
get _nativeView(): android.view.View { get _nativeView(): android.view.View {

View File

@ -1,4 +1,4 @@
import { LayoutBase, View, Style, CssProperty, isIOS, ShorthandProperty, makeValidator, makeParser} from "ui/layouts/layout-base"; import { LayoutBase, View, Style, CssProperty, isIOS, ShorthandProperty, makeValidator, makeParser, unsetValue } from "ui/layouts/layout-base";
export * from "ui/layouts/layout-base"; export * from "ui/layouts/layout-base";
@ -316,17 +316,22 @@ const flexFlowProperty = new ShorthandProperty<Style, string>({
}, },
converter: function (value: string) { converter: function (value: string) {
const properties: [CssProperty<any, any>, any][] = []; const properties: [CssProperty<any, any>, any][] = [];
const trimmed = value && value.trim(); if (value == unsetValue) {
if (trimmed) { properties.push([flexDirectionProperty, value]);
const values = trimmed.split(/\s+/); properties.push([flexWrapProperty, value]);
if (values.length >= 1 && FlexDirection.isValid(values[0])) { } else {
properties.push([flexDirectionProperty, FlexDirection.parse(values[0])]); const trimmed = value && value.trim();
} if (trimmed) {
if (value.length >= 2 && FlexWrap.isValid(values[1])) { const values = trimmed.split(/\s+/);
properties.push([flexWrapProperty, FlexWrap.parse(values[1])]); if (values.length >= 1 && FlexDirection.isValid(values[0])) {
properties.push([flexDirectionProperty, FlexDirection.parse(values[0])]);
}
if (value.length >= 2 && FlexWrap.isValid(values[1])) {
properties.push([flexWrapProperty, FlexWrap.parse(values[1])]);
}
} }
} }
return properties;; return properties;
} }
}) })
flexFlowProperty.register(Style); flexFlowProperty.register(Style);
@ -339,43 +344,48 @@ const flexProperty = new ShorthandProperty<Style, string>({
}, },
converter: function (value: string) { converter: function (value: string) {
const properties: [CssProperty<any, any>, any][] = []; const properties: [CssProperty<any, any>, any][] = [];
const trimmed = value && value.trim(); if (value == unsetValue) {
if (trimmed) { properties.push([flexGrowProperty, value]);
const values = trimmed.split(/\s+/); properties.push([flexShrinkProperty, value]);
if (values.length === 1) { } else {
switch (values[0]) { const trimmed = value && value.trim();
case "inital": if (trimmed) {
properties.push([flexGrowProperty, 0]); const values = trimmed.split(/\s+/);
properties.push([flexShrinkProperty, 1]); if (values.length === 1) {
// properties.push([flexBasisProperty, FlexBasis.AUTO]) switch (values[0]) {
break; case "inital":
case "auto": properties.push([flexGrowProperty, 0]);
properties.push([flexGrowProperty, 1]);
properties.push([flexShrinkProperty, 1]);
// properties.push([flexBasisProperty, FlexBasis.AUTO])
break;
case "none":
properties.push([flexGrowProperty, 0]);
properties.push([flexShrinkProperty, 0]);
// properties.push([flexBasisProperty, FlexBasis.AUTO])
break;
default:
if (FlexGrow.isValid(values[0])) {
properties.push([flexGrowProperty, FlexGrow.parse(values[0])]);
properties.push([flexShrinkProperty, 1]); properties.push([flexShrinkProperty, 1]);
// properties.push([flexBasisProperty, 0]) // properties.push([flexBasisProperty, FlexBasis.AUTO])
} break;
case "auto":
properties.push([flexGrowProperty, 1]);
properties.push([flexShrinkProperty, 1]);
// properties.push([flexBasisProperty, FlexBasis.AUTO])
break;
case "none":
properties.push([flexGrowProperty, 0]);
properties.push([flexShrinkProperty, 0]);
// properties.push([flexBasisProperty, FlexBasis.AUTO])
break;
default:
if (FlexGrow.isValid(values[0])) {
properties.push([flexGrowProperty, FlexGrow.parse(values[0])]);
properties.push([flexShrinkProperty, 1]);
// properties.push([flexBasisProperty, 0])
}
}
} }
} if (values.length >= 2) {
if (values.length >= 2) { if (FlexGrow.isValid(values[0]) && FlexShrink.isValid(values[1])) {
if (FlexGrow.isValid(values[0]) && FlexShrink.isValid(values[1])) { properties.push([flexGrowProperty, FlexGrow.parse(values[0])]);
properties.push([flexGrowProperty, FlexGrow.parse(values[0])]); properties.push([flexShrinkProperty, FlexShrink.parse(values[1])]);
properties.push([flexShrinkProperty, FlexShrink.parse(values[1])]); }
} }
// if (value.length >= 3) {
// properties.push({ property: flexBasisProperty, value: FlexBasis.parse(values[2])})
// }
} }
// if (value.length >= 3) {
// properties.push({ property: flexBasisProperty, value: FlexBasis.parse(values[2])})
// }
} }
return properties; return properties;
@ -394,4 +404,4 @@ Style.prototype.flexShrink = flexShrinkProperty.defaultValue;
Style.prototype.flexWrapBefore = flexWrapBeforeProperty.defaultValue; Style.prototype.flexWrapBefore = flexWrapBeforeProperty.defaultValue;
Style.prototype.alignSelf = alignSelfProperty.defaultValue; Style.prototype.alignSelf = alignSelfProperty.defaultValue;
// No flex-basis in our implementation. // No flex-basis in our implementation.

View File

@ -50,9 +50,11 @@ export class SegmentedBarItem extends SegmentedBarItemBase {
public setNativeView(textView: android.widget.TextView): void { public setNativeView(textView: android.widget.TextView): void {
this._textView = textView; this._textView = textView;
applyNativeSetters(this); if (textView) {
if (this.titleDirty) { applyNativeSetters(this);
this._update(); if (this.titleDirty) {
this._update();
}
} }
} }
@ -67,7 +69,7 @@ export class SegmentedBarItem extends SegmentedBarItemBase {
// titleTextView.setText(this.title || ""); // titleTextView.setText(this.title || "");
// } // }
let tv = this._textView; const tv = this._textView;
if (tv) { if (tv) {
let title = this.title; let title = this.title;
title = (title === null || title === undefined) ? "" : title; title = (title === null || title === undefined) ? "" : title;
@ -98,10 +100,10 @@ export class SegmentedBarItem extends SegmentedBarItemBase {
if (value instanceof Font) { if (value instanceof Font) {
// Set value // Set value
textView.setTypeface(value.getAndroidTypeface()); textView.setTypeface(value.getAndroidTypeface());
if (value.fontSize !== undefined){ if (value.fontSize !== undefined) {
textView.setTextSize(value.fontSize); textView.setTextSize(value.fontSize);
} }
} }
else { else {
// Reset value // Reset value
textView.setTypeface(value.typeface); textView.setTypeface(value.typeface);
@ -247,7 +249,7 @@ export class SegmentedBar extends SegmentedBarBase {
const newItems = value; const newItems = value;
let tabHost = this._android; let tabHost = this._android;
if (newItems) { if (newItems) {
newItems.forEach((item, i,arr) => this.insertTab(item, i)); newItems.forEach((item, i, arr) => this.insertTab(item, i));
if (this.selectedIndex < 0) { if (this.selectedIndex < 0) {
this.selectedIndex = tabHost.getCurrentTab(); this.selectedIndex = tabHost.getCurrentTab();

View File

@ -2,7 +2,8 @@ import {
TabViewBase, TabViewItemBase, itemsProperty, selectedIndexProperty, TabViewBase, TabViewItemBase, itemsProperty, selectedIndexProperty,
tabTextColorProperty, tabBackgroundColorProperty, selectedTabTextColorProperty, tabTextColorProperty, tabBackgroundColorProperty, selectedTabTextColorProperty,
androidSelectedTabHighlightColorProperty, androidOffscreenTabLimitProperty, androidSelectedTabHighlightColorProperty, androidOffscreenTabLimitProperty,
fontInternalProperty, traceCategory, View, layout, Color, Font, traceEnabled, traceWrite fontInternalProperty, traceCategory, View, layout, Color, Font, traceEnabled, traceWrite,
applyNativeSetters
} from "./tab-view-common" } from "./tab-view-common"
import { textTransformProperty, TextTransform, getTransformedText } from "ui/text-base"; import { textTransformProperty, TextTransform, getTransformedText } from "ui/text-base";
import { fromFileOrResource } from "image-source"; import { fromFileOrResource } from "image-source";
@ -16,11 +17,74 @@ const PRIMARY_COLOR = "colorPrimary";
const DEFAULT_ELEVATION = 4; const DEFAULT_ELEVATION = 4;
export class TabViewItem extends TabViewItemBase { export class TabViewItem extends TabViewItemBase {
public nativeView: android.widget.TextView;
public tabItemSpec: org.nativescript.widgets.TabItemSpec;
public index: number;
public _update() { public setNativeView(textView: android.widget.TextView): void {
const parent = <TabView>this.parent; this.nativeView = textView;
if (parent) { if (textView) {
parent._updateTabForItem(this); applyNativeSetters(this);
}
}
public _update(): void {
const tv = this.nativeView;
if (tv) {
const tabLayout = <org.nativescript.widgets.TabLayout>tv.getParent();
tabLayout.updateItemAt(this.index, this.tabItemSpec);
}
}
get [fontInternalProperty.native](): { typeface: android.graphics.Typeface, fontSize: number } {
const tv = this.nativeView;
return {
typeface: tv.getTypeface(),
fontSize: tv.getTextSize()
};
}
set [fontInternalProperty.native](value: Font | { typeface: android.graphics.Typeface, fontSize: number }) {
let typeface: android.graphics.Typeface;
let isFont: boolean;
const fontSize = value.fontSize;
if (value instanceof Font) {
isFont = true;
typeface = value.getAndroidTypeface();
}
else {
typeface = value.typeface;
}
const tv = this.nativeView;
tv.setTypeface(typeface);
if (isFont) {
if (fontSize !== undefined) {
tv.setTextSize(fontSize);
}
}
else {
tv.setTextSize(android.util.TypedValue.COMPLEX_UNIT_PX, fontSize);
}
}
get [textTransformProperty.native](): TextTransform {
return "none";
}
set [textTransformProperty.native](value: TextTransform) {
const tv = this.nativeView;
const result = getTransformedText(this.title, value);
tv.setText(result);
}
get [tabTextColorProperty.native](): android.content.res.ColorStateList {
return this.nativeView.getTextColors();
}
set [tabTextColorProperty.native](value: android.content.res.ColorStateList | Color) {
if (value instanceof Color) {
this.nativeView.setTextColor(value.android);
} else {
this.nativeView.setTextColor(value);
} }
} }
} }
@ -128,7 +192,7 @@ function ensurePagerAdapterClass() {
} }
return true; return true;
} }
owner.eachChildView(childCallback); owner.eachChildView(childCallback);
let bundle = new android.os.Bundle(); let bundle = new android.os.Bundle();
@ -171,6 +235,25 @@ function ensurePageChangedListenerClass() {
PageChangedListenerClass = PageChangedListener; PageChangedListenerClass = PageChangedListener;
} }
function createTabItemSpec(item: TabViewItem): org.nativescript.widgets.TabItemSpec {
let result = new org.nativescript.widgets.TabItemSpec();
result.title = item.title;
if (item.iconSource) {
if (item.iconSource.indexOf(RESOURCE_PREFIX) === 0) {
result.iconId = ad.resources.getDrawableId(item.iconSource.substr(RESOURCE_PREFIX.length));
} else {
let is = fromFileOrResource(item.iconSource);
if (is) {
// TODO: Make this native call that accepts string so that we don't load Bitmap in JS.
result.iconDrawable = new android.graphics.drawable.BitmapDrawable(is.android);
}
}
}
return result;
}
export class TabView extends TabViewBase { export class TabView extends TabViewBase {
private _grid: org.nativescript.widgets.GridLayout; private _grid: org.nativescript.widgets.GridLayout;
private _tabLayout: org.nativescript.widgets.TabLayout; private _tabLayout: org.nativescript.widgets.TabLayout;
@ -184,6 +267,18 @@ export class TabView extends TabViewBase {
return this._grid; return this._grid;
} }
public onItemsChanged(oldItems: TabViewItem[], newItems: TabViewItem[]): void {
super.onItemsChanged(oldItems, newItems);
if (oldItems) {
oldItems.forEach((item: TabViewItem, i, arr) => {
item.index = 0;
item.tabItemSpec = null;
item.setNativeView(null);
});
}
}
public _createNativeView() { public _createNativeView() {
if (traceEnabled) { if (traceEnabled) {
traceWrite("TabView._createUI(" + this + ");", traceCategory); traceWrite("TabView._createUI(" + this + ");", traceCategory);
@ -245,8 +340,11 @@ export class TabView extends TabViewBase {
} }
const tabItems = new Array<org.nativescript.widgets.TabItemSpec>(); const tabItems = new Array<org.nativescript.widgets.TabItemSpec>();
items.forEach((item, idx, arr) => { items.forEach((item, i, arr) => {
tabItems.push(this.createTabItem(item)); const tabItemSpec = createTabItemSpec(item);
item.index = i;
item.tabItemSpec = tabItemSpec;
tabItems.push(tabItemSpec);
}); });
ensurePagerAdapterClass(); ensurePagerAdapterClass();
@ -254,49 +352,17 @@ export class TabView extends TabViewBase {
this._pagerAdapter = new PagerAdapterClass(this, items); this._pagerAdapter = new PagerAdapterClass(this, items);
this._viewPager.setAdapter(this._pagerAdapter); this._viewPager.setAdapter(this._pagerAdapter);
this._tabLayout.setItems(tabItems, this._viewPager); const tabLayout = this._tabLayout;
tabLayout.setItems(tabItems, this._viewPager);
items.forEach((item, i, arr) => {
const tv = tabLayout.getTextViewForItemAt(i);
item.setNativeView(tv);
});
let selectedIndex = this.selectedIndex; let selectedIndex = this.selectedIndex;
if (selectedIndex < 0) { if (selectedIndex < 0) {
this.selectedIndex = this._viewPager.getCurrentItem(); this.selectedIndex = this._viewPager.getCurrentItem();
} }
//<<<<<<< HEAD
//=======
// this._updateSelectedIndexOnItemsPropertyChanged(data.newValue);
// Style properties such as fonts need to re-applied on the newwly created native TextViews
// this.style._syncNativeProperties();
//>>>>>>> ae68368... Fixed: Setting `setTypeface()` to null object
}
public _updateTabForItem(item: TabViewItem) {
let items = this.items;
if (items && items.length > 0) {
let index = this.items.indexOf(item);
if (index >= 0) {
this._tabLayout.updateItemAt(index, this.createTabItem(item));
}
}
}
private createTabItem(item: TabViewItem): org.nativescript.widgets.TabItemSpec {
let result = new org.nativescript.widgets.TabItemSpec();
result.title = item.title;
if (item.iconSource) {
if (item.iconSource.indexOf(RESOURCE_PREFIX) === 0) {
result.iconId = ad.resources.getDrawableId(item.iconSource.substr(RESOURCE_PREFIX.length));
} else {
let is = fromFileOrResource(item.iconSource);
if (is) {
// TODO: Make this native call that accepts string so that we don't load Bitmap in JS.
result.iconDrawable = new android.graphics.drawable.BitmapDrawable(is.android);
}
}
}
return result;
} }
get [androidOffscreenTabLimitProperty.native](): number { get [androidOffscreenTabLimitProperty.native](): number {
@ -316,21 +382,13 @@ export class TabView extends TabViewBase {
this._viewPager.setCurrentItem(value, true); this._viewPager.setCurrentItem(value, true);
} }
get [itemsProperty.native](): TabViewItemBase[] { get [itemsProperty.native](): TabViewItem[] {
return null; return null;
} }
set [itemsProperty.native](value: TabViewItemBase[]) { set [itemsProperty.native](value: TabViewItem[]) {
this.setAdapter(value); this.setAdapter(value);
} }
get [tabTextColorProperty.native](): number {
return this._tabLayout.getTabTextColor();
}
set [tabTextColorProperty.native](value: number | Color) {
let color = value instanceof Color ? value.android : value;
this._tabLayout.setTabTextColor(color);
}
get [tabBackgroundColorProperty.native](): android.graphics.drawable.Drawable { get [tabBackgroundColorProperty.native](): android.graphics.drawable.Drawable {
return this._tabLayout.getBackground(); return this._tabLayout.getBackground();
} }
@ -359,71 +417,4 @@ export class TabView extends TabViewBase {
let color = value instanceof Color ? value.android : value; let color = value instanceof Color ? value.android : value;
tabLayout.setSelectedIndicatorColors([color]); tabLayout.setSelectedIndicatorColors([color]);
} }
// TODO: Move this to TabViewItem
get [fontInternalProperty.native](): { typeface: android.graphics.Typeface, fontSize: number } {
let items = this.items;
if (items && items.length > 0) {
let tabLayout = this._tabLayout;
let tv = tabLayout.getTextViewForItemAt(0);
return {
typeface: tv.getTypeface(),
fontSize: tv.getTextSize()
};
}
return {
typeface: undefined,
fontSize: undefined
};
}
set [fontInternalProperty.native](value: Font | { typeface: android.graphics.Typeface, fontSize: number }) {
let typeface: android.graphics.Typeface;
let fontSize = value.fontSize;
let isFont: boolean;
if (value instanceof Font) {
isFont = true;
typeface = value.getAndroidTypeface();
}
else {
typeface = value.typeface;
}
let items = this.items;
if (items && items.length > 0) {
let tabLayout = this._tabLayout;
for (let i = 0, length = items.length; i < length; i++) {
let tv = tabLayout.getTextViewForItemAt(i);
tv.setTypeface(typeface);
if (isFont) {
if (fontSize !== undefined){
tv.setTextSize(fontSize);
}
}
else {
tv.setTextSize(android.util.TypedValue.COMPLEX_UNIT_PX, fontSize);
}
}
}
}
// TODO: Move this to TabViewItem
get [textTransformProperty.native](): TextTransform {
return "none";
}
set [textTransformProperty.native](value: TextTransform) {
let tabLayout = this._tabLayout;
let items = this.items;
if (!items) {
return;
}
for (let i = 0, count = items.length; i < count; i++) {
const textView = tabLayout.getTextViewForItemAt(i);
const result = getTransformedText(items[i].title, value);
textView.setText(result);
}
}
} }