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/tray_bubble_view.h"
9 #include "third_party/skia/include/core/SkCanvas.h"
10 #include "third_party/skia/include/core/SkColor.h"
11 #include "third_party/skia/include/core/SkPaint.h"
12 #include "third_party/skia/include/core/SkPath.h"
13 #include "third_party/skia/include/effects/SkBlurImageFilter.h"
14 #include "ui/accessibility/ax_view_state.h"
15 #include "ui/aura/window.h"
16 #include "ui/compositor/layer.h"
17 #include "ui/compositor/layer_delegate.h"
18 #include "ui/events/event.h"
19 #include "ui/gfx/canvas.h"
20 #include "ui/gfx/insets.h"
21 #include "ui/gfx/path.h"
22 #include "ui/gfx/rect.h"
23 #include "ui/gfx/skia_util.h"
24 #include "ui/views/bubble/bubble_frame_view.h"
25 #include "ui/views/bubble/bubble_window_targeter.h"
26 #include "ui/views/layout/box_layout.h"
27 #include "ui/views/widget/widget.h"
31 // Inset the arrow a bit from the edge.
32 const int kArrowMinOffset
= 20;
33 const int kBubbleSpacing
= 20;
35 // The new theme adjusts the menus / bubbles to be flush with the shelf when
36 // there is no bubble. These are the offsets which need to be applied.
37 const int kArrowOffsetTopBottom
= 4;
38 const int kArrowOffsetLeft
= 9;
39 const int kArrowOffsetRight
= -5;
40 const int kOffsetLeftRightForTopBottomOrientation
= 5;
42 // The sampling time for mouse position changes in ms - which is roughly a frame
44 const int kFrameTimeInMS
= 30;
51 // Detects any mouse movement. This is needed to detect mouse movements by the
52 // user over the bubble if the bubble got created underneath the cursor.
53 class MouseMoveDetectorHost
: public MouseWatcherHost
{
55 MouseMoveDetectorHost();
56 ~MouseMoveDetectorHost() override
;
58 bool Contains(const gfx::Point
& screen_point
, MouseEventType type
) override
;
61 DISALLOW_COPY_AND_ASSIGN(MouseMoveDetectorHost
);
64 MouseMoveDetectorHost::MouseMoveDetectorHost() {
67 MouseMoveDetectorHost::~MouseMoveDetectorHost() {
70 bool MouseMoveDetectorHost::Contains(const gfx::Point
& screen_point
,
71 MouseEventType type
) {
75 // Custom border for TrayBubbleView. Contains special logic for GetBounds()
76 // to stack bubbles with no arrows correctly. Also calculates the arrow offset.
77 class TrayBubbleBorder
: public BubbleBorder
{
79 TrayBubbleBorder(View
* owner
,
81 TrayBubbleView::InitParams params
)
82 : BubbleBorder(params
.arrow
, params
.shadow
, params
.arrow_color
),
85 tray_arrow_offset_(params
.arrow_offset
),
86 first_item_has_no_margin_(params
.first_item_has_no_margin
) {
87 set_alignment(params
.arrow_alignment
);
88 set_background_color(params
.arrow_color
);
89 set_paint_arrow(params
.arrow_paint_type
);
92 ~TrayBubbleBorder() override
{}
94 // Overridden from BubbleBorder.
95 // Sets the bubble on top of the anchor when it has no arrow.
96 gfx::Rect
GetBounds(const gfx::Rect
& position_relative_to
,
97 const gfx::Size
& contents_size
) const override
{
98 if (has_arrow(arrow())) {
100 BubbleBorder::GetBounds(position_relative_to
, contents_size
);
101 if (first_item_has_no_margin_
) {
102 if (arrow() == BubbleBorder::BOTTOM_RIGHT
||
103 arrow() == BubbleBorder::BOTTOM_LEFT
) {
104 rect
.set_y(rect
.y() + kArrowOffsetTopBottom
);
105 int rtl_factor
= base::i18n::IsRTL() ? -1 : 1;
106 rect
.set_x(rect
.x() +
107 rtl_factor
* kOffsetLeftRightForTopBottomOrientation
);
108 } else if (arrow() == BubbleBorder::LEFT_BOTTOM
) {
109 rect
.set_x(rect
.x() + kArrowOffsetLeft
);
110 } else if (arrow() == BubbleBorder::RIGHT_BOTTOM
) {
111 rect
.set_x(rect
.x() + kArrowOffsetRight
);
117 gfx::Size
border_size(contents_size
);
118 gfx::Insets insets
= GetInsets();
119 border_size
.Enlarge(insets
.width(), insets
.height());
120 const int x
= position_relative_to
.x() +
121 position_relative_to
.width() / 2 - border_size
.width() / 2;
122 // Position the bubble on top of the anchor.
123 const int y
= position_relative_to
.y() - border_size
.height() +
124 insets
.height() - kBubbleSpacing
;
125 return gfx::Rect(x
, y
, border_size
.width(), border_size
.height());
128 void UpdateArrowOffset() {
129 int arrow_offset
= 0;
130 if (arrow() == BubbleBorder::BOTTOM_RIGHT
||
131 arrow() == BubbleBorder::BOTTOM_LEFT
) {
132 // Note: tray_arrow_offset_ is relative to the anchor widget.
133 if (tray_arrow_offset_
==
134 TrayBubbleView::InitParams::kArrowDefaultOffset
) {
135 arrow_offset
= kArrowMinOffset
;
137 const int width
= owner_
->GetWidget()->GetContentsView()->width();
138 gfx::Point
pt(tray_arrow_offset_
, 0);
139 View::ConvertPointToScreen(anchor_
->GetWidget()->GetRootView(), &pt
);
140 View::ConvertPointFromScreen(owner_
->GetWidget()->GetRootView(), &pt
);
141 arrow_offset
= pt
.x();
142 if (arrow() == BubbleBorder::BOTTOM_RIGHT
)
143 arrow_offset
= width
- arrow_offset
;
144 arrow_offset
= std::max(arrow_offset
, kArrowMinOffset
);
147 if (tray_arrow_offset_
==
148 TrayBubbleView::InitParams::kArrowDefaultOffset
) {
149 arrow_offset
= kArrowMinOffset
;
151 gfx::Point
pt(0, tray_arrow_offset_
);
152 View::ConvertPointToScreen(anchor_
->GetWidget()->GetRootView(), &pt
);
153 View::ConvertPointFromScreen(owner_
->GetWidget()->GetRootView(), &pt
);
154 arrow_offset
= pt
.y();
155 arrow_offset
= std::max(arrow_offset
, kArrowMinOffset
);
158 set_arrow_offset(arrow_offset
);
164 const int tray_arrow_offset_
;
166 // If true the first item should not get any additional spacing against the
167 // anchor (without the bubble tip the bubble should be flush to the shelf).
168 const bool first_item_has_no_margin_
;
170 DISALLOW_COPY_AND_ASSIGN(TrayBubbleBorder
);
173 // This mask layer clips the bubble's content so that it does not overwrite the
174 // rounded bubble corners.
175 // TODO(miket): This does not work on Windows. Implement layer masking or
176 // alternate solutions if the TrayBubbleView is needed there in the future.
177 class TrayBubbleContentMask
: public ui::LayerDelegate
{
179 explicit TrayBubbleContentMask(int corner_radius
);
180 ~TrayBubbleContentMask() override
;
182 ui::Layer
* layer() { return &layer_
; }
184 // Overridden from LayerDelegate.
185 void OnPaintLayer(gfx::Canvas
* canvas
) override
;
186 void OnDelegatedFrameDamage(const gfx::Rect
& damage_rect_in_dip
) override
{}
187 void OnDeviceScaleFactorChanged(float device_scale_factor
) override
;
188 base::Closure
PrepareForLayerBoundsChange() override
;
194 DISALLOW_COPY_AND_ASSIGN(TrayBubbleContentMask
);
197 TrayBubbleContentMask::TrayBubbleContentMask(int corner_radius
)
198 : layer_(ui::LAYER_TEXTURED
),
199 corner_radius_(corner_radius
) {
200 layer_
.set_delegate(this);
203 TrayBubbleContentMask::~TrayBubbleContentMask() {
204 layer_
.set_delegate(NULL
);
207 void TrayBubbleContentMask::OnPaintLayer(gfx::Canvas
* canvas
) {
210 paint
.setStyle(SkPaint::kFill_Style
);
211 gfx::Rect
rect(layer()->bounds().size());
212 canvas
->DrawRoundRect(rect
, corner_radius_
, paint
);
215 void TrayBubbleContentMask::OnDeviceScaleFactorChanged(
216 float device_scale_factor
) {
217 // Redrawing will take care of scale factor change.
220 base::Closure
TrayBubbleContentMask::PrepareForLayerBoundsChange() {
221 return base::Closure();
224 // Custom layout for the bubble-view. Does the default box-layout if there is
225 // enough height. Otherwise, makes sure the bottom rows are visible.
226 class BottomAlignedBoxLayout
: public BoxLayout
{
228 explicit BottomAlignedBoxLayout(TrayBubbleView
* bubble_view
)
229 : BoxLayout(BoxLayout::kVertical
, 0, 0, 0),
230 bubble_view_(bubble_view
) {
233 ~BottomAlignedBoxLayout() override
{}
236 void Layout(View
* host
) override
{
237 if (host
->height() >= host
->GetPreferredSize().height() ||
238 !bubble_view_
->is_gesture_dragging()) {
239 BoxLayout::Layout(host
);
243 int consumed_height
= 0;
244 for (int i
= host
->child_count() - 1;
245 i
>= 0 && consumed_height
< host
->height(); --i
) {
246 View
* child
= host
->child_at(i
);
247 if (!child
->visible())
249 gfx::Size size
= child
->GetPreferredSize();
250 child
->SetBounds(0, host
->height() - consumed_height
- size
.height(),
251 host
->width(), size
.height());
252 consumed_height
+= size
.height();
256 TrayBubbleView
* bubble_view_
;
258 DISALLOW_COPY_AND_ASSIGN(BottomAlignedBoxLayout
);
261 } // namespace internal
263 using internal::TrayBubbleBorder
;
264 using internal::TrayBubbleContentMask
;
265 using internal::BottomAlignedBoxLayout
;
268 const int TrayBubbleView::InitParams::kArrowDefaultOffset
= -1;
270 TrayBubbleView::InitParams::InitParams(AnchorType anchor_type
,
271 AnchorAlignment anchor_alignment
,
274 : anchor_type(anchor_type
),
275 anchor_alignment(anchor_alignment
),
276 min_width(min_width
),
277 max_width(max_width
),
280 close_on_deactivate(true),
281 arrow_color(SK_ColorBLACK
),
282 first_item_has_no_margin(false),
283 arrow(BubbleBorder::NONE
),
284 arrow_offset(kArrowDefaultOffset
),
285 arrow_paint_type(BubbleBorder::PAINT_NORMAL
),
286 shadow(BubbleBorder::BIG_SHADOW
),
287 arrow_alignment(BubbleBorder::ALIGN_EDGE_TO_ANCHOR_EDGE
) {
291 TrayBubbleView
* TrayBubbleView::Create(gfx::NativeView parent_window
,
294 InitParams
* init_params
) {
295 // Set arrow here so that it can be passed to the BubbleView constructor.
296 if (init_params
->anchor_type
== ANCHOR_TYPE_TRAY
) {
297 if (init_params
->anchor_alignment
== ANCHOR_ALIGNMENT_BOTTOM
) {
298 init_params
->arrow
= base::i18n::IsRTL() ?
299 BubbleBorder::BOTTOM_LEFT
: BubbleBorder::BOTTOM_RIGHT
;
300 } else if (init_params
->anchor_alignment
== ANCHOR_ALIGNMENT_TOP
) {
301 init_params
->arrow
= BubbleBorder::TOP_LEFT
;
302 } else if (init_params
->anchor_alignment
== ANCHOR_ALIGNMENT_LEFT
) {
303 init_params
->arrow
= BubbleBorder::LEFT_BOTTOM
;
305 init_params
->arrow
= BubbleBorder::RIGHT_BOTTOM
;
308 init_params
->arrow
= BubbleBorder::NONE
;
311 return new TrayBubbleView(parent_window
, anchor
, delegate
, *init_params
);
314 TrayBubbleView::TrayBubbleView(gfx::NativeView parent_window
,
317 const InitParams
& init_params
)
318 : BubbleDelegateView(anchor
, init_params
.arrow
),
319 params_(init_params
),
321 preferred_width_(init_params
.min_width
),
322 bubble_border_(NULL
),
323 is_gesture_dragging_(false),
324 mouse_actively_entered_(false) {
325 set_parent_window(parent_window
);
326 set_notify_enter_exit_on_child(true);
327 set_close_on_deactivate(init_params
.close_on_deactivate
);
328 set_margins(gfx::Insets());
329 bubble_border_
= new TrayBubbleBorder(this, GetAnchorView(), params_
);
330 SetPaintToLayer(true);
331 SetFillsBoundsOpaquely(true);
333 bubble_content_mask_
.reset(
334 new TrayBubbleContentMask(bubble_border_
->GetBorderCornerRadius()));
337 TrayBubbleView::~TrayBubbleView() {
338 mouse_watcher_
.reset();
339 // Inform host items (models) that their views are being destroyed.
341 delegate_
->BubbleViewDestroyed();
344 void TrayBubbleView::InitializeAndShowBubble() {
345 // Must occur after call to BubbleDelegateView::CreateBubble().
346 SetAlignment(params_
.arrow_alignment
);
347 bubble_border_
->UpdateArrowOffset();
349 layer()->parent()->SetMaskLayer(bubble_content_mask_
->layer());
352 GetWidget()->GetNativeWindow()->SetEventTargeter(
353 scoped_ptr
<ui::EventTargeter
>(new BubbleWindowTargeter(this)));
357 void TrayBubbleView::UpdateBubble() {
359 bubble_content_mask_
->layer()->SetBounds(layer()->bounds());
360 GetWidget()->GetRootView()->SchedulePaint();
363 void TrayBubbleView::SetMaxHeight(int height
) {
364 params_
.max_height
= height
;
369 void TrayBubbleView::SetWidth(int width
) {
370 width
= std::max(std::min(width
, params_
.max_width
), params_
.min_width
);
371 if (preferred_width_
== width
)
373 preferred_width_
= width
;
378 void TrayBubbleView::SetArrowPaintType(
379 views::BubbleBorder::ArrowPaintType paint_type
) {
380 bubble_border_
->set_paint_arrow(paint_type
);
384 gfx::Insets
TrayBubbleView::GetBorderInsets() const {
385 return bubble_border_
->GetInsets();
388 void TrayBubbleView::Init() {
389 BoxLayout
* layout
= new BottomAlignedBoxLayout(this);
390 layout
->SetDefaultFlex(1);
391 SetLayoutManager(layout
);
394 gfx::Rect
TrayBubbleView::GetAnchorRect() const {
397 return delegate_
->GetAnchorRect(anchor_widget(),
399 params_
.anchor_alignment
);
402 bool TrayBubbleView::CanActivate() const {
403 return params_
.can_activate
;
406 NonClientFrameView
* TrayBubbleView::CreateNonClientFrameView(Widget
* widget
) {
407 BubbleFrameView
* frame
= new BubbleFrameView(margins());
408 frame
->SetBubbleBorder(scoped_ptr
<views::BubbleBorder
>(bubble_border_
));
412 bool TrayBubbleView::WidgetHasHitTestMask() const {
416 void TrayBubbleView::GetWidgetHitTestMask(gfx::Path
* mask
) const {
418 mask
->addRect(gfx::RectToSkRect(GetBubbleFrameView()->GetContentsBounds()));
421 gfx::Size
TrayBubbleView::GetPreferredSize() const {
422 return gfx::Size(preferred_width_
, GetHeightForWidth(preferred_width_
));
425 gfx::Size
TrayBubbleView::GetMaximumSize() const {
426 gfx::Size size
= GetPreferredSize();
427 size
.set_width(params_
.max_width
);
431 int TrayBubbleView::GetHeightForWidth(int width
) const {
432 int height
= GetInsets().height();
433 width
= std::max(width
- GetInsets().width(), 0);
434 for (int i
= 0; i
< child_count(); ++i
) {
435 const View
* child
= child_at(i
);
436 if (child
->visible())
437 height
+= child
->GetHeightForWidth(width
);
440 return (params_
.max_height
!= 0) ?
441 std::min(height
, params_
.max_height
) : height
;
444 void TrayBubbleView::OnMouseEntered(const ui::MouseEvent
& event
) {
445 mouse_watcher_
.reset();
446 if (delegate_
&& !(event
.flags() & ui::EF_IS_SYNTHESIZED
)) {
447 // Coming here the user was actively moving the mouse over the bubble and
448 // we inform the delegate that we entered. This will prevent the bubble
450 delegate_
->OnMouseEnteredView();
451 mouse_actively_entered_
= true;
453 // Coming here the bubble got shown and the mouse was 'accidentally' over it
454 // which is not a reason to prevent the bubble to auto close. As such we
455 // do not call the delegate, but wait for the first mouse move within the
456 // bubble. The used MouseWatcher will notify use of a movement and call
457 // |MouseMovedOutOfHost|.
458 mouse_watcher_
.reset(new MouseWatcher(
459 new views::internal::MouseMoveDetectorHost(),
461 // Set the mouse sampling frequency to roughly a frame time so that the user
463 mouse_watcher_
->set_notify_on_exit_time(
464 base::TimeDelta::FromMilliseconds(kFrameTimeInMS
));
465 mouse_watcher_
->Start();
469 void TrayBubbleView::OnMouseExited(const ui::MouseEvent
& event
) {
470 // If there was a mouse watcher waiting for mouse movements we disable it
471 // immediately since we now leave the bubble.
472 mouse_watcher_
.reset();
473 // Do not notify the delegate of an exit if we never told it that we entered.
474 if (delegate_
&& mouse_actively_entered_
)
475 delegate_
->OnMouseExitedView();
478 void TrayBubbleView::GetAccessibleState(ui::AXViewState
* state
) {
479 if (delegate_
&& params_
.can_activate
) {
480 state
->role
= ui::AX_ROLE_WINDOW
;
481 state
->name
= delegate_
->GetAccessibleNameForBubble();
485 void TrayBubbleView::MouseMovedOutOfHost() {
486 // The mouse was accidentally over the bubble when it opened and the AutoClose
487 // logic was not activated. Now that the user did move the mouse we tell the
488 // delegate to disable AutoClose.
489 delegate_
->OnMouseEnteredView();
490 mouse_actively_entered_
= true;
491 mouse_watcher_
->Stop();
494 void TrayBubbleView::ChildPreferredSizeChanged(View
* child
) {
498 void TrayBubbleView::ViewHierarchyChanged(
499 const ViewHierarchyChangedDetails
& details
) {
500 if (details
.is_add
&& details
.child
== this) {
501 details
.parent
->SetPaintToLayer(true);
502 details
.parent
->SetFillsBoundsOpaquely(true);
503 details
.parent
->layer()->SetMasksToBounds(true);