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

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: comment 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)) {
770 return YES; 770 return YES;
771 }
771 772
772 // If it wasn't reasonable, return NO. 773 // If it wasn't reasonable, return NO.
773 return NO; 774 return NO;
774 } 775 }
775 776
776 // Adjusts the window height by the given amount. 777 // Adjusts the window height by the given amount.
777 - (BOOL)adjustWindowHeightBy:(CGFloat)deltaH { 778 - (BOOL)adjustWindowHeightBy:(CGFloat)deltaH {
778 // By not adjusting the window height when initializing, we can ensure that 779 // 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. 780 // the window opens with the same size that was saved on close.
780 if (initializing_ || [self isFullscreen] || deltaH == 0) 781 if (initializing_ || [self isFullscreen] || deltaH == 0)
781 return NO; 782 return NO;
782 783
783 NSWindow* window = [self window]; 784 NSWindow* window = [self window];
784 NSRect windowFrame = [window frame]; 785 NSRect windowFrame = [window frame];
785 NSRect workarea = [[window screen] visibleFrame]; 786 NSRect workarea = [[window screen] visibleFrame];
786 787
787 // If the window is not already fully in the workarea, do not adjust its frame 788 // If the window is not already fully in the workarea, do not adjust its frame
788 // at all. 789 // at all.
789 if (!NSContainsRect(workarea, windowFrame)) 790 if (!NSContainsRect(workarea, windowFrame))
790 return NO; 791 return NO;
791 792
792 // Record the position of the top/bottom of the window, so we can easily check 793 // Record the position of the top/bottom of the window, so we can easily check
793 // whether we grew the window upwards/downwards. 794 // whether we grew the window upwards/downwards.
794 CGFloat oldWindowMaxY = NSMaxY(windowFrame); 795 CGFloat oldWindowMaxY = NSMaxY(windowFrame);
795 CGFloat oldWindowMinY = NSMinY(windowFrame); 796 CGFloat oldWindowMinY = NSMinY(windowFrame);
796 797
797 // We are "zoomed" if we occupy the full vertical space. 798 // We are "zoomed" if we occupy the full vertical space.
798 bool isZoomed = (windowFrame.origin.y == workarea.origin.y && 799 bool isZoomed = (windowFrame.origin.y == workarea.origin.y &&
799 windowFrame.size.height == workarea.size.height); 800 NSHeight(windowFrame) == NSHeight(workarea));
800 801
801 // If we're shrinking the window.... 802 // If we're shrinking the window....
802 if (deltaH < 0) { 803 if (deltaH < 0) {
803 bool didChange = false; 804 bool didChange = false;
804 805
805 // Don't reset if not currently zoomed since shrinking can take several 806 // Don't reset if not currently zoomed since shrinking can take several
806 // steps! 807 // steps!
807 if (isZoomed) 808 if (isZoomed)
808 isShrinkingFromZoomed_ = YES; 809 isShrinkingFromZoomed_ = YES;
809 810
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
852 // Resize the window as requested, keeping the top left corner fixed. 853 // Resize the window as requested, keeping the top left corner fixed.
853 windowFrame.origin.y -= deltaH; 854 windowFrame.origin.y -= deltaH;
854 windowFrame.size.height += deltaH; 855 windowFrame.size.height += deltaH;
855 856
856 // If the bottom left corner is now outside the visible frame, move the 857 // 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 858 // window up to make it fit, but make sure not to move the top left corner
858 // out of the visible frame. 859 // out of the visible frame.
859 if (windowFrame.origin.y < workarea.origin.y) { 860 if (windowFrame.origin.y < workarea.origin.y) {
860 windowFrame.origin.y = workarea.origin.y; 861 windowFrame.origin.y = workarea.origin.y;
861 windowFrame.size.height = 862 windowFrame.size.height =
862 std::min(windowFrame.size.height, workarea.size.height); 863 std::min(NSHeight(windowFrame), NSHeight(workarea));
863 } 864 }
864 865
865 // Record (if applicable) how much we grew the window in either direction. 866 // Record (if applicable) how much we grew the window in either direction.
866 // (N.B.: These only record growth, not shrinkage.) 867 // (N.B.: These only record growth, not shrinkage.)
867 if (NSMaxY(windowFrame) > oldWindowMaxY) 868 if (NSMaxY(windowFrame) > oldWindowMaxY)
868 windowTopGrowth_ += NSMaxY(windowFrame) - oldWindowMaxY; 869 windowTopGrowth_ += NSMaxY(windowFrame) - oldWindowMaxY;
869 if (NSMinY(windowFrame) < oldWindowMinY) 870 if (NSMinY(windowFrame) < oldWindowMinY)
870 windowBottomGrowth_ += oldWindowMinY - NSMinY(windowFrame); 871 windowBottomGrowth_ += oldWindowMinY - NSMinY(windowFrame);
871 } 872 }
872 873
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
910 // 911 //
911 // We do not adjust the window height for bookmark bar changes on the NTP. 912 // We do not adjust the window height for bookmark bar changes on the NTP.
912 BOOL shouldAdjustBookmarkHeight = 913 BOOL shouldAdjustBookmarkHeight =
913 [bookmarkBarController_ isAnimatingBetweenState:bookmarks::kHiddenState 914 [bookmarkBarController_ isAnimatingBetweenState:bookmarks::kHiddenState
914 andState:bookmarks::kShowingState]; 915 andState:bookmarks::kShowingState];
915 916
916 BOOL resizeRectDirty = NO; 917 BOOL resizeRectDirty = NO;
917 if ((shouldAdjustBookmarkHeight && view == [bookmarkBarController_ view]) || 918 if ((shouldAdjustBookmarkHeight && view == [bookmarkBarController_ view]) ||
918 view == [downloadShelfController_ view]) { 919 view == [downloadShelfController_ view]) {
919 [[self window] disableScreenUpdatesUntilFlush]; 920 [[self window] disableScreenUpdatesUntilFlush];
920 CGFloat deltaH = height - frame.size.height; 921 CGFloat deltaH = height - NSHeight(frame);
921 if ([self adjustWindowHeightBy:deltaH] && 922 if ([self adjustWindowHeightBy:deltaH] &&
922 view == [downloadShelfController_ view]) { 923 view == [downloadShelfController_ view]) {
923 // If the window height didn't change, the download shelf will change the 924 // 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 925 // 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.) 926 // an explicit grow box invalidation (else, the resize message does that.)
926 resizeRectDirty = YES; 927 resizeRectDirty = YES;
927 } 928 }
928 } 929 }
929 930
930 frame.size.height = height; 931 frame.size.height = height;
(...skipping 247 matching lines...) Expand 10 before | Expand all | Expand 10 after
1178 - (NSRect)selectedTabGrowBoxRect { 1179 - (NSRect)selectedTabGrowBoxRect {
1179 NSWindow* window = [self window]; 1180 NSWindow* window = [self window];
1180 if (![window respondsToSelector:@selector(_growBoxRect)]) 1181 if (![window respondsToSelector:@selector(_growBoxRect)])
1181 return NSZeroRect; 1182 return NSZeroRect;
1182 1183
1183 // Before we return a rect, we need to convert it from window coordinates 1184 // Before we return a rect, we need to convert it from window coordinates
1184 // to tab content area coordinates and flip the coordinate system. 1185 // to tab content area coordinates and flip the coordinate system.
1185 NSRect growBoxRect = 1186 NSRect growBoxRect =
1186 [[self tabContentArea] convertRect:[window _growBoxRect] fromView:nil]; 1187 [[self tabContentArea] convertRect:[window _growBoxRect] fromView:nil];
1187 growBoxRect.origin.y = 1188 growBoxRect.origin.y =
1188 [[self tabContentArea] frame].size.height - growBoxRect.size.height - 1189 NSHeight([[self tabContentArea] frame]) - NSMaxY(growBoxRect);
1189 growBoxRect.origin.y;
1190 return growBoxRect; 1190 return growBoxRect;
1191 } 1191 }
1192 1192
1193 // Accept tabs from a BrowserWindowController with the same Profile. 1193 // Accept tabs from a BrowserWindowController with the same Profile.
1194 - (BOOL)canReceiveFrom:(TabWindowController*)source { 1194 - (BOOL)canReceiveFrom:(TabWindowController*)source {
1195 if (![source isKindOfClass:[BrowserWindowController class]]) { 1195 if (![source isKindOfClass:[BrowserWindowController class]]) {
1196 return NO; 1196 return NO;
1197 } 1197 }
1198 1198
1199 BrowserWindowController* realSource = 1199 BrowserWindowController* realSource =
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after
1308 // Fetch the tab contents for the tab being dragged. 1308 // Fetch the tab contents for the tab being dragged.
1309 int index = [tabStripController_ modelIndexForTabView:tabView]; 1309 int index = [tabStripController_ modelIndexForTabView:tabView];
1310 TabContentsWrapper* contents = browser_->GetTabContentsWrapperAt(index); 1310 TabContentsWrapper* contents = browser_->GetTabContentsWrapperAt(index);
1311 1311
1312 // Set the window size. Need to do this before we detach the tab so it's 1312 // 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 1313 // still in the window. We have to flip the coordinates as that's what
1314 // is expected by the Browser code. 1314 // is expected by the Browser code.
1315 NSWindow* sourceWindow = [tabView window]; 1315 NSWindow* sourceWindow = [tabView window];
1316 NSRect windowRect = [sourceWindow frame]; 1316 NSRect windowRect = [sourceWindow frame];
1317 NSScreen* screen = [sourceWindow screen]; 1317 NSScreen* screen = [sourceWindow screen];
1318 windowRect.origin.y = 1318 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, 1319 gfx::Rect browserRect(windowRect.origin.x, windowRect.origin.y,
1322 windowRect.size.width, windowRect.size.height); 1320 NSWidth(windowRect), NSHeight(windowRect));
1323 1321
1324 NSRect sourceTabRect = [tabView frame]; 1322 NSRect sourceTabRect = [tabView frame];
1325 NSView* tabStrip = [self tabStripView]; 1323 NSView* tabStrip = [self tabStripView];
1326 1324
1327 // Pushes tabView's frame back inside the tabstrip. 1325 // Pushes tabView's frame back inside the tabstrip.
1328 NSSize tabOverflow = 1326 NSSize tabOverflow =
1329 [self overflowFrom:[tabStrip convertRectToBase:sourceTabRect] 1327 [self overflowFrom:[tabStrip convertRectToBase:sourceTabRect]
1330 to:[tabStrip frame]]; 1328 to:[tabStrip frame]];
1331 NSRect tabRect = NSOffsetRect(sourceTabRect, 1329 NSRect tabRect = NSOffsetRect(sourceTabRect,
1332 -tabOverflow.width, -tabOverflow.height); 1330 -tabOverflow.width, -tabOverflow.height);
(...skipping 587 matching lines...) Expand 10 before | Expand all | Expand 10 after
1920 return NSZeroRect; 1918 return NSZeroRect;
1921 1919
1922 NSRect frame = [view convertRect:[view bounds] toView:nil]; 1920 NSRect frame = [view convertRect:[view bounds] toView:nil];
1923 NSPoint originInScreenCoords = 1921 NSPoint originInScreenCoords =
1924 [[view window] convertBaseToScreen:frame.origin]; 1922 [[view window] convertBaseToScreen:frame.origin];
1925 frame.origin = originInScreenCoords; 1923 frame.origin = originInScreenCoords;
1926 1924
1927 // Account for the bookmark bar height if it is currently in the detached 1925 // Account for the bookmark bar height if it is currently in the detached
1928 // state on the new tab page. 1926 // state on the new tab page.
1929 if ([bookmarkBarController_ isInState:(bookmarks::kDetachedState)]) 1927 if ([bookmarkBarController_ isInState:(bookmarks::kDetachedState)])
1930 frame.size.height += [[bookmarkBarController_ view] bounds].size.height; 1928 frame.size.height += NSHeight([[bookmarkBarController_ view] bounds]);
1931 1929
1932 return frame; 1930 return frame;
1933 } 1931 }
1934 1932
1935 - (void)sheetDidEnd:(NSWindow*)sheet 1933 - (void)sheetDidEnd:(NSWindow*)sheet
1936 returnCode:(NSInteger)code 1934 returnCode:(NSInteger)code
1937 context:(void*)context { 1935 context:(void*)context {
1938 [sheet orderOut:self]; 1936 [sheet orderOut:self];
1939 } 1937 }
1940 1938
(...skipping 281 matching lines...) Expand 10 before | Expand all | Expand 10 after
2222 2220
2223 - (BOOL)supportsBookmarkBar { 2221 - (BOOL)supportsBookmarkBar {
2224 return [self supportsWindowFeature:Browser::FEATURE_BOOKMARKBAR]; 2222 return [self supportsWindowFeature:Browser::FEATURE_BOOKMARKBAR];
2225 } 2223 }
2226 2224
2227 - (BOOL)isTabbedWindow { 2225 - (BOOL)isTabbedWindow {
2228 return browser_->is_type_tabbed(); 2226 return browser_->is_type_tabbed();
2229 } 2227 }
2230 2228
2231 @end // @implementation BrowserWindowController(WindowType) 2229 @end // @implementation BrowserWindowController(WindowType)
OLDNEW
« no previous file with comments | « chrome/browser/ui/cocoa/browser_window_cocoa.mm ('k') | chrome/browser/ui/cocoa/confirm_bubble_cocoa.mm » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698