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/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
);
48 BubbleDelegateView::BubbleDelegateView()
49 : close_on_esc_(true),
50 close_on_deactivate_(true),
51 anchor_view_storage_id_(ViewStorage::GetInstance()->CreateStorageID()),
53 arrow_(BubbleBorder::TOP_LEFT
),
54 shadow_(BubbleBorder::SMALL_SHADOW
),
55 color_explicitly_set_(false),
56 margins_(kDefaultMargin
, kDefaultMargin
, kDefaultMargin
, kDefaultMargin
),
57 use_focusless_(false),
59 border_accepts_events_(true),
60 adjust_if_offscreen_(true),
61 parent_window_(NULL
) {
62 AddAccelerator(ui::Accelerator(ui::VKEY_ESCAPE
, ui::EF_NONE
));
63 UpdateColorsFromTheme(GetNativeTheme());
66 BubbleDelegateView::BubbleDelegateView(
68 BubbleBorder::Arrow arrow
)
69 : close_on_esc_(true),
70 close_on_deactivate_(true),
71 anchor_view_storage_id_(ViewStorage::GetInstance()->CreateStorageID()),
74 shadow_(BubbleBorder::SMALL_SHADOW
),
75 color_explicitly_set_(false),
76 margins_(kDefaultMargin
, kDefaultMargin
, kDefaultMargin
, kDefaultMargin
),
77 use_focusless_(false),
79 border_accepts_events_(true),
80 adjust_if_offscreen_(true),
81 parent_window_(NULL
) {
82 SetAnchorView(anchor_view
);
83 AddAccelerator(ui::Accelerator(ui::VKEY_ESCAPE
, ui::EF_NONE
));
84 UpdateColorsFromTheme(GetNativeTheme());
87 BubbleDelegateView::~BubbleDelegateView() {
89 GetWidget()->RemoveObserver(this);
90 SetLayoutManager(NULL
);
95 Widget
* BubbleDelegateView::CreateBubble(BubbleDelegateView
* bubble_delegate
) {
96 bubble_delegate
->Init();
97 // Get the latest anchor widget from the anchor view at bubble creation time.
98 bubble_delegate
->SetAnchorView(bubble_delegate
->GetAnchorView());
99 Widget
* bubble_widget
= CreateBubbleWidget(bubble_delegate
);
102 // If glass is enabled, the bubble is allowed to extend outside the bounds of
103 // the parent frame and let DWM handle compositing. If not, then we don't
104 // want to allow the bubble to extend the frame because it will be clipped.
105 bubble_delegate
->set_adjust_if_offscreen(ui::win::IsAeroGlassEnabled());
106 #elif defined(OS_LINUX) && !defined(OS_CHROMEOS)
107 // Linux clips bubble windows that extend outside their parent window bounds.
108 bubble_delegate
->set_adjust_if_offscreen(false);
111 bubble_delegate
->SizeToContents();
112 bubble_widget
->AddObserver(bubble_delegate
);
113 return bubble_widget
;
116 BubbleDelegateView
* BubbleDelegateView::AsBubbleDelegate() {
120 bool BubbleDelegateView::CanActivate() const {
121 return !use_focusless();
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 void BubbleDelegateView::OnWidgetDestroying(Widget
* widget
) {
151 if (anchor_widget() == widget
)
155 void BubbleDelegateView::OnWidgetVisibilityChanging(Widget
* widget
,
158 // On Windows we need to handle this before the bubble is visible or hidden.
159 // Please see the comment on the OnWidgetVisibilityChanging function. On
160 // other platforms it is fine to handle it after the bubble is shown/hidden.
161 HandleVisibilityChanged(widget
, visible
);
165 void BubbleDelegateView::OnWidgetVisibilityChanged(Widget
* widget
,
168 HandleVisibilityChanged(widget
, visible
);
172 void BubbleDelegateView::OnWidgetActivationChanged(Widget
* widget
,
174 if (close_on_deactivate() && widget
== GetWidget() && !active
)
175 GetWidget()->Close();
178 void BubbleDelegateView::OnWidgetBoundsChanged(Widget
* widget
,
179 const gfx::Rect
& new_bounds
) {
180 if (anchor_widget() == widget
)
184 View
* BubbleDelegateView::GetAnchorView() const {
185 return ViewStorage::GetInstance()->RetrieveView(anchor_view_storage_id_
);
188 gfx::Rect
BubbleDelegateView::GetAnchorRect() const {
189 if (!GetAnchorView())
192 anchor_rect_
= GetAnchorView()->GetBoundsInScreen();
193 anchor_rect_
.Inset(anchor_view_insets_
);
197 void BubbleDelegateView::OnBeforeBubbleWidgetInit(Widget::InitParams
* params
,
198 Widget
* widget
) const {
201 void BubbleDelegateView::SetAlignment(BubbleBorder::BubbleAlignment alignment
) {
202 GetBubbleFrameView()->bubble_border()->set_alignment(alignment
);
206 void BubbleDelegateView::SetArrowPaintType(
207 BubbleBorder::ArrowPaintType paint_type
) {
208 GetBubbleFrameView()->bubble_border()->set_paint_arrow(paint_type
);
212 void BubbleDelegateView::OnAnchorBoundsChanged() {
216 bool BubbleDelegateView::AcceleratorPressed(
217 const ui::Accelerator
& accelerator
) {
218 if (!close_on_esc() || accelerator
.key_code() != ui::VKEY_ESCAPE
)
220 GetWidget()->Close();
224 void BubbleDelegateView::OnNativeThemeChanged(const ui::NativeTheme
* theme
) {
225 UpdateColorsFromTheme(theme
);
228 void BubbleDelegateView::Init() {}
230 void BubbleDelegateView::SetAnchorView(View
* anchor_view
) {
231 // When the anchor view gets set the associated anchor widget might
233 if (!anchor_view
|| anchor_widget() != anchor_view
->GetWidget()) {
234 if (anchor_widget()) {
235 anchor_widget_
->RemoveObserver(this);
236 anchor_widget_
= NULL
;
239 anchor_widget_
= anchor_view
->GetWidget();
241 anchor_widget_
->AddObserver(this);
245 // Remove the old storage item and set the new (if there is one).
246 ViewStorage
* view_storage
= ViewStorage::GetInstance();
247 if (view_storage
->RetrieveView(anchor_view_storage_id_
))
248 view_storage
->RemoveView(anchor_view_storage_id_
);
250 view_storage
->StoreView(anchor_view_storage_id_
, anchor_view
);
252 // Do not update anchoring for NULL views; this could indicate that our
253 // NativeWindow is being destroyed, so it would be dangerous for us to update
254 // our anchor bounds at that point. (It's safe to skip this, since if we were
255 // to update the bounds when |anchor_view| is NULL, the bubble won't move.)
256 if (anchor_view
&& GetWidget())
257 OnAnchorBoundsChanged();
260 void BubbleDelegateView::SetAnchorRect(const gfx::Rect
& rect
) {
263 OnAnchorBoundsChanged();
266 void BubbleDelegateView::SizeToContents() {
267 GetWidget()->SetBounds(GetBubbleBounds());
270 BubbleFrameView
* BubbleDelegateView::GetBubbleFrameView() const {
271 const NonClientView
* view
=
272 GetWidget() ? GetWidget()->non_client_view() : NULL
;
273 return view
? static_cast<BubbleFrameView
*>(view
->frame_view()) : NULL
;
276 gfx::Rect
BubbleDelegateView::GetBubbleBounds() {
277 // The argument rect has its origin at the bubble's arrow anchor point;
278 // its size is the preferred size of the bubble's client view (this view).
279 bool anchor_minimized
= anchor_widget() && anchor_widget()->IsMinimized();
280 return GetBubbleFrameView()->GetUpdatedWindowBounds(GetAnchorRect(),
281 GetPreferredSize(), adjust_if_offscreen_
&& !anchor_minimized
);
284 const gfx::FontList
& BubbleDelegateView::GetTitleFontList() const {
285 ui::ResourceBundle
& rb
= ui::ResourceBundle::GetSharedInstance();
286 return rb
.GetFontList(ui::ResourceBundle::MediumFont
);
290 void BubbleDelegateView::UpdateColorsFromTheme(const ui::NativeTheme
* theme
) {
291 if (!color_explicitly_set_
)
292 color_
= theme
->GetSystemColor(ui::NativeTheme::kColorId_DialogBackground
);
293 set_background(Background::CreateSolidBackground(color()));
294 BubbleFrameView
* frame_view
= GetBubbleFrameView();
296 frame_view
->bubble_border()->set_background_color(color());
299 void BubbleDelegateView::HandleVisibilityChanged(Widget
* widget
, bool visible
) {
300 if (widget
== GetWidget() && anchor_widget() &&
301 anchor_widget()->GetTopLevelWidget()) {
303 anchor_widget()->GetTopLevelWidget()->DisableInactiveRendering();
305 anchor_widget()->GetTopLevelWidget()->EnableInactiveRendering();