OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/version_independent_window.h" | 5 #import "chrome/browser/ui/cocoa/version_independent_window.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/mac/mac_util.h" | 8 #include "base/mac/mac_util.h" |
| 9 #include "base/mac/scoped_nsobject.h" |
| 10 |
| 11 // This view always takes the size of its superview. It is intended to be used |
| 12 // as a NSWindow's contentView. It is needed because NSWindow's implementation |
| 13 // explicitly resizes the contentView at inopportune times. |
| 14 @interface FullSizeContentView : NSView |
| 15 @end |
| 16 |
| 17 namespace { |
| 18 |
| 19 // Reorders the subviews of NSWindow's root view so that the contentView is |
| 20 // moved to the back, and the ordering of the other views is unchanged. |
| 21 // |context| should be an NSArray containing the subviews of the root view as |
| 22 // they were previously ordered. |
| 23 int ReorderContentViewToBack(id firstView, id secondView, void* context) { |
| 24 NSView* contentView = [[firstView window] contentView]; |
| 25 NSArray* subviews = static_cast<NSArray*>(context); |
| 26 if (firstView == contentView) |
| 27 return NSOrderedAscending; |
| 28 if (secondView == contentView) |
| 29 return NSOrderedDescending; |
| 30 NSUInteger index1 = [subviews indexOfObject:firstView]; |
| 31 NSUInteger index2 = [subviews indexOfObject:secondView]; |
| 32 return (index1 < index2) ? NSOrderedAscending : NSOrderedDescending; |
| 33 } |
| 34 |
| 35 } // namespace |
9 | 36 |
10 @interface VersionIndependentWindow () | 37 @interface VersionIndependentWindow () |
11 | 38 |
12 + (BOOL)shouldUseFullSizeContentViewForStyle:(NSUInteger)windowStyle; | 39 + (BOOL)shouldUseFullSizeContentViewForStyle:(NSUInteger)windowStyle; |
13 | 40 |
14 - (NSView*)chromeWindowView; | 41 - (NSView*)chromeWindowView; |
15 | 42 |
16 @end | 43 @end |
17 | 44 |
18 // This view always takes the size of its superview. It is intended to be used | |
19 // as a NSWindow's contentView. It is needed because NSWindow's implementation | |
20 // explicitly resizes the contentView at inopportune times. | |
21 @interface FullSizeContentView : NSView | |
22 @end | |
23 | |
24 @implementation FullSizeContentView | 45 @implementation FullSizeContentView |
25 | 46 |
26 // This method is directly called by NSWindow during a window resize on OSX | 47 // This method is directly called by NSWindow during a window resize on OSX |
27 // 10.10.0, beta 2. We must override it to prevent the content view from | 48 // 10.10.0, beta 2. We must override it to prevent the content view from |
28 // shrinking. | 49 // shrinking. |
29 - (void)setFrameSize:(NSSize)size { | 50 - (void)setFrameSize:(NSSize)size { |
30 if ([self superview]) | 51 if ([self superview]) |
31 size = [[self superview] bounds].size; | 52 size = [[self superview] bounds].size; |
32 [super setFrameSize:size]; | 53 [super setFrameSize:size]; |
33 } | 54 } |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
68 backing:bufferingType | 89 backing:bufferingType |
69 defer:deferCreation]; | 90 defer:deferCreation]; |
70 if (self) { | 91 if (self) { |
71 if ([VersionIndependentWindow | 92 if ([VersionIndependentWindow |
72 shouldUseFullSizeContentViewForStyle:windowStyle]) { | 93 shouldUseFullSizeContentViewForStyle:windowStyle]) { |
73 chromeWindowView_.reset([[FullSizeContentView alloc] init]); | 94 chromeWindowView_.reset([[FullSizeContentView alloc] init]); |
74 [chromeWindowView_ | 95 [chromeWindowView_ |
75 setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable]; | 96 setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable]; |
76 [self setContentView:chromeWindowView_]; | 97 [self setContentView:chromeWindowView_]; |
77 [chromeWindowView_ setFrame:[[[self contentView] superview] bounds]]; | 98 [chromeWindowView_ setFrame:[[[self contentView] superview] bounds]]; |
| 99 |
| 100 // Move the content view to the back. |
| 101 // In Yosemite, the content view takes up the full size of the window, |
| 102 // and when it is in front of the zoom/fullscreen button, alt-clicking |
| 103 // the button has the wrong effect. |
| 104 // Adding subviews to the NSThemeFrame provokes a warning in Yosemite, so |
| 105 // we sort the subviews in place. |
| 106 // http://crbug.com/393808 |
| 107 NSView* superview = [[self contentView] superview]; |
| 108 base::scoped_nsobject<NSArray> subviews([[superview subviews] copy]); |
| 109 [superview sortSubviewsUsingFunction:&ReorderContentViewToBack |
| 110 context:subviews.get()]; |
78 } | 111 } |
79 } | 112 } |
80 return self; | 113 return self; |
81 } | 114 } |
82 | 115 |
83 #pragma mark - Private Methods | 116 #pragma mark - Private Methods |
84 | 117 |
85 + (BOOL)shouldUseFullSizeContentViewForStyle:(NSUInteger)windowStyle { | 118 + (BOOL)shouldUseFullSizeContentViewForStyle:(NSUInteger)windowStyle { |
86 return (windowStyle & NSTitledWindowMask) && base::mac::IsOSYosemiteOrLater(); | 119 return (windowStyle & NSTitledWindowMask) && base::mac::IsOSYosemiteOrLater(); |
87 } | 120 } |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
134 return [super contentRectForFrameRect:fRect styleMask:aStyle]; | 167 return [super contentRectForFrameRect:fRect styleMask:aStyle]; |
135 } | 168 } |
136 | 169 |
137 - (NSRect)contentRectForFrameRect:(NSRect)frameRect { | 170 - (NSRect)contentRectForFrameRect:(NSRect)frameRect { |
138 if (chromeWindowView_) | 171 if (chromeWindowView_) |
139 return frameRect; | 172 return frameRect; |
140 return [super contentRectForFrameRect:frameRect]; | 173 return [super contentRectForFrameRect:frameRect]; |
141 } | 174 } |
142 | 175 |
143 @end | 176 @end |
OLD | NEW |