Brad Robinson
Brad Robinson

Reputation: 46887

NSWindow restored frame location

On OS X is it possible to programmatically access the restored window location of a zoomed or maximized window.

ie: in most OS X apps you can double click the title bar to zoom it. If you double click it again it restores it to it's previous position.

I'd like to be able to get and set that saved position rectangle.

Upvotes: 3

Views: 586

Answers (1)

dacap
dacap

Reputation: 488

You can create a custom NSWindowDelegate to save the NSWindow frame just before it's maximized:

@interface MyWindowDelegate : NSObject {
@private
  NSRect m_maximizedFrame;
  NSRect m_restoredFrame;
}

- (NSRect)windowWillUseStandardFrame:(NSWindow*)window
                        defaultFrame:(NSRect)newFrame
{
  // Save the expected frame when the window is maximized
  m_maximizedFrame = newFrame;
  return newFrame;
}

- (BOOL)windowShouldZoom:(NSWindow*)window
                 toFrame:(NSRect)newFrame
{
  // The NSWindow is going to be maximized...
  if (NSEqualRects(newFrame, m_maximizedFrame)) {
    // Save the frame before it's maximized
    m_restoredFrame = [window frame];
  }
  return YES;
}
@end

The m_restoredFrame will be valid only if the window is not resized after that (i.e. [window isZoomed] must be true). I'm not sure if there is a better way.

Upvotes: 0

Related Questions