OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "views/bubble/bubble_delegate.h" | 5 #include "views/bubble/bubble_delegate.h" |
6 | 6 |
7 #include "ui/base/animation/slide_animation.h" | 7 #include "ui/base/animation/slide_animation.h" |
8 #include "views/bubble/bubble_frame_view.h" | 8 #include "views/bubble/bubble_frame_view.h" |
9 #include "views/widget/widget.h" | 9 #include "views/widget/widget.h" |
10 | 10 |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
102 } | 102 } |
103 | 103 |
104 BubbleDelegateView::~BubbleDelegateView() { | 104 BubbleDelegateView::~BubbleDelegateView() { |
105 if (border_widget_) | 105 if (border_widget_) |
106 border_widget_->Close(); | 106 border_widget_->Close(); |
107 } | 107 } |
108 | 108 |
109 // static | 109 // static |
110 Widget* BubbleDelegateView::CreateBubble(BubbleDelegateView* bubble_delegate) { | 110 Widget* BubbleDelegateView::CreateBubble(BubbleDelegateView* bubble_delegate) { |
111 bubble_delegate->Init(); | 111 bubble_delegate->Init(); |
112 Widget* parent_widget = bubble_delegate->anchor_view() ? | 112 Widget* parent = bubble_delegate->anchor_view() ? |
113 bubble_delegate->anchor_view()->GetWidget() : NULL; | 113 bubble_delegate->anchor_view()->GetWidget() : NULL; |
114 Widget* bubble_widget = CreateBubbleWidget(bubble_delegate, parent_widget); | 114 Widget* bubble_widget = CreateBubbleWidget(bubble_delegate, parent); |
115 | 115 |
116 #if defined(OS_WIN) && !defined(USE_AURA) | 116 #if defined(OS_WIN) && !defined(USE_AURA) |
117 // First set the contents view to initialize view bounds for widget sizing. | 117 // First set the contents view to initialize view bounds for widget sizing. |
118 bubble_widget->SetContentsView(bubble_delegate->GetContentsView()); | 118 bubble_widget->SetContentsView(bubble_delegate->GetContentsView()); |
119 bubble_delegate->InitializeBorderWidget(parent_widget); | 119 bubble_delegate->border_widget_ = CreateBorderWidget(bubble_delegate, parent); |
120 bubble_widget->SetBounds(bubble_delegate->GetBubbleClientBounds()); | |
121 #else | |
122 bubble_widget->SetBounds(bubble_delegate->GetBubbleBounds()); | |
123 #endif | 120 #endif |
124 | 121 |
| 122 bubble_delegate->SizeToContents(); |
125 bubble_widget->AddObserver(bubble_delegate); | 123 bubble_widget->AddObserver(bubble_delegate); |
126 if (parent_widget && parent_widget->GetTopLevelWidget()) | 124 if (parent && parent->GetTopLevelWidget()) |
127 parent_widget->GetTopLevelWidget()->DisableInactiveRendering(); | 125 parent->GetTopLevelWidget()->DisableInactiveRendering(); |
128 return bubble_widget; | 126 return bubble_widget; |
129 } | 127 } |
130 | 128 |
131 View* BubbleDelegateView::GetInitiallyFocusedView() { | 129 View* BubbleDelegateView::GetInitiallyFocusedView() { |
132 return this; | 130 return this; |
133 } | 131 } |
134 | 132 |
135 BubbleDelegateView* BubbleDelegateView::AsBubbleDelegate() { | 133 BubbleDelegateView* BubbleDelegateView::AsBubbleDelegate() { |
136 return this; | 134 return this; |
137 } | 135 } |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
216 bool BubbleDelegateView::AcceleratorPressed( | 214 bool BubbleDelegateView::AcceleratorPressed( |
217 const ui::Accelerator& accelerator) { | 215 const ui::Accelerator& accelerator) { |
218 if (!close_on_esc() || accelerator.key_code() != ui::VKEY_ESCAPE) | 216 if (!close_on_esc() || accelerator.key_code() != ui::VKEY_ESCAPE) |
219 return false; | 217 return false; |
220 if (fade_animation_.get()) | 218 if (fade_animation_.get()) |
221 fade_animation_->Reset(); | 219 fade_animation_->Reset(); |
222 GetWidget()->Close(); | 220 GetWidget()->Close(); |
223 return true; | 221 return true; |
224 } | 222 } |
225 | 223 |
226 void BubbleDelegateView::Init() {} | |
227 | |
228 void BubbleDelegateView::AnimationEnded(const ui::Animation* animation) { | 224 void BubbleDelegateView::AnimationEnded(const ui::Animation* animation) { |
229 DCHECK_EQ(animation, fade_animation_.get()); | 225 if (animation != fade_animation_.get()) |
| 226 return; |
230 bool closed = fade_animation_->GetCurrentValue() == 0; | 227 bool closed = fade_animation_->GetCurrentValue() == 0; |
231 fade_animation_->Reset(); | 228 fade_animation_->Reset(); |
232 if (closed) | 229 if (closed) |
233 GetWidget()->Close(); | 230 GetWidget()->Close(); |
234 } | 231 } |
235 | 232 |
236 void BubbleDelegateView::AnimationProgressed(const ui::Animation* animation) { | 233 void BubbleDelegateView::AnimationProgressed(const ui::Animation* animation) { |
237 DCHECK_EQ(animation, fade_animation_.get()); | 234 if (animation != fade_animation_.get()) |
| 235 return; |
238 DCHECK(fade_animation_->is_animating()); | 236 DCHECK(fade_animation_->is_animating()); |
239 unsigned char opacity = fade_animation_->GetCurrentValue() * 255; | 237 unsigned char opacity = fade_animation_->GetCurrentValue() * 255; |
240 #if defined(OS_WIN) && !defined(USE_AURA) | 238 #if defined(OS_WIN) && !defined(USE_AURA) |
241 // Explicitly set the content Widget's layered style and set transparency via | 239 // Explicitly set the content Widget's layered style and set transparency via |
242 // SetLayeredWindowAttributes. This is done because initializing the Widget as | 240 // SetLayeredWindowAttributes. This is done because initializing the Widget as |
243 // transparent and setting opacity via UpdateLayeredWindow doesn't support | 241 // transparent and setting opacity via UpdateLayeredWindow doesn't support |
244 // hosting child native Windows controls. | 242 // hosting child native Windows controls. |
245 const HWND hwnd = GetWidget()->GetNativeView(); | 243 const HWND hwnd = GetWidget()->GetNativeView(); |
246 const DWORD style = GetWindowLong(hwnd, GWL_EXSTYLE); | 244 const DWORD style = GetWindowLong(hwnd, GWL_EXSTYLE); |
247 if ((opacity == 255) == !!(style & WS_EX_LAYERED)) | 245 if ((opacity == 255) == !!(style & WS_EX_LAYERED)) |
248 SetWindowLong(hwnd, GWL_EXSTYLE, style ^ WS_EX_LAYERED); | 246 SetWindowLong(hwnd, GWL_EXSTYLE, style ^ WS_EX_LAYERED); |
249 SetLayeredWindowAttributes(hwnd, 0, opacity, LWA_ALPHA); | 247 SetLayeredWindowAttributes(hwnd, 0, opacity, LWA_ALPHA); |
250 // Update the border widget's opacity. | 248 // Update the border widget's opacity. |
251 border_widget_->SetOpacity(opacity); | 249 border_widget_->SetOpacity(opacity); |
252 border_widget_->non_client_view()->SchedulePaint(); | 250 border_widget_->non_client_view()->SchedulePaint(); |
253 #endif | 251 #endif |
254 GetWidget()->SetOpacity(opacity); | 252 GetWidget()->SetOpacity(opacity); |
255 SchedulePaint(); | 253 SchedulePaint(); |
256 } | 254 } |
257 | 255 |
| 256 void BubbleDelegateView::Init() {} |
| 257 |
| 258 void BubbleDelegateView::SizeToContents() { |
| 259 #if defined(OS_WIN) && !defined(USE_AURA) |
| 260 border_widget_->SetBounds(GetBubbleBounds()); |
| 261 GetWidget()->SetBounds(GetBubbleClientBounds()); |
| 262 #else |
| 263 GetWidget()->SetBounds(GetBubbleBounds()); |
| 264 #endif |
| 265 } |
| 266 |
258 BubbleFrameView* BubbleDelegateView::GetBubbleFrameView() const { | 267 BubbleFrameView* BubbleDelegateView::GetBubbleFrameView() const { |
259 const Widget* widget = border_widget_ ? border_widget_ : GetWidget(); | 268 const Widget* widget = border_widget_ ? border_widget_ : GetWidget(); |
260 return static_cast<BubbleFrameView*>(widget->non_client_view()->frame_view()); | 269 return static_cast<BubbleFrameView*>(widget->non_client_view()->frame_view()); |
261 } | 270 } |
262 | 271 |
263 gfx::Rect BubbleDelegateView::GetBubbleBounds() { | 272 gfx::Rect BubbleDelegateView::GetBubbleBounds() { |
264 // The argument rect has its origin at the bubble's arrow anchor point; | 273 // The argument rect has its origin at the bubble's arrow anchor point; |
265 // its size is the preferred size of the bubble's client view (this view). | 274 // its size is the preferred size of the bubble's client view (this view). |
266 return GetBubbleFrameView()->GetWindowBoundsForClientBounds( | 275 return GetBubbleFrameView()->GetWindowBoundsForClientBounds( |
267 gfx::Rect(GetAnchorPoint(), GetPreferredSize())); | 276 gfx::Rect(GetAnchorPoint(), GetPreferredSize())); |
268 } | 277 } |
269 | 278 |
270 #if defined(OS_WIN) && !defined(USE_AURA) | 279 #if defined(OS_WIN) && !defined(USE_AURA) |
271 void BubbleDelegateView::InitializeBorderWidget(Widget* parent_widget) { | |
272 border_widget_ = CreateBorderWidget(this, parent_widget); | |
273 border_widget_->SetBounds(GetBubbleBounds()); | |
274 } | |
275 | |
276 gfx::Rect BubbleDelegateView::GetBubbleClientBounds() const { | 280 gfx::Rect BubbleDelegateView::GetBubbleClientBounds() const { |
277 gfx::Rect client_bounds(GetBubbleFrameView()->GetBoundsForClientView()); | 281 gfx::Rect client_bounds(GetBubbleFrameView()->GetBoundsForClientView()); |
278 client_bounds.Offset(border_widget_->GetWindowScreenBounds().origin()); | 282 client_bounds.Offset(border_widget_->GetWindowScreenBounds().origin()); |
279 return client_bounds; | 283 return client_bounds; |
280 } | 284 } |
281 #endif | 285 #endif |
282 | 286 |
283 } // namespace views | 287 } // namespace views |
OLD | NEW |