Supervised user whitelists: Cleanup
[chromium-blink-merge.git] / ui / views / bubble / bubble_delegate.cc
blob0378892b708185ebb446df7b904366ff4c6b0161
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"
17 #if defined(OS_WIN)
18 #include "ui/base/win/shell.h"
19 #endif
21 // The defaut margin between the content and the inside border, in pixels.
22 static const int kDefaultMargin = 6;
24 namespace views {
26 namespace {
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 return bubble_widget;
46 } // namespace
48 // static
49 const char BubbleDelegateView::kViewClassName[] = "BubbleDelegateView";
51 BubbleDelegateView::BubbleDelegateView()
52 : close_on_esc_(true),
53 close_on_deactivate_(true),
54 anchor_view_storage_id_(ViewStorage::GetInstance()->CreateStorageID()),
55 anchor_widget_(NULL),
56 arrow_(BubbleBorder::TOP_LEFT),
57 shadow_(BubbleBorder::SMALL_SHADOW),
58 color_explicitly_set_(false),
59 margins_(kDefaultMargin, kDefaultMargin, kDefaultMargin, kDefaultMargin),
60 accept_events_(true),
61 border_accepts_events_(true),
62 adjust_if_offscreen_(true),
63 parent_window_(NULL) {
64 AddAccelerator(ui::Accelerator(ui::VKEY_ESCAPE, ui::EF_NONE));
65 UpdateColorsFromTheme(GetNativeTheme());
68 BubbleDelegateView::BubbleDelegateView(
69 View* anchor_view,
70 BubbleBorder::Arrow arrow)
71 : close_on_esc_(true),
72 close_on_deactivate_(true),
73 anchor_view_storage_id_(ViewStorage::GetInstance()->CreateStorageID()),
74 anchor_widget_(NULL),
75 arrow_(arrow),
76 shadow_(BubbleBorder::SMALL_SHADOW),
77 color_explicitly_set_(false),
78 margins_(kDefaultMargin, kDefaultMargin, kDefaultMargin, kDefaultMargin),
79 accept_events_(true),
80 border_accepts_events_(true),
81 adjust_if_offscreen_(true),
82 parent_window_(NULL) {
83 SetAnchorView(anchor_view);
84 AddAccelerator(ui::Accelerator(ui::VKEY_ESCAPE, ui::EF_NONE));
85 UpdateColorsFromTheme(GetNativeTheme());
88 BubbleDelegateView::~BubbleDelegateView() {
89 if (GetWidget())
90 GetWidget()->RemoveObserver(this);
91 SetLayoutManager(NULL);
92 SetAnchorView(NULL);
95 // static
96 Widget* BubbleDelegateView::CreateBubble(BubbleDelegateView* bubble_delegate) {
97 bubble_delegate->Init();
98 // Get the latest anchor widget from the anchor view at bubble creation time.
99 bubble_delegate->SetAnchorView(bubble_delegate->GetAnchorView());
100 Widget* bubble_widget = CreateBubbleWidget(bubble_delegate);
102 #if defined(OS_WIN)
103 // If glass is enabled, the bubble is allowed to extend outside the bounds of
104 // the parent frame and let DWM handle compositing. If not, then we don't
105 // want to allow the bubble to extend the frame because it will be clipped.
106 bubble_delegate->set_adjust_if_offscreen(ui::win::IsAeroGlassEnabled());
107 #elif defined(OS_LINUX) && !defined(OS_CHROMEOS)
108 // Linux clips bubble windows that extend outside their parent window bounds.
109 bubble_delegate->set_adjust_if_offscreen(false);
110 #endif
112 bubble_delegate->SizeToContents();
113 bubble_widget->AddObserver(bubble_delegate);
114 return bubble_widget;
117 BubbleDelegateView* BubbleDelegateView::AsBubbleDelegate() {
118 return this;
121 bool BubbleDelegateView::ShouldShowCloseButton() const {
122 return false;
125 View* BubbleDelegateView::GetContentsView() {
126 return this;
129 NonClientFrameView* BubbleDelegateView::CreateNonClientFrameView(
130 Widget* widget) {
131 BubbleFrameView* frame = new BubbleFrameView(margins());
132 // Note: In CreateBubble, the call to SizeToContents() will cause
133 // the relayout that this call requires.
134 frame->SetTitleFontList(GetTitleFontList());
135 BubbleBorder::Arrow adjusted_arrow = arrow();
136 if (base::i18n::IsRTL())
137 adjusted_arrow = BubbleBorder::horizontal_mirror(adjusted_arrow);
138 frame->SetBubbleBorder(scoped_ptr<BubbleBorder>(
139 new BubbleBorder(adjusted_arrow, shadow(), color())));
140 return frame;
143 void BubbleDelegateView::GetAccessibleState(ui::AXViewState* state) {
144 state->role = ui::AX_ROLE_DIALOG;
147 const char* BubbleDelegateView::GetClassName() const {
148 return kViewClassName;
151 void BubbleDelegateView::OnWidgetDestroying(Widget* widget) {
152 if (anchor_widget() == widget)
153 SetAnchorView(NULL);
156 void BubbleDelegateView::OnWidgetVisibilityChanging(Widget* widget,
157 bool visible) {
158 #if defined(OS_WIN)
159 // On Windows we need to handle this before the bubble is visible or hidden.
160 // Please see the comment on the OnWidgetVisibilityChanging function. On
161 // other platforms it is fine to handle it after the bubble is shown/hidden.
162 HandleVisibilityChanged(widget, visible);
163 #endif
166 void BubbleDelegateView::OnWidgetVisibilityChanged(Widget* widget,
167 bool visible) {
168 #if !defined(OS_WIN)
169 HandleVisibilityChanged(widget, visible);
170 #endif
173 void BubbleDelegateView::OnWidgetActivationChanged(Widget* widget,
174 bool active) {
175 if (close_on_deactivate() && widget == GetWidget() && !active)
176 GetWidget()->Close();
179 void BubbleDelegateView::OnWidgetBoundsChanged(Widget* widget,
180 const gfx::Rect& new_bounds) {
181 if (anchor_widget() == widget)
182 SizeToContents();
185 View* BubbleDelegateView::GetAnchorView() const {
186 return ViewStorage::GetInstance()->RetrieveView(anchor_view_storage_id_);
189 gfx::Rect BubbleDelegateView::GetAnchorRect() const {
190 if (!GetAnchorView())
191 return anchor_rect_;
193 anchor_rect_ = GetAnchorView()->GetBoundsInScreen();
194 anchor_rect_.Inset(anchor_view_insets_);
195 return anchor_rect_;
198 void BubbleDelegateView::OnBeforeBubbleWidgetInit(Widget::InitParams* params,
199 Widget* widget) const {
202 void BubbleDelegateView::SetAlignment(BubbleBorder::BubbleAlignment alignment) {
203 GetBubbleFrameView()->bubble_border()->set_alignment(alignment);
204 SizeToContents();
207 void BubbleDelegateView::SetArrowPaintType(
208 BubbleBorder::ArrowPaintType paint_type) {
209 GetBubbleFrameView()->bubble_border()->set_paint_arrow(paint_type);
210 SizeToContents();
213 void BubbleDelegateView::OnAnchorBoundsChanged() {
214 SizeToContents();
217 bool BubbleDelegateView::AcceleratorPressed(
218 const ui::Accelerator& accelerator) {
219 if (!close_on_esc() || accelerator.key_code() != ui::VKEY_ESCAPE)
220 return false;
221 GetWidget()->Close();
222 return true;
225 void BubbleDelegateView::OnNativeThemeChanged(const ui::NativeTheme* theme) {
226 UpdateColorsFromTheme(theme);
229 void BubbleDelegateView::Init() {}
231 void BubbleDelegateView::SetAnchorView(View* anchor_view) {
232 // When the anchor view gets set the associated anchor widget might
233 // change as well.
234 if (!anchor_view || anchor_widget() != anchor_view->GetWidget()) {
235 if (anchor_widget()) {
236 anchor_widget_->RemoveObserver(this);
237 anchor_widget_ = NULL;
239 if (anchor_view) {
240 anchor_widget_ = anchor_view->GetWidget();
241 if (anchor_widget_)
242 anchor_widget_->AddObserver(this);
246 // Remove the old storage item and set the new (if there is one).
247 ViewStorage* view_storage = ViewStorage::GetInstance();
248 if (view_storage->RetrieveView(anchor_view_storage_id_))
249 view_storage->RemoveView(anchor_view_storage_id_);
250 if (anchor_view)
251 view_storage->StoreView(anchor_view_storage_id_, anchor_view);
253 // Do not update anchoring for NULL views; this could indicate that our
254 // NativeWindow is being destroyed, so it would be dangerous for us to update
255 // our anchor bounds at that point. (It's safe to skip this, since if we were
256 // to update the bounds when |anchor_view| is NULL, the bubble won't move.)
257 if (anchor_view && GetWidget())
258 OnAnchorBoundsChanged();
261 void BubbleDelegateView::SetAnchorRect(const gfx::Rect& rect) {
262 anchor_rect_ = rect;
263 if (GetWidget())
264 OnAnchorBoundsChanged();
267 void BubbleDelegateView::SizeToContents() {
268 GetWidget()->SetBounds(GetBubbleBounds());
271 BubbleFrameView* BubbleDelegateView::GetBubbleFrameView() const {
272 const NonClientView* view =
273 GetWidget() ? GetWidget()->non_client_view() : NULL;
274 return view ? static_cast<BubbleFrameView*>(view->frame_view()) : NULL;
277 gfx::Rect BubbleDelegateView::GetBubbleBounds() {
278 // The argument rect has its origin at the bubble's arrow anchor point;
279 // its size is the preferred size of the bubble's client view (this view).
280 bool anchor_minimized = anchor_widget() && anchor_widget()->IsMinimized();
281 return GetBubbleFrameView()->GetUpdatedWindowBounds(GetAnchorRect(),
282 GetPreferredSize(), adjust_if_offscreen_ && !anchor_minimized);
285 const gfx::FontList& BubbleDelegateView::GetTitleFontList() const {
286 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
287 return rb.GetFontList(ui::ResourceBundle::MediumFont);
291 void BubbleDelegateView::UpdateColorsFromTheme(const ui::NativeTheme* theme) {
292 if (!color_explicitly_set_)
293 color_ = theme->GetSystemColor(ui::NativeTheme::kColorId_DialogBackground);
294 set_background(Background::CreateSolidBackground(color()));
295 BubbleFrameView* frame_view = GetBubbleFrameView();
296 if (frame_view)
297 frame_view->bubble_border()->set_background_color(color());
300 void BubbleDelegateView::HandleVisibilityChanged(Widget* widget, bool visible) {
301 if (widget == GetWidget() && anchor_widget() &&
302 anchor_widget()->GetTopLevelWidget()) {
303 if (visible)
304 anchor_widget()->GetTopLevelWidget()->DisableInactiveRendering();
305 else
306 anchor_widget()->GetTopLevelWidget()->EnableInactiveRendering();
309 // Fire AX_EVENT_ALERT for bubbles marked as AX_ROLE_ALERT_DIALOG; this
310 // instructs accessibility tools to read the bubble in its entirety rather
311 // than just its title and initially focused view. See
312 // http://crbug.com/474622 for details.
313 if (widget == GetWidget() && visible) {
314 ui::AXViewState state;
315 GetAccessibleState(&state);
316 if (state.role == ui::AX_ROLE_ALERT_DIALOG)
317 NotifyAccessibilityEvent(ui::AX_EVENT_ALERT, true);
321 } // namespace views