Search code examples
macoszoomingwindow-resizemaximize-window

How to set what window size is preferred when OS X "zooms" it?


In OS X, window "zoom" (the green window button/titlebar doubleclick since Yosemite) is supposed to expand window to its preferred size (larger than content, but not maximum).

Content in my window varies, so the ideal size is not known at compile time, but I can compute it at run time.

I would like the zoom to adjust window size to its preferred size, but I don't want to constrain window size otherwise (i.e. user should still be free to resize it to much larger or smaller than the ideal).

What is the right way to tell OS X what window (or window content's) size it should use when zooming?


Solution

  • When the user zooms or unzooms the window, the window will send its delegate a windowWillUseStandardFrame:defaultFrame: message.

    The first argument is the window being zoomed; the second is the default standard frame, which is the size of the screen. You return the preferred (“standard”) frame.

    If the window's frame is already equal to the standard frame you return, then the window will unzoom to the user's preferred size (as they expressed by manually resizing it). Otherwise, the window will zoom to the standard frame.

    A corollary to this is that if the standard frame changes between zooms, the window will zoom the first time and will zoom again (to the new standard frame) the second time. This is the behavior you might expect if the size of the content changed between zooms.