forked from NativeScript/NativeScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathframe.ios.ts
More file actions
292 lines (235 loc) · 9.61 KB
/
frame.ios.ts
File metadata and controls
292 lines (235 loc) · 9.61 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
import frameCommon = require("ui/frame/frame-common");
import definition = require("ui/frame");
import trace = require("trace");
import imageSource = require("image-source");
import pages = require("ui/page");
import enums = require("ui/enums");
declare var exports;
require("utils/module-merge").merge(frameCommon, exports);
var ENTRY = "_entry";
var navDepth = 0;
export class Frame extends frameCommon.Frame {
private _ios: iOSFrame;
private _paramToNavigate: any;
public shouldSkipNativePop: boolean = false;
constructor() {
super();
this._ios = new iOSFrame(this);
}
public onLoaded() {
super.onLoaded();
if (this._paramToNavigate) {
this.navigate(this._paramToNavigate);
this._paramToNavigate = undefined;
}
}
public navigate(param: any) {
if (this.isLoaded) {
super.navigate(param);
}
else {
this._paramToNavigate = param;
}
}
public _navigateCore(backstackEntry: definition.BackstackEntry) {
var viewController = backstackEntry.resolvedPage.ios;
if (!viewController) {
throw new Error("Required page does have an viewController created.");
}
var animated = false;
if (this.currentPage) {
animated = this._getIsAnimatedNavigation(backstackEntry.entry);
}
this.updateNavigationBar();
viewController[ENTRY] = backstackEntry;
navDepth++;
trace.write("Frame<" + this._domId + ">.pushViewControllerAnimated depth = " + navDepth, trace.categories.Navigation);
this._ios.controller.pushViewControllerAnimated(viewController, animated);
}
public _goBackCore(entry: definition.NavigationEntry) {
navDepth--;
trace.write("Frame<" + this._domId + ">.popViewControllerAnimated depth = " + navDepth, trace.categories.Navigation);
if (!this.shouldSkipNativePop) {
this._ios.controller.popViewControllerAnimated(this._getIsAnimatedNavigation(entry));
}
}
public updateNavigationBar(page?: pages.Page): void {
switch (this._ios.navBarVisibility) {
case enums.NavigationBarVisibility.always:
this._ios.showNavigationBar = true;
break;
case enums.NavigationBarVisibility.never:
this._ios.showNavigationBar = false;
break;
case enums.NavigationBarVisibility.auto:
var pageInstance: pages.Page = page || this.currentPage;
this._ios.showNavigationBar = this.backStack.length > 0 || (pageInstance && pageInstance.optionsMenu.getItems().length > 0);
break;
}
}
public get ios(): iOSFrame {
return this._ios;
}
get _nativeView(): any {
return this._ios.controller.view;
}
public static get defaultAnimatedNavigation(): boolean {
return frameCommon.Frame.defaultAnimatedNavigation;
}
public static set defaultAnimatedNavigation(value: boolean) {
frameCommon.Frame.defaultAnimatedNavigation = value;
}
public requestLayout(): void {
super.requestLayout();
// Invalidate our Window so that layout is triggered again.
var window = this._nativeView.window;
if (window) {
window.setNeedsLayout();
}
}
public layoutNativeView(left: number, top: number, right: number, bottom: number): void {
// We don't call super here because we set frame on our first subview.
// This is done because when rotated in iOS7 there is rotation applied on the first subview on the Window which is our frame.nativeView.view.
// If we set it it should be transformed so it is correct.
var frame = CGRectMake(left, top, right - left, bottom - top);
var nativeView: UIView;
// When in landscape in iOS 7 there is transformation on the first subview of the window so we set frame to its subview.
// in iOS 8 we set frame to subview again otherwise we get clipped.
if (!this.parent && this._nativeView.subviews.count > 0) {
nativeView = (<UIView>this._nativeView.subviews[0]);
}
else {
nativeView = this._nativeView;
}
if (!CGRectEqualToRect(nativeView.frame, frame)) {
trace.write(this + ", Native setFrame: " + NSStringFromCGRect(frame), trace.categories.Layout);
nativeView.frame = frame;
}
}
protected get navigationBarHeight(): number {
var navigationBar = this._ios.controller.navigationBar;
return (navigationBar && !this._ios.controller.navigationBarHidden) ? navigationBar.frame.size.height : 0;
}
public _invalidateOptionsMenu() {
this.populateMenuItems(this.currentPage);
}
populateMenuItems(page: pages.Page) {
var items = page.optionsMenu.getItems();
var navigationItem: UINavigationItem = (<UIViewController>page.ios).navigationItem;
var array: NSMutableArray = items.length > 0 ? NSMutableArray.new() : null;
for (var i = 0; i < items.length; i++) {
var item = items[i];
var tapHandler = TapBarItemHandlerImpl.new().initWithOwner(item);
// associate handler with menuItem or it will get collected by JSC.
(<any>item).handler = tapHandler;
var barButtonItem: UIBarButtonItem;
if (item.icon) {
var img = imageSource.fromResource(item.icon);
barButtonItem = UIBarButtonItem.alloc().initWithImageStyleTargetAction(img.ios, UIBarButtonItemStyle.UIBarButtonItemStylePlain, tapHandler, "tap");
}
else {
barButtonItem = UIBarButtonItem.alloc().initWithTitleStyleTargetAction(item.text, UIBarButtonItemStyle.UIBarButtonItemStylePlain, tapHandler, "tap");
}
array.addObject(barButtonItem);
}
if (array) {
navigationItem.setRightBarButtonItemsAnimated(array, true);
}
}
}
class UINavigationControllerImpl extends UINavigationController implements UINavigationControllerDelegate {
public static ObjCProtocols = [UINavigationControllerDelegate];
static new(): UINavigationControllerImpl {
return <UINavigationControllerImpl>super.new();
}
private _owner: Frame;
public initWithOwner(owner: Frame): UINavigationControllerImpl {
this._owner = owner;
return this;
}
public viewDidLoad(): void {
this.view.autoresizesSubviews = false;
this.view.autoresizingMask = UIViewAutoresizing.UIViewAutoresizingNone;
this._owner.onLoaded();
}
public viewDidLayoutSubviews(): void {
trace.write(this._owner + " viewDidLayoutSubviews, isLoaded = " + this._owner.isLoaded, trace.categories.ViewHierarchy);
this._owner._updateLayout();
}
public navigationControllerDidShowViewControllerAnimated(navigationController: UINavigationController, viewController: UIViewController, animated: boolean): void {
var frame: Frame = this._owner;
var backStack = frame.backStack;
var currentEntry = backStack.length > 0 ? backStack[backStack.length - 1] : null;
var newEntry: definition.BackstackEntry = viewController[ENTRY];
if (newEntry === currentEntry && currentEntry) {
try {
frame.shouldSkipNativePop = true;
frame.goBack();
}
finally {
frame.shouldSkipNativePop = false;
}
}
var page = frame.currentPage;
if (page) {
frame._removeView(page);
}
var newPage = newEntry.resolvedPage;
frame._currentEntry = newEntry;
frame._addView(newPage);
frame.populateMenuItems(newPage);
frame.updateNavigationBar();
// notify the page
newPage.onNavigatedTo(newEntry.entry.context);
frame._processNavigationQueue(newPage);
}
public supportedInterfaceOrientation(): number {
return UIInterfaceOrientationMask.UIInterfaceOrientationMaskAll;
}
}
/* tslint:disable */
class iOSFrame implements definition.iOSFrame {
/* tslint:enable */
private _controller: UINavigationControllerImpl;
private _showNavigationBar: boolean;
private _navBarVisibility: string;
constructor(owner: Frame) {
this._controller = UINavigationControllerImpl.new().initWithOwner(owner);
this._controller.delegate = this._controller;
this._controller.automaticallyAdjustsScrollViewInsets = false;
this.showNavigationBar = false;
this._navBarVisibility = enums.NavigationBarVisibility.auto;
}
public get controller() {
return this._controller;
}
public get showNavigationBar(): boolean {
return this._showNavigationBar;
}
public set showNavigationBar(value: boolean) {
this._showNavigationBar = value;
this._controller.navigationBarHidden = !value;
}
public get navBarVisibility(): string {
return this._navBarVisibility;
}
public set navBarVisibility(value: string) {
this._navBarVisibility = value;
}
}
class TapBarItemHandlerImpl extends NSObject {
static new(): TapBarItemHandlerImpl {
return <TapBarItemHandlerImpl>super.new();
}
private _owner: pages.MenuItem;
public initWithOwner(owner: pages.MenuItem): TapBarItemHandlerImpl {
this._owner = owner;
return this;
}
public tap(args) {
this._owner._raiseTap();
}
public static ObjCExposedMethods = {
"tap": { returns: interop.types.void, params: [interop.types.id] }
};
}