1 // Copyright 2013 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 "ash/shelf/shelf_tooltip_manager.h"
7 #include "ash/shelf/shelf_layout_manager.h"
8 #include "ash/shelf/shelf_view.h"
10 #include "ash/shell_window_ids.h"
11 #include "ash/wm/window_animations.h"
12 #include "base/bind.h"
13 #include "base/message_loop/message_loop.h"
14 #include "base/time/time.h"
15 #include "base/timer/timer.h"
16 #include "ui/aura/window.h"
17 #include "ui/aura/window_event_dispatcher.h"
18 #include "ui/events/event.h"
19 #include "ui/events/event_constants.h"
20 #include "ui/gfx/insets.h"
21 #include "ui/views/bubble/bubble_delegate.h"
22 #include "ui/views/bubble/bubble_frame_view.h"
23 #include "ui/views/controls/label.h"
24 #include "ui/views/layout/fill_layout.h"
25 #include "ui/views/widget/widget.h"
29 const int kTooltipTopBottomMargin
= 3;
30 const int kTooltipLeftRightMargin
= 10;
31 const int kTooltipAppearanceDelay
= 1000; // msec
32 const int kTooltipMinHeight
= 29 - 2 * kTooltipTopBottomMargin
;
33 const SkColor kTooltipTextColor
= SkColorSetRGB(0x22, 0x22, 0x22);
35 // The maximum width of the tooltip bubble. Borrowed the value from
36 // ash/tooltip/tooltip_controller.cc
37 const int kTooltipMaxWidth
= 250;
39 // The offset for the tooltip bubble - making sure that the bubble is flush
40 // with the shelf. The offset includes the arrow size in pixels as well as
41 // the activation bar and other spacing elements.
42 const int kArrowOffsetLeftRight
= 11;
43 const int kArrowOffsetTopBottom
= 7;
47 // The implementation of tooltip of the launcher.
48 class ShelfTooltipManager::ShelfTooltipBubble
49 : public views::BubbleDelegateView
{
51 ShelfTooltipBubble(views::View
* anchor
,
52 views::BubbleBorder::Arrow arrow
,
53 ShelfTooltipManager
* host
);
55 void SetText(const base::string16
& text
);
59 // views::WidgetDelegate overrides:
60 virtual void WindowClosing() OVERRIDE
;
62 // views::View overrides:
63 virtual gfx::Size
GetPreferredSize() OVERRIDE
;
65 ShelfTooltipManager
* host_
;
68 DISALLOW_COPY_AND_ASSIGN(ShelfTooltipBubble
);
71 ShelfTooltipManager::ShelfTooltipBubble::ShelfTooltipBubble(
73 views::BubbleBorder::Arrow arrow
,
74 ShelfTooltipManager
* host
)
75 : views::BubbleDelegateView(anchor
, arrow
), host_(host
) {
76 // Make sure that the bubble follows the animation of the shelf.
77 set_move_with_anchor(true);
78 gfx::Insets insets
= gfx::Insets(kArrowOffsetTopBottom
,
79 kArrowOffsetLeftRight
,
80 kArrowOffsetTopBottom
,
81 kArrowOffsetLeftRight
);
82 // Shelf items can have an asymmetrical border for spacing reasons.
83 // Adjust anchor location for this.
85 insets
+= anchor
->border()->GetInsets();
87 set_anchor_view_insets(insets
);
88 set_close_on_esc(false);
89 set_close_on_deactivate(false);
90 set_use_focusless(true);
91 set_accept_events(false);
92 set_margins(gfx::Insets(kTooltipTopBottomMargin
, kTooltipLeftRightMargin
,
93 kTooltipTopBottomMargin
, kTooltipLeftRightMargin
));
94 set_shadow(views::BubbleBorder::SMALL_SHADOW
);
95 SetLayoutManager(new views::FillLayout());
96 // The anchor may not have the widget in tests.
97 if (anchor
->GetWidget() && anchor
->GetWidget()->GetNativeView()) {
98 aura::Window
* root_window
=
99 anchor
->GetWidget()->GetNativeView()->GetRootWindow();
100 set_parent_window(ash::Shell::GetInstance()->GetContainer(
101 root_window
, ash::kShellWindowId_SettingBubbleContainer
));
103 label_
= new views::Label
;
104 label_
->SetHorizontalAlignment(gfx::ALIGN_LEFT
);
105 label_
->SetEnabledColor(kTooltipTextColor
);
106 AddChildView(label_
);
107 views::BubbleDelegateView::CreateBubble(this);
110 void ShelfTooltipManager::ShelfTooltipBubble::SetText(
111 const base::string16
& text
) {
112 label_
->SetText(text
);
116 void ShelfTooltipManager::ShelfTooltipBubble::Close() {
119 GetWidget()->Close();
123 void ShelfTooltipManager::ShelfTooltipBubble::WindowClosing() {
124 views::BubbleDelegateView::WindowClosing();
126 host_
->OnBubbleClosed(this);
129 gfx::Size
ShelfTooltipManager::ShelfTooltipBubble::GetPreferredSize() {
130 gfx::Size pref_size
= views::BubbleDelegateView::GetPreferredSize();
131 if (pref_size
.height() < kTooltipMinHeight
)
132 pref_size
.set_height(kTooltipMinHeight
);
133 if (pref_size
.width() > kTooltipMaxWidth
)
134 pref_size
.set_width(kTooltipMaxWidth
);
138 ShelfTooltipManager::ShelfTooltipManager(
139 ShelfLayoutManager
* shelf_layout_manager
,
140 ShelfView
* shelf_view
)
144 shelf_layout_manager_(shelf_layout_manager
),
145 shelf_view_(shelf_view
),
146 weak_factory_(this) {
147 if (shelf_layout_manager
)
148 shelf_layout_manager
->AddObserver(this);
149 if (Shell::HasInstance())
150 Shell::GetInstance()->AddPreTargetHandler(this);
153 ShelfTooltipManager::~ShelfTooltipManager() {
154 CancelHidingAnimation();
156 if (shelf_layout_manager_
)
157 shelf_layout_manager_
->RemoveObserver(this);
158 if (Shell::HasInstance())
159 Shell::GetInstance()->RemovePreTargetHandler(this);
162 void ShelfTooltipManager::ShowDelayed(views::View
* anchor
,
163 const base::string16
& text
) {
165 if (timer_
.get() && timer_
->IsRunning()) {
168 CancelHidingAnimation();
173 if (shelf_layout_manager_
&& !shelf_layout_manager_
->IsVisible())
176 CreateBubble(anchor
, text
);
180 void ShelfTooltipManager::ShowImmediately(views::View
* anchor
,
181 const base::string16
& text
) {
183 if (timer_
.get() && timer_
->IsRunning())
185 CancelHidingAnimation();
189 if (shelf_layout_manager_
&& !shelf_layout_manager_
->IsVisible())
192 CreateBubble(anchor
, text
);
196 void ShelfTooltipManager::Close() {
205 void ShelfTooltipManager::OnBubbleClosed(views::BubbleDelegateView
* view
) {
212 void ShelfTooltipManager::UpdateArrow() {
214 CancelHidingAnimation();
216 ShowImmediately(anchor_
, text_
);
220 void ShelfTooltipManager::ResetTimer() {
221 if (timer_
.get() && timer_
->IsRunning()) {
226 // We don't start the timer if the shelf isn't visible.
227 if (shelf_layout_manager_
&& !shelf_layout_manager_
->IsVisible())
230 CreateTimer(kTooltipAppearanceDelay
);
233 void ShelfTooltipManager::StopTimer() {
237 bool ShelfTooltipManager::IsVisible() {
238 if (timer_
.get() && timer_
->IsRunning())
241 return widget_
&& widget_
->IsVisible();
244 void ShelfTooltipManager::CreateZeroDelayTimerForTest() {
248 void ShelfTooltipManager::OnMouseEvent(ui::MouseEvent
* event
) {
250 DCHECK(event
->target());
251 if (!widget_
|| !widget_
->IsVisible())
257 // Pressing the mouse button anywhere should close the tooltip.
258 if (event
->type() == ui::ET_MOUSE_PRESSED
) {
263 aura::Window
* target
= static_cast<aura::Window
*>(event
->target());
264 if (widget_
->GetNativeWindow()->GetRootWindow() != target
->GetRootWindow()) {
269 gfx::Point location_in_shelf_view
= event
->location();
270 aura::Window::ConvertPointToTarget(
271 target
, shelf_view_
->GetWidget()->GetNativeWindow(),
272 &location_in_shelf_view
);
274 if (shelf_view_
->ShouldHideTooltip(location_in_shelf_view
)) {
275 // Because this mouse event may arrive to |view_|, here we just schedule
276 // the closing event rather than directly calling Close().
281 void ShelfTooltipManager::OnTouchEvent(ui::TouchEvent
* event
) {
282 aura::Window
* target
= static_cast<aura::Window
*>(event
->target());
283 if (widget_
&& widget_
->IsVisible() && widget_
->GetNativeWindow() != target
)
287 void ShelfTooltipManager::OnGestureEvent(ui::GestureEvent
* event
) {
288 if (widget_
&& widget_
->IsVisible()) {
289 // Because this mouse event may arrive to |view_|, here we just schedule
290 // the closing event rather than directly calling Close().
295 void ShelfTooltipManager::OnCancelMode(ui::CancelModeEvent
* event
) {
299 void ShelfTooltipManager::WillDeleteShelf() {
300 shelf_layout_manager_
= NULL
;
303 void ShelfTooltipManager::WillChangeVisibilityState(
304 ShelfVisibilityState new_state
) {
305 if (new_state
== SHELF_HIDDEN
) {
311 void ShelfTooltipManager::OnAutoHideStateChanged(ShelfAutoHideState new_state
) {
312 if (new_state
== SHELF_AUTO_HIDE_HIDDEN
) {
314 // AutoHide state change happens during an event filter, so immediate close
315 // may cause a crash in the HandleMouseEvent() after the filter. So we just
316 // schedule the Close here.
321 void ShelfTooltipManager::CancelHidingAnimation() {
322 if (!widget_
|| !widget_
->GetNativeView())
325 gfx::NativeView native_view
= widget_
->GetNativeView();
326 wm::SetWindowVisibilityAnimationTransition(
327 native_view
, wm::ANIMATE_NONE
);
330 void ShelfTooltipManager::CloseSoon() {
331 base::MessageLoopForUI::current()->PostTask(
333 base::Bind(&ShelfTooltipManager::Close
, weak_factory_
.GetWeakPtr()));
336 void ShelfTooltipManager::ShowInternal() {
338 view_
->GetWidget()->Show();
343 void ShelfTooltipManager::CreateBubble(views::View
* anchor
,
344 const base::string16
& text
) {
349 views::BubbleBorder::Arrow arrow
=
350 shelf_layout_manager_
->SelectValueForShelfAlignment(
351 views::BubbleBorder::BOTTOM_CENTER
,
352 views::BubbleBorder::LEFT_CENTER
,
353 views::BubbleBorder::RIGHT_CENTER
,
354 views::BubbleBorder::TOP_CENTER
);
356 view_
= new ShelfTooltipBubble(anchor
, arrow
, this);
357 widget_
= view_
->GetWidget();
358 view_
->SetText(text_
);
360 gfx::NativeView native_view
= widget_
->GetNativeView();
361 wm::SetWindowVisibilityAnimationType(
362 native_view
, wm::WINDOW_VISIBILITY_ANIMATION_TYPE_VERTICAL
);
363 wm::SetWindowVisibilityAnimationTransition(
364 native_view
, wm::ANIMATE_HIDE
);
367 void ShelfTooltipManager::CreateTimer(int delay_in_ms
) {
368 base::OneShotTimer
<ShelfTooltipManager
>* new_timer
=
369 new base::OneShotTimer
<ShelfTooltipManager
>();
370 new_timer
->Start(FROM_HERE
,
371 base::TimeDelta::FromMilliseconds(delay_in_ms
),
373 &ShelfTooltipManager::ShowInternal
);
374 timer_
.reset(new_timer
);