BookmarkManager: Fix 'new folder text field size changes on clicking it' issue.
[chromium-blink-merge.git] / chrome / browser / ui / cocoa / full_size_content_window.mm
blob04c76986226669f6222f2ff82c5be074ee75fd01
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;
14 @end
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;
25 @end
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 {
32   if ([self superview])
33     size = [[self superview] bounds].size;
34   [super setFrameSize:size];
37 - (void)forceFrameSize:(NSSize)size {
38   [super setFrameSize:size];
41 @end
43 @implementation FullSizeContentWindow
45 #pragma mark - Lifecycle
47 - (instancetype)init {
48   NOTREACHED();
49   return nil;
52 - (instancetype)initWithContentRect:(NSRect)contentRect
53                           styleMask:(NSUInteger)windowStyle
54                             backing:(NSBackingStoreType)bufferingType
55                               defer:(BOOL)deferCreation {
56   return [self initWithContentRect:contentRect
57                          styleMask:windowStyle
58                            backing:bufferingType
59                              defer:deferCreation
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
69                           styleMask:windowStyle
70                             backing:bufferingType
71                               defer:deferCreation];
72   if (self) {
73     if (wantsViewsOverTitlebar &&
74         [FullSizeContentWindow
75             shouldUseFullSizeContentViewForStyle:windowStyle]) {
76       chromeWindowView_.reset([[FullSizeContentView alloc] init]);
77       [chromeWindowView_
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
88                  relativeTo:nil];
89     }
90   }
91   return self;
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])
110     return cRect;
111   return [super frameRectForContentRect:cRect styleMask:aStyle];
114 - (NSRect)frameRectForContentRect:(NSRect)contentRect {
115   if (chromeWindowView_)
116     return contentRect;
117   return [super frameRectForContentRect:contentRect];
120 + (NSRect)contentRectForFrameRect:(NSRect)fRect styleMask:(NSUInteger)aStyle {
121   if ([self shouldUseFullSizeContentViewForStyle:aStyle])
122     return fRect;
123   return [super contentRectForFrameRect:fRect styleMask:aStyle];
126 - (NSRect)contentRectForFrameRect:(NSRect)frameRect {
127   if (chromeWindowView_)
128     return frameRect;
129   return [super contentRectForFrameRect:frameRect];
132 @end