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/full_size_content_window.h"
7 #include "base/logging.h"
8 #include "base/mac/foundation_util.h"
10 @interface FullSizeContentWindow ()
12 + (BOOL)shouldUseFullSizeContentViewForStyle:(NSUInteger)windowStyle;
16 // This view always takes the size of its superview. It is intended to be used
17 // as a NSWindow's contentView. It is needed because NSWindow's implementation
18 // explicitly resizes the contentView at inopportune times.
19 @interface FullSizeContentView : NSView
21 // This method allows us to set the content view size since setFrameSize is
22 // overridden to prevent the view from shrinking.
23 - (void)forceFrameSize:(NSSize)size;
27 @implementation FullSizeContentView
29 // This method is directly called by AppKit during a live window resize.
30 // Override it to prevent the content view from shrinking.
31 - (void)setFrameSize:(NSSize)size {
33 size = [[self superview] bounds].size;
34 [super setFrameSize:size];
37 - (void)forceFrameSize:(NSSize)size {
38 [super setFrameSize:size];
43 @implementation FullSizeContentWindow
45 #pragma mark - Lifecycle
47 - (instancetype)init {
52 - (instancetype)initWithContentRect:(NSRect)contentRect
53 styleMask:(NSUInteger)windowStyle
54 backing:(NSBackingStoreType)bufferingType
55 defer:(BOOL)deferCreation {
56 return [self initWithContentRect:contentRect
60 wantsViewsOverTitlebar:NO];
63 - (instancetype)initWithContentRect:(NSRect)contentRect
64 styleMask:(NSUInteger)windowStyle
65 backing:(NSBackingStoreType)bufferingType
66 defer:(BOOL)deferCreation
67 wantsViewsOverTitlebar:(BOOL)wantsViewsOverTitlebar {
68 self = [super initWithContentRect:contentRect
73 if (wantsViewsOverTitlebar &&
74 [FullSizeContentWindow
75 shouldUseFullSizeContentViewForStyle:windowStyle]) {
76 chromeWindowView_.reset([[FullSizeContentView alloc] init]);
78 setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
79 [self setContentView:chromeWindowView_];
80 [chromeWindowView_ setFrame:[[chromeWindowView_ superview] bounds]];
82 // Our content view overlaps the window control buttons, so we must ensure
83 // it is positioned below the buttons.
84 NSView* superview = [chromeWindowView_ superview];
85 [chromeWindowView_ removeFromSuperview];
86 [superview addSubview:chromeWindowView_
87 positioned:NSWindowBelow
94 - (void)forceContentViewSize:(NSSize)size {
95 FullSizeContentView* contentView =
96 base::mac::ObjCCast<FullSizeContentView>(chromeWindowView_);
97 [contentView forceFrameSize:size];
100 #pragma mark - Private Methods
102 + (BOOL)shouldUseFullSizeContentViewForStyle:(NSUInteger)windowStyle {
103 return windowStyle & NSTitledWindowMask;
106 #pragma mark - NSWindow Overrides
108 + (NSRect)frameRectForContentRect:(NSRect)cRect styleMask:(NSUInteger)aStyle {
109 if ([self shouldUseFullSizeContentViewForStyle:aStyle])
111 return [super frameRectForContentRect:cRect styleMask:aStyle];
114 - (NSRect)frameRectForContentRect:(NSRect)contentRect {
115 if (chromeWindowView_)
117 return [super frameRectForContentRect:contentRect];
120 + (NSRect)contentRectForFrameRect:(NSRect)fRect styleMask:(NSUInteger)aStyle {
121 if ([self shouldUseFullSizeContentViewForStyle:aStyle])
123 return [super contentRectForFrameRect:fRect styleMask:aStyle];
126 - (NSRect)contentRectForFrameRect:(NSRect)frameRect {
127 if (chromeWindowView_)
129 return [super contentRectForFrameRect:frameRect];