OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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/launcher/launcher_tooltip_manager.h" | 5 #include "ash/launcher/launcher_tooltip_manager.h" |
6 | 6 |
7 #include "ash/launcher/launcher_view.h" | |
7 #include "ash/shell.h" | 8 #include "ash/shell.h" |
8 #include "ash/shell_window_ids.h" | 9 #include "ash/shell_window_ids.h" |
9 #include "ash/wm/window_animations.h" | 10 #include "ash/wm/window_animations.h" |
10 #include "base/bind.h" | 11 #include "base/bind.h" |
11 #include "base/message_loop.h" | 12 #include "base/message_loop.h" |
12 #include "base/time.h" | 13 #include "base/time.h" |
13 #include "base/timer.h" | 14 #include "base/timer.h" |
15 #include "ui/aura/event.h" | |
16 #include "ui/aura/root_window.h" | |
14 #include "ui/aura/window.h" | 17 #include "ui/aura/window.h" |
18 #include "ui/base/events.h" | |
15 #include "ui/gfx/insets.h" | 19 #include "ui/gfx/insets.h" |
16 #include "ui/views/bubble/bubble_delegate.h" | 20 #include "ui/views/bubble/bubble_delegate.h" |
17 #include "ui/views/controls/label.h" | 21 #include "ui/views/controls/label.h" |
18 #include "ui/views/layout/fill_layout.h" | 22 #include "ui/views/layout/fill_layout.h" |
19 #include "ui/views/widget/widget.h" | 23 #include "ui/views/widget/widget.h" |
20 | 24 |
21 namespace ash { | 25 namespace ash { |
22 namespace internal { | 26 namespace internal { |
23 namespace { | 27 namespace { |
24 const int kTooltipMargin = 3; | 28 const int kTooltipMargin = 3; |
(...skipping 19 matching lines...) Expand all Loading... | |
44 | 48 |
45 // The implementation of tooltip of the launcher. | 49 // The implementation of tooltip of the launcher. |
46 class LauncherTooltipManager::LauncherTooltipBubble | 50 class LauncherTooltipManager::LauncherTooltipBubble |
47 : public views::BubbleDelegateView { | 51 : public views::BubbleDelegateView { |
48 public: | 52 public: |
49 LauncherTooltipBubble(views::View* anchor, | 53 LauncherTooltipBubble(views::View* anchor, |
50 views::BubbleBorder::ArrowLocation arrow_location, | 54 views::BubbleBorder::ArrowLocation arrow_location, |
51 LauncherTooltipManager* host); | 55 LauncherTooltipManager* host); |
52 | 56 |
53 void SetText(const string16& text); | 57 void SetText(const string16& text); |
58 void Close(); | |
54 | 59 |
55 private: | 60 private: |
56 // views::View overrides: | |
57 virtual void OnMouseExited(const views::MouseEvent& event) OVERRIDE; | |
58 | |
59 // views::WidgetDelegate overrides; | 61 // views::WidgetDelegate overrides; |
60 virtual void WindowClosing() OVERRIDE; | 62 virtual void WindowClosing() OVERRIDE; |
61 | 63 |
62 LauncherTooltipManager* host_; | 64 LauncherTooltipManager* host_; |
63 views::Label* label_; | 65 views::Label* label_; |
64 }; | 66 }; |
65 | 67 |
66 LauncherTooltipManager::LauncherTooltipBubble::LauncherTooltipBubble( | 68 LauncherTooltipManager::LauncherTooltipBubble::LauncherTooltipBubble( |
67 views::View* anchor, | 69 views::View* anchor, |
68 views::BubbleBorder::ArrowLocation arrow_location, | 70 views::BubbleBorder::ArrowLocation arrow_location, |
(...skipping 19 matching lines...) Expand all Loading... | |
88 } | 90 } |
89 | 91 |
90 void LauncherTooltipManager::LauncherTooltipBubble::SetText( | 92 void LauncherTooltipManager::LauncherTooltipBubble::SetText( |
91 const string16& text) { | 93 const string16& text) { |
92 label_->SetText(text); | 94 label_->SetText(text); |
93 label_->SetMultiLine(true); | 95 label_->SetMultiLine(true); |
94 label_->SizeToFit(kTooltipMaxWidth); | 96 label_->SizeToFit(kTooltipMaxWidth); |
95 SizeToContents(); | 97 SizeToContents(); |
96 } | 98 } |
97 | 99 |
98 void LauncherTooltipManager::LauncherTooltipBubble::OnMouseExited( | 100 void LauncherTooltipManager::LauncherTooltipBubble::Close() { |
99 const views::MouseEvent& event) { | 101 if (GetWidget()) { |
100 GetWidget()->Close(); | 102 host_ = NULL; |
101 host_->OnBubbleClosed(this); | 103 GetWidget()->Close(); |
104 } | |
102 } | 105 } |
103 | 106 |
104 void LauncherTooltipManager::LauncherTooltipBubble::WindowClosing() { | 107 void LauncherTooltipManager::LauncherTooltipBubble::WindowClosing() { |
105 views::BubbleDelegateView::WindowClosing(); | 108 views::BubbleDelegateView::WindowClosing(); |
106 host_->OnBubbleClosed(this); | 109 if (host_) |
110 host_->OnBubbleClosed(this); | |
107 } | 111 } |
108 | 112 |
109 LauncherTooltipManager::LauncherTooltipManager( | 113 LauncherTooltipManager::LauncherTooltipManager( |
110 ShelfAlignment alignment, ShelfLayoutManager* shelf_layout_manager) | 114 ShelfAlignment alignment, |
115 ShelfLayoutManager* shelf_layout_manager, | |
116 LauncherView* launcher_view) | |
111 : view_(NULL), | 117 : view_(NULL), |
118 widget_(NULL), | |
112 anchor_(NULL), | 119 anchor_(NULL), |
113 alignment_(alignment), | 120 alignment_(alignment), |
114 shelf_layout_manager_(shelf_layout_manager) { | 121 shelf_layout_manager_(shelf_layout_manager), |
122 launcher_view_(launcher_view) { | |
115 if (shelf_layout_manager) | 123 if (shelf_layout_manager) |
116 shelf_layout_manager->AddObserver(this); | 124 shelf_layout_manager->AddObserver(this); |
125 if (Shell::HasInstance()) | |
126 Shell::GetInstance()->AddEnvEventFilter(this); | |
117 } | 127 } |
118 | 128 |
119 LauncherTooltipManager::~LauncherTooltipManager() { | 129 LauncherTooltipManager::~LauncherTooltipManager() { |
120 Close(); | 130 Close(); |
121 if (shelf_layout_manager_) | 131 if (shelf_layout_manager_) |
122 shelf_layout_manager_->RemoveObserver(this); | 132 shelf_layout_manager_->RemoveObserver(this); |
133 if (Shell::HasInstance()) | |
134 Shell::GetInstance()->RemoveEnvEventFilter(this); | |
123 } | 135 } |
124 | 136 |
125 void LauncherTooltipManager::ShowDelayed(views::View* anchor, | 137 void LauncherTooltipManager::ShowDelayed(views::View* anchor, |
126 const string16& text) { | 138 const string16& text) { |
127 if (view_) { | 139 if (view_) { |
128 if (timer_.get() && timer_->IsRunning()) | 140 if (timer_.get() && timer_->IsRunning()) |
129 return; | 141 return; |
130 else | 142 else |
131 Close(); | 143 Close(); |
132 } | 144 } |
133 | 145 |
134 if (shelf_layout_manager_ && !shelf_layout_manager_->IsVisible()) | 146 if (shelf_layout_manager_ && !shelf_layout_manager_->IsVisible()) |
135 return; | 147 return; |
136 | 148 |
137 CreateBubble(anchor, text); | 149 CreateBubble(anchor, text); |
138 gfx::NativeView native_view = view_->GetWidget()->GetNativeView(); | 150 gfx::NativeView native_view = widget_->GetNativeView(); |
139 SetWindowVisibilityAnimationType( | 151 SetWindowVisibilityAnimationType( |
140 native_view, WINDOW_VISIBILITY_ANIMATION_TYPE_VERTICAL); | 152 native_view, WINDOW_VISIBILITY_ANIMATION_TYPE_VERTICAL); |
141 SetWindowVisibilityAnimationTransition(native_view, ANIMATE_SHOW); | 153 SetWindowVisibilityAnimationTransition(native_view, ANIMATE_SHOW); |
142 ResetTimer(); | 154 ResetTimer(); |
143 } | 155 } |
144 | 156 |
145 void LauncherTooltipManager::ShowImmediately(views::View* anchor, | 157 void LauncherTooltipManager::ShowImmediately(views::View* anchor, |
146 const string16& text) { | 158 const string16& text) { |
147 if (view_) { | 159 if (view_) { |
148 if (timer_.get() && timer_->IsRunning()) | 160 if (timer_.get() && timer_->IsRunning()) |
149 StopTimer(); | 161 StopTimer(); |
150 Close(); | 162 Close(); |
151 } | 163 } |
152 | 164 |
153 if (shelf_layout_manager_ && !shelf_layout_manager_->IsVisible()) | 165 if (shelf_layout_manager_ && !shelf_layout_manager_->IsVisible()) |
154 return; | 166 return; |
155 | 167 |
156 CreateBubble(anchor, text); | 168 CreateBubble(anchor, text); |
157 gfx::NativeView native_view = view_->GetWidget()->GetNativeView(); | 169 gfx::NativeView native_view = widget_->GetNativeView(); |
158 SetWindowVisibilityAnimationTransition(native_view, ANIMATE_NONE); | 170 SetWindowVisibilityAnimationTransition(native_view, ANIMATE_NONE); |
159 ShowInternal(); | 171 ShowInternal(); |
160 } | 172 } |
161 | 173 |
162 void LauncherTooltipManager::Close() { | 174 void LauncherTooltipManager::Close() { |
163 if (view_) { | 175 if (view_) { |
164 view_->GetWidget()->Close(); | 176 view_->Close(); |
165 view_ = NULL; | 177 view_ = NULL; |
178 widget_ = NULL; | |
166 } | 179 } |
167 } | 180 } |
168 | 181 |
169 void LauncherTooltipManager::OnBubbleClosed( | 182 void LauncherTooltipManager::OnBubbleClosed(views::BubbleDelegateView* view) { |
170 views::BubbleDelegateView* view) { | 183 if (view == view_) { |
171 if (view == view_) | |
172 view_ = NULL; | 184 view_ = NULL; |
185 widget_ = NULL; | |
186 } | |
173 } | 187 } |
174 | 188 |
175 void LauncherTooltipManager::SetArrowLocation(ShelfAlignment alignment) { | 189 void LauncherTooltipManager::SetArrowLocation(ShelfAlignment alignment) { |
176 if (alignment_ == alignment) | 190 if (alignment_ == alignment) |
177 return; | 191 return; |
178 | 192 |
179 alignment_ = alignment; | 193 alignment_ = alignment; |
180 if (view_) { | 194 if (view_) { |
181 Close(); | 195 Close(); |
182 ShowImmediately(anchor_, text_); | 196 ShowImmediately(anchor_, text_); |
(...skipping 21 matching lines...) Expand all Loading... | |
204 } | 218 } |
205 | 219 |
206 void LauncherTooltipManager::StopTimer() { | 220 void LauncherTooltipManager::StopTimer() { |
207 timer_.reset(); | 221 timer_.reset(); |
208 } | 222 } |
209 | 223 |
210 bool LauncherTooltipManager::IsVisible() { | 224 bool LauncherTooltipManager::IsVisible() { |
211 if (timer_.get() && timer_->IsRunning()) | 225 if (timer_.get() && timer_->IsRunning()) |
212 return false; | 226 return false; |
213 | 227 |
214 return view_ && view_->GetWidget() && view_->GetWidget()->IsVisible(); | 228 return widget_ && widget_->IsVisible(); |
229 } | |
230 | |
231 bool LauncherTooltipManager::PreHandleKeyEvent(aura::Window* target, | |
232 aura::KeyEvent* event) { | |
233 // Not handled. | |
234 return false; | |
235 } | |
236 | |
237 bool LauncherTooltipManager::PreHandleMouseEvent(aura::Window* target, | |
238 aura::MouseEvent* event) { | |
239 DCHECK(target); | |
240 DCHECK(event); | |
241 if (!widget_ || !widget_->IsVisible()) | |
242 return false; | |
243 | |
244 DCHECK(view_); | |
245 DCHECK(launcher_view_); | |
246 | |
247 if (widget_->GetNativeWindow()->GetRootWindow() != target->GetRootWindow()) { | |
248 MessageLoopForUI::current()->PostTask( | |
Daniel Erat
2012/07/24 16:39:50
nit: is it worthwhile to move this into a PostClos
Jun Mukai
2012/07/25 07:10:05
Added CloseSoon() thanks.
| |
249 FROM_HERE, | |
250 base::Bind(&LauncherTooltipManager::Close, base::Unretained(this))); | |
251 return false; | |
252 } | |
253 | |
254 gfx::Point location_in_launcher_view = event->location(); | |
255 aura::Window::ConvertPointToWindow( | |
256 target, launcher_view_->GetWidget()->GetNativeWindow(), | |
257 &location_in_launcher_view); | |
258 | |
259 gfx::Point location_on_screen = event->location(); | |
260 aura::Window::ConvertPointToWindow( | |
261 target, target->GetRootWindow(), &location_on_screen); | |
262 gfx::Rect bubble_rect = widget_->GetWindowBoundsInScreen(); | |
263 | |
264 if (launcher_view_->ShouldHideTooltip(location_in_launcher_view) && | |
265 !bubble_rect.Contains(location_on_screen)) { | |
266 // Because this mouse event may arrive to |view_|, here we just schedule | |
267 // the closing event rather than directly calling Close(). | |
268 MessageLoopForUI::current()->PostTask( | |
269 FROM_HERE, | |
270 base::Bind(&LauncherTooltipManager::Close, base::Unretained(this))); | |
271 } | |
272 | |
273 return false; | |
274 } | |
275 | |
276 ui::TouchStatus LauncherTooltipManager::PreHandleTouchEvent( | |
277 aura::Window* target, aura::TouchEvent* event) { | |
278 if (widget_ && widget_->IsVisible() && widget_->GetNativeWindow() != target) | |
279 Close(); | |
280 return ui::TOUCH_STATUS_UNKNOWN; | |
281 } | |
282 | |
283 ui::GestureStatus LauncherTooltipManager::PreHandleGestureEvent( | |
284 aura::Window* target, aura::GestureEvent* event) { | |
285 if (widget_ && widget_->IsVisible()) { | |
286 // Because this mouse event may arrive to |view_|, here we just schedule | |
287 // the closing event rather than directly calling Close(). | |
288 MessageLoopForUI::current()->PostTask( | |
289 FROM_HERE, | |
290 base::Bind(&LauncherTooltipManager::Close, base::Unretained(this))); | |
291 } | |
292 | |
293 return ui::GESTURE_STATUS_UNKNOWN; | |
215 } | 294 } |
216 | 295 |
217 void LauncherTooltipManager::WillDeleteShelf() { | 296 void LauncherTooltipManager::WillDeleteShelf() { |
218 shelf_layout_manager_ = NULL; | 297 shelf_layout_manager_ = NULL; |
219 } | 298 } |
220 | 299 |
221 void LauncherTooltipManager::WillChangeVisibilityState( | 300 void LauncherTooltipManager::WillChangeVisibilityState( |
222 ShelfLayoutManager::VisibilityState new_state) { | 301 ShelfLayoutManager::VisibilityState new_state) { |
223 if (new_state == ShelfLayoutManager::HIDDEN) { | 302 if (new_state == ShelfLayoutManager::HIDDEN) { |
224 StopTimer(); | 303 StopTimer(); |
(...skipping 23 matching lines...) Expand all Loading... | |
248 | 327 |
249 void LauncherTooltipManager::CreateBubble(views::View* anchor, | 328 void LauncherTooltipManager::CreateBubble(views::View* anchor, |
250 const string16& text) { | 329 const string16& text) { |
251 DCHECK(!view_); | 330 DCHECK(!view_); |
252 | 331 |
253 anchor_ = anchor; | 332 anchor_ = anchor; |
254 text_ = text; | 333 text_ = text; |
255 view_ = new LauncherTooltipBubble( | 334 view_ = new LauncherTooltipBubble( |
256 anchor, GetArrowLocation(alignment_), this); | 335 anchor, GetArrowLocation(alignment_), this); |
257 views::BubbleDelegateView::CreateBubble(view_); | 336 views::BubbleDelegateView::CreateBubble(view_); |
337 widget_ = view_->GetWidget(); | |
258 view_->SetText(text_); | 338 view_->SetText(text_); |
259 } | 339 } |
260 | 340 |
261 } // namespace internal | 341 } // namespace internal |
262 } // namespace ash | 342 } // namespace ash |
OLD | NEW |