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
3 // found in the LICENSE file.
5 #include "ui/views/bubble/bubble_delegate.h"
7 #include "ui/base/accessibility/accessible_view_state.h"
8 #include "ui/gfx/animation/slide_animation.h"
9 #include "ui/gfx/color_utils.h"
10 #include "ui/gfx/rect.h"
11 #include "ui/native_theme/native_theme.h"
12 #include "ui/views/bubble/bubble_frame_view.h"
13 #include "ui/views/focus/view_storage.h"
14 #include "ui/views/widget/widget.h"
15 #include "ui/views/widget/widget_observer.h"
18 #include "ui/base/win/shell.h"
21 // The duration of the fade animation in milliseconds.
22 static const int kHideFadeDurationMS = 200;
24 // The defaut margin between the content and the inside border, in pixels.
25 static const int kDefaultMargin = 6;
31 // Create a widget to host the bubble.
32 Widget* CreateBubbleWidget(BubbleDelegateView* bubble) {
33 Widget* bubble_widget = new Widget();
34 Widget::InitParams bubble_params(Widget::InitParams::TYPE_BUBBLE);
35 bubble_params.delegate = bubble;
36 bubble_params.opacity = Widget::InitParams::TRANSLUCENT_WINDOW;
37 bubble_params.accept_events = bubble->accept_events();
38 if (bubble->parent_window())
39 bubble_params.parent = bubble->parent_window();
40 else if (bubble->anchor_widget())
41 bubble_params.parent = bubble->anchor_widget()->GetNativeView();
42 bubble_params.can_activate = bubble->CanActivate();
43 bubble->OnBeforeBubbleWidgetInit(&bubble_params, bubble_widget);
44 bubble_widget->Init(bubble_params);
50 BubbleDelegateView::BubbleDelegateView()
51 : close_on_esc_(true),
52 close_on_deactivate_(true),
53 anchor_view_storage_id_(ViewStorage::GetInstance()->CreateStorageID()),
55 move_with_anchor_(false),
56 arrow_(BubbleBorder::TOP_LEFT),
57 shadow_(BubbleBorder::SMALL_SHADOW),
58 color_explicitly_set_(false),
59 margins_(kDefaultMargin, kDefaultMargin, kDefaultMargin, kDefaultMargin),
60 original_opacity_(255),
61 use_focusless_(false),
63 border_accepts_events_(true),
64 adjust_if_offscreen_(true),
65 parent_window_(NULL) {
66 AddAccelerator(ui::Accelerator(ui::VKEY_ESCAPE, ui::EF_NONE));
67 UpdateColorsFromTheme(GetNativeTheme());
70 BubbleDelegateView::BubbleDelegateView(
72 BubbleBorder::Arrow arrow)
73 : close_on_esc_(true),
74 close_on_deactivate_(true),
75 anchor_view_storage_id_(ViewStorage::GetInstance()->CreateStorageID()),
77 move_with_anchor_(false),
79 shadow_(BubbleBorder::SMALL_SHADOW),
80 color_explicitly_set_(false),
81 margins_(kDefaultMargin, kDefaultMargin, kDefaultMargin, kDefaultMargin),
82 original_opacity_(255),
83 use_focusless_(false),
85 border_accepts_events_(true),
86 adjust_if_offscreen_(true),
87 parent_window_(NULL) {
88 SetAnchorView(anchor_view);
89 AddAccelerator(ui::Accelerator(ui::VKEY_ESCAPE, ui::EF_NONE));
90 UpdateColorsFromTheme(GetNativeTheme());
93 BubbleDelegateView::~BubbleDelegateView() {
94 SetLayoutManager(NULL);
99 Widget* BubbleDelegateView::CreateBubble(BubbleDelegateView* bubble_delegate) {
100 bubble_delegate->Init();
101 // Get the latest anchor widget from the anchor view at bubble creation time.
102 bubble_delegate->SetAnchorView(bubble_delegate->GetAnchorView());
103 Widget* bubble_widget = CreateBubbleWidget(bubble_delegate);
105 #if defined(OS_WIN) && defined(USE_AURA)
106 // If glass is enabled, the bubble is allowed to extend outside the bounds of
107 // the parent frame and let DWM handle compositing. If not, then we don't
108 // want to allow the bubble to extend the frame because it will be clipped.
109 bubble_delegate->set_adjust_if_offscreen(ui::win::IsAeroGlassEnabled());
112 bubble_delegate->SizeToContents();
113 bubble_widget->AddObserver(bubble_delegate);
114 return bubble_widget;
117 BubbleDelegateView* BubbleDelegateView::AsBubbleDelegate() {
121 bool BubbleDelegateView::CanActivate() const {
122 return !use_focusless();
125 bool BubbleDelegateView::ShouldShowCloseButton() const {
129 View* BubbleDelegateView::GetContentsView() {
133 NonClientFrameView* BubbleDelegateView::CreateNonClientFrameView(
135 BubbleFrameView* frame = new BubbleFrameView(margins());
136 BubbleBorder::Arrow adjusted_arrow = arrow();
137 if (base::i18n::IsRTL())
138 adjusted_arrow = BubbleBorder::horizontal_mirror(adjusted_arrow);
139 frame->SetBubbleBorder(scoped_ptr<BubbleBorder>(
140 new BubbleBorder(adjusted_arrow, shadow(), color())));
144 void BubbleDelegateView::GetAccessibleState(ui::AccessibleViewState* state) {
145 state->role = ui::AccessibilityTypes::ROLE_DIALOG;
148 void BubbleDelegateView::OnWidgetDestroying(Widget* widget) {
149 if (anchor_widget() == widget)
153 void BubbleDelegateView::OnWidgetVisibilityChanging(Widget* widget,
156 // On Windows we need to handle this before the bubble is visible or hidden.
157 // Please see the comment on the OnWidgetVisibilityChanging function. On
158 // other platforms it is fine to handle it after the bubble is shown/hidden.
159 HandleVisibilityChanged(widget, visible);
163 void BubbleDelegateView::OnWidgetVisibilityChanged(Widget* widget,
166 HandleVisibilityChanged(widget, visible);
170 void BubbleDelegateView::OnWidgetActivationChanged(Widget* widget,
172 if (close_on_deactivate() && widget == GetWidget() && !active)
173 GetWidget()->Close();
176 void BubbleDelegateView::OnWidgetBoundsChanged(Widget* widget,
177 const gfx::Rect& new_bounds) {
178 if (anchor_widget() == widget) {
179 if (move_with_anchor())
182 GetWidget()->Close();
186 View* BubbleDelegateView::GetAnchorView() const {
187 return ViewStorage::GetInstance()->RetrieveView(anchor_view_storage_id_);
190 gfx::Rect BubbleDelegateView::GetAnchorRect() {
191 if (!GetAnchorView())
194 anchor_rect_ = GetAnchorView()->GetBoundsInScreen();
195 anchor_rect_.Inset(anchor_view_insets_);
199 void BubbleDelegateView::OnBeforeBubbleWidgetInit(Widget::InitParams* params,
200 Widget* widget) const {
203 void BubbleDelegateView::StartFade(bool fade_in) {
204 #if defined(USE_AURA)
205 // Use AURA's window layer animation instead of fading. This ensures that
206 // hosts which rely on the layer animation callbacks to close the window
211 GetWidget()->Close();
213 fade_animation_.reset(new gfx::SlideAnimation(this));
214 fade_animation_->SetSlideDuration(GetFadeDuration());
215 fade_animation_->Reset(fade_in ? 0.0 : 1.0);
217 original_opacity_ = 0;
218 GetWidget()->SetOpacity(original_opacity_);
220 fade_animation_->Show();
222 original_opacity_ = 255;
223 fade_animation_->Hide();
228 void BubbleDelegateView::ResetFade() {
229 fade_animation_.reset();
230 GetWidget()->SetOpacity(original_opacity_);
233 void BubbleDelegateView::SetAlignment(BubbleBorder::BubbleAlignment alignment) {
234 GetBubbleFrameView()->bubble_border()->set_alignment(alignment);
238 void BubbleDelegateView::SetArrowPaintType(
239 BubbleBorder::ArrowPaintType paint_type) {
240 GetBubbleFrameView()->bubble_border()->set_paint_arrow(paint_type);
244 void BubbleDelegateView::OnAnchorBoundsChanged() {
248 bool BubbleDelegateView::AcceleratorPressed(
249 const ui::Accelerator& accelerator) {
250 if (!close_on_esc() || accelerator.key_code() != ui::VKEY_ESCAPE)
252 if (fade_animation_.get())
253 fade_animation_->Reset();
254 GetWidget()->Close();
258 void BubbleDelegateView::OnNativeThemeChanged(const ui::NativeTheme* theme) {
259 UpdateColorsFromTheme(theme);
262 void BubbleDelegateView::AnimationEnded(const gfx::Animation* animation) {
263 if (animation != fade_animation_.get())
265 bool closed = fade_animation_->GetCurrentValue() == 0;
266 fade_animation_->Reset();
268 GetWidget()->Close();
271 void BubbleDelegateView::AnimationProgressed(const gfx::Animation* animation) {
272 if (animation != fade_animation_.get())
274 DCHECK(fade_animation_->is_animating());
275 GetWidget()->SetOpacity(fade_animation_->GetCurrentValue() * 255);
278 void BubbleDelegateView::Init() {}
280 void BubbleDelegateView::SetAnchorView(View* anchor_view) {
281 // When the anchor view gets set the associated anchor widget might
283 if (!anchor_view || anchor_widget() != anchor_view->GetWidget()) {
284 if (anchor_widget()) {
285 anchor_widget_->RemoveObserver(this);
286 anchor_widget_ = NULL;
289 anchor_widget_ = anchor_view->GetWidget();
291 anchor_widget_->AddObserver(this);
295 // Remove the old storage item and set the new (if there is one).
296 ViewStorage* view_storage = ViewStorage::GetInstance();
297 if (view_storage->RetrieveView(anchor_view_storage_id_))
298 view_storage->RemoveView(anchor_view_storage_id_);
300 view_storage->StoreView(anchor_view_storage_id_, anchor_view);
303 OnAnchorBoundsChanged();
306 void BubbleDelegateView::SetAnchorRect(const gfx::Rect& rect) {
309 OnAnchorBoundsChanged();
312 void BubbleDelegateView::SizeToContents() {
313 GetWidget()->SetBounds(GetBubbleBounds());
316 BubbleFrameView* BubbleDelegateView::GetBubbleFrameView() const {
317 const NonClientView* view =
318 GetWidget() ? GetWidget()->non_client_view() : NULL;
319 return view ? static_cast<BubbleFrameView*>(view->frame_view()) : NULL;
322 gfx::Rect BubbleDelegateView::GetBubbleBounds() {
323 // The argument rect has its origin at the bubble's arrow anchor point;
324 // its size is the preferred size of the bubble's client view (this view).
325 return GetBubbleFrameView()->GetUpdatedWindowBounds(GetAnchorRect(),
326 GetPreferredSize(), adjust_if_offscreen_);
329 int BubbleDelegateView::GetFadeDuration() {
330 return kHideFadeDurationMS;
333 void BubbleDelegateView::UpdateColorsFromTheme(const ui::NativeTheme* theme) {
334 if (!color_explicitly_set_) {
335 color_ = GetNativeTheme()->GetSystemColor(
336 ui::NativeTheme::kColorId_WindowBackground);
338 set_background(Background::CreateSolidBackground(color()));
339 BubbleFrameView* frame_view = GetBubbleFrameView();
341 frame_view->bubble_border()->set_background_color(color());
344 void BubbleDelegateView::HandleVisibilityChanged(Widget* widget, bool visible) {
345 if (widget == GetWidget() && visible && anchor_widget() &&
346 anchor_widget()->GetTopLevelWidget()) {
347 anchor_widget()->GetTopLevelWidget()->DisableInactiveRendering();