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

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

Issue 10810011: Add EventFilter for LauncherTooltip. (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 21 matching lines...) Expand all
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
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);
116 if (Shell::HasInstance())
117 Shell::GetInstance()->AddEnvEventFilter(this);
117 } 118 }
118 119
119 LauncherTooltipManager::~LauncherTooltipManager() { 120 LauncherTooltipManager::~LauncherTooltipManager() {
120 Close(); 121 Close();
121 if (shelf_layout_manager_) 122 if (shelf_layout_manager_)
122 shelf_layout_manager_->RemoveObserver(this); 123 shelf_layout_manager_->RemoveObserver(this);
124 if (Shell::HasInstance())
125 Shell::GetInstance()->RemoveEnvEventFilter(this);
123 } 126 }
124 127
125 void LauncherTooltipManager::ShowDelayed(views::View* anchor, 128 void LauncherTooltipManager::ShowDelayed(views::View* anchor,
126 const string16& text) { 129 const string16& text) {
127 if (view_) { 130 if (view_) {
128 if (timer_.get() && timer_->IsRunning()) 131 if (timer_.get() && timer_->IsRunning())
129 return; 132 return;
130 else 133 else
131 Close(); 134 Close();
132 } 135 }
133 136
134 if (shelf_layout_manager_ && !shelf_layout_manager_->IsVisible()) 137 if (shelf_layout_manager_ && !shelf_layout_manager_->IsVisible())
135 return; 138 return;
136 139
137 CreateBubble(anchor, text); 140 CreateBubble(anchor, text);
138 gfx::NativeView native_view = view_->GetWidget()->GetNativeView(); 141 gfx::NativeView native_view = widget_->GetNativeView();
139 SetWindowVisibilityAnimationType( 142 SetWindowVisibilityAnimationType(
140 native_view, WINDOW_VISIBILITY_ANIMATION_TYPE_VERTICAL); 143 native_view, WINDOW_VISIBILITY_ANIMATION_TYPE_VERTICAL);
141 SetWindowVisibilityAnimationTransition(native_view, ANIMATE_SHOW); 144 SetWindowVisibilityAnimationTransition(native_view, ANIMATE_SHOW);
142 ResetTimer(); 145 ResetTimer();
143 } 146 }
144 147
145 void LauncherTooltipManager::ShowImmediately(views::View* anchor, 148 void LauncherTooltipManager::ShowImmediately(views::View* anchor,
146 const string16& text) { 149 const string16& text) {
147 if (view_) { 150 if (view_) {
148 if (timer_.get() && timer_->IsRunning()) 151 if (timer_.get() && timer_->IsRunning())
149 StopTimer(); 152 StopTimer();
150 Close(); 153 Close();
151 } 154 }
152 155
153 if (shelf_layout_manager_ && !shelf_layout_manager_->IsVisible()) 156 if (shelf_layout_manager_ && !shelf_layout_manager_->IsVisible())
154 return; 157 return;
155 158
156 CreateBubble(anchor, text); 159 CreateBubble(anchor, text);
157 gfx::NativeView native_view = view_->GetWidget()->GetNativeView(); 160 gfx::NativeView native_view = widget_->GetNativeView();
158 SetWindowVisibilityAnimationTransition(native_view, ANIMATE_NONE); 161 SetWindowVisibilityAnimationTransition(native_view, ANIMATE_NONE);
159 ShowInternal(); 162 ShowInternal();
160 } 163 }
161 164
162 void LauncherTooltipManager::Close() { 165 void LauncherTooltipManager::Close() {
163 if (view_) { 166 if (widget_) {
164 view_->GetWidget()->Close(); 167 if (widget_->IsVisible())
168 widget_->Close();
165 view_ = NULL; 169 view_ = NULL;
170 widget_ = NULL;
166 } 171 }
167 } 172 }
168 173
169 void LauncherTooltipManager::OnBubbleClosed( 174 void LauncherTooltipManager::OnBubbleClosed(
170 views::BubbleDelegateView* view) { 175 views::BubbleDelegateView* view) {
171 if (view == view_) 176 if (view == view_) {
172 view_ = NULL; 177 view_ = NULL;
178 widget_ = NULL;
179 }
173 } 180 }
174 181
175 void LauncherTooltipManager::SetArrowLocation(ShelfAlignment alignment) { 182 void LauncherTooltipManager::SetArrowLocation(ShelfAlignment alignment) {
176 if (alignment_ == alignment) 183 if (alignment_ == alignment)
177 return; 184 return;
178 185
179 alignment_ = alignment; 186 alignment_ = alignment;
180 if (view_) { 187 if (view_) {
181 Close(); 188 Close();
182 ShowImmediately(anchor_, text_); 189 ShowImmediately(anchor_, text_);
(...skipping 21 matching lines...) Expand all
204 } 211 }
205 212
206 void LauncherTooltipManager::StopTimer() { 213 void LauncherTooltipManager::StopTimer() {
207 timer_.reset(); 214 timer_.reset();
208 } 215 }
209 216
210 bool LauncherTooltipManager::IsVisible() { 217 bool LauncherTooltipManager::IsVisible() {
211 if (timer_.get() && timer_->IsRunning()) 218 if (timer_.get() && timer_->IsRunning())
212 return false; 219 return false;
213 220
214 return view_ && view_->GetWidget() && view_->GetWidget()->IsVisible(); 221 return widget_ && widget_->IsVisible();
222 }
223
224 bool LauncherTooltipManager::PreHandleKeyEvent(aura::Window* target,
225 aura::KeyEvent* event) {
226 // Not handled.
227 return false;
228 }
229
230 bool LauncherTooltipManager::PreHandleMouseEvent(aura::Window* target,
231 aura::MouseEvent* event) {
232 DCHECK(target);
233 DCHECK(event);
234 if (!widget_ || !widget_->IsVisible())
235 return false;
236
237 DCHECK(view_);
238 DCHECK(launcher_view_);
239
240 if (widget_->GetNativeWindow()->GetRootWindow() != target->GetRootWindow()) {
241 MessageLoopForUI::current()->PostTask(
242 FROM_HERE,
243 base::Bind(&LauncherTooltipManager::Close, base::Unretained(this)));
244 return false;
245 }
246
247 gfx::Point location_in_launcher_view = event->location();
248 aura::Window::ConvertPointToWindow(
249 target, launcher_view_->GetWidget()->GetNativeWindow(),
250 &location_in_launcher_view);
251
252 gfx::Point location_on_screen = event->location();
253 aura::Window::ConvertPointToWindow(
254 target, target->GetRootWindow(), &location_on_screen);
255 gfx::Rect bubble_rect = widget_->GetWindowBoundsInScreen();
256
257 if (launcher_view_->ShouldHideTooltip(location_in_launcher_view) &&
258 !bubble_rect.Contains(location_on_screen)) {
259 // Because this mouse event may arrive to |view_|, here we just schedule
260 // the closing event rather than directly calling Close().
261 MessageLoopForUI::current()->PostTask(
262 FROM_HERE,
263 base::Bind(&LauncherTooltipManager::Close, base::Unretained(this)));
264 }
265
266 return false;
267 }
268
269 ui::TouchStatus LauncherTooltipManager::PreHandleTouchEvent(
270 aura::Window* target, aura::TouchEvent* event) {
271 if (widget_ && widget_->IsVisible() && widget_->GetNativeWindow() != target)
272 Close();
273 return ui::TOUCH_STATUS_UNKNOWN;
274 }
275
276 ui::GestureStatus LauncherTooltipManager::PreHandleGestureEvent(
277 aura::Window* target, aura::GestureEvent* event) {
278 if (widget_ && widget_->IsVisible()) {
279 // Because this mouse event may arrive to |view_|, here we just schedule
280 // the closing event rather than directly calling Close().
281 MessageLoopForUI::current()->PostTask(
282 FROM_HERE,
283 base::Bind(&LauncherTooltipManager::Close, base::Unretained(this)));
284 }
285
286 return ui::GESTURE_STATUS_UNKNOWN;
215 } 287 }
216 288
217 void LauncherTooltipManager::WillDeleteShelf() { 289 void LauncherTooltipManager::WillDeleteShelf() {
218 shelf_layout_manager_ = NULL; 290 shelf_layout_manager_ = NULL;
219 } 291 }
220 292
221 void LauncherTooltipManager::WillChangeVisibilityState( 293 void LauncherTooltipManager::WillChangeVisibilityState(
222 ShelfLayoutManager::VisibilityState new_state) { 294 ShelfLayoutManager::VisibilityState new_state) {
223 if (new_state == ShelfLayoutManager::HIDDEN) { 295 if (new_state == ShelfLayoutManager::HIDDEN) {
224 StopTimer(); 296 StopTimer();
225 Close(); 297 Close();
226 } 298 }
227 } 299 }
228 300
229 void LauncherTooltipManager::OnAutoHideStateChanged( 301 void LauncherTooltipManager::OnAutoHideStateChanged(
230 ShelfLayoutManager::AutoHideState new_state) { 302 ShelfLayoutManager::AutoHideState new_state) {
231 if (new_state == ShelfLayoutManager::AUTO_HIDE_HIDDEN) { 303 if (new_state == ShelfLayoutManager::AUTO_HIDE_HIDDEN) {
232 StopTimer(); 304 StopTimer();
233 // AutoHide state change happens during an event filter, so immediate close 305 // 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 306 // may cause a crash in the HandleMouseEvent() after the filter. So we just
235 // schedule the Close here. 307 // schedule the Close here.
236 MessageLoopForUI::current()->PostTask( 308 MessageLoopForUI::current()->PostTask(
237 FROM_HERE, 309 FROM_HERE,
238 base::Bind(&LauncherTooltipManager::Close, base::Unretained(this))); 310 base::Bind(&LauncherTooltipManager::Close, base::Unretained(this)));
239 } 311 }
240 } 312 }
241 313
242 void LauncherTooltipManager::ShowInternal() { 314 void LauncherTooltipManager::ShowInternal() {
243 if (view_) 315 if (view_) {
244 view_->Show(); 316 view_->Show();
317 }
245 318
246 timer_.reset(); 319 timer_.reset();
247 } 320 }
248 321
249 void LauncherTooltipManager::CreateBubble(views::View* anchor, 322 void LauncherTooltipManager::CreateBubble(views::View* anchor,
250 const string16& text) { 323 const string16& text) {
251 DCHECK(!view_); 324 DCHECK(!view_);
252 325
253 anchor_ = anchor; 326 anchor_ = anchor;
254 text_ = text; 327 text_ = text;
255 view_ = new LauncherTooltipBubble( 328 view_ = new LauncherTooltipBubble(
256 anchor, GetArrowLocation(alignment_), this); 329 anchor, GetArrowLocation(alignment_), this);
257 views::BubbleDelegateView::CreateBubble(view_); 330 views::BubbleDelegateView::CreateBubble(view_);
331 widget_ = view_->GetWidget();
258 view_->SetText(text_); 332 view_->SetText(text_);
259 } 333 }
260 334
261 } // namespace internal 335 } // namespace internal
262 } // namespace ash 336 } // 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