3
votes

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.

1
What have you tried? There are a bunch of ways you could do this (NSUserDefaults, Applescript, grab it from com.apple.finder.plist, etc.). Also, how are you planning to trigger the saving of the bounds, and which position are you wanting, the zoomed in or zoomed out?l'L'l
@l'L'l We'll I've looked at the docs for NSWindow and NSWindowController. (NSUserDefaults, Applescript and plist stuff doesn't make sense in this context). I have an NSWindow that my app created - I want the non-zoomed, non-maximized position (that OS X obviously has somewhere) but cant find an API for it.Brad Robinson
The standard and likely most efficient way would be to use setFrameAutosaveName: developer.apple.com/library/mac/documentation/Cocoa/Conceptual/…l'L'l
@l'L'l Except that I need more finely grained control than that. (eg: I'm trying to manage multiple user switchable window layouts). Also, that approach doesn't seem to save the restored position anyway.Brad Robinson
@l'L'l Sorry, perhaps my background in Windows programming causing me to cross terms. By "restored position" I mean the non-maximized/non-zoomed position. The position that OS X resets the window to when you unzoom/unmaximize it. When an NSWindow is maximized/zoomed, somewhere OS X keeps a copy of the old frame position - how do I get that?Brad Robinson

1 Answers

0
votes

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.