Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(60)

Side by Side Diff: ash/launcher/launcher_tooltip_manager.cc

Issue 10808102: Add EventFilter for LauncherTooltip (2nd try). (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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
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
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 CloseSoon();
249 return false;
250 }
251
252 gfx::Point location_in_launcher_view = event->location();
253 aura::Window::ConvertPointToWindow(
254 target, launcher_view_->GetWidget()->GetNativeWindow(),
255 &location_in_launcher_view);
256
257 gfx::Point location_on_screen = event->location();
258 aura::Window::ConvertPointToWindow(
259 target, target->GetRootWindow(), &location_on_screen);
260 gfx::Rect bubble_rect = widget_->GetWindowBoundsInScreen();
261
262 if (launcher_view_->ShouldHideTooltip(location_in_launcher_view) &&
263 !bubble_rect.Contains(location_on_screen)) {
264 // Because this mouse event may arrive to |view_|, here we just schedule
265 // the closing event rather than directly calling Close().
266 CloseSoon();
267 }
268
269 return false;
270 }
271
272 ui::TouchStatus LauncherTooltipManager::PreHandleTouchEvent(
273 aura::Window* target, aura::TouchEvent* event) {
274 if (widget_ && widget_->IsVisible() && widget_->GetNativeWindow() != target)
275 Close();
276 return ui::TOUCH_STATUS_UNKNOWN;
277 }
278
279 ui::GestureStatus LauncherTooltipManager::PreHandleGestureEvent(
280 aura::Window* target, aura::GestureEvent* event) {
281 if (widget_ && widget_->IsVisible()) {
282 // Because this mouse event may arrive to |view_|, here we just schedule
283 // the closing event rather than directly calling Close().
284 CloseSoon();
285 }
286
287 return ui::GESTURE_STATUS_UNKNOWN;
215 } 288 }
216 289
217 void LauncherTooltipManager::WillDeleteShelf() { 290 void LauncherTooltipManager::WillDeleteShelf() {
218 shelf_layout_manager_ = NULL; 291 shelf_layout_manager_ = NULL;
219 } 292 }
220 293
221 void LauncherTooltipManager::WillChangeVisibilityState( 294 void LauncherTooltipManager::WillChangeVisibilityState(
222 ShelfLayoutManager::VisibilityState new_state) { 295 ShelfLayoutManager::VisibilityState new_state) {
223 if (new_state == ShelfLayoutManager::HIDDEN) { 296 if (new_state == ShelfLayoutManager::HIDDEN) {
224 StopTimer(); 297 StopTimer();
225 Close(); 298 Close();
226 } 299 }
227 } 300 }
228 301
229 void LauncherTooltipManager::OnAutoHideStateChanged( 302 void LauncherTooltipManager::OnAutoHideStateChanged(
230 ShelfLayoutManager::AutoHideState new_state) { 303 ShelfLayoutManager::AutoHideState new_state) {
231 if (new_state == ShelfLayoutManager::AUTO_HIDE_HIDDEN) { 304 if (new_state == ShelfLayoutManager::AUTO_HIDE_HIDDEN) {
232 StopTimer(); 305 StopTimer();
233 // AutoHide state change happens during an event filter, so immediate close 306 // 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 307 // may cause a crash in the HandleMouseEvent() after the filter. So we just
235 // schedule the Close here. 308 // schedule the Close here.
236 MessageLoopForUI::current()->PostTask( 309 CloseSoon();
237 FROM_HERE,
238 base::Bind(&LauncherTooltipManager::Close, base::Unretained(this)));
239 } 310 }
240 } 311 }
241 312
313 void LauncherTooltipManager::CloseSoon() {
314 MessageLoopForUI::current()->PostTask(
315 FROM_HERE,
316 base::Bind(&LauncherTooltipManager::Close, base::Unretained(this)));
317 }
318
242 void LauncherTooltipManager::ShowInternal() { 319 void LauncherTooltipManager::ShowInternal() {
243 if (view_) 320 if (view_)
244 view_->Show(); 321 view_->Show();
245 322
246 timer_.reset(); 323 timer_.reset();
247 } 324 }
248 325
249 void LauncherTooltipManager::CreateBubble(views::View* anchor, 326 void LauncherTooltipManager::CreateBubble(views::View* anchor,
250 const string16& text) { 327 const string16& text) {
251 DCHECK(!view_); 328 DCHECK(!view_);
252 329
253 anchor_ = anchor; 330 anchor_ = anchor;
254 text_ = text; 331 text_ = text;
255 view_ = new LauncherTooltipBubble( 332 view_ = new LauncherTooltipBubble(
256 anchor, GetArrowLocation(alignment_), this); 333 anchor, GetArrowLocation(alignment_), this);
257 views::BubbleDelegateView::CreateBubble(view_); 334 views::BubbleDelegateView::CreateBubble(view_);
335 widget_ = view_->GetWidget();
258 view_->SetText(text_); 336 view_->SetText(text_);
259 } 337 }
260 338
261 } // namespace internal 339 } // namespace internal
262 } // namespace ash 340 } // namespace ash
OLDNEW
« no previous file with comments | « ash/launcher/launcher_tooltip_manager.h ('k') | ash/launcher/launcher_tooltip_manager_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698