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/accessibility/ax_view_state.h"
8 #include "ui/base/resource/resource_bundle.h"
9 #include "ui/gfx/color_utils.h"
10 #include "ui/gfx/geometry/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 defaut margin between the content and the inside border, in pixels.
22 static const int kDefaultMargin
= 6;
28 // Create a widget to host the bubble.
29 Widget
* CreateBubbleWidget(BubbleDelegateView
* bubble
) {
30 Widget
* bubble_widget
= new Widget();
31 Widget::InitParams
bubble_params(Widget::InitParams::TYPE_BUBBLE
);
32 bubble_params
.delegate
= bubble
;
33 bubble_params
.opacity
= Widget::InitParams::TRANSLUCENT_WINDOW
;
34 bubble_params
.accept_events
= bubble
->accept_events();
35 if (bubble
->parent_window())
36 bubble_params
.parent
= bubble
->parent_window();
37 else if (bubble
->anchor_widget())
38 bubble_params
.parent
= bubble
->anchor_widget()->GetNativeView();
39 bubble_params
.activatable
= bubble
->CanActivate() ?
40 Widget::InitParams::ACTIVATABLE_YES
: Widget::InitParams::ACTIVATABLE_NO
;
41 bubble
->OnBeforeBubbleWidgetInit(&bubble_params
, bubble_widget
);
42 bubble_widget
->Init(bubble_params
);
43 if (bubble_params
.parent
)
44 bubble_widget
->StackAbove(bubble_params
.parent
);
51 const char BubbleDelegateView::kViewClassName
[] = "BubbleDelegateView";
53 BubbleDelegateView::BubbleDelegateView()
54 : close_on_esc_(true),
55 close_on_deactivate_(true),
56 anchor_view_storage_id_(ViewStorage::GetInstance()->CreateStorageID()),
58 arrow_(BubbleBorder::TOP_LEFT
),
59 shadow_(BubbleBorder::SMALL_SHADOW
),
60 color_explicitly_set_(false),
61 margins_(kDefaultMargin
, kDefaultMargin
, kDefaultMargin
, kDefaultMargin
),
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()),
78 shadow_(BubbleBorder::SMALL_SHADOW
),
79 color_explicitly_set_(false),
80 margins_(kDefaultMargin
, kDefaultMargin
, kDefaultMargin
, kDefaultMargin
),
82 border_accepts_events_(true),
83 adjust_if_offscreen_(true),
84 parent_window_(NULL
) {
85 SetAnchorView(anchor_view
);
86 AddAccelerator(ui::Accelerator(ui::VKEY_ESCAPE
, ui::EF_NONE
));
87 UpdateColorsFromTheme(GetNativeTheme());
90 BubbleDelegateView::~BubbleDelegateView() {
92 GetWidget()->RemoveObserver(this);
93 SetLayoutManager(NULL
);
98 Widget
* BubbleDelegateView::CreateBubble(BubbleDelegateView
* bubble_delegate
) {
99 bubble_delegate
->Init();
100 // Get the latest anchor widget from the anchor view at bubble creation time.
101 bubble_delegate
->SetAnchorView(bubble_delegate
->GetAnchorView());
102 Widget
* bubble_widget
= CreateBubbleWidget(bubble_delegate
);
105 // If glass is enabled, the bubble is allowed to extend outside the bounds of
106 // the parent frame and let DWM handle compositing. If not, then we don't
107 // want to allow the bubble to extend the frame because it will be clipped.
108 bubble_delegate
->set_adjust_if_offscreen(ui::win::IsAeroGlassEnabled());
109 #elif (defined(OS_LINUX) && !defined(OS_CHROMEOS)) || defined(OS_MACOSX)
110 // Linux clips bubble windows that extend outside their parent window bounds.
111 // Mac never adjusts.
112 bubble_delegate
->set_adjust_if_offscreen(false);
115 bubble_delegate
->SizeToContents();
116 bubble_widget
->AddObserver(bubble_delegate
);
117 return bubble_widget
;
120 BubbleDelegateView
* BubbleDelegateView::AsBubbleDelegate() {
124 bool BubbleDelegateView::ShouldShowCloseButton() const {
128 View
* BubbleDelegateView::GetContentsView() {
132 NonClientFrameView
* BubbleDelegateView::CreateNonClientFrameView(
134 BubbleFrameView
* frame
= new BubbleFrameView(margins());
135 // Note: In CreateBubble, the call to SizeToContents() will cause
136 // the relayout that this call requires.
137 frame
->SetTitleFontList(GetTitleFontList());
138 BubbleBorder::Arrow adjusted_arrow
= arrow();
139 if (base::i18n::IsRTL())
140 adjusted_arrow
= BubbleBorder::horizontal_mirror(adjusted_arrow
);
141 frame
->SetBubbleBorder(scoped_ptr
<BubbleBorder
>(
142 new BubbleBorder(adjusted_arrow
, shadow(), color())));
146 void BubbleDelegateView::GetAccessibleState(ui::AXViewState
* state
) {
147 state
->role
= ui::AX_ROLE_DIALOG
;
150 const char* BubbleDelegateView::GetClassName() const {
151 return kViewClassName
;
154 void BubbleDelegateView::OnWidgetDestroying(Widget
* widget
) {
155 if (anchor_widget() == widget
)
159 void BubbleDelegateView::OnWidgetVisibilityChanging(Widget
* widget
,
162 // On Windows we need to handle this before the bubble is visible or hidden.
163 // Please see the comment on the OnWidgetVisibilityChanging function. On
164 // other platforms it is fine to handle it after the bubble is shown/hidden.
165 HandleVisibilityChanged(widget
, visible
);
169 void BubbleDelegateView::OnWidgetVisibilityChanged(Widget
* widget
,
172 HandleVisibilityChanged(widget
, visible
);
176 void BubbleDelegateView::OnWidgetActivationChanged(Widget
* widget
,
178 if (close_on_deactivate() && widget
== GetWidget() && !active
)
179 GetWidget()->Close();
182 void BubbleDelegateView::OnWidgetBoundsChanged(Widget
* widget
,
183 const gfx::Rect
& new_bounds
) {
184 if (GetBubbleFrameView() && anchor_widget() == widget
)
188 View
* BubbleDelegateView::GetAnchorView() const {
189 return ViewStorage::GetInstance()->RetrieveView(anchor_view_storage_id_
);
192 gfx::Rect
BubbleDelegateView::GetAnchorRect() const {
193 if (!GetAnchorView())
196 anchor_rect_
= GetAnchorView()->GetBoundsInScreen();
197 anchor_rect_
.Inset(anchor_view_insets_
);
201 void BubbleDelegateView::OnBeforeBubbleWidgetInit(Widget::InitParams
* params
,
202 Widget
* widget
) const {
205 void BubbleDelegateView::SetAlignment(BubbleBorder::BubbleAlignment alignment
) {
206 GetBubbleFrameView()->bubble_border()->set_alignment(alignment
);
210 void BubbleDelegateView::SetArrowPaintType(
211 BubbleBorder::ArrowPaintType paint_type
) {
212 GetBubbleFrameView()->bubble_border()->set_paint_arrow(paint_type
);
216 void BubbleDelegateView::OnAnchorBoundsChanged() {
220 bool BubbleDelegateView::AcceleratorPressed(
221 const ui::Accelerator
& accelerator
) {
222 if (!close_on_esc() || accelerator
.key_code() != ui::VKEY_ESCAPE
)
224 GetWidget()->Close();
228 void BubbleDelegateView::OnNativeThemeChanged(const ui::NativeTheme
* theme
) {
229 UpdateColorsFromTheme(theme
);
232 void BubbleDelegateView::Init() {}
234 void BubbleDelegateView::SetAnchorView(View
* anchor_view
) {
235 // When the anchor view gets set the associated anchor widget might
237 if (!anchor_view
|| anchor_widget() != anchor_view
->GetWidget()) {
238 if (anchor_widget()) {
239 anchor_widget_
->RemoveObserver(this);
240 anchor_widget_
= NULL
;
243 anchor_widget_
= anchor_view
->GetWidget();
245 anchor_widget_
->AddObserver(this);
249 // Remove the old storage item and set the new (if there is one).
250 ViewStorage
* view_storage
= ViewStorage::GetInstance();
251 if (view_storage
->RetrieveView(anchor_view_storage_id_
))
252 view_storage
->RemoveView(anchor_view_storage_id_
);
254 view_storage
->StoreView(anchor_view_storage_id_
, anchor_view
);
256 // Do not update anchoring for NULL views; this could indicate that our
257 // NativeWindow is being destroyed, so it would be dangerous for us to update
258 // our anchor bounds at that point. (It's safe to skip this, since if we were
259 // to update the bounds when |anchor_view| is NULL, the bubble won't move.)
260 if (anchor_view
&& GetWidget())
261 OnAnchorBoundsChanged();
264 void BubbleDelegateView::SetAnchorRect(const gfx::Rect
& rect
) {
267 OnAnchorBoundsChanged();
270 void BubbleDelegateView::SizeToContents() {
271 GetWidget()->SetBounds(GetBubbleBounds());
274 BubbleFrameView
* BubbleDelegateView::GetBubbleFrameView() const {
275 const NonClientView
* view
=
276 GetWidget() ? GetWidget()->non_client_view() : NULL
;
277 return view
? static_cast<BubbleFrameView
*>(view
->frame_view()) : NULL
;
280 gfx::Rect
BubbleDelegateView::GetBubbleBounds() {
281 // The argument rect has its origin at the bubble's arrow anchor point;
282 // its size is the preferred size of the bubble's client view (this view).
283 bool anchor_minimized
= anchor_widget() && anchor_widget()->IsMinimized();
284 return GetBubbleFrameView()->GetUpdatedWindowBounds(GetAnchorRect(),
285 GetPreferredSize(), adjust_if_offscreen_
&& !anchor_minimized
);
288 const gfx::FontList
& BubbleDelegateView::GetTitleFontList() const {
289 ui::ResourceBundle
& rb
= ui::ResourceBundle::GetSharedInstance();
290 return rb
.GetFontList(ui::ResourceBundle::MediumFont
);
294 void BubbleDelegateView::UpdateColorsFromTheme(const ui::NativeTheme
* theme
) {
295 if (!color_explicitly_set_
)
296 color_
= theme
->GetSystemColor(ui::NativeTheme::kColorId_DialogBackground
);
297 set_background(Background::CreateSolidBackground(color()));
298 BubbleFrameView
* frame_view
= GetBubbleFrameView();
300 frame_view
->bubble_border()->set_background_color(color());
303 void BubbleDelegateView::HandleVisibilityChanged(Widget
* widget
, bool visible
) {
304 if (widget
== GetWidget() && anchor_widget() &&
305 anchor_widget()->GetTopLevelWidget()) {
307 anchor_widget()->GetTopLevelWidget()->DisableInactiveRendering();
309 anchor_widget()->GetTopLevelWidget()->EnableInactiveRendering();
312 // Fire AX_EVENT_ALERT for bubbles marked as AX_ROLE_ALERT_DIALOG; this
313 // instructs accessibility tools to read the bubble in its entirety rather
314 // than just its title and initially focused view. See
315 // http://crbug.com/474622 for details.
316 if (widget
== GetWidget() && visible
) {
317 ui::AXViewState state
;
318 GetAccessibleState(&state
);
319 if (state
.role
== ui::AX_ROLE_ALERT_DIALOG
)
320 NotifyAccessibilityEvent(ui::AX_EVENT_ALERT
, true);