Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Darkroom

Guest
Original poster
Dec 15, 2006
2,445
0
Montréal, Canada
i've set up my own quit function with my app, that fades the window out before quitting. here is the code:

PHP:
- (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

Moderator emeritus
Jun 15, 2000
7,958
7
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

Guest
Original poster
Dec 15, 2006
2,445
0
Montréal, Canada
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:

PHP:
- (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

Moderator emeritus
Jun 15, 2000
7,958
7
But you need to setup the NSApp delegate. Usually you do it through the MainMenu.nib but you can also do it via
Code:
[NSApp setDelegate:self];
 

Darkroom

Guest
Original poster
Dec 15, 2006
2,445
0
Montréal, Canada
But you need to setup the NSApp delegate. Usually you do it through the MainMenu.nib but you can also do it via
Code:
[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

Moderator emeritus
Jun 15, 2000
7,958
7
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

Guest
Original poster
Dec 15, 2006
2,445
0
Montréal, Canada
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:

PHP:
- (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

Moderator emeritus
Jun 15, 2000
7,958
7
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.