Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "ui/views/bubble/bubble_delegate.h" | 5 #include "ui/views/bubble/bubble_delegate.h" |
| 6 | 6 |
| 7 #include "ui/gfx/animation/slide_animation.h" | 7 #include "ui/gfx/animation/slide_animation.h" |
| 8 #include "ui/gfx/color_utils.h" | 8 #include "ui/gfx/color_utils.h" |
| 9 #include "ui/gfx/rect.h" | 9 #include "ui/gfx/rect.h" |
| 10 #include "ui/native_theme/native_theme.h" | 10 #include "ui/native_theme/native_theme.h" |
| 11 #include "ui/views/bubble/bubble_frame_view.h" | 11 #include "ui/views/bubble/bubble_frame_view.h" |
| 12 #include "ui/views/focus/view_storage.h" | |
| 12 #include "ui/views/widget/widget.h" | 13 #include "ui/views/widget/widget.h" |
| 13 #include "ui/views/widget/widget_observer.h" | 14 #include "ui/views/widget/widget_observer.h" |
| 14 | 15 |
| 15 #if defined(OS_WIN) | 16 #if defined(OS_WIN) |
| 16 #include "ui/base/win/shell.h" | 17 #include "ui/base/win/shell.h" |
| 17 #endif | 18 #endif |
| 18 | 19 |
| 19 // The duration of the fade animation in milliseconds. | 20 // The duration of the fade animation in milliseconds. |
| 20 static const int kHideFadeDurationMS = 200; | 21 static const int kHideFadeDurationMS = 200; |
| 21 | 22 |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 121 return border_widget; | 122 return border_widget; |
| 122 } | 123 } |
| 123 | 124 |
| 124 } // namespace | 125 } // namespace |
| 125 | 126 |
| 126 #endif | 127 #endif |
| 127 | 128 |
| 128 BubbleDelegateView::BubbleDelegateView() | 129 BubbleDelegateView::BubbleDelegateView() |
| 129 : close_on_esc_(true), | 130 : close_on_esc_(true), |
| 130 close_on_deactivate_(true), | 131 close_on_deactivate_(true), |
| 131 anchor_view_(NULL), | 132 anchor_view_storage_id_(ViewStorage::GetInstance()->CreateStorageID()), |
| 132 anchor_widget_(NULL), | 133 anchor_widget_(NULL), |
| 133 move_with_anchor_(false), | 134 move_with_anchor_(false), |
| 134 arrow_(BubbleBorder::TOP_LEFT), | 135 arrow_(BubbleBorder::TOP_LEFT), |
| 135 shadow_(BubbleBorder::SMALL_SHADOW), | 136 shadow_(BubbleBorder::SMALL_SHADOW), |
| 136 color_explicitly_set_(false), | 137 color_explicitly_set_(false), |
| 137 margins_(kDefaultMargin, kDefaultMargin, kDefaultMargin, kDefaultMargin), | 138 margins_(kDefaultMargin, kDefaultMargin, kDefaultMargin, kDefaultMargin), |
| 138 original_opacity_(255), | 139 original_opacity_(255), |
| 139 border_widget_(NULL), | 140 border_widget_(NULL), |
| 140 use_focusless_(false), | 141 use_focusless_(false), |
| 141 accept_events_(true), | 142 accept_events_(true), |
| 142 border_accepts_events_(true), | 143 border_accepts_events_(true), |
| 143 adjust_if_offscreen_(true), | 144 adjust_if_offscreen_(true), |
| 144 parent_window_(NULL) { | 145 parent_window_(NULL) { |
| 145 AddAccelerator(ui::Accelerator(ui::VKEY_ESCAPE, ui::EF_NONE)); | 146 AddAccelerator(ui::Accelerator(ui::VKEY_ESCAPE, ui::EF_NONE)); |
| 146 UpdateColorsFromTheme(GetNativeTheme()); | 147 UpdateColorsFromTheme(GetNativeTheme()); |
| 147 } | 148 } |
| 148 | 149 |
| 149 BubbleDelegateView::BubbleDelegateView( | 150 BubbleDelegateView::BubbleDelegateView( |
| 150 View* anchor_view, | 151 View* anchor_view, |
| 151 BubbleBorder::Arrow arrow) | 152 BubbleBorder::Arrow arrow) |
| 152 : close_on_esc_(true), | 153 : close_on_esc_(true), |
| 153 close_on_deactivate_(true), | 154 close_on_deactivate_(true), |
| 154 anchor_view_(anchor_view), | 155 anchor_view_storage_id_(ViewStorage::GetInstance()->CreateStorageID()), |
| 155 anchor_widget_(NULL), | 156 anchor_widget_(NULL), |
| 156 move_with_anchor_(false), | 157 move_with_anchor_(false), |
| 157 arrow_(arrow), | 158 arrow_(arrow), |
| 158 shadow_(BubbleBorder::SMALL_SHADOW), | 159 shadow_(BubbleBorder::SMALL_SHADOW), |
| 159 color_explicitly_set_(false), | 160 color_explicitly_set_(false), |
| 160 margins_(kDefaultMargin, kDefaultMargin, kDefaultMargin, kDefaultMargin), | 161 margins_(kDefaultMargin, kDefaultMargin, kDefaultMargin, kDefaultMargin), |
| 161 original_opacity_(255), | 162 original_opacity_(255), |
| 162 border_widget_(NULL), | 163 border_widget_(NULL), |
| 163 use_focusless_(false), | 164 use_focusless_(false), |
| 164 accept_events_(true), | 165 accept_events_(true), |
| 165 border_accepts_events_(true), | 166 border_accepts_events_(true), |
| 166 adjust_if_offscreen_(true), | 167 adjust_if_offscreen_(true), |
| 167 parent_window_(NULL) { | 168 parent_window_(NULL) { |
| 169 // We set the view into the storage here. CreateBubble will then do the rest | |
|
msw
2013/09/27 23:45:56
Just call SetAnchorView here. Even if the anchor's
Mr4D (OOO till 08-26)
2013/09/28 00:01:18
Done.
| |
| 170 // when calling SetAnchorView. | |
| 171 ViewStorage::GetInstance()->StoreView(anchor_view_storage_id_, anchor_view); | |
| 168 AddAccelerator(ui::Accelerator(ui::VKEY_ESCAPE, ui::EF_NONE)); | 172 AddAccelerator(ui::Accelerator(ui::VKEY_ESCAPE, ui::EF_NONE)); |
| 169 UpdateColorsFromTheme(GetNativeTheme()); | 173 UpdateColorsFromTheme(GetNativeTheme()); |
| 170 } | 174 } |
| 171 | 175 |
| 172 BubbleDelegateView::~BubbleDelegateView() { | 176 BubbleDelegateView::~BubbleDelegateView() { |
| 173 if (anchor_widget() != NULL) | 177 SetAnchorView(NULL); |
| 174 anchor_widget()->RemoveObserver(this); | |
| 175 anchor_widget_ = NULL; | |
| 176 anchor_view_ = NULL; | |
| 177 } | 178 } |
| 178 | 179 |
| 179 // static | 180 // static |
| 180 Widget* BubbleDelegateView::CreateBubble(BubbleDelegateView* bubble_delegate) { | 181 Widget* BubbleDelegateView::CreateBubble(BubbleDelegateView* bubble_delegate) { |
| 181 bubble_delegate->Init(); | 182 bubble_delegate->Init(); |
| 182 // Determine the anchor widget from the anchor view at bubble creation time. | 183 bubble_delegate->SetAnchorView(bubble_delegate->GetAnchorView()); |
| 183 bubble_delegate->anchor_widget_ = bubble_delegate->anchor_view() ? | |
| 184 bubble_delegate->anchor_view()->GetWidget() : NULL; | |
| 185 if (bubble_delegate->anchor_widget()) | |
| 186 bubble_delegate->anchor_widget()->AddObserver(bubble_delegate); | |
| 187 | |
| 188 Widget* bubble_widget = CreateBubbleWidget(bubble_delegate); | 184 Widget* bubble_widget = CreateBubbleWidget(bubble_delegate); |
| 189 | 185 |
| 190 #if defined(OS_WIN) | 186 #if defined(OS_WIN) |
| 191 #if defined(USE_AURA) | 187 #if defined(USE_AURA) |
| 192 // If glass is enabled, the bubble is allowed to extend outside the bounds of | 188 // If glass is enabled, the bubble is allowed to extend outside the bounds of |
| 193 // the parent frame and let DWM handle compositing. If not, then we don't | 189 // the parent frame and let DWM handle compositing. If not, then we don't |
| 194 // want to allow the bubble to extend the frame because it will be clipped. | 190 // want to allow the bubble to extend the frame because it will be clipped. |
| 195 bubble_delegate->set_adjust_if_offscreen(ui::win::IsAeroGlassEnabled()); | 191 bubble_delegate->set_adjust_if_offscreen(ui::win::IsAeroGlassEnabled()); |
| 196 #else | 192 #else |
| 197 // First set the contents view to initialize view bounds for widget sizing. | 193 // First set the contents view to initialize view bounds for widget sizing. |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 224 NonClientFrameView* BubbleDelegateView::CreateNonClientFrameView( | 220 NonClientFrameView* BubbleDelegateView::CreateNonClientFrameView( |
| 225 Widget* widget) { | 221 Widget* widget) { |
| 226 BubbleFrameView* frame = new BubbleFrameView(margins()); | 222 BubbleFrameView* frame = new BubbleFrameView(margins()); |
| 227 const BubbleBorder::Arrow adjusted_arrow = base::i18n::IsRTL() ? | 223 const BubbleBorder::Arrow adjusted_arrow = base::i18n::IsRTL() ? |
| 228 BubbleBorder::horizontal_mirror(arrow()) : arrow(); | 224 BubbleBorder::horizontal_mirror(arrow()) : arrow(); |
| 229 frame->SetBubbleBorder(new BubbleBorder(adjusted_arrow, shadow(), color())); | 225 frame->SetBubbleBorder(new BubbleBorder(adjusted_arrow, shadow(), color())); |
| 230 return frame; | 226 return frame; |
| 231 } | 227 } |
| 232 | 228 |
| 233 void BubbleDelegateView::OnWidgetDestroying(Widget* widget) { | 229 void BubbleDelegateView::OnWidgetDestroying(Widget* widget) { |
| 234 if (anchor_widget() == widget) { | 230 if (anchor_widget() == widget) |
| 235 anchor_widget_->RemoveObserver(this); | 231 SetAnchorView(NULL); |
| 236 anchor_view_ = NULL; | |
| 237 anchor_widget_ = NULL; | |
| 238 } | |
| 239 } | 232 } |
| 240 | 233 |
| 241 void BubbleDelegateView::OnWidgetVisibilityChanging(Widget* widget, | 234 void BubbleDelegateView::OnWidgetVisibilityChanging(Widget* widget, |
| 242 bool visible) { | 235 bool visible) { |
| 243 #if defined(OS_WIN) | 236 #if defined(OS_WIN) |
| 244 // On Windows we need to handle this before the bubble is visible or hidden. | 237 // On Windows we need to handle this before the bubble is visible or hidden. |
| 245 // Please see the comment on the OnWidgetVisibilityChanging function. On | 238 // Please see the comment on the OnWidgetVisibilityChanging function. On |
| 246 // other platforms it is fine to handle it after the bubble is shown/hidden. | 239 // other platforms it is fine to handle it after the bubble is shown/hidden. |
| 247 HandleVisibilityChanged(widget, visible); | 240 HandleVisibilityChanged(widget, visible); |
| 248 #endif | 241 #endif |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 260 if (close_on_deactivate() && widget == GetWidget() && !active) | 253 if (close_on_deactivate() && widget == GetWidget() && !active) |
| 261 GetWidget()->Close(); | 254 GetWidget()->Close(); |
| 262 } | 255 } |
| 263 | 256 |
| 264 void BubbleDelegateView::OnWidgetBoundsChanged(Widget* widget, | 257 void BubbleDelegateView::OnWidgetBoundsChanged(Widget* widget, |
| 265 const gfx::Rect& new_bounds) { | 258 const gfx::Rect& new_bounds) { |
| 266 if (move_with_anchor() && anchor_widget() == widget) | 259 if (move_with_anchor() && anchor_widget() == widget) |
| 267 SizeToContents(); | 260 SizeToContents(); |
| 268 } | 261 } |
| 269 | 262 |
| 263 View* BubbleDelegateView::GetAnchorView() const { | |
| 264 return ViewStorage::GetInstance()->RetrieveView(anchor_view_storage_id_); | |
| 265 } | |
| 266 | |
| 267 void BubbleDelegateView::SetAnchorView(View* anchor_view) { | |
| 268 // When the anchor view gets set the associated anchor widget might | |
| 269 // change as well. | |
| 270 if (!anchor_view || anchor_widget() != anchor_view->GetWidget()) { | |
| 271 if (anchor_widget()) { | |
| 272 anchor_widget_->RemoveObserver(this); | |
| 273 anchor_widget_ = NULL; | |
| 274 } | |
| 275 if (anchor_view) { | |
| 276 anchor_widget_ = anchor_view->GetWidget(); | |
| 277 if (anchor_widget_) | |
| 278 anchor_widget_->AddObserver(this); | |
| 279 } | |
| 280 } | |
| 281 | |
| 282 // Remove the old storage item and set the new (if there is one). | |
| 283 ViewStorage* view_storage = ViewStorage::GetInstance(); | |
| 284 if (view_storage->RetrieveView(anchor_view_storage_id_)) | |
| 285 view_storage->RemoveView(anchor_view_storage_id_); | |
| 286 | |
| 287 if (!anchor_view) | |
|
msw
2013/09/27 23:45:56
nit: invert this check and call StoreView in its s
Mr4D (OOO till 08-26)
2013/09/28 00:01:18
Done.
| |
| 288 return; | |
| 289 | |
| 290 view_storage->StoreView(anchor_view_storage_id_, anchor_view); | |
| 291 } | |
| 292 | |
| 270 gfx::Rect BubbleDelegateView::GetAnchorRect() { | 293 gfx::Rect BubbleDelegateView::GetAnchorRect() { |
| 271 if (!anchor_view()) | 294 if (!GetAnchorView()) |
| 272 return anchor_rect_; | 295 return anchor_rect_; |
| 273 gfx::Rect anchor_bounds = anchor_view()->GetBoundsInScreen(); | 296 |
| 274 anchor_bounds.Inset(anchor_view_insets_); | 297 anchor_rect_ = GetAnchorView()->GetBoundsInScreen(); |
| 275 return anchor_bounds; | 298 anchor_rect_.Inset(anchor_view_insets_); |
| 299 return anchor_rect_; | |
| 276 } | 300 } |
| 277 | 301 |
| 278 void BubbleDelegateView::StartFade(bool fade_in) { | 302 void BubbleDelegateView::StartFade(bool fade_in) { |
| 279 #if defined(USE_AURA) | 303 #if defined(USE_AURA) |
| 280 // Use AURA's window layer animation instead of fading. This ensures that | 304 // Use AURA's window layer animation instead of fading. This ensures that |
| 281 // hosts which rely on the layer animation callbacks to close the window | 305 // hosts which rely on the layer animation callbacks to close the window |
| 282 // work correctly. | 306 // work correctly. |
| 283 if (fade_in) | 307 if (fade_in) |
| 284 GetWidget()->Show(); | 308 GetWidget()->Show(); |
| 285 else | 309 else |
| (...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 431 border_widget_->ShowInactive(); | 455 border_widget_->ShowInactive(); |
| 432 if (anchor_widget() && anchor_widget()->GetTopLevelWidget()) | 456 if (anchor_widget() && anchor_widget()->GetTopLevelWidget()) |
| 433 anchor_widget()->GetTopLevelWidget()->DisableInactiveRendering(); | 457 anchor_widget()->GetTopLevelWidget()->DisableInactiveRendering(); |
| 434 } else { | 458 } else { |
| 435 if (border_widget_) | 459 if (border_widget_) |
| 436 border_widget_->Hide(); | 460 border_widget_->Hide(); |
| 437 } | 461 } |
| 438 } | 462 } |
| 439 | 463 |
| 440 } // namespace views | 464 } // namespace views |
| OLD | NEW |