PDA

View Full Version : Terminate App From Dock With Fadeout?




Darkroom
Mar 8, 2008, 02:08 PM
i've set up my own quit function with my app, that fades the window out before quitting. here is the code:


- (IBAction)quitApplication:(id)sender {
SetSystemUIMode(kUIModeNormal, 0);
timer = [[NSTimer scheduledTimerWithTimeInterval:0.02 target:self selector:@selector(allWindowsFadeOUT:) userInfo:nil repeats:YES] retain];
}

- (void)allWindowsFadeOUT:(NSTimer *)theTimer
{
if (([mainWindow alphaValue] > 0.0) || ([mainWindow2 alphaValue] > 0.0)){
[mainWindow setAlphaValue:[mainWindow alphaValue] - 0.1];
[mainWindow2 setAlphaValue:[mainWindow2 alphaValue] - 0.1];
[aboutWindow setAlphaValue:[aboutWindow alphaValue] - 0.1];
[[NSColorPanel sharedColorPanel] setAlphaValue:[[NSColorPanel sharedColorPanel] alphaValue] -0.1];
}
else
{
[timer invalidate];
[timer release];
timer = nil;
[NSApp terminate:nil];
}
}


it works from the menu bar and Command+Q as i added the key equivalent to the menu item. however, when i quit the app from the dock menu, it does a normal quit (no fadeout)... how can i make the dock menu's quit function reflect my own quit function?



kainjow
Mar 8, 2008, 02:11 PM
You shouldn't use a custom IBAction for quitting from the menu bar.

The proper way is to setup an NSApplication delegate, and in that delegate implement the applicationWillTerminate: method.

Darkroom
Mar 8, 2008, 02:32 PM
You shouldn't use a custom IBAction for quitting from the menu bar.

The proper way is to setup an NSApplication delegate, and in that delegate implement the applicationWillTerminate: method.

so i replace my IBAction with this:


- (void)applicationWillTerminate:(NSNotification *)aNotification {
SetSystemUIMode(kUIModeNormal, 0);
timer = [[NSTimer scheduledTimerWithTimeInterval:0.02 target:self selector:@selector(allWindowsFadeOUT:) userInfo:nil repeats:YES] retain];
}

- (void)allWindowsFadeOUT:(NSTimer *)theTimer
{
if (([mainWindow alphaValue] > 0.0) || ([mainWindow2 alphaValue] > 0.0)){
[mainWindow setAlphaValue:[mainWindow alphaValue] - 0.1];
[mainWindow2 setAlphaValue:[mainWindow2 alphaValue] - 0.1];
[aboutWindow setAlphaValue:[aboutWindow alphaValue] - 0.1];
[[NSColorPanel sharedColorPanel] setAlphaValue:[[NSColorPanel sharedColorPanel] alphaValue] -0.1];
}
else
{
[timer invalidate];
[timer release];
timer = nil;
[NSApp terminate:nil];
}
}


?

kainjow
Mar 8, 2008, 02:39 PM
But you need to setup the NSApp delegate. Usually you do it through the MainMenu.nib but you can also do it via [NSApp setDelegate:self];

Darkroom
Mar 8, 2008, 02:46 PM
But you need to setup the NSApp delegate. Usually you do it through the MainMenu.nib but you can also do it via [NSApp setDelegate:self];

so i control+click the "File's Owner" in IB, and dragged a line to "Fade Controller", which has the new code (applicationWillTerminate), and selected Delegate... still quits normally. am i suppose to set up something else? and outlet?

sorry, i haven't worked with this project in a long time so i'm forgetting some things.

kainjow
Mar 8, 2008, 03:00 PM
You probably need to use applicationShouldTerminate: to delay the quit and then after the window has faded out, use [NSApp terminate:nil]; and that time allow the quit to go through.

Darkroom
Mar 8, 2008, 03:12 PM
You probably need to use applicationShouldTerminate: to delay the quit and then after the window has faded out, use [NSApp terminate:nil]; and that time allow the quit to go through.

the File's Owner delegate is set to FadeController:

FadeController.m:


- (void)applicationShouldTerminate:(NSNotification *)aNotification {
SetSystemUIMode(kUIModeNormal, 0);
timer = [[NSTimer scheduledTimerWithTimeInterval:0.02 target:self selector:@selector(allWindowsFadeOUT:) userInfo:nil repeats:YES] retain];
}

- (void)allWindowsFadeOUT:(NSTimer *)theTimer
{
if (([mainWindow alphaValue] > 0.0) || ([mainWindow2 alphaValue] > 0.0)){
[mainWindow setAlphaValue:[mainWindow alphaValue] - 0.1];
[mainWindow2 setAlphaValue:[mainWindow2 alphaValue] - 0.1];
[aboutWindow setAlphaValue:[aboutWindow alphaValue] - 0.1];
[[NSColorPanel sharedColorPanel] setAlphaValue:[[NSColorPanel sharedColorPanel] alphaValue] -0.1];
}
else
{
[timer invalidate];
[timer release];
timer = nil;
[NSApp terminate:nil];
}
}


still isn't working...

kainjow
Mar 8, 2008, 03:14 PM
What's not working? Please try to debug some of this yourself. You can use simple things like NSLog() to find out where things are being called or not, etc.