New: added delegate methods applicationShouldTerminate and applicationShouldTerminateMessage

This PR adds the delegate methods applicationShouldTerminate and applicationShouldTerminateMessage in CPApplicationDelegateProtocol.

The delegate applicationShouldTerminate does not exactly work as in Cocoa. In Cocoa, this method is called in CPApp -terminate, but in cappuccino it is called in the method onbeforeunload of the window. In JS, this is the only time where we can prevent to reload the HTML page. If the developer cancel to reload the page, the browser will ask the user if he wants to reload or not the page thought (natural behavior of js).

The method applicationShouldTerminateMessage allows you to define what will be the text displayed in the confirmation alert.

Test app in Tests/Manual/CPPlatformWindow/
This commit is contained in:
Alexandre Wilhelm committed 2015-07-02 16:35:35 -07:00
1 parent 1ad8d34ecb
commit 058a73f8a0
4 files changed
+58 -6

No files matched your search

+32 -6
View File
@@ -47,6 +47,8 @@ var CPMainCibFile = @"CPMainCibFile",
@protocol CPApplicationDelegate <CPObject>
@optional
- (CPApplicationTerminateReply)applicationShouldTerminate:(CPApplication)sender;
- (CPString)applicationShouldTerminateMessage:(CPApplication)sender;
- (void)applicationDidBecomeActive:(CPNotification)aNotification;
- (void)applicationDidChangeScreenParameters:(CPNotification)aNotification;
- (void)applicationDidFinishLaunching:(CPNotification)aNotification;
@@ -58,6 +60,9 @@ var CPMainCibFile = @"CPMainCibFile",
@end
var CPApplicationDelegate_applicationShouldTerminate_ = 1 << 0,
CPApplicationDelegate_applicationShouldTerminateMessage_ = 1 << 1;
/*!
@ingroup appkit
@class CPApplication
@@ -103,6 +108,8 @@ var CPMainCibFile = @"CPMainCibFile",
//
id <CPApplicationDelegate> _delegate;
CPInteger _implementedDelegateMethods;
BOOL _finishedLaunching;
BOOL _isActive;
@@ -165,6 +172,8 @@ var CPMainCibFile = @"CPMainCibFile",
if (_delegate == aDelegate)
return;
_implementedDelegateMethods = 0;
var defaultCenter = [CPNotificationCenter defaultCenter],
delegateNotifications =
[
@@ -205,6 +214,12 @@ var CPMainCibFile = @"CPMainCibFile",
if ([_delegate respondsToSelector:selector])
[defaultCenter addObserver:_delegate selector:selector name:notificationName object:self];
}
if ([_delegate respondsToSelector:@selector(applicationShouldTerminate:)])
_implementedDelegateMethods |= CPApplicationDelegate_applicationShouldTerminate_
if ([_delegate respondsToSelector:@selector(applicationShouldTerminateMessage:)])
_implementedDelegateMethods |= CPApplicationDelegate_applicationShouldTerminateMessage_
}
/*!
@@ -426,12 +441,7 @@ var CPMainCibFile = @"CPMainCibFile",
{
// callback method for terminate:
if (didCloseAll)
{
if ([_delegate respondsToSelector:@selector(applicationShouldTerminate:)])
[self replyToApplicationShouldTerminate:[_delegate applicationShouldTerminate:self]];
else
[self replyToApplicationShouldTerminate:YES];
}
[self replyToApplicationShouldTerminate:[self _sendDelegateApplicationShouldTerminate]];
}
- (void)replyToApplicationShouldTerminate:(BOOL)terminate
@@ -1210,6 +1220,22 @@ var CPMainCibFile = @"CPMainCibFile",
userInfo:nil];
}
- (BOOL)_sendDelegateApplicationShouldTerminate
{
if (!(_implementedDelegateMethods & CPApplicationDelegate_applicationShouldTerminate_))
return YES;
return [_delegate applicationShouldTerminate:self];
}
- (CPString)_sendDelegateApplicationShouldTerminateMessage
{
if (!(_implementedDelegateMethods & CPApplicationDelegate_applicationShouldTerminateMessage_))
return @"You have attempted to leave this page. Are you sure you want to exit this page?";
return [_delegate applicationShouldTerminateMessage:self];
}
- (void)_didResignActive
{
if (self._activeMenu)
+1
View File
@@ -31,6 +31,7 @@ CPApplicationWillResignActiveNotification = @"CPApplicationWillResignA
CPApplicationDidResignActiveNotification = @"CPApplicationDidResignActiveNotification";
CPApplicationDidChangeScreenParametersNotification = @"CPApplicationDidChangeScreenParametersNotification";
@typedef CPApplicationTerminateReply
CPTerminateNow = YES;
CPTerminateCancel = NO;
CPTerminateLater = -1; // not currently supported
+8
View File
@@ -49,6 +49,14 @@ var screenNeedsInitialization = NO,
if ([CPPlatform isBrowser])
{
// This differ from cocoa, where shouldTerminate is called in the method terminate of CPApp
// Cappuccino acts like this because we can not close a window openend by the user with a script (so not possible in terminate), and we can only prevent the action in the method onbeforeunload in js.
window.onbeforeunload = function()
{
if ([CPApp _sendDelegateApplicationShouldTerminate] != CPTerminateNow)
return [CPApp _sendDelegateApplicationShouldTerminateMessage];
};
window.onunload = function()
{
[self closeAllPlatformWindows];
@@ -27,6 +27,23 @@
[theWindow setDelegate:self];
}
- (void)applicationWillTerminate:(CPApplication)sender
{
CPLog.debug(@"applicationWillTerminate");
}
- (CPApplicationTerminateReply)applicationShouldTerminate:(CPApplication)sender
{
CPLog.debug(@"applicationShouldTerminate");
return CPTerminateCancel;
}
- (CPString)applicationShouldTerminateMessage:(CPApplication)sender
{
CPLog.debug(@"applicationShouldTerminate");
return @"Oula attention - achtung - carefull - you are about to reload";
}
- (void)awakeFromCib
{
// This is called when the cib is done loading.