OLD | NEW |
| (Empty) |
1 // Copyright 2011 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/tab_contents/previewable_contents_controller.h" | |
6 | |
7 #include "base/mac/bundle_locations.h" | |
8 #include "chrome/browser/ui/cocoa/browser_window_controller.h" | |
9 #include "chrome/browser/ui/cocoa/tab_contents/instant_preview_controller_mac.h" | |
10 #include "chrome/browser/ui/cocoa/tab_contents/preview_drop_shadow_view.h" | |
11 #include "content/public/browser/web_contents.h" | |
12 #include "content/public/browser/web_contents_view.h" | |
13 | |
14 @interface PreviewableContentsController() | |
15 - (void)viewDidResize:(NSNotification*)note; | |
16 - (void)layoutViews; | |
17 - (CGFloat)previewHeightInPixels; | |
18 @end | |
19 | |
20 @implementation PreviewableContentsController | |
21 | |
22 @synthesize drawDropShadow = drawDropShadow_; | |
23 @synthesize activeContainerOffset = activeContainerOffset_; | |
24 | |
25 - (id)initWithBrowser:(Browser*)browser | |
26 windowController:(BrowserWindowController*)windowController { | |
27 if ((self = [super init])) { | |
28 windowController_ = windowController; | |
29 scoped_nsobject<NSView> view([[NSView alloc] initWithFrame:NSZeroRect]); | |
30 [view setAutoresizingMask:NSViewHeightSizable | NSViewWidthSizable]; | |
31 [view setAutoresizesSubviews:NO]; | |
32 [[NSNotificationCenter defaultCenter] | |
33 addObserver:self | |
34 selector:@selector(viewDidResize:) | |
35 name:NSViewFrameDidChangeNotification | |
36 object:view]; | |
37 [self setView:view]; | |
38 | |
39 activeContainer_.reset([[NSView alloc] initWithFrame:NSZeroRect]); | |
40 [view addSubview:activeContainer_]; | |
41 | |
42 instantPreviewController_.reset( | |
43 new InstantPreviewControllerMac(browser, windowController, self)); | |
44 } | |
45 return self; | |
46 } | |
47 | |
48 - (void)dealloc { | |
49 [[NSNotificationCenter defaultCenter] removeObserver:self]; | |
50 [super dealloc]; | |
51 } | |
52 | |
53 - (void)setPreview:(content::WebContents*)preview | |
54 height:(CGFloat)height | |
55 heightUnits:(InstantSizeUnits)heightUnits | |
56 drawDropShadow:(BOOL)drawDropShadow { | |
57 // If drawing drop shadow, clip the bottom 1-px-thick separator out of | |
58 // preview. | |
59 // TODO(sail): remove this when GWS gives chrome the height without the | |
60 // separator. | |
61 if (drawDropShadow && heightUnits != INSTANT_SIZE_PERCENT) | |
62 --height; | |
63 | |
64 if (previewContents_ == preview && | |
65 previewHeight_ == height && | |
66 previewHeightUnits_ == heightUnits && | |
67 drawDropShadow_ == drawDropShadow) { | |
68 return; | |
69 } | |
70 | |
71 // Remove any old preview contents before showing the new one. | |
72 if (previewContents_) { | |
73 [previewContents_->GetView()->GetNativeView() removeFromSuperview]; | |
74 previewContents_->WasHidden(); | |
75 } | |
76 | |
77 previewContents_ = preview; | |
78 previewHeight_ = height; | |
79 previewHeightUnits_ = heightUnits; | |
80 drawDropShadow_ = drawDropShadow; | |
81 | |
82 // Add the preview contents. | |
83 if (previewContents_) { | |
84 [[[self view] window] disableScreenUpdatesUntilFlush]; | |
85 previewContents_->GetView()->SetAllowOverlappingViews(true); | |
86 [[self view] addSubview:previewContents_->GetView()->GetNativeView()]; | |
87 } | |
88 | |
89 if (drawDropShadow_) { | |
90 if (!dropShadowView_) { | |
91 dropShadowView_.reset( | |
92 [[PreviewDropShadowView alloc] initWithFrame:NSZeroRect]); | |
93 [[self view] addSubview:dropShadowView_]; | |
94 } | |
95 } else { | |
96 [dropShadowView_ removeFromSuperview]; | |
97 dropShadowView_.reset(); | |
98 } | |
99 | |
100 [self layoutViews]; | |
101 | |
102 if (previewContents_) | |
103 previewContents_->WasShown(); | |
104 } | |
105 | |
106 - (void)onActivateTabWithContents:(content::WebContents*)contents { | |
107 if (previewContents_ == contents) { | |
108 if (previewContents_) { | |
109 [previewContents_->GetView()->GetNativeView() removeFromSuperview]; | |
110 previewContents_ = NULL; | |
111 } | |
112 [self setPreview:NULL | |
113 height:0 | |
114 heightUnits:INSTANT_SIZE_PIXELS | |
115 drawDropShadow:NO]; | |
116 } | |
117 } | |
118 | |
119 - (BOOL)isShowingPreview { | |
120 return previewContents_ != nil; | |
121 } | |
122 | |
123 - (InstantPreviewControllerMac*)instantPreviewController { | |
124 return instantPreviewController_.get(); | |
125 } | |
126 | |
127 - (NSView*)activeContainer { | |
128 return activeContainer_.get(); | |
129 } | |
130 | |
131 - (NSView*)dropShadowView { | |
132 return dropShadowView_.get(); | |
133 } | |
134 | |
135 - (void)setActiveContainerOffset:(CGFloat)activeContainerOffset { | |
136 if (activeContainerOffset_ == activeContainerOffset) | |
137 return; | |
138 | |
139 activeContainerOffset_ = activeContainerOffset; | |
140 [self layoutViews]; | |
141 } | |
142 | |
143 - (void)viewDidResize:(NSNotification*)note { | |
144 [self layoutViews]; | |
145 } | |
146 | |
147 - (void)layoutViews { | |
148 NSRect bounds = [[self view] bounds]; | |
149 | |
150 if (previewContents_) { | |
151 NSRect previewFrame = bounds; | |
152 previewFrame.size.height = [self previewHeightInPixels]; | |
153 previewFrame.origin.y = NSMaxY(bounds) - NSHeight(previewFrame); | |
154 [previewContents_->GetView()->GetNativeView() setFrame:previewFrame]; | |
155 | |
156 if (dropShadowView_) { | |
157 NSRect dropShadowFrame = bounds; | |
158 dropShadowFrame.size.height = [PreviewDropShadowView preferredHeight]; | |
159 dropShadowFrame.origin.y = | |
160 NSMinY(previewFrame) - NSHeight(dropShadowFrame); | |
161 [dropShadowView_ setFrame:dropShadowFrame]; | |
162 } | |
163 } | |
164 | |
165 NSRect activeFrame = bounds; | |
166 activeFrame.size.height -= activeContainerOffset_; | |
167 [activeContainer_ setFrame:activeFrame]; | |
168 } | |
169 | |
170 - (CGFloat)previewHeightInPixels { | |
171 CGFloat height = NSHeight([[self view] bounds]); | |
172 switch (previewHeightUnits_) { | |
173 case INSTANT_SIZE_PERCENT: | |
174 return std::min(height, (height * previewHeight_) / 100); | |
175 case INSTANT_SIZE_PIXELS: | |
176 return std::min(height, previewHeight_); | |
177 } | |
178 } | |
179 | |
180 @end | |
OLD | NEW |