It’s like a lost art; many people nowadays don’t bother and punt to the default
full-screen size. It doesn’t help that some APIs don’t make it obvious what
their optimized size should be.
> - (NSRect)windowWillUseStandardFrame:(NSWindow *)window
> defaultFrame:(NSRect)newFrame {
> NSParameterAssert(self.window == window);
>
> // Based on the web content, get the maximum desired width and height.
> NSView<WebDocumentView> * const view =
> self.webView.mainFrame.frameView.documentView;
> NSSize const desiredContentSize = NSMakeSize(NSWidth(view.frame),
> NSHeight(view.frame) + ((CGFloat)!!self.isLoadingBarVisible *
> MyLoadingBarHeight) + ((CGFloat)!!self.isStatusBarVisible *
> MyStatusBarHeight));
>
> // Adjust that desired size to what's actually available.
> NSRect frame = [window contentRectForFrameRect:newFrame];
>
> frame.size.width = MIN(desiredContentSize.width, frame.size.width);
> frame.size.height = MIN(desiredContentSize.height, frame.size.height);
>
> // Adjust to the window's size bounds.
> frame = [window frameRectForContentRect:frame];
> frame.size.width = MAX(window.minSize.width, frame.size.width);
> frame.size.height = MAX(window.minSize.height, frame.size.height);
> NSAssert(frame.size.width <= newFrame.size.width, @"Standard web-browser
> window size too wide.");
> NSAssert(frame.size.height <= newFrame.size.height, @"Standard
> web-browser window size too tall.");
>
> // Try minimizing the amount the window moves from its current spot on
> the chosen screen.
> NSRect const oldOverlapFrame = NSIntersectionRect(window.frame,
> newFrame);
>
> frame = NSOffsetRect(frame, NSMidX(oldOverlapFrame) - NSMidX(frame),
> NSMidY(oldOverlapFrame) - NSMidY(frame));
> if (NSMaxX(frame) > NSMaxX(newFrame)) {
> frame = NSOffsetRect(frame, NSMaxX(newFrame) - NSMaxX(frame), 0.0);
> } else if (NSMinX(frame) < NSMinX(newFrame)) {
> frame = NSOffsetRect(frame, NSMinX(newFrame) - NSMinX(frame), 0.0);
> }
> if (NSMaxY(frame) > NSMaxY(newFrame)) {
> frame = NSOffsetRect(frame, 0.0, NSMaxY(newFrame) - NSMaxY(frame));
> } else if (NSMinY(frame) < NSMinY(newFrame)) {
> frame = NSOffsetRect(frame, 0.0, NSMinY(newFrame) - NSMinY(frame));
> }
>
> return frame;
> }
The WebView instance takes the window’s width, but optionally shares the height
with the loading and/or status bars. I’ve read that the upper-left corner is
usually left the same when the window needs to move around, but I forgot where
I saw that and/or an example, so I chose to compare frame centers instead
because it’s easier (especially trying to remember if the coordinate system is
flipped or not).
—
Daryle Walker
Mac, Internet, and Video Game Junkie
darylew AT mac DOT com
_______________________________________________
Cocoa-dev mailing list ([email protected])
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com
This email sent to [email protected]