-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathindex.android.ts
More file actions
143 lines (125 loc) · 4.45 KB
/
index.android.ts
File metadata and controls
143 lines (125 loc) · 4.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import { GridLayoutBase, ItemSpec as ItemSpecBase, rowProperty, columnProperty, rowSpanProperty, columnSpanProperty, GridUnitType } from './grid-layout-common';
import { View } from '../../core/view';
import { layout } from '../../../utils';
export * from './grid-layout-common';
function makeNativeSetter<T>(setter: (lp: org.nativescript.widgets.CommonLayoutParams, value: T) => void) {
return function (this: View, value: T) {
const nativeView: android.view.View = this.nativeViewProtected;
const lp = nativeView.getLayoutParams() || new org.nativescript.widgets.CommonLayoutParams();
if (lp instanceof org.nativescript.widgets.CommonLayoutParams) {
setter(lp, value);
nativeView.setLayoutParams(lp);
}
};
}
View.prototype[rowProperty.setNative] = makeNativeSetter<number>((lp, value) => (lp.row = value));
View.prototype[columnProperty.setNative] = makeNativeSetter<number>((lp, value) => (lp.column = value));
View.prototype[rowSpanProperty.setNative] = makeNativeSetter<number>((lp, value) => (lp.rowSpan = value));
View.prototype[columnSpanProperty.setNative] = makeNativeSetter<number>((lp, value) => (lp.columnSpan = value));
ItemSpecBase.prototype.toJSON = function () {
let result;
switch (this.gridUnitType) {
case GridUnitType.AUTO:
result = { type: 0 /* org.nativescript.widgets.GridUnitType.auto */, value: this.value };
break;
case GridUnitType.PIXEL:
result = { type: 1 /* org.nativescript.widgets.GridUnitType.pixel */, value: this.value * layout.getDisplayDensity() };
break;
case GridUnitType.STAR:
result = { type: 2 /* org.nativescript.widgets.GridUnitType.star */, value: this.value };
break;
default:
return null;
}
return result;
};
interface ItemSpec extends ItemSpecBase {
toJSON(): { value: number; type: number };
}
export class GridLayout extends GridLayoutBase {
nativeViewProtected: org.nativescript.widgets.GridLayout;
public createNativeView() {
return new org.nativescript.widgets.GridLayout(this._context);
}
public initNativeView(): void {
super.initNativeView();
// Update native GridLayout
const jsonRows = JSON.stringify(this.rowsInternal.map((itemSpec: ItemSpec) => itemSpec.toJSON()).filter((j) => !!j));
const jsonColumns = JSON.stringify(this.columnsInternal.map((itemSpec: ItemSpec) => itemSpec.toJSON()).filter((j) => !!j));
this.nativeViewProtected.addRowsAndColumnsFromJSON(jsonRows, jsonColumns);
}
public resetNativeView() {
// Update native GridLayout
this.nativeViewProtected.reset();
super.resetNativeView();
}
public _onRowAdded(itemSpec: ItemSpec) {
if (this.nativeViewProtected) {
this.nativeViewProtected.addRowsFromJSON(JSON.stringify([itemSpec.toJSON()]));
}
}
public addRows(itemSpecs: ItemSpec[]) {
const jsonArray = [];
const nativeView = this.nativeViewProtected;
const initialized = !!nativeView;
for (let index = 0; index < itemSpecs.length; index++) {
const itemSpec = itemSpecs[index];
this._addRow(itemSpec);
if (initialized) {
jsonArray.push(itemSpec.toJSON());
}
}
if (initialized) {
nativeView.addRowsFromJSON(JSON.stringify(jsonArray.filter((s) => !!s)));
}
}
public addColumns(itemSpecs: ItemSpec[]) {
const jsonArray = [];
const nativeView = this.nativeViewProtected;
const initialized = !!nativeView;
for (let index = 0; index < itemSpecs.length; index++) {
const itemSpec = itemSpecs[index];
this._addColumn(itemSpec);
if (initialized) {
jsonArray.push(itemSpec.toJSON());
}
}
if (initialized) {
nativeView.addColumnsFromJSON(JSON.stringify(jsonArray.filter((s) => !!s)));
}
}
public _onColumnAdded(itemSpec: ItemSpec) {
if (this.nativeViewProtected) {
this.nativeViewProtected.addColumnsFromJSON(JSON.stringify([itemSpec.toJSON()]));
}
}
public removeColumns() {
if (this._cols.length) {
if (this.nativeViewProtected) {
this.nativeViewProtected.clearColumns();
}
this._cols.length = 0;
}
}
public removeRows() {
if (this._rows.length) {
if (this.nativeViewProtected) {
this.nativeViewProtected.clearRows();
}
this._rows.length = 0;
}
}
public _onRowRemoved(itemSpec: ItemSpec, index: number) {
if (this.nativeViewProtected) {
this.nativeViewProtected.removeRowAt(index);
}
}
public _onColumnRemoved(itemSpec: ItemSpec, index: number) {
if (this.nativeViewProtected) {
this.nativeViewProtected.removeColumnAt(index);
}
}
protected invalidate(): void {
// No need to request layout for android because it will be done in the native call.
}
}