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;
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
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
29 - (void)setFrameSize:(NSSize)size {
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]];
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];
51 return chromeWindowView;
54 return [[self contentView] superview];
59 @implementation VersionIndependentWindow
61 #pragma mark - Lifecycle
63 - (instancetype)init {
68 - (instancetype)initWithContentRect:(NSRect)contentRect
69 styleMask:(NSUInteger)windowStyle
70 backing:(NSBackingStoreType)bufferingType
71 defer:(BOOL)deferCreation {
72 self = [super initWithContentRect:contentRect
77 if ([VersionIndependentWindow
78 shouldUseFullSizeContentViewForStyle:windowStyle]) {
79 chromeWindowView_.reset([[FullSizeContentView alloc] init]);
81 setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
82 [chromeWindowView_ setFrame:[[[self contentView] superview] bounds]];
83 [self setContentView:chromeWindowView_];
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
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];
125 + (NSRect)frameRectForContentRect:(NSRect)cRect styleMask:(NSUInteger)aStyle {
126 if ([self shouldUseFullSizeContentViewForStyle:aStyle])
128 return [super frameRectForContentRect:cRect styleMask:aStyle];
131 - (NSRect)frameRectForContentRect:(NSRect)contentRect {
132 if (chromeWindowView_)
134 return [super frameRectForContentRect:contentRect];
137 + (NSRect)contentRectForFrameRect:(NSRect)fRect styleMask:(NSUInteger)aStyle {
138 if ([self shouldUseFullSizeContentViewForStyle:aStyle])
140 return [super contentRectForFrameRect:fRect styleMask:aStyle];
143 - (NSRect)contentRectForFrameRect:(NSRect)frameRect {
144 if (chromeWindowView_)
146 return [super contentRectForFrameRect:frameRect];