Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(35)

Side by Side Diff: chrome/browser/ui/cocoa/browser_window_controller.mm

Issue 10545050: mac: Use NSWidth() and friends in a few more places. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: . Created 8 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #import "chrome/browser/ui/cocoa/browser_window_controller.h" 5 #import "chrome/browser/ui/cocoa/browser_window_controller.h"
6 6
7 #include <cmath> 7 #include <cmath>
8 #include <numeric> 8 #include <numeric>
9 9
10 #include "base/command_line.h" 10 #include "base/command_line.h"
(...skipping 677 matching lines...) Expand 10 before | Expand all | Expand 10 after
688 // "switchers" happy. 688 // "switchers" happy.
689 if ([[NSApp currentEvent] modifierFlags] & NSShiftKeyMask) { 689 if ([[NSApp currentEvent] modifierFlags] & NSShiftKeyMask) {
690 return frame; 690 return frame;
691 } 691 }
692 692
693 // To prevent strange results on portrait displays, the basic minimum zoomed 693 // To prevent strange results on portrait displays, the basic minimum zoomed
694 // width is the larger of: 60% of available width, 60% of available height 694 // width is the larger of: 60% of available width, 60% of available height
695 // (bounded by available width). 695 // (bounded by available width).
696 const CGFloat kProportion = 0.6; 696 const CGFloat kProportion = 0.6;
697 CGFloat zoomedWidth = 697 CGFloat zoomedWidth =
698 std::max(kProportion * frame.size.width, 698 std::max(kProportion * NSWidth(frame),
699 std::min(kProportion * frame.size.height, frame.size.width)); 699 std::min(kProportion * NSHeight(frame), NSWidth(frame)));
700 700
701 WebContents* contents = browser_->GetSelectedWebContents(); 701 WebContents* contents = browser_->GetSelectedWebContents();
702 if (contents) { 702 if (contents) {
703 // If the intrinsic width is bigger, then make it the zoomed width. 703 // If the intrinsic width is bigger, then make it the zoomed width.
704 const int kScrollbarWidth = 16; // TODO(viettrungluu): ugh. 704 const int kScrollbarWidth = 16; // TODO(viettrungluu): ugh.
705 CGFloat intrinsicWidth = static_cast<CGFloat>( 705 CGFloat intrinsicWidth = static_cast<CGFloat>(
706 contents->GetPreferredSize().width() + kScrollbarWidth); 706 contents->GetPreferredSize().width() + kScrollbarWidth);
707 zoomedWidth = std::max(zoomedWidth, 707 zoomedWidth = std::max(zoomedWidth,
708 std::min(intrinsicWidth, frame.size.width)); 708 std::min(intrinsicWidth, NSWidth(frame)));
709 } 709 }
710 710
711 // Never shrink from the current size on zoom (see above). 711 // Never shrink from the current size on zoom (see above).
712 NSRect currentFrame = [[self window] frame]; 712 NSRect currentFrame = [[self window] frame];
713 zoomedWidth = std::max(zoomedWidth, currentFrame.size.width); 713 zoomedWidth = std::max(zoomedWidth, NSWidth(currentFrame));
714 714
715 // |frame| determines our maximum extents. We need to set the origin of the 715 // |frame| determines our maximum extents. We need to set the origin of the
716 // frame -- and only move it left if necessary. 716 // frame -- and only move it left if necessary.
717 if (currentFrame.origin.x + zoomedWidth > frame.origin.x + frame.size.width) 717 if (currentFrame.origin.x + zoomedWidth > NSMaxX(frame))
718 frame.origin.x = frame.origin.x + frame.size.width - zoomedWidth; 718 frame.origin.x = NSMaxX(frame) - zoomedWidth;
719 else 719 else
720 frame.origin.x = currentFrame.origin.x; 720 frame.origin.x = currentFrame.origin.x;
721 721
722 // Set the width. Don't touch y or height. 722 // Set the width. Don't touch y or height.
723 frame.size.width = zoomedWidth; 723 frame.size.width = zoomedWidth;
724 724
725 return frame; 725 return frame;
726 } 726 }
727 727
728 - (void)activate { 728 - (void)activate {
729 [BrowserWindowUtils activateWindowForController:self]; 729 [BrowserWindowUtils activateWindowForController:self];
730 } 730 }
731 731
732 // Determine whether we should let a window zoom/unzoom to the given |newFrame|. 732 // Determine whether we should let a window zoom/unzoom to the given |newFrame|.
733 // We avoid letting unzoom move windows between screens, because it's really 733 // We avoid letting unzoom move windows between screens, because it's really
734 // strange and unintuitive. 734 // strange and unintuitive.
735 - (BOOL)windowShouldZoom:(NSWindow*)window toFrame:(NSRect)newFrame { 735 - (BOOL)windowShouldZoom:(NSWindow*)window toFrame:(NSRect)newFrame {
736 // Figure out which screen |newFrame| is on. 736 // Figure out which screen |newFrame| is on.
737 NSScreen* newScreen = nil; 737 NSScreen* newScreen = nil;
738 CGFloat newScreenOverlapArea = 0.0; 738 CGFloat newScreenOverlapArea = 0.0;
739 for (NSScreen* screen in [NSScreen screens]) { 739 for (NSScreen* screen in [NSScreen screens]) {
740 NSRect overlap = NSIntersectionRect(newFrame, [screen frame]); 740 NSRect overlap = NSIntersectionRect(newFrame, [screen frame]);
741 CGFloat overlapArea = overlap.size.width * overlap.size.height; 741 CGFloat overlapArea = NSWidth(overlap) * NSHeight(overlap);
742 if (overlapArea > newScreenOverlapArea) { 742 if (overlapArea > newScreenOverlapArea) {
743 newScreen = screen; 743 newScreen = screen;
744 newScreenOverlapArea = overlapArea; 744 newScreenOverlapArea = overlapArea;
745 } 745 }
746 } 746 }
747 // If we're somehow not on any screen, allow the zoom. 747 // If we're somehow not on any screen, allow the zoom.
748 if (!newScreen) 748 if (!newScreen)
749 return YES; 749 return YES;
750 750
751 // If the new screen is the current screen, we can return a definitive YES. 751 // If the new screen is the current screen, we can return a definitive YES.
752 // Note: This check is not strictly necessary, but just short-circuits in the 752 // Note: This check is not strictly necessary, but just short-circuits in the
753 // "no-brainer" case. To test the complicated logic below, comment this out! 753 // "no-brainer" case. To test the complicated logic below, comment this out!
754 NSScreen* curScreen = [window screen]; 754 NSScreen* curScreen = [window screen];
755 if (newScreen == curScreen) 755 if (newScreen == curScreen)
756 return YES; 756 return YES;
757 757
758 // Worry a little: What happens when a window is on two (or more) screens? 758 // Worry a little: What happens when a window is on two (or more) screens?
759 // E.g., what happens in a 50-50 scenario? Cocoa may reasonably elect to zoom 759 // E.g., what happens in a 50-50 scenario? Cocoa may reasonably elect to zoom
760 // to the other screen rather than staying on the officially current one. So 760 // to the other screen rather than staying on the officially current one. So
761 // we compare overlaps with the current window frame, and see if Cocoa's 761 // we compare overlaps with the current window frame, and see if Cocoa's
762 // choice was reasonable (allowing a small rounding error). This should 762 // choice was reasonable (allowing a small rounding error). This should
763 // hopefully avoid us ever erroneously denying a zoom when a window is on 763 // hopefully avoid us ever erroneously denying a zoom when a window is on
764 // multiple screens. 764 // multiple screens.
765 NSRect curFrame = [window frame]; 765 NSRect curFrame = [window frame];
766 NSRect newScrIntersectCurFr = NSIntersectionRect([newScreen frame], curFrame); 766 NSRect newScrIntersectCurFr = NSIntersectionRect([newScreen frame], curFrame);
767 NSRect curScrIntersectCurFr = NSIntersectionRect([curScreen frame], curFrame); 767 NSRect curScrIntersectCurFr = NSIntersectionRect([curScreen frame], curFrame);
768 if (newScrIntersectCurFr.size.width*newScrIntersectCurFr.size.height >= 768 if (NSWidth(newScrIntersectCurFr) * NSHeight(newScrIntersectCurFr) >=
769 (curScrIntersectCurFr.size.width*curScrIntersectCurFr.size.height - 1.0)) 769 (NSWidth(curScrIntersectCurFr) * NSHeight(curScrIntersectCurFr) - 1.0))
Robert Sesek 2012/06/07 16:52:38 nit: This if should have braces because the condit
Nico 2012/06/07 17:40:23 Done.
770 return YES; 770 return YES;
771 771
772 // If it wasn't reasonable, return NO. 772 // If it wasn't reasonable, return NO.
773 return NO; 773 return NO;
774 } 774 }
775 775
776 // Adjusts the window height by the given amount. 776 // Adjusts the window height by the given amount.
777 - (BOOL)adjustWindowHeightBy:(CGFloat)deltaH { 777 - (BOOL)adjustWindowHeightBy:(CGFloat)deltaH {
778 // By not adjusting the window height when initializing, we can ensure that 778 // By not adjusting the window height when initializing, we can ensure that
779 // the window opens with the same size that was saved on close. 779 // the window opens with the same size that was saved on close.
780 if (initializing_ || [self isFullscreen] || deltaH == 0) 780 if (initializing_ || [self isFullscreen] || deltaH == 0)
781 return NO; 781 return NO;
782 782
783 NSWindow* window = [self window]; 783 NSWindow* window = [self window];
784 NSRect windowFrame = [window frame]; 784 NSRect windowFrame = [window frame];
785 NSRect workarea = [[window screen] visibleFrame]; 785 NSRect workarea = [[window screen] visibleFrame];
786 786
787 // If the window is not already fully in the workarea, do not adjust its frame 787 // If the window is not already fully in the workarea, do not adjust its frame
788 // at all. 788 // at all.
789 if (!NSContainsRect(workarea, windowFrame)) 789 if (!NSContainsRect(workarea, windowFrame))
790 return NO; 790 return NO;
791 791
792 // Record the position of the top/bottom of the window, so we can easily check 792 // Record the position of the top/bottom of the window, so we can easily check
793 // whether we grew the window upwards/downwards. 793 // whether we grew the window upwards/downwards.
794 CGFloat oldWindowMaxY = NSMaxY(windowFrame); 794 CGFloat oldWindowMaxY = NSMaxY(windowFrame);
795 CGFloat oldWindowMinY = NSMinY(windowFrame); 795 CGFloat oldWindowMinY = NSMinY(windowFrame);
796 796
797 // We are "zoomed" if we occupy the full vertical space. 797 // We are "zoomed" if we occupy the full vertical space.
798 bool isZoomed = (windowFrame.origin.y == workarea.origin.y && 798 bool isZoomed = (windowFrame.origin.y == workarea.origin.y &&
799 windowFrame.size.height == workarea.size.height); 799 NSHeight(windowFrame) == NSHeight(workarea));
800 800
801 // If we're shrinking the window.... 801 // If we're shrinking the window....
802 if (deltaH < 0) { 802 if (deltaH < 0) {
803 bool didChange = false; 803 bool didChange = false;
804 804
805 // Don't reset if not currently zoomed since shrinking can take several 805 // Don't reset if not currently zoomed since shrinking can take several
806 // steps! 806 // steps!
807 if (isZoomed) 807 if (isZoomed)
808 isShrinkingFromZoomed_ = YES; 808 isShrinkingFromZoomed_ = YES;
809 809
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
852 // Resize the window as requested, keeping the top left corner fixed. 852 // Resize the window as requested, keeping the top left corner fixed.
853 windowFrame.origin.y -= deltaH; 853 windowFrame.origin.y -= deltaH;
854 windowFrame.size.height += deltaH; 854 windowFrame.size.height += deltaH;
855 855
856 // If the bottom left corner is now outside the visible frame, move the 856 // If the bottom left corner is now outside the visible frame, move the
857 // window up to make it fit, but make sure not to move the top left corner 857 // window up to make it fit, but make sure not to move the top left corner
858 // out of the visible frame. 858 // out of the visible frame.
859 if (windowFrame.origin.y < workarea.origin.y) { 859 if (windowFrame.origin.y < workarea.origin.y) {
860 windowFrame.origin.y = workarea.origin.y; 860 windowFrame.origin.y = workarea.origin.y;
861 windowFrame.size.height = 861 windowFrame.size.height =
862 std::min(windowFrame.size.height, workarea.size.height); 862 std::min(NSHeight(windowFrame), NSHeight(workarea));
863 } 863 }
864 864
865 // Record (if applicable) how much we grew the window in either direction. 865 // Record (if applicable) how much we grew the window in either direction.
866 // (N.B.: These only record growth, not shrinkage.) 866 // (N.B.: These only record growth, not shrinkage.)
867 if (NSMaxY(windowFrame) > oldWindowMaxY) 867 if (NSMaxY(windowFrame) > oldWindowMaxY)
868 windowTopGrowth_ += NSMaxY(windowFrame) - oldWindowMaxY; 868 windowTopGrowth_ += NSMaxY(windowFrame) - oldWindowMaxY;
869 if (NSMinY(windowFrame) < oldWindowMinY) 869 if (NSMinY(windowFrame) < oldWindowMinY)
870 windowBottomGrowth_ += oldWindowMinY - NSMinY(windowFrame); 870 windowBottomGrowth_ += oldWindowMinY - NSMinY(windowFrame);
871 } 871 }
872 872
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
910 // 910 //
911 // We do not adjust the window height for bookmark bar changes on the NTP. 911 // We do not adjust the window height for bookmark bar changes on the NTP.
912 BOOL shouldAdjustBookmarkHeight = 912 BOOL shouldAdjustBookmarkHeight =
913 [bookmarkBarController_ isAnimatingBetweenState:bookmarks::kHiddenState 913 [bookmarkBarController_ isAnimatingBetweenState:bookmarks::kHiddenState
914 andState:bookmarks::kShowingState]; 914 andState:bookmarks::kShowingState];
915 915
916 BOOL resizeRectDirty = NO; 916 BOOL resizeRectDirty = NO;
917 if ((shouldAdjustBookmarkHeight && view == [bookmarkBarController_ view]) || 917 if ((shouldAdjustBookmarkHeight && view == [bookmarkBarController_ view]) ||
918 view == [downloadShelfController_ view]) { 918 view == [downloadShelfController_ view]) {
919 [[self window] disableScreenUpdatesUntilFlush]; 919 [[self window] disableScreenUpdatesUntilFlush];
920 CGFloat deltaH = height - frame.size.height; 920 CGFloat deltaH = height - NSHeight(frame);
921 if ([self adjustWindowHeightBy:deltaH] && 921 if ([self adjustWindowHeightBy:deltaH] &&
922 view == [downloadShelfController_ view]) { 922 view == [downloadShelfController_ view]) {
923 // If the window height didn't change, the download shelf will change the 923 // If the window height didn't change, the download shelf will change the
924 // size of the contents. If the contents size doesn't change, send it 924 // size of the contents. If the contents size doesn't change, send it
925 // an explicit grow box invalidation (else, the resize message does that.) 925 // an explicit grow box invalidation (else, the resize message does that.)
926 resizeRectDirty = YES; 926 resizeRectDirty = YES;
927 } 927 }
928 } 928 }
929 929
930 frame.size.height = height; 930 frame.size.height = height;
(...skipping 247 matching lines...) Expand 10 before | Expand all | Expand 10 after
1178 - (NSRect)selectedTabGrowBoxRect { 1178 - (NSRect)selectedTabGrowBoxRect {
1179 NSWindow* window = [self window]; 1179 NSWindow* window = [self window];
1180 if (![window respondsToSelector:@selector(_growBoxRect)]) 1180 if (![window respondsToSelector:@selector(_growBoxRect)])
1181 return NSZeroRect; 1181 return NSZeroRect;
1182 1182
1183 // Before we return a rect, we need to convert it from window coordinates 1183 // Before we return a rect, we need to convert it from window coordinates
1184 // to tab content area coordinates and flip the coordinate system. 1184 // to tab content area coordinates and flip the coordinate system.
1185 NSRect growBoxRect = 1185 NSRect growBoxRect =
1186 [[self tabContentArea] convertRect:[window _growBoxRect] fromView:nil]; 1186 [[self tabContentArea] convertRect:[window _growBoxRect] fromView:nil];
1187 growBoxRect.origin.y = 1187 growBoxRect.origin.y =
1188 [[self tabContentArea] frame].size.height - growBoxRect.size.height - 1188 NSHeight([[self tabContentArea] frame]) - NSMaxY(growBoxRect);
1189 growBoxRect.origin.y;
1190 return growBoxRect; 1189 return growBoxRect;
1191 } 1190 }
1192 1191
1193 // Accept tabs from a BrowserWindowController with the same Profile. 1192 // Accept tabs from a BrowserWindowController with the same Profile.
1194 - (BOOL)canReceiveFrom:(TabWindowController*)source { 1193 - (BOOL)canReceiveFrom:(TabWindowController*)source {
1195 if (![source isKindOfClass:[BrowserWindowController class]]) { 1194 if (![source isKindOfClass:[BrowserWindowController class]]) {
1196 return NO; 1195 return NO;
1197 } 1196 }
1198 1197
1199 BrowserWindowController* realSource = 1198 BrowserWindowController* realSource =
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after
1308 // Fetch the tab contents for the tab being dragged. 1307 // Fetch the tab contents for the tab being dragged.
1309 int index = [tabStripController_ modelIndexForTabView:tabView]; 1308 int index = [tabStripController_ modelIndexForTabView:tabView];
1310 TabContentsWrapper* contents = browser_->GetTabContentsWrapperAt(index); 1309 TabContentsWrapper* contents = browser_->GetTabContentsWrapperAt(index);
1311 1310
1312 // Set the window size. Need to do this before we detach the tab so it's 1311 // Set the window size. Need to do this before we detach the tab so it's
1313 // still in the window. We have to flip the coordinates as that's what 1312 // still in the window. We have to flip the coordinates as that's what
1314 // is expected by the Browser code. 1313 // is expected by the Browser code.
1315 NSWindow* sourceWindow = [tabView window]; 1314 NSWindow* sourceWindow = [tabView window];
1316 NSRect windowRect = [sourceWindow frame]; 1315 NSRect windowRect = [sourceWindow frame];
1317 NSScreen* screen = [sourceWindow screen]; 1316 NSScreen* screen = [sourceWindow screen];
1318 windowRect.origin.y = 1317 windowRect.origin.y = NSHeight([screen frame]) - NSMaxY(windowRect);
1319 [screen frame].size.height - windowRect.size.height -
1320 windowRect.origin.y;
1321 gfx::Rect browserRect(windowRect.origin.x, windowRect.origin.y, 1318 gfx::Rect browserRect(windowRect.origin.x, windowRect.origin.y,
1322 windowRect.size.width, windowRect.size.height); 1319 NSWidth(windowRect), NSHeight(windowRect));
1323 1320
1324 NSRect sourceTabRect = [tabView frame]; 1321 NSRect sourceTabRect = [tabView frame];
1325 NSView* tabStrip = [self tabStripView]; 1322 NSView* tabStrip = [self tabStripView];
1326 1323
1327 // Pushes tabView's frame back inside the tabstrip. 1324 // Pushes tabView's frame back inside the tabstrip.
1328 NSSize tabOverflow = 1325 NSSize tabOverflow =
1329 [self overflowFrom:[tabStrip convertRectToBase:sourceTabRect] 1326 [self overflowFrom:[tabStrip convertRectToBase:sourceTabRect]
1330 to:[tabStrip frame]]; 1327 to:[tabStrip frame]];
1331 NSRect tabRect = NSOffsetRect(sourceTabRect, 1328 NSRect tabRect = NSOffsetRect(sourceTabRect,
1332 -tabOverflow.width, -tabOverflow.height); 1329 -tabOverflow.width, -tabOverflow.height);
(...skipping 587 matching lines...) Expand 10 before | Expand all | Expand 10 after
1920 return NSZeroRect; 1917 return NSZeroRect;
1921 1918
1922 NSRect frame = [view convertRect:[view bounds] toView:nil]; 1919 NSRect frame = [view convertRect:[view bounds] toView:nil];
1923 NSPoint originInScreenCoords = 1920 NSPoint originInScreenCoords =
1924 [[view window] convertBaseToScreen:frame.origin]; 1921 [[view window] convertBaseToScreen:frame.origin];
1925 frame.origin = originInScreenCoords; 1922 frame.origin = originInScreenCoords;
1926 1923
1927 // Account for the bookmark bar height if it is currently in the detached 1924 // Account for the bookmark bar height if it is currently in the detached
1928 // state on the new tab page. 1925 // state on the new tab page.
1929 if ([bookmarkBarController_ isInState:(bookmarks::kDetachedState)]) 1926 if ([bookmarkBarController_ isInState:(bookmarks::kDetachedState)])
1930 frame.size.height += [[bookmarkBarController_ view] bounds].size.height; 1927 frame.size.height += NSHeight([[bookmarkBarController_ view] bounds]);
1931 1928
1932 return frame; 1929 return frame;
1933 } 1930 }
1934 1931
1935 - (void)sheetDidEnd:(NSWindow*)sheet 1932 - (void)sheetDidEnd:(NSWindow*)sheet
1936 returnCode:(NSInteger)code 1933 returnCode:(NSInteger)code
1937 context:(void*)context { 1934 context:(void*)context {
1938 [sheet orderOut:self]; 1935 [sheet orderOut:self];
1939 } 1936 }
1940 1937
(...skipping 281 matching lines...) Expand 10 before | Expand all | Expand 10 after
2222 2219
2223 - (BOOL)supportsBookmarkBar { 2220 - (BOOL)supportsBookmarkBar {
2224 return [self supportsWindowFeature:Browser::FEATURE_BOOKMARKBAR]; 2221 return [self supportsWindowFeature:Browser::FEATURE_BOOKMARKBAR];
2225 } 2222 }
2226 2223
2227 - (BOOL)isTabbedWindow { 2224 - (BOOL)isTabbedWindow {
2228 return browser_->is_type_tabbed(); 2225 return browser_->is_type_tabbed();
2229 } 2226 }
2230 2227
2231 @end // @implementation BrowserWindowController(WindowType) 2228 @end // @implementation BrowserWindowController(WindowType)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698