How and where can I change the animation of closing a NSPanel when I click the red close button?

StackOverflow https://stackoverflow.com/questions/21633727

  •  08-10-2022
  •  | 
  •  

Question

I present a NSPanel in a NSWindowController. When I press the little red button of the titlebar of the NSPanel I would like to change the way it disappears with a different animation. How do I do that? Is there something like -(void)closeButtonPressed that I can change. Because the -(void)close of the NSWindow is not working like I want it. When I present it I do it like this:

[self.imagePanelController.previewPanel setFrame:NSRectFromCGRect(CGRectMake(self.window.frame.origin.x + self.window.frame.size.width/4 , self.window.frame.origin.y + self.bounds.size.height/4, self.ciImage.extent.size.width, self.ciImage.extent.size.height)) display:YES animate:YES];

I would like to set a new frame again and animate that, when pressing the panel close button. Any ideas?

Was it helpful?

Solution

So I figured it out myself, and it works for me, I don't know if that is the best solution, but does exactly what I needed. So I just post this, if someone is looking for something similar.

I am getting the closeButton of the NSPanel and perform than a action on it to close the panel exactly in the way it animated to open the panel.

NSButton *closeButton = [self.imagePanelController.previewPanel standardWindowButton:NSWindowCloseButton];
    [closeButton setTarget:self];
    [closeButton setAction:@selector(closePanel)];

- (void)closePanel
{

    int calcX = self.window.frame.size.width - self.bounds.size.width;
    int x = self.window.frame.origin.x + calcX;
    int y = self.window.frame.origin.y;
    int width = self.bounds.size.width;
    int height = self.bounds.size.height;

    [self.imagePanelController.previewPanel setFrame:NSRectFromCGRect(CGRectMake(x, y, width, height)) display:YES animate:YES];
    [self.imagePanelController.previewPanel close];

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top