Terminate App From Dock With Fadeout?

Discussion in 'Mac Programming' started by Darkroom, Mar 8, 2008.

  1. Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #1
    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(kUIModeNormal0);
        
    timer = [[NSTimer scheduledTimerWithTimeInterval:0.02 target:self selector:@selector(allWindowsFadeOUT:) userInfo:nil repeats:YESretain];
    }

    - (
    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 sharedColorPanelsetAlphaValue:[[NSColorPanel sharedColorPanelalphaValue] -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?
     
  2. Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #2
    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.
     
  3. thread starter Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #3
    so i replace my IBAction with this:

    PHP:
    - (void)applicationWillTerminate:(NSNotification *)aNotification {
        
    SetSystemUIMode(kUIModeNormal0);
        
    timer = [[NSTimer scheduledTimerWithTimeInterval:0.02 target:self selector:@selector(allWindowsFadeOUT:) userInfo:nil repeats:YESretain];
    }

    - (
    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 sharedColorPanelsetAlphaValue:[[NSColorPanel sharedColorPanelalphaValue] -0.1]; 
        } 
        else 
        { 
        [
    timer invalidate]; 
        [
    timer release]; 
        
    timer nil
        [
    NSApp terminate:nil]; 
        } 
    }
    ?
     
  4. Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #4
    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];
     
  5. thread starter Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #5
    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.
     
  6. Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #6
    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.
     
  7. thread starter Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #7
    the File's Owner delegate is set to FadeController:

    FadeController.m:

    PHP:
    - (void)applicationShouldTerminate:(NSNotification *)aNotification {
        
    SetSystemUIMode(kUIModeNormal0);
        
    timer = [[NSTimer scheduledTimerWithTimeInterval:0.02 target:self selector:@selector(allWindowsFadeOUT:) userInfo:nil repeats:YESretain];
    }

    - (
    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 sharedColorPanelsetAlphaValue:[[NSColorPanel sharedColorPanelalphaValue] -0.1];
        }
        else
        {
        [
    timer invalidate];
        [
    timer release];
        
    timer nil;
        [
    NSApp terminate:nil];
        }
    }
    still isn't working...
     
  8. Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #8
    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.
     

Share This Page