mac: Don't add subviews to NSThemeFrame on OSX 10.10.
[chromium-blink-merge.git] / chrome / browser / ui / cocoa / version_independent_window.mm
blob24284a490d064e8b943e5d236653544bd56db79c
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
3 // found in the LICENSE file.
5 #import "chrome/browser/ui/cocoa/version_independent_window.h"
7 #include "base/logging.h"
8 #include "base/mac/mac_util.h"
10 @interface VersionIndependentWindow ()
12 + (BOOL)shouldUseFullSizeContentViewForStyle:(NSUInteger)windowStyle;
14 - (NSView*)chromeWindowView;
16 @end
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
24 @implementation FullSizeContentView
26 // 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
28 // shrinking.
29 - (void)setFrameSize:(NSSize)size {
30   if ([self superview])
31     size = [[self superview] bounds].size;
32   [super setFrameSize:size];
35 // The contentView gets moved around during certain full-screen operations.
36 // This is less than ideal, and should eventually be removed.
37 - (void)viewDidMoveToSuperview {
38   [self setFrame:[[self superview] bounds]];
41 @end
43 @implementation NSWindow (VersionIndependentWindow)
45 - (NSView*)cr_windowView {
46   if ([self isKindOfClass:[VersionIndependentWindow class]]) {
47     VersionIndependentWindow* window =
48         static_cast<VersionIndependentWindow*>(self);
49     NSView* chromeWindowView = [window chromeWindowView];
50     if (chromeWindowView)
51       return chromeWindowView;
52   }
54   return [[self contentView] superview];
57 @end
59 @implementation VersionIndependentWindow
61 #pragma mark - Lifecycle
63 - (instancetype)init {
64   NOTREACHED();
65   return nil;
68 - (instancetype)initWithContentRect:(NSRect)contentRect
69                           styleMask:(NSUInteger)windowStyle
70                             backing:(NSBackingStoreType)bufferingType
71                               defer:(BOOL)deferCreation {
72   self = [super initWithContentRect:contentRect
73                           styleMask:windowStyle
74                             backing:bufferingType
75                               defer:deferCreation];
76   if (self) {
77     if ([VersionIndependentWindow
78         shouldUseFullSizeContentViewForStyle:windowStyle]) {
79       chromeWindowView_.reset([[FullSizeContentView alloc] init]);
80       [chromeWindowView_
81           setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
82       [chromeWindowView_ setFrame:[[[self contentView] superview] bounds]];
83       [self setContentView:chromeWindowView_];
84     }
85   }
86   return self;
89 #pragma mark - Private Methods
91 + (BOOL)shouldUseFullSizeContentViewForStyle:(NSUInteger)windowStyle {
92   return (windowStyle & NSTitledWindowMask) && base::mac::IsOSYosemiteOrLater();
95 - (NSView*)chromeWindowView {
96   return chromeWindowView_;
99 #pragma mark - NSWindow Overrides
101 #ifndef NDEBUG
103 - (void)setContentSize:(NSSize)size {
104   DCHECK(!chromeWindowView_);
105   [super setContentSize:size];
108 - (void)setContentMinSize:(NSSize)size {
109   DCHECK(!chromeWindowView_);
110   [super setContentMinSize:size];
113 - (void)setContentMaxSize:(NSSize)size {
114   DCHECK(!chromeWindowView_);
115   [super setContentMaxSize:size];
118 - (void)setContentAspectRatio:(NSSize)ratio {
119   DCHECK(!chromeWindowView_);
120   [super setContentAspectRatio:ratio];
123 #endif  // NDEBUG
125 + (NSRect)frameRectForContentRect:(NSRect)cRect styleMask:(NSUInteger)aStyle {
126   if ([self shouldUseFullSizeContentViewForStyle:aStyle])
127     return cRect;
128   return [super frameRectForContentRect:cRect styleMask:aStyle];
131 - (NSRect)frameRectForContentRect:(NSRect)contentRect {
132   if (chromeWindowView_)
133     return contentRect;
134   return [super frameRectForContentRect:contentRect];
137 + (NSRect)contentRectForFrameRect:(NSRect)fRect styleMask:(NSUInteger)aStyle {
138   if ([self shouldUseFullSizeContentViewForStyle:aStyle])
139     return fRect;
140   return [super contentRectForFrameRect:fRect styleMask:aStyle];
143 - (NSRect)contentRectForFrameRect:(NSRect)frameRect {
144   if (chromeWindowView_)
145     return frameRect;
146   return [super contentRectForFrameRect:frameRect];
149 @end