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/gfx/color_utils.h"
9 #include "ui/gfx/rect.h"
10 #include "ui/native_theme/native_theme.h"
11 #include "ui/views/bubble/bubble_frame_view.h"
12 #include "ui/views/focus/view_storage.h"
13 #include "ui/views/widget/widget.h"
14 #include "ui/views/widget/widget_observer.h"
17 #include "ui/base/win/shell.h"
20 // The defaut margin between the content and the inside border, in pixels.
21 static const int kDefaultMargin = 6;
27 // Create a widget to host the bubble.
28 Widget* CreateBubbleWidget(BubbleDelegateView* bubble) {
29 Widget* bubble_widget = new Widget();
30 Widget::InitParams bubble_params(Widget::InitParams::TYPE_BUBBLE);
31 bubble_params.delegate = bubble;
32 bubble_params.opacity = Widget::InitParams::TRANSLUCENT_WINDOW;
33 bubble_params.accept_events = bubble->accept_events();
34 if (bubble->parent_window())
35 bubble_params.parent = bubble->parent_window();
36 else if (bubble->anchor_widget())
37 bubble_params.parent = bubble->anchor_widget()->GetNativeView();
38 bubble_params.can_activate = bubble->CanActivate();
39 bubble->OnBeforeBubbleWidgetInit(&bubble_params, bubble_widget);
40 bubble_widget->Init(bubble_params);
46 BubbleDelegateView::BubbleDelegateView()
47 : close_on_esc_(true),
48 close_on_deactivate_(true),
49 anchor_view_storage_id_(ViewStorage::GetInstance()->CreateStorageID()),
51 arrow_(BubbleBorder::TOP_LEFT),
52 shadow_(BubbleBorder::SMALL_SHADOW),
53 color_explicitly_set_(false),
54 margins_(kDefaultMargin, kDefaultMargin, kDefaultMargin, kDefaultMargin),
55 use_focusless_(false),
57 border_accepts_events_(true),
58 adjust_if_offscreen_(true),
59 parent_window_(NULL) {
60 AddAccelerator(ui::Accelerator(ui::VKEY_ESCAPE, ui::EF_NONE));
61 UpdateColorsFromTheme(GetNativeTheme());
64 BubbleDelegateView::BubbleDelegateView(
66 BubbleBorder::Arrow arrow)
67 : close_on_esc_(true),
68 close_on_deactivate_(true),
69 anchor_view_storage_id_(ViewStorage::GetInstance()->CreateStorageID()),
72 shadow_(BubbleBorder::SMALL_SHADOW),
73 color_explicitly_set_(false),
74 margins_(kDefaultMargin, kDefaultMargin, kDefaultMargin, kDefaultMargin),
75 use_focusless_(false),
77 border_accepts_events_(true),
78 adjust_if_offscreen_(true),
79 parent_window_(NULL) {
80 SetAnchorView(anchor_view);
81 AddAccelerator(ui::Accelerator(ui::VKEY_ESCAPE, ui::EF_NONE));
82 UpdateColorsFromTheme(GetNativeTheme());
85 BubbleDelegateView::~BubbleDelegateView() {
87 GetWidget()->RemoveObserver(this);
88 SetLayoutManager(NULL);
93 Widget* BubbleDelegateView::CreateBubble(BubbleDelegateView* bubble_delegate) {
94 bubble_delegate->Init();
95 // Get the latest anchor widget from the anchor view at bubble creation time.
96 bubble_delegate->SetAnchorView(bubble_delegate->GetAnchorView());
97 Widget* bubble_widget = CreateBubbleWidget(bubble_delegate);
100 // If glass is enabled, the bubble is allowed to extend outside the bounds of
101 // the parent frame and let DWM handle compositing. If not, then we don't
102 // want to allow the bubble to extend the frame because it will be clipped.
103 bubble_delegate->set_adjust_if_offscreen(ui::win::IsAeroGlassEnabled());
104 #elif defined(OS_LINUX) && !defined(OS_CHROMEOS)
105 // Linux clips bubble windows that extend outside their parent window bounds.
106 bubble_delegate->set_adjust_if_offscreen(false);
109 bubble_delegate->SizeToContents();
110 bubble_widget->AddObserver(bubble_delegate);
111 return bubble_widget;
114 BubbleDelegateView* BubbleDelegateView::AsBubbleDelegate() {
118 bool BubbleDelegateView::CanActivate() const {
119 return !use_focusless();
122 bool BubbleDelegateView::ShouldShowCloseButton() const {
126 View* BubbleDelegateView::GetContentsView() {
130 NonClientFrameView* BubbleDelegateView::CreateNonClientFrameView(
132 BubbleFrameView* frame = new BubbleFrameView(margins());
133 BubbleBorder::Arrow adjusted_arrow = arrow();
134 if (base::i18n::IsRTL())
135 adjusted_arrow = BubbleBorder::horizontal_mirror(adjusted_arrow);
136 frame->SetBubbleBorder(scoped_ptr<BubbleBorder>(
137 new BubbleBorder(adjusted_arrow, shadow(), color())));
141 void BubbleDelegateView::GetAccessibleState(ui::AXViewState* state) {
142 state->role = ui::AX_ROLE_DIALOG;
145 void BubbleDelegateView::OnWidgetDestroying(Widget* widget) {
146 if (anchor_widget() == widget)
150 void BubbleDelegateView::OnWidgetVisibilityChanging(Widget* widget,
153 // On Windows we need to handle this before the bubble is visible or hidden.
154 // Please see the comment on the OnWidgetVisibilityChanging function. On
155 // other platforms it is fine to handle it after the bubble is shown/hidden.
156 HandleVisibilityChanged(widget, visible);
160 void BubbleDelegateView::OnWidgetVisibilityChanged(Widget* widget,
163 HandleVisibilityChanged(widget, visible);
167 void BubbleDelegateView::OnWidgetActivationChanged(Widget* widget,
169 if (close_on_deactivate() && widget == GetWidget() && !active)
170 GetWidget()->Close();
173 void BubbleDelegateView::OnWidgetBoundsChanged(Widget* widget,
174 const gfx::Rect& new_bounds) {
175 if (anchor_widget() == widget)
179 View* BubbleDelegateView::GetAnchorView() const {
180 return ViewStorage::GetInstance()->RetrieveView(anchor_view_storage_id_);
183 gfx::Rect BubbleDelegateView::GetAnchorRect() {
184 if (!GetAnchorView())
187 anchor_rect_ = GetAnchorView()->GetBoundsInScreen();
188 anchor_rect_.Inset(anchor_view_insets_);
192 void BubbleDelegateView::OnBeforeBubbleWidgetInit(Widget::InitParams* params,
193 Widget* widget) const {
196 void BubbleDelegateView::SetAlignment(BubbleBorder::BubbleAlignment alignment) {
197 GetBubbleFrameView()->bubble_border()->set_alignment(alignment);
201 void BubbleDelegateView::SetArrowPaintType(
202 BubbleBorder::ArrowPaintType paint_type) {
203 GetBubbleFrameView()->bubble_border()->set_paint_arrow(paint_type);
207 void BubbleDelegateView::OnAnchorBoundsChanged() {
211 bool BubbleDelegateView::AcceleratorPressed(
212 const ui::Accelerator& accelerator) {
213 if (!close_on_esc() || accelerator.key_code() != ui::VKEY_ESCAPE)
215 GetWidget()->Close();
219 void BubbleDelegateView::OnNativeThemeChanged(const ui::NativeTheme* theme) {
220 UpdateColorsFromTheme(theme);
223 void BubbleDelegateView::Init() {}
225 void BubbleDelegateView::SetAnchorView(View* anchor_view) {
226 // When the anchor view gets set the associated anchor widget might
228 if (!anchor_view || anchor_widget() != anchor_view->GetWidget()) {
229 if (anchor_widget()) {
230 anchor_widget_->RemoveObserver(this);
231 anchor_widget_ = NULL;
234 anchor_widget_ = anchor_view->GetWidget();
236 anchor_widget_->AddObserver(this);
240 // Remove the old storage item and set the new (if there is one).
241 ViewStorage* view_storage = ViewStorage::GetInstance();
242 if (view_storage->RetrieveView(anchor_view_storage_id_))
243 view_storage->RemoveView(anchor_view_storage_id_);
245 view_storage->StoreView(anchor_view_storage_id_, anchor_view);
247 // Do not update anchoring for NULL views; this could indicate that our
248 // NativeWindow is being destroyed, so it would be dangerous for us to update
249 // our anchor bounds at that point. (It's safe to skip this, since if we were
250 // to update the bounds when |anchor_view| is NULL, the bubble won't move.)
251 if (anchor_view && GetWidget())
252 OnAnchorBoundsChanged();
255 void BubbleDelegateView::SetAnchorRect(const gfx::Rect& rect) {
258 OnAnchorBoundsChanged();
261 void BubbleDelegateView::SizeToContents() {
262 GetWidget()->SetBounds(GetBubbleBounds());
265 BubbleFrameView* BubbleDelegateView::GetBubbleFrameView() const {
266 const NonClientView* view =
267 GetWidget() ? GetWidget()->non_client_view() : NULL;
268 return view ? static_cast<BubbleFrameView*>(view->frame_view()) : NULL;
271 gfx::Rect BubbleDelegateView::GetBubbleBounds() {
272 // The argument rect has its origin at the bubble's arrow anchor point;
273 // its size is the preferred size of the bubble's client view (this view).
274 return GetBubbleFrameView()->GetUpdatedWindowBounds(GetAnchorRect(),
275 GetPreferredSize(), adjust_if_offscreen_);
278 void BubbleDelegateView::UpdateColorsFromTheme(const ui::NativeTheme* theme) {
279 if (!color_explicitly_set_)
280 color_ = theme->GetSystemColor(ui::NativeTheme::kColorId_WindowBackground);
281 set_background(Background::CreateSolidBackground(color()));
282 BubbleFrameView* frame_view = GetBubbleFrameView();
284 frame_view->bubble_border()->set_background_color(color());
287 void BubbleDelegateView::HandleVisibilityChanged(Widget* widget, bool visible) {
288 if (widget == GetWidget() && anchor_widget() &&
289 anchor_widget()->GetTopLevelWidget()) {
291 anchor_widget()->GetTopLevelWidget()->DisableInactiveRendering();
293 anchor_widget()->GetTopLevelWidget()->EnableInactiveRendering();