OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ash/shelf/shelf_tooltip_manager.h" | 5 #include "ash/shelf/shelf_tooltip_manager.h" |
6 | 6 |
7 #include "ash/shelf/shelf.h" | 7 #include "ash/shelf/shelf.h" |
8 #include "ash/shelf/shelf_layout_manager.h" | 8 #include "ash/shelf/shelf_layout_manager.h" |
9 #include "ash/shelf/shelf_view.h" | 9 #include "ash/shelf/shelf_view.h" |
10 #include "ash/shell.h" | 10 #include "ash/shell.h" |
11 #include "ash/shell_window_ids.h" | 11 #include "ash/shell_window_ids.h" |
12 #include "ash/wm/window_animations.h" | 12 #include "ash/wm/window_animations.h" |
13 #include "base/bind.h" | 13 #include "base/bind.h" |
14 #include "base/thread_task_runner_handle.h" | 14 #include "base/thread_task_runner_handle.h" |
15 #include "base/time/time.h" | 15 #include "base/time/time.h" |
16 #include "ui/aura/window.h" | 16 #include "ui/aura/window.h" |
17 #include "ui/events/event.h" | 17 #include "ui/events/event.h" |
18 #include "ui/events/event_constants.h" | 18 #include "ui/events/event_constants.h" |
19 #include "ui/gfx/geometry/insets.h" | 19 #include "ui/gfx/geometry/insets.h" |
20 #include "ui/views/bubble/bubble_delegate.h" | 20 #include "ui/views/bubble/bubble_delegate.h" |
21 #include "ui/views/controls/label.h" | 21 #include "ui/views/controls/label.h" |
22 #include "ui/views/layout/fill_layout.h" | 22 #include "ui/views/layout/fill_layout.h" |
23 #include "ui/views/widget/widget.h" | 23 #include "ui/views/widget/widget.h" |
24 | 24 |
25 namespace ash { | 25 namespace ash { |
26 namespace { | 26 namespace { |
27 | |
28 const int kTooltipTopBottomMargin = 3; | 27 const int kTooltipTopBottomMargin = 3; |
29 const int kTooltipLeftRightMargin = 10; | 28 const int kTooltipLeftRightMargin = 10; |
30 const int kTooltipAppearanceDelay = 1000; // msec | 29 const int kTooltipAppearanceDelay = 1000; // msec |
31 const int kTooltipMinHeight = 29 - 2 * kTooltipTopBottomMargin; | 30 const int kTooltipMinHeight = 29 - 2 * kTooltipTopBottomMargin; |
32 const SkColor kTooltipTextColor = SkColorSetRGB(0x22, 0x22, 0x22); | 31 const SkColor kTooltipTextColor = SkColorSetRGB(0x22, 0x22, 0x22); |
33 | 32 |
34 // The maximum width of the tooltip bubble. Borrowed the value from | 33 // The maximum width of the tooltip bubble. Borrowed the value from |
35 // ash/tooltip/tooltip_controller.cc | 34 // ash/tooltip/tooltip_controller.cc |
36 const int kTooltipMaxWidth = 250; | 35 const int kTooltipMaxWidth = 250; |
37 | 36 |
38 // The offset for the tooltip bubble - making sure that the bubble is flush | 37 // The offset for the tooltip bubble - making sure that the bubble is flush |
39 // with the shelf. The offset includes the arrow size in pixels as well as | 38 // with the shelf. The offset includes the arrow size in pixels as well as |
40 // the activation bar and other spacing elements. | 39 // the activation bar and other spacing elements. |
41 const int kArrowOffsetLeftRight = 11; | 40 const int kArrowOffsetLeftRight = 11; |
42 const int kArrowOffsetTopBottom = 7; | 41 const int kArrowOffsetTopBottom = 7; |
43 | 42 |
44 } // namespace | 43 } // namespace |
45 | 44 |
46 // The implementation of tooltip of the launcher. | 45 // The implementation of tooltip of the launcher. |
47 class ShelfTooltipManager::ShelfTooltipBubble | 46 class ShelfTooltipManager::ShelfTooltipBubble |
48 : public views::BubbleDelegateView { | 47 : public views::BubbleDelegateView { |
49 public: | 48 public: |
50 ShelfTooltipBubble(views::View* anchor, | 49 ShelfTooltipBubble(views::View* anchor, |
51 views::BubbleBorder::Arrow arrow, | 50 views::BubbleBorder::Arrow arrow, |
52 const base::string16& text) | 51 const base::string16& text); |
53 : views::BubbleDelegateView(anchor, arrow) { | |
54 gfx::Insets insets = | |
55 gfx::Insets(kArrowOffsetTopBottom, kArrowOffsetLeftRight); | |
56 // Adjust the anchor location for asymmetrical borders of shelf item. | |
57 if (anchor->border()) | |
58 insets += anchor->border()->GetInsets(); | |
59 | |
60 set_anchor_view_insets(insets); | |
61 set_close_on_esc(false); | |
62 set_close_on_deactivate(false); | |
63 set_can_activate(false); | |
64 set_accept_events(false); | |
65 set_margins(gfx::Insets(kTooltipTopBottomMargin, kTooltipLeftRightMargin)); | |
66 set_shadow(views::BubbleBorder::SMALL_SHADOW); | |
67 SetLayoutManager(new views::FillLayout()); | |
68 // The anchor may not have the widget in tests. | |
69 if (anchor->GetWidget() && anchor->GetWidget()->GetNativeWindow()) { | |
70 set_parent_window(ash::Shell::GetContainer( | |
71 anchor->GetWidget()->GetNativeWindow()->GetRootWindow(), | |
72 ash::kShellWindowId_SettingBubbleContainer)); | |
73 } | |
74 views::Label* label = new views::Label(text); | |
75 label->SetHorizontalAlignment(gfx::ALIGN_LEFT); | |
76 label->SetEnabledColor(kTooltipTextColor); | |
77 AddChildView(label); | |
78 views::BubbleDelegateView::CreateBubble(this); | |
79 SizeToContents(); | |
80 } | |
81 | 52 |
82 private: | 53 private: |
83 // views::View overrides: | 54 // views::View overrides: |
84 gfx::Size GetPreferredSize() const override { | 55 gfx::Size GetPreferredSize() const override; |
85 const gfx::Size size = views::BubbleDelegateView::GetPreferredSize(); | |
86 return gfx::Size(std::min(size.width(), kTooltipMaxWidth), | |
87 std::max(size.height(), kTooltipMinHeight)); | |
88 } | |
89 | 56 |
90 DISALLOW_COPY_AND_ASSIGN(ShelfTooltipBubble); | 57 DISALLOW_COPY_AND_ASSIGN(ShelfTooltipBubble); |
91 }; | 58 }; |
92 | 59 |
| 60 ShelfTooltipManager::ShelfTooltipBubble::ShelfTooltipBubble( |
| 61 views::View* anchor, |
| 62 views::BubbleBorder::Arrow arrow, |
| 63 const base::string16& text) |
| 64 : views::BubbleDelegateView(anchor, arrow) { |
| 65 gfx::Insets insets = gfx::Insets(kArrowOffsetTopBottom, |
| 66 kArrowOffsetLeftRight); |
| 67 // Adjust the anchor location for asymmetrical borders of shelf item. |
| 68 if (anchor->border()) |
| 69 insets += anchor->border()->GetInsets(); |
| 70 |
| 71 set_anchor_view_insets(insets); |
| 72 set_close_on_esc(false); |
| 73 set_close_on_deactivate(false); |
| 74 set_can_activate(false); |
| 75 set_accept_events(false); |
| 76 set_margins(gfx::Insets(kTooltipTopBottomMargin, kTooltipLeftRightMargin)); |
| 77 set_shadow(views::BubbleBorder::SMALL_SHADOW); |
| 78 SetLayoutManager(new views::FillLayout()); |
| 79 // The anchor may not have the widget in tests. |
| 80 if (anchor->GetWidget() && anchor->GetWidget()->GetNativeWindow()) { |
| 81 set_parent_window(ash::Shell::GetContainer( |
| 82 anchor->GetWidget()->GetNativeWindow()->GetRootWindow(), |
| 83 ash::kShellWindowId_SettingBubbleContainer)); |
| 84 } |
| 85 views::Label* label = new views::Label(text); |
| 86 label->SetHorizontalAlignment(gfx::ALIGN_LEFT); |
| 87 label->SetEnabledColor(kTooltipTextColor); |
| 88 AddChildView(label); |
| 89 views::BubbleDelegateView::CreateBubble(this); |
| 90 SizeToContents(); |
| 91 } |
| 92 |
| 93 gfx::Size ShelfTooltipManager::ShelfTooltipBubble::GetPreferredSize() const { |
| 94 const gfx::Size size = views::BubbleDelegateView::GetPreferredSize(); |
| 95 return gfx::Size(std::min(size.width(), kTooltipMaxWidth), |
| 96 std::max(size.height(), kTooltipMinHeight)); |
| 97 } |
| 98 |
93 ShelfTooltipManager::ShelfTooltipManager(ShelfView* shelf_view) | 99 ShelfTooltipManager::ShelfTooltipManager(ShelfView* shelf_view) |
94 : timer_delay_(kTooltipAppearanceDelay), | 100 : timer_delay_(kTooltipAppearanceDelay), |
95 shelf_view_(shelf_view), | 101 shelf_view_(shelf_view), |
96 shelf_layout_manager_(nullptr), | 102 shelf_layout_manager_(nullptr), |
97 bubble_(nullptr), | 103 bubble_(nullptr), |
98 weak_factory_(this) {} | 104 weak_factory_(this) {} |
99 | 105 |
100 ShelfTooltipManager::~ShelfTooltipManager() { | 106 ShelfTooltipManager::~ShelfTooltipManager() { |
101 WillDeleteShelf(); | 107 WillDeleteShelf(); |
102 } | 108 } |
103 | 109 |
104 void ShelfTooltipManager::Init() { | 110 void ShelfTooltipManager::Init() { |
105 shelf_layout_manager_ = shelf_view_->shelf()->shelf_layout_manager(); | 111 shelf_layout_manager_ = shelf_view_->shelf()->shelf_layout_manager(); |
106 shelf_layout_manager_->AddObserver(this); | 112 shelf_layout_manager_->AddObserver(this); |
107 | 113 // TODO(msw): Capture events outside the shelf to close tooltips? |
108 // TODO(msw): Observe events outside the shelf in mash, to close tooltips. | 114 shelf_view_->GetWidget()->GetNativeWindow()->AddPreTargetHandler(this); |
109 aura::Window* shelf_window = shelf_view_->GetWidget()->GetNativeWindow(); | |
110 shelf_window->GetRootWindow()->AddPreTargetHandler(this); | |
111 } | 115 } |
112 | 116 |
113 void ShelfTooltipManager::Close() { | 117 void ShelfTooltipManager::Close() { |
114 timer_.Stop(); | 118 timer_.Stop(); |
115 if (bubble_) | 119 if (bubble_) |
116 bubble_->GetWidget()->Close(); | 120 bubble_->GetWidget()->Close(); |
117 bubble_ = nullptr; | 121 bubble_ = nullptr; |
118 } | 122 } |
119 | 123 |
120 bool ShelfTooltipManager::IsVisible() const { | 124 bool ShelfTooltipManager::IsVisible() const { |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
153 | 157 |
154 void ShelfTooltipManager::ShowTooltipWithDelay(views::View* view) { | 158 void ShelfTooltipManager::ShowTooltipWithDelay(views::View* view) { |
155 if (!shelf_layout_manager_ || shelf_layout_manager_->IsVisible()) { | 159 if (!shelf_layout_manager_ || shelf_layout_manager_->IsVisible()) { |
156 timer_.Start(FROM_HERE, base::TimeDelta::FromMilliseconds(timer_delay_), | 160 timer_.Start(FROM_HERE, base::TimeDelta::FromMilliseconds(timer_delay_), |
157 base::Bind(&ShelfTooltipManager::ShowTooltip, | 161 base::Bind(&ShelfTooltipManager::ShowTooltip, |
158 weak_factory_.GetWeakPtr(), view)); | 162 weak_factory_.GetWeakPtr(), view)); |
159 } | 163 } |
160 } | 164 } |
161 | 165 |
162 void ShelfTooltipManager::OnEvent(ui::Event* event) { | 166 void ShelfTooltipManager::OnEvent(ui::Event* event) { |
| 167 // Close the tooltip on mouse press or exit, and on most non-mouse events. |
163 if (event->type() == ui::ET_MOUSE_PRESSED || | 168 if (event->type() == ui::ET_MOUSE_PRESSED || |
164 event->type() == ui::ET_MOUSE_EXITED || !event->IsMouseEvent() || | 169 event->type() == ui::ET_MOUSE_EXITED || !event->IsMouseEvent()) { |
165 event->target() != shelf_view_->GetWidget()->GetNativeWindow()) { | |
166 if (!event->IsKeyEvent()) | 170 if (!event->IsKeyEvent()) |
167 Close(); | 171 Close(); |
168 return; | 172 return; |
169 } | 173 } |
170 | 174 |
171 gfx::Point point = static_cast<ui::LocatedEvent*>(event)->location(); | 175 gfx::Point point = static_cast<ui::LocatedEvent*>(event)->location(); |
172 aura::Window::ConvertPointToTarget( | |
173 static_cast<aura::Window*>(event->target()), | |
174 shelf_view_->GetWidget()->GetNativeWindow(), &point); | |
175 views::View::ConvertPointFromWidget(shelf_view_, &point); | 176 views::View::ConvertPointFromWidget(shelf_view_, &point); |
| 177 if (IsVisible() && shelf_view_->ShouldHideTooltip(point)) { |
| 178 Close(); |
| 179 return; |
| 180 } |
| 181 |
176 views::View* view = shelf_view_->GetTooltipHandlerForPoint(point); | 182 views::View* view = shelf_view_->GetTooltipHandlerForPoint(point); |
177 const bool should_show = shelf_view_->ShouldShowTooltipForView(view); | 183 const bool should_show = shelf_view_->ShouldShowTooltipForView(view); |
| 184 if (IsVisible() && bubble_->GetAnchorView() != view && should_show) |
| 185 ShowTooltip(view); |
178 | 186 |
179 timer_.Stop(); | 187 if (!IsVisible() && event->type() == ui::ET_MOUSE_MOVED) { |
180 if (IsVisible() && should_show && bubble_->GetAnchorView() != view) | 188 timer_.Stop(); |
181 ShowTooltip(view); | 189 if (should_show) |
182 else if (!IsVisible() && should_show && event->type() == ui::ET_MOUSE_MOVED) | 190 ShowTooltipWithDelay(view); |
183 ShowTooltipWithDelay(view); | 191 } |
184 else if (IsVisible() && shelf_view_->ShouldHideTooltip(point)) | |
185 Close(); | |
186 } | 192 } |
187 | 193 |
188 void ShelfTooltipManager::WillDeleteShelf() { | 194 void ShelfTooltipManager::WillDeleteShelf() { |
189 if (shelf_layout_manager_) | 195 if (shelf_layout_manager_) |
190 shelf_layout_manager_->RemoveObserver(this); | 196 shelf_layout_manager_->RemoveObserver(this); |
191 shelf_layout_manager_ = nullptr; | 197 shelf_layout_manager_ = nullptr; |
192 | 198 |
193 if (shelf_view_ && shelf_view_->GetWidget()) { | 199 views::Widget* widget = shelf_view_ ? shelf_view_->GetWidget() : nullptr; |
194 aura::Window* shelf_window = shelf_view_->GetWidget()->GetNativeWindow(); | 200 if (widget && widget->GetNativeWindow()) |
195 if (shelf_window && shelf_window->GetRootWindow()) | 201 widget->GetNativeWindow()->RemovePreTargetHandler(this); |
196 shelf_window->GetRootWindow()->RemovePreTargetHandler(this); | |
197 } | |
198 shelf_view_ = nullptr; | 202 shelf_view_ = nullptr; |
199 } | 203 } |
200 | 204 |
201 void ShelfTooltipManager::WillChangeVisibilityState( | 205 void ShelfTooltipManager::WillChangeVisibilityState( |
202 ShelfVisibilityState new_state) { | 206 ShelfVisibilityState new_state) { |
203 if (new_state == SHELF_HIDDEN) | 207 if (new_state == SHELF_HIDDEN) |
204 Close(); | 208 Close(); |
205 } | 209 } |
206 | 210 |
207 void ShelfTooltipManager::OnAutoHideStateChanged(ShelfAutoHideState new_state) { | 211 void ShelfTooltipManager::OnAutoHideStateChanged(ShelfAutoHideState new_state) { |
208 if (new_state == SHELF_AUTO_HIDE_HIDDEN) { | 212 if (new_state == SHELF_AUTO_HIDE_HIDDEN) { |
209 timer_.Stop(); | 213 timer_.Stop(); |
210 // AutoHide state change happens during an event filter, so immediate close | 214 // AutoHide state change happens during an event filter, so immediate close |
211 // may cause a crash in the HandleMouseEvent() after the filter. So we just | 215 // may cause a crash in the HandleMouseEvent() after the filter. So we just |
212 // schedule the Close here. | 216 // schedule the Close here. |
213 base::ThreadTaskRunnerHandle::Get()->PostTask( | 217 base::ThreadTaskRunnerHandle::Get()->PostTask( |
214 FROM_HERE, | 218 FROM_HERE, |
215 base::Bind(&ShelfTooltipManager::Close, weak_factory_.GetWeakPtr())); | 219 base::Bind(&ShelfTooltipManager::Close, weak_factory_.GetWeakPtr())); |
216 } | 220 } |
217 } | 221 } |
218 | 222 |
219 } // namespace ash | 223 } // namespace ash |
OLD | NEW |