1
1
/* *
2
2
* Appcelerator Titanium Mobile
3
- * Copyright (c) 2009-2014 by Appcelerator, Inc. All Rights Reserved.
3
+ * Copyright (c) 2009-2021 by Appcelerator, Inc. All Rights Reserved.
4
4
* Licensed under the terms of the Apache Public License
5
5
* Please see the LICENSE included with this distribution for details.
6
6
*/
7
7
8
8
#import " TiWindowProxy.h"
9
+ #import " KrollPromise.h"
9
10
#import " TiApp.h"
10
11
#import " TiErrorController.h"
11
12
#import " TiUIWindow.h"
@@ -37,6 +38,9 @@ - (void)dealloc
37
38
RELEASE_TO_NIL (transitionProxy)
38
39
}
39
40
41
+ RELEASE_TO_NIL (openPromise);
42
+ RELEASE_TO_NIL (closePromise);
43
+
40
44
[super dealloc ];
41
45
}
42
46
@@ -113,6 +117,10 @@ - (void)windowDidOpen
113
117
if ([self _hasListeners: @" open" ]) {
114
118
[self fireEvent: @" open" withObject: nil withSource: self propagate: NO reportSuccess: NO errorCode: 0 message: nil ];
115
119
}
120
+ if (openPromise != nil ) {
121
+ [openPromise resolve: @[]];
122
+ RELEASE_TO_NIL (openPromise);
123
+ }
116
124
if (focussed) {
117
125
[self fireFocusEvent ];
118
126
}
@@ -140,6 +148,10 @@ - (void)windowDidClose
140
148
if ([self _hasListeners: @" close" ]) {
141
149
[self fireEvent: @" close" withObject: nil withSource: self propagate: NO reportSuccess: NO errorCode: 0 message: nil ];
142
150
}
151
+ if (closePromise != nil ) {
152
+ [closePromise resolve: @[]];
153
+ RELEASE_TO_NIL (closePromise);
154
+ }
143
155
[self forgetProxy: closeAnimation];
144
156
RELEASE_TO_NIL (closeAnimation);
145
157
if (tab == nil && !self.isManaged ) {
@@ -202,32 +214,39 @@ - (BOOL)isRootViewAttached
202
214
}
203
215
204
216
#pragma mark - TiWindowProtocol Base Methods
205
- - (void )open : (id )args
217
+ - (KrollPromise * )open : (id )args
206
218
{
207
- // If an error is up, Go away
219
+ JSContext *context = [self currentContext ];
220
+
221
+ // If an error is up, Go away
208
222
if ([[[[TiApp app ] controller ] topPresentedController ] isKindOfClass: [TiErrorNavigationController class ]]) {
209
223
DebugLog (@" [ERROR] ErrorController is up. ABORTING open" );
210
- return ;
224
+ return [KrollPromise rejectedWithErrorMessage: @" ErrorController is up. ABORTING open " inContext: context] ;
211
225
}
212
226
213
- // I am already open or will be soon. Go Away
227
+ // I am already open or will be soon. Go Away
214
228
if (opening || opened) {
215
- return ;
229
+ return [KrollPromise rejectedWithErrorMessage: @" Window is already opened or opening. " inContext: context] ;
216
230
}
217
231
218
- // Lets keep ourselves safe
232
+ // Lets keep ourselves safe
219
233
[self rememberSelf ];
220
234
235
+ // Don't recreate if we're calling ourselves again because root view is not loaded/attached!
236
+ if (openPromise == nil ) {
237
+ openPromise = [[KrollPromise alloc ] initInContext: context];
238
+ }
239
+
221
240
// Make sure our RootView Controller is attached
222
241
if (![self isRootViewLoaded ]) {
223
242
DebugLog (@" [WARN] ROOT VIEW NOT LOADED. WAITING" );
224
243
[self performSelector: @selector (open: ) withObject: args afterDelay: 0.1 ];
225
- return ;
244
+ return openPromise ;
226
245
}
227
246
if (![self isRootViewAttached ]) {
228
247
DebugLog (@" [WARN] ROOT VIEW NOT ATTACHED. WAITING" );
229
248
[self performSelector: @selector (open: ) withObject: args afterDelay: 0.1 ];
230
- return ;
249
+ return openPromise ;
231
250
}
232
251
233
252
opening = YES ;
@@ -262,6 +281,7 @@ - (void)open:(id)args
262
281
[self openOnUIThread: args];
263
282
},
264
283
NO );
284
+ return openPromise;
265
285
}
266
286
267
287
- (void )setStatusBarStyle : (id )style
@@ -278,23 +298,26 @@ - (void)setStatusBarStyle:(id)style
278
298
}
279
299
}
280
300
281
- - (void )close : (id )args
301
+ - (KrollPromise * )close : (id )args
282
302
{
303
+ JSContext *context = [self currentContext ];
304
+
283
305
if (!opened) {
284
306
// If I've been asked to open but haven't yet, short-circuit it and tell it not to open
285
307
if (opening) {
286
308
opening = NO ; // _handleOpen: should check this and abort opening
287
309
DebugLog (@" Window is not open yet. Attempting to stop it from opening..." );
288
- return ;
310
+ // Should we reject or resolve here?! This feels more like "success", so maybe resolve? or do we have to wait on the end result of the openPromise to know?
311
+ return [KrollPromise resolved: @[] inContext: context];
289
312
}
290
313
291
314
DebugLog (@" Window is not open. Ignoring this close call" );
292
- return ;
315
+ return [KrollPromise rejectedWithErrorMessage: @" Window is not open. Ignoring this close call " inContext: context] ;
293
316
}
294
317
295
318
if (closing) {
296
319
DebugLog (@" Window is already closing. Ignoring this close call." );
297
- return ;
320
+ return [KrollPromise rejectedWithErrorMessage: @" Window is already closing. Ignoring this close call. " inContext: context] ;
298
321
}
299
322
300
323
if (tab != nil ) {
@@ -303,22 +326,26 @@ - (void)close:(id)args
303
326
} else {
304
327
args = [NSArray arrayWithObject: self ];
305
328
}
306
- [tab closeWindow: args];
307
- return ;
329
+ return [tab closeWindow: args];
330
+ }
331
+
332
+ if (closePromise == nil ) {
333
+ closePromise = [[KrollPromise alloc ] initInContext: context];
308
334
}
309
335
310
336
closing = YES ;
311
337
312
- // TODO Argument Processing
338
+ // TODO: Argument Processing
313
339
closeAnimation = [[TiAnimation animationFromArg: args context: [self pageContext ] create: NO ] retain ];
314
340
[self rememberProxy: closeAnimation];
315
341
316
- // GO ahead and call close on UI thread
342
+ // GO ahead and call close on UI thread
317
343
TiThreadPerformOnMainThread (
318
344
^{
319
345
[self closeOnUIThread: args];
320
346
},
321
347
NO );
348
+ return closePromise;
322
349
}
323
350
324
351
- (NSNumber *)closed
@@ -595,6 +622,11 @@ - (void)openOnUIThread:(NSArray *)args
595
622
opened = NO ;
596
623
[self forgetProxy: openAnimation];
597
624
RELEASE_TO_NIL (openAnimation);
625
+ // reject the openPromise!
626
+ if (openPromise != nil ) {
627
+ [openPromise rejectWithErrorMessage: @" open aborted" ];
628
+ RELEASE_TO_NIL (openPromise);
629
+ }
598
630
}
599
631
}
600
632
@@ -619,6 +651,10 @@ - (void)closeOnUIThread:(NSArray *)args
619
651
DebugLog (@" [WARN] CLOSE ABORTED. _handleClose returned NO" );
620
652
closing = NO ;
621
653
RELEASE_TO_NIL (closeAnimation);
654
+ if (closePromise != nil ) {
655
+ [closePromise rejectWithErrorMessage: @" close aborted" ];
656
+ RELEASE_TO_NIL (closePromise);
657
+ }
622
658
}
623
659
}
624
660
0 commit comments