Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / ui / views / bubble / bubble_delegate.cc
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.
4
5 #include "ui/views/bubble/bubble_delegate.h"
6
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"
16
17 #if defined(OS_WIN)
18 #include "ui/base/win/shell.h"
19 #endif
20
21 // The duration of the fade animation in milliseconds.
22 static const int kHideFadeDurationMS = 200;
23
24 // The defaut margin between the content and the inside border, in pixels.
25 static const int kDefaultMargin = 6;
26
27 namespace views {
28
29 namespace {
30
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);
45   return bubble_widget;
46 }
47
48 }  // namespace
49
50 BubbleDelegateView::BubbleDelegateView()
51     : close_on_esc_(true),
52       close_on_deactivate_(true),
53       anchor_view_storage_id_(ViewStorage::GetInstance()->CreateStorageID()),
54       anchor_widget_(NULL),
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),
62       accept_events_(true),
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());
68 }
69
70 BubbleDelegateView::BubbleDelegateView(
71     View* anchor_view,
72     BubbleBorder::Arrow arrow)
73     : close_on_esc_(true),
74       close_on_deactivate_(true),
75       anchor_view_storage_id_(ViewStorage::GetInstance()->CreateStorageID()),
76       anchor_widget_(NULL),
77       move_with_anchor_(false),
78       arrow_(arrow),
79       shadow_(BubbleBorder::SMALL_SHADOW),
80       color_explicitly_set_(false),
81       margins_(kDefaultMargin, kDefaultMargin, kDefaultMargin, kDefaultMargin),
82       original_opacity_(255),
83       use_focusless_(false),
84       accept_events_(true),
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());
91 }
92
93 BubbleDelegateView::~BubbleDelegateView() {
94   SetLayoutManager(NULL);
95   SetAnchorView(NULL);
96 }
97
98 // static
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);
104
105 #if defined(OS_WIN)
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());
110 #endif
111
112   bubble_delegate->SizeToContents();
113   bubble_widget->AddObserver(bubble_delegate);
114   return bubble_widget;
115 }
116
117 BubbleDelegateView* BubbleDelegateView::AsBubbleDelegate() {
118   return this;
119 }
120
121 bool BubbleDelegateView::CanActivate() const {
122   return !use_focusless();
123 }
124
125 bool BubbleDelegateView::ShouldShowCloseButton() const {
126   return false;
127 }
128
129 View* BubbleDelegateView::GetContentsView() {
130   return this;
131 }
132
133 NonClientFrameView* BubbleDelegateView::CreateNonClientFrameView(
134     Widget* widget) {
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())));
141   return frame;
142 }
143
144 void BubbleDelegateView::GetAccessibleState(ui::AccessibleViewState* state) {
145   state->role = ui::AccessibilityTypes::ROLE_DIALOG;
146 }
147
148 void BubbleDelegateView::OnWidgetDestroying(Widget* widget) {
149   if (anchor_widget() == widget)
150     SetAnchorView(NULL);
151 }
152
153 void BubbleDelegateView::OnWidgetVisibilityChanging(Widget* widget,
154                                                     bool visible) {
155 #if defined(OS_WIN)
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);
160 #endif
161 }
162
163 void BubbleDelegateView::OnWidgetVisibilityChanged(Widget* widget,
164                                                    bool visible) {
165 #if !defined(OS_WIN)
166   HandleVisibilityChanged(widget, visible);
167 #endif
168 }
169
170 void BubbleDelegateView::OnWidgetActivationChanged(Widget* widget,
171                                                    bool active) {
172   if (close_on_deactivate() && widget == GetWidget() && !active)
173     GetWidget()->Close();
174 }
175
176 void BubbleDelegateView::OnWidgetBoundsChanged(Widget* widget,
177                                                const gfx::Rect& new_bounds) {
178   if (anchor_widget() == widget) {
179     if (move_with_anchor())
180       SizeToContents();
181     else
182       GetWidget()->Close();
183   }
184 }
185
186 View* BubbleDelegateView::GetAnchorView() const {
187   return ViewStorage::GetInstance()->RetrieveView(anchor_view_storage_id_);
188 }
189
190 gfx::Rect BubbleDelegateView::GetAnchorRect() {
191   if (!GetAnchorView())
192     return anchor_rect_;
193
194   anchor_rect_ = GetAnchorView()->GetBoundsInScreen();
195   anchor_rect_.Inset(anchor_view_insets_);
196   return anchor_rect_;
197 }
198
199 void BubbleDelegateView::OnBeforeBubbleWidgetInit(Widget::InitParams* params,
200                                                   Widget* widget) const {
201 }
202
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
207   // work correctly.
208   if (fade_in)
209     GetWidget()->Show();
210   else
211     GetWidget()->Close();
212 #else
213   fade_animation_.reset(new gfx::SlideAnimation(this));
214   fade_animation_->SetSlideDuration(GetFadeDuration());
215   fade_animation_->Reset(fade_in ? 0.0 : 1.0);
216   if (fade_in) {
217     original_opacity_ = 0;
218     GetWidget()->SetOpacity(original_opacity_);
219     GetWidget()->Show();
220     fade_animation_->Show();
221   } else {
222     original_opacity_ = 255;
223     fade_animation_->Hide();
224   }
225 #endif
226 }
227
228 void BubbleDelegateView::ResetFade() {
229   fade_animation_.reset();
230   GetWidget()->SetOpacity(original_opacity_);
231 }
232
233 void BubbleDelegateView::SetAlignment(BubbleBorder::BubbleAlignment alignment) {
234   GetBubbleFrameView()->bubble_border()->set_alignment(alignment);
235   SizeToContents();
236 }
237
238 void BubbleDelegateView::SetArrowPaintType(
239     BubbleBorder::ArrowPaintType paint_type) {
240   GetBubbleFrameView()->bubble_border()->set_paint_arrow(paint_type);
241   SizeToContents();
242 }
243
244 void BubbleDelegateView::OnAnchorBoundsChanged() {
245   SizeToContents();
246 }
247
248 bool BubbleDelegateView::AcceleratorPressed(
249     const ui::Accelerator& accelerator) {
250   if (!close_on_esc() || accelerator.key_code() != ui::VKEY_ESCAPE)
251     return false;
252   if (fade_animation_.get())
253     fade_animation_->Reset();
254   GetWidget()->Close();
255   return true;
256 }
257
258 void BubbleDelegateView::OnNativeThemeChanged(const ui::NativeTheme* theme) {
259   UpdateColorsFromTheme(theme);
260 }
261
262 void BubbleDelegateView::AnimationEnded(const gfx::Animation* animation) {
263   if (animation != fade_animation_.get())
264     return;
265   bool closed = fade_animation_->GetCurrentValue() == 0;
266   fade_animation_->Reset();
267   if (closed)
268     GetWidget()->Close();
269 }
270
271 void BubbleDelegateView::AnimationProgressed(const gfx::Animation* animation) {
272   if (animation != fade_animation_.get())
273     return;
274   DCHECK(fade_animation_->is_animating());
275   GetWidget()->SetOpacity(fade_animation_->GetCurrentValue() * 255);
276 }
277
278 void BubbleDelegateView::Init() {}
279
280 void BubbleDelegateView::SetAnchorView(View* anchor_view) {
281   // When the anchor view gets set the associated anchor widget might
282   // change as well.
283   if (!anchor_view || anchor_widget() != anchor_view->GetWidget()) {
284     if (anchor_widget()) {
285       anchor_widget_->RemoveObserver(this);
286       anchor_widget_ = NULL;
287     }
288     if (anchor_view) {
289       anchor_widget_ = anchor_view->GetWidget();
290       if (anchor_widget_)
291         anchor_widget_->AddObserver(this);
292     }
293   }
294
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_);
299   if (anchor_view)
300     view_storage->StoreView(anchor_view_storage_id_, anchor_view);
301
302   if (GetWidget())
303     OnAnchorBoundsChanged();
304 }
305
306 void BubbleDelegateView::SetAnchorRect(const gfx::Rect& rect) {
307   anchor_rect_ = rect;
308   if (GetWidget())
309     OnAnchorBoundsChanged();
310 }
311
312 void BubbleDelegateView::SizeToContents() {
313   GetWidget()->SetBounds(GetBubbleBounds());
314 }
315
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;
320 }
321
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_);
327 }
328
329 int BubbleDelegateView::GetFadeDuration() {
330   return kHideFadeDurationMS;
331 }
332
333 void BubbleDelegateView::UpdateColorsFromTheme(const ui::NativeTheme* theme) {
334   if (!color_explicitly_set_) {
335     color_ = GetNativeTheme()->GetSystemColor(
336         ui::NativeTheme::kColorId_WindowBackground);
337   }
338   set_background(Background::CreateSolidBackground(color()));
339   BubbleFrameView* frame_view = GetBubbleFrameView();
340   if (frame_view)
341     frame_view->bubble_border()->set_background_color(color());
342 }
343
344 void BubbleDelegateView::HandleVisibilityChanged(Widget* widget, bool visible) {
345   if (widget == GetWidget() && anchor_widget() &&
346       anchor_widget()->GetTopLevelWidget()) {
347     if (visible)
348       anchor_widget()->GetTopLevelWidget()->DisableInactiveRendering();
349     else
350       anchor_widget()->GetTopLevelWidget()->EnableInactiveRendering();
351   }
352 }
353
354 }  // namespace views