version_independent_window.mm revision 116680a4aac90f2aa7413d9095a592090648e557
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.
4
5#import "chrome/browser/ui/cocoa/version_independent_window.h"
6
7#include "base/logging.h"
8#include "base/mac/mac_util.h"
9
10@interface VersionIndependentWindow ()
11
12+ (BOOL)shouldUseFullSizeContentViewForStyle:(NSUInteger)windowStyle;
13
14- (NSView*)chromeWindowView;
15
16@end
17
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
25
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];
33}
34
35@end
36
37@implementation NSWindow (VersionIndependentWindow)
38
39- (NSView*)cr_windowView {
40  if ([self isKindOfClass:[VersionIndependentWindow class]]) {
41    VersionIndependentWindow* window =
42        static_cast<VersionIndependentWindow*>(self);
43    NSView* chromeWindowView = [window chromeWindowView];
44    if (chromeWindowView)
45      return chromeWindowView;
46  }
47
48  return [[self contentView] superview];
49}
50
51@end
52
53@implementation VersionIndependentWindow
54
55#pragma mark - Lifecycle
56
57- (instancetype)init {
58  NOTREACHED();
59  return nil;
60}
61
62- (instancetype)initWithContentRect:(NSRect)contentRect
63                          styleMask:(NSUInteger)windowStyle
64                            backing:(NSBackingStoreType)bufferingType
65                              defer:(BOOL)deferCreation {
66  self = [super initWithContentRect:contentRect
67                          styleMask:windowStyle
68                            backing:bufferingType
69                              defer:deferCreation];
70  if (self) {
71    if ([VersionIndependentWindow
72        shouldUseFullSizeContentViewForStyle:windowStyle]) {
73      chromeWindowView_.reset([[FullSizeContentView alloc] init]);
74      [chromeWindowView_
75          setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
76      [self setContentView:chromeWindowView_];
77      [chromeWindowView_ setFrame:[[[self contentView] superview] bounds]];
78    }
79  }
80  return self;
81}
82
83#pragma mark - Private Methods
84
85+ (BOOL)shouldUseFullSizeContentViewForStyle:(NSUInteger)windowStyle {
86  return (windowStyle & NSTitledWindowMask) && base::mac::IsOSYosemiteOrLater();
87}
88
89- (NSView*)chromeWindowView {
90  return chromeWindowView_;
91}
92
93#pragma mark - NSWindow Overrides
94
95#ifndef NDEBUG
96
97- (void)setContentSize:(NSSize)size {
98  DCHECK(!chromeWindowView_);
99  [super setContentSize:size];
100}
101
102- (void)setContentMinSize:(NSSize)size {
103  DCHECK(!chromeWindowView_);
104  [super setContentMinSize:size];
105}
106
107- (void)setContentMaxSize:(NSSize)size {
108  DCHECK(!chromeWindowView_);
109  [super setContentMaxSize:size];
110}
111
112- (void)setContentAspectRatio:(NSSize)ratio {
113  DCHECK(!chromeWindowView_);
114  [super setContentAspectRatio:ratio];
115}
116
117#endif  // NDEBUG
118
119+ (NSRect)frameRectForContentRect:(NSRect)cRect styleMask:(NSUInteger)aStyle {
120  if ([self shouldUseFullSizeContentViewForStyle:aStyle])
121    return cRect;
122  return [super frameRectForContentRect:cRect styleMask:aStyle];
123}
124
125- (NSRect)frameRectForContentRect:(NSRect)contentRect {
126  if (chromeWindowView_)
127    return contentRect;
128  return [super frameRectForContentRect:contentRect];
129}
130
131+ (NSRect)contentRectForFrameRect:(NSRect)fRect styleMask:(NSUInteger)aStyle {
132  if ([self shouldUseFullSizeContentViewForStyle:aStyle])
133    return fRect;
134  return [super contentRectForFrameRect:fRect styleMask:aStyle];
135}
136
137- (NSRect)contentRectForFrameRect:(NSRect)frameRect {
138  if (chromeWindowView_)
139    return frameRect;
140  return [super contentRectForFrameRect:frameRect];
141}
142
143@end
144