Refactor the way enhanced bookmark gets large icons
[chromium-blink-merge.git] / ui / views / bubble / bubble_delegate.cc
blobfd0f112ef32a2a2383c772087dffc15ef1bc538f
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 if (bubble_params.parent)
44 bubble_widget->StackAbove(bubble_params.parent);
45 return bubble_widget;
48 } // namespace
50 // static
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()),
57 anchor_widget_(NULL),
58 arrow_(BubbleBorder::TOP_LEFT),
59 shadow_(BubbleBorder::SMALL_SHADOW),
60 color_explicitly_set_(false),
61 margins_(kDefaultMargin, kDefaultMargin, kDefaultMargin, kDefaultMargin),
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());
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 arrow_(arrow),
78 shadow_(BubbleBorder::SMALL_SHADOW),
79 color_explicitly_set_(false),
80 margins_(kDefaultMargin, kDefaultMargin, kDefaultMargin, kDefaultMargin),
81 accept_events_(true),
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() {
91 if (GetWidget())
92 GetWidget()->RemoveObserver(this);
93 SetLayoutManager(NULL);
94 SetAnchorView(NULL);
97 // static
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);
104 #if defined(OS_WIN)
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)
110 // Linux clips bubble windows that extend outside their parent window bounds.
111 bubble_delegate->set_adjust_if_offscreen(false);
112 #endif
114 bubble_delegate->SizeToContents();
115 bubble_widget->AddObserver(bubble_delegate);
116 return bubble_widget;
119 BubbleDelegateView* BubbleDelegateView::AsBubbleDelegate() {
120 return this;
123 bool BubbleDelegateView::ShouldShowCloseButton() const {
124 return false;
127 View* BubbleDelegateView::GetContentsView() {
128 return this;
131 NonClientFrameView* BubbleDelegateView::CreateNonClientFrameView(
132 Widget* widget) {
133 BubbleFrameView* frame = new BubbleFrameView(margins());
134 // Note: In CreateBubble, the call to SizeToContents() will cause
135 // the relayout that this call requires.
136 frame->SetTitleFontList(GetTitleFontList());
137 BubbleBorder::Arrow adjusted_arrow = arrow();
138 if (base::i18n::IsRTL())
139 adjusted_arrow = BubbleBorder::horizontal_mirror(adjusted_arrow);
140 frame->SetBubbleBorder(scoped_ptr<BubbleBorder>(
141 new BubbleBorder(adjusted_arrow, shadow(), color())));
142 return frame;
145 void BubbleDelegateView::GetAccessibleState(ui::AXViewState* state) {
146 state->role = ui::AX_ROLE_DIALOG;
149 const char* BubbleDelegateView::GetClassName() const {
150 return kViewClassName;
153 void BubbleDelegateView::OnWidgetDestroying(Widget* widget) {
154 if (anchor_widget() == widget)
155 SetAnchorView(NULL);
158 void BubbleDelegateView::OnWidgetVisibilityChanging(Widget* widget,
159 bool visible) {
160 #if defined(OS_WIN)
161 // On Windows we need to handle this before the bubble is visible or hidden.
162 // Please see the comment on the OnWidgetVisibilityChanging function. On
163 // other platforms it is fine to handle it after the bubble is shown/hidden.
164 HandleVisibilityChanged(widget, visible);
165 #endif
168 void BubbleDelegateView::OnWidgetVisibilityChanged(Widget* widget,
169 bool visible) {
170 #if !defined(OS_WIN)
171 HandleVisibilityChanged(widget, visible);
172 #endif
175 void BubbleDelegateView::OnWidgetActivationChanged(Widget* widget,
176 bool active) {
177 if (close_on_deactivate() && widget == GetWidget() && !active)
178 GetWidget()->Close();
181 void BubbleDelegateView::OnWidgetBoundsChanged(Widget* widget,
182 const gfx::Rect& new_bounds) {
183 if (GetBubbleFrameView() && anchor_widget() == widget)
184 SizeToContents();
187 View* BubbleDelegateView::GetAnchorView() const {
188 return ViewStorage::GetInstance()->RetrieveView(anchor_view_storage_id_);
191 gfx::Rect BubbleDelegateView::GetAnchorRect() const {
192 if (!GetAnchorView())
193 return anchor_rect_;
195 anchor_rect_ = GetAnchorView()->GetBoundsInScreen();
196 anchor_rect_.Inset(anchor_view_insets_);
197 return anchor_rect_;
200 void BubbleDelegateView::OnBeforeBubbleWidgetInit(Widget::InitParams* params,
201 Widget* widget) const {
204 void BubbleDelegateView::SetAlignment(BubbleBorder::BubbleAlignment alignment) {
205 GetBubbleFrameView()->bubble_border()->set_alignment(alignment);
206 SizeToContents();
209 void BubbleDelegateView::SetArrowPaintType(
210 BubbleBorder::ArrowPaintType paint_type) {
211 GetBubbleFrameView()->bubble_border()->set_paint_arrow(paint_type);
212 SizeToContents();
215 void BubbleDelegateView::OnAnchorBoundsChanged() {
216 SizeToContents();
219 bool BubbleDelegateView::AcceleratorPressed(
220 const ui::Accelerator& accelerator) {
221 if (!close_on_esc() || accelerator.key_code() != ui::VKEY_ESCAPE)
222 return false;
223 GetWidget()->Close();
224 return true;
227 void BubbleDelegateView::OnNativeThemeChanged(const ui::NativeTheme* theme) {
228 UpdateColorsFromTheme(theme);
231 void BubbleDelegateView::Init() {}
233 void BubbleDelegateView::SetAnchorView(View* anchor_view) {
234 // When the anchor view gets set the associated anchor widget might
235 // change as well.
236 if (!anchor_view || anchor_widget() != anchor_view->GetWidget()) {
237 if (anchor_widget()) {
238 anchor_widget_->RemoveObserver(this);
239 anchor_widget_ = NULL;
241 if (anchor_view) {
242 anchor_widget_ = anchor_view->GetWidget();
243 if (anchor_widget_)
244 anchor_widget_->AddObserver(this);
248 // Remove the old storage item and set the new (if there is one).
249 ViewStorage* view_storage = ViewStorage::GetInstance();
250 if (view_storage->RetrieveView(anchor_view_storage_id_))
251 view_storage->RemoveView(anchor_view_storage_id_);
252 if (anchor_view)
253 view_storage->StoreView(anchor_view_storage_id_, anchor_view);
255 // Do not update anchoring for NULL views; this could indicate that our
256 // NativeWindow is being destroyed, so it would be dangerous for us to update
257 // our anchor bounds at that point. (It's safe to skip this, since if we were
258 // to update the bounds when |anchor_view| is NULL, the bubble won't move.)
259 if (anchor_view && GetWidget())
260 OnAnchorBoundsChanged();
263 void BubbleDelegateView::SetAnchorRect(const gfx::Rect& rect) {
264 anchor_rect_ = rect;
265 if (GetWidget())
266 OnAnchorBoundsChanged();
269 void BubbleDelegateView::SizeToContents() {
270 GetWidget()->SetBounds(GetBubbleBounds());
273 BubbleFrameView* BubbleDelegateView::GetBubbleFrameView() const {
274 const NonClientView* view =
275 GetWidget() ? GetWidget()->non_client_view() : NULL;
276 return view ? static_cast<BubbleFrameView*>(view->frame_view()) : NULL;
279 gfx::Rect BubbleDelegateView::GetBubbleBounds() {
280 // The argument rect has its origin at the bubble's arrow anchor point;
281 // its size is the preferred size of the bubble's client view (this view).
282 bool anchor_minimized = anchor_widget() && anchor_widget()->IsMinimized();
283 return GetBubbleFrameView()->GetUpdatedWindowBounds(GetAnchorRect(),
284 GetPreferredSize(), adjust_if_offscreen_ && !anchor_minimized);
287 const gfx::FontList& BubbleDelegateView::GetTitleFontList() const {
288 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
289 return rb.GetFontList(ui::ResourceBundle::MediumFont);
293 void BubbleDelegateView::UpdateColorsFromTheme(const ui::NativeTheme* theme) {
294 if (!color_explicitly_set_)
295 color_ = theme->GetSystemColor(ui::NativeTheme::kColorId_DialogBackground);
296 set_background(Background::CreateSolidBackground(color()));
297 BubbleFrameView* frame_view = GetBubbleFrameView();
298 if (frame_view)
299 frame_view->bubble_border()->set_background_color(color());
302 void BubbleDelegateView::HandleVisibilityChanged(Widget* widget, bool visible) {
303 if (widget == GetWidget() && anchor_widget() &&
304 anchor_widget()->GetTopLevelWidget()) {
305 if (visible)
306 anchor_widget()->GetTopLevelWidget()->DisableInactiveRendering();
307 else
308 anchor_widget()->GetTopLevelWidget()->EnableInactiveRendering();
311 // Fire AX_EVENT_ALERT for bubbles marked as AX_ROLE_ALERT_DIALOG; this
312 // instructs accessibility tools to read the bubble in its entirety rather
313 // than just its title and initially focused view. See
314 // http://crbug.com/474622 for details.
315 if (widget == GetWidget() && visible) {
316 ui::AXViewState state;
317 GetAccessibleState(&state);
318 if (state.role == ui::AX_ROLE_ALERT_DIALOG)
319 NotifyAccessibilityEvent(ui::AX_EVENT_ALERT, true);
323 } // namespace views