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 21 matching lines...) Expand all Loading... | |
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); |
54 | 58 |
55 private: | 59 private: |
56 // views::View overrides: | |
57 virtual void OnMouseExited(const views::MouseEvent& event) OVERRIDE; | |
58 | |
59 // views::WidgetDelegate overrides; | 60 // views::WidgetDelegate overrides; |
60 virtual void WindowClosing() OVERRIDE; | 61 virtual void WindowClosing() OVERRIDE; |
61 | 62 |
62 LauncherTooltipManager* host_; | 63 LauncherTooltipManager* host_; |
63 views::Label* label_; | 64 views::Label* label_; |
64 }; | 65 }; |
65 | 66 |
66 LauncherTooltipManager::LauncherTooltipBubble::LauncherTooltipBubble( | 67 LauncherTooltipManager::LauncherTooltipBubble::LauncherTooltipBubble( |
67 views::View* anchor, | 68 views::View* anchor, |
68 views::BubbleBorder::ArrowLocation arrow_location, | 69 views::BubbleBorder::ArrowLocation arrow_location, |
(...skipping 19 matching lines...) Expand all Loading... | |
88 } | 89 } |
89 | 90 |
90 void LauncherTooltipManager::LauncherTooltipBubble::SetText( | 91 void LauncherTooltipManager::LauncherTooltipBubble::SetText( |
91 const string16& text) { | 92 const string16& text) { |
92 label_->SetText(text); | 93 label_->SetText(text); |
93 label_->SetMultiLine(true); | 94 label_->SetMultiLine(true); |
94 label_->SizeToFit(kTooltipMaxWidth); | 95 label_->SizeToFit(kTooltipMaxWidth); |
95 SizeToContents(); | 96 SizeToContents(); |
96 } | 97 } |
97 | 98 |
98 void LauncherTooltipManager::LauncherTooltipBubble::OnMouseExited( | |
99 const views::MouseEvent& event) { | |
100 GetWidget()->Close(); | |
101 host_->OnBubbleClosed(this); | |
102 } | |
103 | |
104 void LauncherTooltipManager::LauncherTooltipBubble::WindowClosing() { | 99 void LauncherTooltipManager::LauncherTooltipBubble::WindowClosing() { |
105 views::BubbleDelegateView::WindowClosing(); | 100 views::BubbleDelegateView::WindowClosing(); |
106 host_->OnBubbleClosed(this); | 101 host_->OnBubbleClosed(this); |
107 } | 102 } |
108 | 103 |
109 LauncherTooltipManager::LauncherTooltipManager( | 104 LauncherTooltipManager::LauncherTooltipManager( |
110 ShelfAlignment alignment, ShelfLayoutManager* shelf_layout_manager) | 105 ShelfAlignment alignment, |
106 ShelfLayoutManager* shelf_layout_manager, | |
107 LauncherView* launcher_view) | |
111 : view_(NULL), | 108 : view_(NULL), |
109 widget_(NULL), | |
112 anchor_(NULL), | 110 anchor_(NULL), |
113 alignment_(alignment), | 111 alignment_(alignment), |
114 shelf_layout_manager_(shelf_layout_manager) { | 112 shelf_layout_manager_(shelf_layout_manager), |
113 launcher_view_(launcher_view) { | |
115 if (shelf_layout_manager) | 114 if (shelf_layout_manager) |
116 shelf_layout_manager->AddObserver(this); | 115 shelf_layout_manager->AddObserver(this); |
117 } | 116 } |
118 | 117 |
119 LauncherTooltipManager::~LauncherTooltipManager() { | 118 LauncherTooltipManager::~LauncherTooltipManager() { |
120 Close(); | 119 Close(); |
121 if (shelf_layout_manager_) | 120 if (shelf_layout_manager_) |
122 shelf_layout_manager_->RemoveObserver(this); | 121 shelf_layout_manager_->RemoveObserver(this); |
123 } | 122 } |
124 | 123 |
125 void LauncherTooltipManager::ShowDelayed(views::View* anchor, | 124 void LauncherTooltipManager::ShowDelayed(views::View* anchor, |
126 const string16& text) { | 125 const string16& text) { |
127 if (view_) { | 126 if (view_) { |
128 if (timer_.get() && timer_->IsRunning()) | 127 if (timer_.get() && timer_->IsRunning()) |
129 return; | 128 return; |
130 else | 129 else |
131 Close(); | 130 Close(); |
132 } | 131 } |
133 | 132 |
134 if (shelf_layout_manager_ && !shelf_layout_manager_->IsVisible()) | 133 if (shelf_layout_manager_ && !shelf_layout_manager_->IsVisible()) |
135 return; | 134 return; |
136 | 135 |
137 CreateBubble(anchor, text); | 136 CreateBubble(anchor, text); |
138 gfx::NativeView native_view = view_->GetWidget()->GetNativeView(); | 137 gfx::NativeView native_view = widget_->GetNativeView(); |
139 SetWindowVisibilityAnimationType( | 138 SetWindowVisibilityAnimationType( |
140 native_view, WINDOW_VISIBILITY_ANIMATION_TYPE_VERTICAL); | 139 native_view, WINDOW_VISIBILITY_ANIMATION_TYPE_VERTICAL); |
141 SetWindowVisibilityAnimationTransition(native_view, ANIMATE_SHOW); | 140 SetWindowVisibilityAnimationTransition(native_view, ANIMATE_SHOW); |
142 ResetTimer(); | 141 ResetTimer(); |
143 } | 142 } |
144 | 143 |
145 void LauncherTooltipManager::ShowImmediately(views::View* anchor, | 144 void LauncherTooltipManager::ShowImmediately(views::View* anchor, |
146 const string16& text) { | 145 const string16& text) { |
147 if (view_) { | 146 if (view_) { |
148 if (timer_.get() && timer_->IsRunning()) | 147 if (timer_.get() && timer_->IsRunning()) |
149 StopTimer(); | 148 StopTimer(); |
150 Close(); | 149 Close(); |
151 } | 150 } |
152 | 151 |
153 if (shelf_layout_manager_ && !shelf_layout_manager_->IsVisible()) | 152 if (shelf_layout_manager_ && !shelf_layout_manager_->IsVisible()) |
154 return; | 153 return; |
155 | 154 |
156 CreateBubble(anchor, text); | 155 CreateBubble(anchor, text); |
157 gfx::NativeView native_view = view_->GetWidget()->GetNativeView(); | 156 gfx::NativeView native_view = widget_->GetNativeView(); |
158 SetWindowVisibilityAnimationTransition(native_view, ANIMATE_NONE); | 157 SetWindowVisibilityAnimationTransition(native_view, ANIMATE_NONE); |
159 ShowInternal(); | 158 ShowInternal(); |
160 } | 159 } |
161 | 160 |
162 void LauncherTooltipManager::Close() { | 161 void LauncherTooltipManager::Close() { |
163 if (view_) { | 162 if (widget_) { |
164 view_->GetWidget()->Close(); | 163 if (Shell::HasInstance()) |
164 Shell::GetInstance()->RemoveEnvEventFilter(this); | |
165 widget_->Close(); | |
165 view_ = NULL; | 166 view_ = NULL; |
167 widget_ = NULL; | |
166 } | 168 } |
167 } | 169 } |
168 | 170 |
169 void LauncherTooltipManager::OnBubbleClosed( | 171 void LauncherTooltipManager::OnBubbleClosed( |
170 views::BubbleDelegateView* view) { | 172 views::BubbleDelegateView* view) { |
171 if (view == view_) | 173 if (view == view_) |
172 view_ = NULL; | 174 view_ = NULL; |
173 } | 175 } |
174 | 176 |
175 void LauncherTooltipManager::SetArrowLocation(ShelfAlignment alignment) { | 177 void LauncherTooltipManager::SetArrowLocation(ShelfAlignment alignment) { |
(...skipping 28 matching lines...) Expand all Loading... | |
204 } | 206 } |
205 | 207 |
206 void LauncherTooltipManager::StopTimer() { | 208 void LauncherTooltipManager::StopTimer() { |
207 timer_.reset(); | 209 timer_.reset(); |
208 } | 210 } |
209 | 211 |
210 bool LauncherTooltipManager::IsVisible() { | 212 bool LauncherTooltipManager::IsVisible() { |
211 if (timer_.get() && timer_->IsRunning()) | 213 if (timer_.get() && timer_->IsRunning()) |
212 return false; | 214 return false; |
213 | 215 |
214 return view_ && view_->GetWidget() && view_->GetWidget()->IsVisible(); | 216 return widget_ && widget_->IsVisible(); |
217 } | |
218 | |
219 bool LauncherTooltipManager::PreHandleKeyEvent(aura::Window* target, | |
220 aura::KeyEvent* event) { | |
221 // Not handled. | |
222 return false; | |
223 } | |
224 | |
225 bool LauncherTooltipManager::PreHandleMouseEvent(aura::Window* target, | |
226 aura::MouseEvent* event) { | |
227 DCHECK(target); | |
228 DCHECK(event); | |
229 DCHECK(view_); | |
230 DCHECK(launcher_view_); | |
231 | |
232 if (widget_->GetNativeWindow()->GetRootWindow() != target->GetRootWindow()) { | |
233 MessageLoopForUI::current()->PostTask( | |
234 FROM_HERE, | |
235 base::Bind(&LauncherTooltipManager::Close, base::Unretained(this))); | |
236 return false; | |
237 } | |
238 | |
239 gfx::Point location_in_launcher_view = event->location(); | |
240 aura::Window::ConvertPointToWindow( | |
241 target, launcher_view_->GetWidget()->GetNativeWindow(), | |
242 &location_in_launcher_view); | |
243 | |
244 gfx::Point location_on_screen = event->location(); | |
245 aura::Window::ConvertPointToWindow( | |
246 target, target->GetRootWindow(), &location_on_screen); | |
247 gfx::Rect bubble_rect = widget_->GetWindowScreenBounds(); | |
248 | |
249 if (launcher_view_->ShouldHideTooltip(location_in_launcher_view) && | |
250 !bubble_rect.Contains(location_on_screen)) { | |
251 // Because this mouse event may arrive to |view_|, here we just schedules | |
Daniel Erat
2012/07/19 14:51:18
nit: s/schedules/schedule/
Jun Mukai
2012/07/20 05:00:11
Done.
| |
252 // the closing event rather than directly calls Close(). | |
Daniel Erat
2012/07/19 14:51:18
nit: s/calls/calling/
Jun Mukai
2012/07/20 05:00:11
Done.
| |
253 MessageLoopForUI::current()->PostTask( | |
254 FROM_HERE, | |
255 base::Bind(&LauncherTooltipManager::Close, base::Unretained(this))); | |
256 } | |
257 | |
258 return false; | |
259 } | |
260 | |
261 ui::TouchStatus LauncherTooltipManager::PreHandleTouchEvent( | |
262 aura::Window* target, aura::TouchEvent* event) { | |
263 if (widget_->GetNativeWindow() != target) | |
264 Close(); | |
265 return ui::TOUCH_STATUS_UNKNOWN; | |
266 } | |
267 | |
268 ui::GestureStatus LauncherTooltipManager::PreHandleGestureEvent( | |
269 aura::Window* target, aura::GestureEvent* event) { | |
270 // Because this mouse event may arrive to |view_|, here we just schedules | |
Daniel Erat
2012/07/19 14:51:18
nit: same updates as described above
Jun Mukai
2012/07/20 05:00:11
Done.
| |
271 // the closing event rather than directly calls Close(). | |
272 MessageLoopForUI::current()->PostTask( | |
273 FROM_HERE, | |
274 base::Bind(&LauncherTooltipManager::Close, base::Unretained(this))); | |
275 return ui::GESTURE_STATUS_UNKNOWN; | |
215 } | 276 } |
216 | 277 |
217 void LauncherTooltipManager::WillDeleteShelf() { | 278 void LauncherTooltipManager::WillDeleteShelf() { |
218 shelf_layout_manager_ = NULL; | 279 shelf_layout_manager_ = NULL; |
219 } | 280 } |
220 | 281 |
221 void LauncherTooltipManager::WillChangeVisibilityState( | 282 void LauncherTooltipManager::WillChangeVisibilityState( |
222 ShelfLayoutManager::VisibilityState new_state) { | 283 ShelfLayoutManager::VisibilityState new_state) { |
223 if (new_state == ShelfLayoutManager::HIDDEN) { | 284 if (new_state == ShelfLayoutManager::HIDDEN) { |
224 StopTimer(); | 285 StopTimer(); |
225 Close(); | 286 Close(); |
226 } | 287 } |
227 } | 288 } |
228 | 289 |
229 void LauncherTooltipManager::OnAutoHideStateChanged( | 290 void LauncherTooltipManager::OnAutoHideStateChanged( |
230 ShelfLayoutManager::AutoHideState new_state) { | 291 ShelfLayoutManager::AutoHideState new_state) { |
231 if (new_state == ShelfLayoutManager::AUTO_HIDE_HIDDEN) { | 292 if (new_state == ShelfLayoutManager::AUTO_HIDE_HIDDEN) { |
232 StopTimer(); | 293 StopTimer(); |
233 // AutoHide state change happens during an event filter, so immediate close | 294 // AutoHide state change happens during an event filter, so immediate close |
234 // may cause a crash in the HandleMouseEvent() after the filter. So we just | 295 // may cause a crash in the HandleMouseEvent() after the filter. So we just |
235 // schedule the Close here. | 296 // schedule the Close here. |
236 MessageLoopForUI::current()->PostTask( | 297 MessageLoopForUI::current()->PostTask( |
237 FROM_HERE, | 298 FROM_HERE, |
238 base::Bind(&LauncherTooltipManager::Close, base::Unretained(this))); | 299 base::Bind(&LauncherTooltipManager::Close, base::Unretained(this))); |
239 } | 300 } |
240 } | 301 } |
241 | 302 |
242 void LauncherTooltipManager::ShowInternal() { | 303 void LauncherTooltipManager::ShowInternal() { |
243 if (view_) | 304 if (view_) { |
244 view_->Show(); | 305 view_->Show(); |
306 if (Shell::HasInstance()) | |
307 Shell::GetInstance()->AddEnvEventFilter(this); | |
Daniel Erat
2012/07/19 14:51:18
ShowInternal() looks like it gets called by ShowIm
Jun Mukai
2012/07/20 05:00:11
fixed the code a bit. Close() is called in the de
Jun Mukai
2012/07/20 05:06:46
sorry, I changed my mind and uploaded another patc
| |
308 } | |
245 | 309 |
246 timer_.reset(); | 310 timer_.reset(); |
247 } | 311 } |
248 | 312 |
249 void LauncherTooltipManager::CreateBubble(views::View* anchor, | 313 void LauncherTooltipManager::CreateBubble(views::View* anchor, |
250 const string16& text) { | 314 const string16& text) { |
251 DCHECK(!view_); | 315 DCHECK(!view_); |
252 | 316 |
253 anchor_ = anchor; | 317 anchor_ = anchor; |
254 text_ = text; | 318 text_ = text; |
255 view_ = new LauncherTooltipBubble( | 319 view_ = new LauncherTooltipBubble( |
256 anchor, GetArrowLocation(alignment_), this); | 320 anchor, GetArrowLocation(alignment_), this); |
257 views::BubbleDelegateView::CreateBubble(view_); | 321 views::BubbleDelegateView::CreateBubble(view_); |
322 widget_ = view_->GetWidget(); | |
258 view_->SetText(text_); | 323 view_->SetText(text_); |
259 } | 324 } |
260 | 325 |
261 } // namespace internal | 326 } // namespace internal |
262 } // namespace ash | 327 } // namespace ash |
OLD | NEW |