| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "ui/views/widget/root_view.h" | |
| 6 | |
| 7 #include <algorithm> | |
| 8 | |
| 9 #include "base/logging.h" | |
| 10 #include "base/message_loop/message_loop.h" | |
| 11 #include "ui/accessibility/ax_view_state.h" | |
| 12 #include "ui/base/cursor/cursor.h" | |
| 13 #include "ui/base/dragdrop/drag_drop_types.h" | |
| 14 #include "ui/base/ui_base_switches_util.h" | |
| 15 #include "ui/compositor/layer.h" | |
| 16 #include "ui/events/event.h" | |
| 17 #include "ui/events/keycodes/keyboard_codes.h" | |
| 18 #include "ui/gfx/canvas.h" | |
| 19 #include "ui/views/drag_controller.h" | |
| 20 #include "ui/views/focus/view_storage.h" | |
| 21 #include "ui/views/layout/fill_layout.h" | |
| 22 #include "ui/views/view_targeter.h" | |
| 23 #include "ui/views/widget/root_view_targeter.h" | |
| 24 #include "ui/views/widget/widget.h" | |
| 25 #include "ui/views/widget/widget_delegate.h" | |
| 26 | |
| 27 typedef ui::EventDispatchDetails DispatchDetails; | |
| 28 | |
| 29 namespace views { | |
| 30 namespace internal { | |
| 31 | |
| 32 namespace { | |
| 33 | |
| 34 enum EventType { | |
| 35 EVENT_ENTER, | |
| 36 EVENT_EXIT | |
| 37 }; | |
| 38 | |
| 39 class MouseEnterExitEvent : public ui::MouseEvent { | |
| 40 public: | |
| 41 MouseEnterExitEvent(const ui::MouseEvent& event, ui::EventType type) | |
| 42 : ui::MouseEvent(event, | |
| 43 static_cast<View*>(NULL), | |
| 44 static_cast<View*>(NULL)) { | |
| 45 DCHECK(type == ui::ET_MOUSE_ENTERED || | |
| 46 type == ui::ET_MOUSE_EXITED); | |
| 47 SetType(type); | |
| 48 } | |
| 49 | |
| 50 virtual ~MouseEnterExitEvent() {} | |
| 51 }; | |
| 52 | |
| 53 } // namespace | |
| 54 | |
| 55 // This event handler receives events in the pre-target phase and takes care of | |
| 56 // the following: | |
| 57 // - Shows keyboard-triggered context menus. | |
| 58 class PreEventDispatchHandler : public ui::EventHandler { | |
| 59 public: | |
| 60 explicit PreEventDispatchHandler(View* owner) | |
| 61 : owner_(owner) { | |
| 62 } | |
| 63 virtual ~PreEventDispatchHandler() {} | |
| 64 | |
| 65 private: | |
| 66 // ui::EventHandler: | |
| 67 virtual void OnKeyEvent(ui::KeyEvent* event) override { | |
| 68 CHECK_EQ(ui::EP_PRETARGET, event->phase()); | |
| 69 if (event->handled()) | |
| 70 return; | |
| 71 | |
| 72 View* v = NULL; | |
| 73 if (owner_->GetFocusManager()) // Can be NULL in unittests. | |
| 74 v = owner_->GetFocusManager()->GetFocusedView(); | |
| 75 | |
| 76 // Special case to handle keyboard-triggered context menus. | |
| 77 if (v && v->enabled() && ((event->key_code() == ui::VKEY_APPS) || | |
| 78 (event->key_code() == ui::VKEY_F10 && event->IsShiftDown()))) { | |
| 79 // Clamp the menu location within the visible bounds of each ancestor view | |
| 80 // to avoid showing the menu over a completely different view or window. | |
| 81 gfx::Point location = v->GetKeyboardContextMenuLocation(); | |
| 82 for (View* parent = v->parent(); parent; parent = parent->parent()) { | |
| 83 const gfx::Rect& parent_bounds = parent->GetBoundsInScreen(); | |
| 84 location.SetToMax(parent_bounds.origin()); | |
| 85 location.SetToMin(parent_bounds.bottom_right()); | |
| 86 } | |
| 87 v->ShowContextMenu(location, ui::MENU_SOURCE_KEYBOARD); | |
| 88 event->StopPropagation(); | |
| 89 } | |
| 90 } | |
| 91 | |
| 92 View* owner_; | |
| 93 | |
| 94 DISALLOW_COPY_AND_ASSIGN(PreEventDispatchHandler); | |
| 95 }; | |
| 96 | |
| 97 // This event handler receives events in the post-target phase and takes care of | |
| 98 // the following: | |
| 99 // - Generates context menu, or initiates drag-and-drop, from gesture events. | |
| 100 class PostEventDispatchHandler : public ui::EventHandler { | |
| 101 public: | |
| 102 PostEventDispatchHandler() | |
| 103 : touch_dnd_enabled_(::switches::IsTouchDragDropEnabled()) { | |
| 104 } | |
| 105 virtual ~PostEventDispatchHandler() {} | |
| 106 | |
| 107 private: | |
| 108 // Overridden from ui::EventHandler: | |
| 109 virtual void OnGestureEvent(ui::GestureEvent* event) override { | |
| 110 DCHECK_EQ(ui::EP_POSTTARGET, event->phase()); | |
| 111 if (event->handled()) | |
| 112 return; | |
| 113 | |
| 114 View* target = static_cast<View*>(event->target()); | |
| 115 gfx::Point location = event->location(); | |
| 116 if (touch_dnd_enabled_ && | |
| 117 event->type() == ui::ET_GESTURE_LONG_PRESS && | |
| 118 (!target->drag_controller() || | |
| 119 target->drag_controller()->CanStartDragForView( | |
| 120 target, location, location))) { | |
| 121 if (target->DoDrag(*event, location, | |
| 122 ui::DragDropTypes::DRAG_EVENT_SOURCE_TOUCH)) { | |
| 123 event->StopPropagation(); | |
| 124 return; | |
| 125 } | |
| 126 } | |
| 127 | |
| 128 if (target->context_menu_controller() && | |
| 129 (event->type() == ui::ET_GESTURE_LONG_PRESS || | |
| 130 event->type() == ui::ET_GESTURE_LONG_TAP || | |
| 131 event->type() == ui::ET_GESTURE_TWO_FINGER_TAP)) { | |
| 132 gfx::Point screen_location(location); | |
| 133 View::ConvertPointToScreen(target, &screen_location); | |
| 134 target->ShowContextMenu(screen_location, ui::MENU_SOURCE_TOUCH); | |
| 135 event->StopPropagation(); | |
| 136 } | |
| 137 } | |
| 138 | |
| 139 bool touch_dnd_enabled_; | |
| 140 | |
| 141 DISALLOW_COPY_AND_ASSIGN(PostEventDispatchHandler); | |
| 142 }; | |
| 143 | |
| 144 // static | |
| 145 const char RootView::kViewClassName[] = "RootView"; | |
| 146 | |
| 147 //////////////////////////////////////////////////////////////////////////////// | |
| 148 // RootView, public: | |
| 149 | |
| 150 // Creation and lifetime ------------------------------------------------------- | |
| 151 | |
| 152 RootView::RootView(Widget* widget) | |
| 153 : widget_(widget), | |
| 154 mouse_pressed_handler_(NULL), | |
| 155 mouse_move_handler_(NULL), | |
| 156 last_click_handler_(NULL), | |
| 157 explicit_mouse_handler_(false), | |
| 158 last_mouse_event_flags_(0), | |
| 159 last_mouse_event_x_(-1), | |
| 160 last_mouse_event_y_(-1), | |
| 161 gesture_handler_(NULL), | |
| 162 gesture_handler_set_before_processing_(false), | |
| 163 pre_dispatch_handler_(new internal::PreEventDispatchHandler(this)), | |
| 164 post_dispatch_handler_(new internal::PostEventDispatchHandler), | |
| 165 focus_search_(this, false, false), | |
| 166 focus_traversable_parent_(NULL), | |
| 167 focus_traversable_parent_view_(NULL), | |
| 168 event_dispatch_target_(NULL), | |
| 169 old_dispatch_target_(NULL) { | |
| 170 AddPreTargetHandler(pre_dispatch_handler_.get()); | |
| 171 AddPostTargetHandler(post_dispatch_handler_.get()); | |
| 172 SetEventTargeter(scoped_ptr<ViewTargeter>(new RootViewTargeter(this, this))); | |
| 173 } | |
| 174 | |
| 175 RootView::~RootView() { | |
| 176 // If we have children remove them explicitly so to make sure a remove | |
| 177 // notification is sent for each one of them. | |
| 178 if (has_children()) | |
| 179 RemoveAllChildViews(true); | |
| 180 } | |
| 181 | |
| 182 // Tree operations ------------------------------------------------------------- | |
| 183 | |
| 184 void RootView::SetContentsView(View* contents_view) { | |
| 185 DCHECK(contents_view && GetWidget()->native_widget()) << | |
| 186 "Can't be called until after the native widget is created!"; | |
| 187 // The ContentsView must be set up _after_ the window is created so that its | |
| 188 // Widget pointer is valid. | |
| 189 SetLayoutManager(new FillLayout); | |
| 190 if (has_children()) | |
| 191 RemoveAllChildViews(true); | |
| 192 AddChildView(contents_view); | |
| 193 | |
| 194 // Force a layout now, since the attached hierarchy won't be ready for the | |
| 195 // containing window's bounds. Note that we call Layout directly rather than | |
| 196 // calling the widget's size changed handler, since the RootView's bounds may | |
| 197 // not have changed, which will cause the Layout not to be done otherwise. | |
| 198 Layout(); | |
| 199 } | |
| 200 | |
| 201 View* RootView::GetContentsView() { | |
| 202 return child_count() > 0 ? child_at(0) : NULL; | |
| 203 } | |
| 204 | |
| 205 void RootView::NotifyNativeViewHierarchyChanged() { | |
| 206 PropagateNativeViewHierarchyChanged(); | |
| 207 } | |
| 208 | |
| 209 // Focus ----------------------------------------------------------------------- | |
| 210 | |
| 211 void RootView::SetFocusTraversableParent(FocusTraversable* focus_traversable) { | |
| 212 DCHECK(focus_traversable != this); | |
| 213 focus_traversable_parent_ = focus_traversable; | |
| 214 } | |
| 215 | |
| 216 void RootView::SetFocusTraversableParentView(View* view) { | |
| 217 focus_traversable_parent_view_ = view; | |
| 218 } | |
| 219 | |
| 220 // System events --------------------------------------------------------------- | |
| 221 | |
| 222 void RootView::ThemeChanged() { | |
| 223 View::PropagateThemeChanged(); | |
| 224 } | |
| 225 | |
| 226 void RootView::LocaleChanged() { | |
| 227 View::PropagateLocaleChanged(); | |
| 228 } | |
| 229 | |
| 230 //////////////////////////////////////////////////////////////////////////////// | |
| 231 // RootView, FocusTraversable implementation: | |
| 232 | |
| 233 FocusSearch* RootView::GetFocusSearch() { | |
| 234 return &focus_search_; | |
| 235 } | |
| 236 | |
| 237 FocusTraversable* RootView::GetFocusTraversableParent() { | |
| 238 return focus_traversable_parent_; | |
| 239 } | |
| 240 | |
| 241 View* RootView::GetFocusTraversableParentView() { | |
| 242 return focus_traversable_parent_view_; | |
| 243 } | |
| 244 | |
| 245 //////////////////////////////////////////////////////////////////////////////// | |
| 246 // RootView, ui::EventProcessor overrides: | |
| 247 | |
| 248 ui::EventTarget* RootView::GetRootTarget() { | |
| 249 return this; | |
| 250 } | |
| 251 | |
| 252 void RootView::OnEventProcessingStarted(ui::Event* event) { | |
| 253 if (!event->IsGestureEvent()) | |
| 254 return; | |
| 255 | |
| 256 ui::GestureEvent* gesture_event = event->AsGestureEvent(); | |
| 257 | |
| 258 // Do not process ui::ET_GESTURE_BEGIN events. | |
| 259 if (gesture_event->type() == ui::ET_GESTURE_BEGIN) { | |
| 260 event->SetHandled(); | |
| 261 return; | |
| 262 } | |
| 263 | |
| 264 // Do not process ui::ET_GESTURE_END events if they do not correspond to the | |
| 265 // removal of the final touch point or if no gesture handler has already | |
| 266 // been set. | |
| 267 if (gesture_event->type() == ui::ET_GESTURE_END && | |
| 268 (gesture_event->details().touch_points() > 1 || | |
| 269 !gesture_handler_)) { | |
| 270 event->SetHandled(); | |
| 271 return; | |
| 272 } | |
| 273 | |
| 274 // Do not process subsequent gesture scroll events if no handler was set for | |
| 275 // a ui::ET_GESTURE_SCROLL_BEGIN event. | |
| 276 if (!gesture_handler_ && | |
| 277 (gesture_event->type() == ui::ET_GESTURE_SCROLL_UPDATE || | |
| 278 gesture_event->type() == ui::ET_GESTURE_SCROLL_END || | |
| 279 gesture_event->type() == ui::ET_SCROLL_FLING_START)) { | |
| 280 event->SetHandled(); | |
| 281 return; | |
| 282 } | |
| 283 | |
| 284 gesture_handler_set_before_processing_ = !!gesture_handler_; | |
| 285 } | |
| 286 | |
| 287 void RootView::OnEventProcessingFinished(ui::Event* event) { | |
| 288 // If |event| was not handled and |gesture_handler_| was not set by the | |
| 289 // dispatch of a previous gesture event, then no default gesture handler | |
| 290 // should be set prior to the next gesture event being received. | |
| 291 if (event->IsGestureEvent() && | |
| 292 !event->handled() && | |
| 293 !gesture_handler_set_before_processing_) { | |
| 294 gesture_handler_ = NULL; | |
| 295 } | |
| 296 } | |
| 297 | |
| 298 //////////////////////////////////////////////////////////////////////////////// | |
| 299 // RootView, View overrides: | |
| 300 | |
| 301 const Widget* RootView::GetWidget() const { | |
| 302 return widget_; | |
| 303 } | |
| 304 | |
| 305 Widget* RootView::GetWidget() { | |
| 306 return const_cast<Widget*>(const_cast<const RootView*>(this)->GetWidget()); | |
| 307 } | |
| 308 | |
| 309 bool RootView::IsDrawn() const { | |
| 310 return visible(); | |
| 311 } | |
| 312 | |
| 313 void RootView::Layout() { | |
| 314 View::Layout(); | |
| 315 widget_->OnRootViewLayout(); | |
| 316 } | |
| 317 | |
| 318 const char* RootView::GetClassName() const { | |
| 319 return kViewClassName; | |
| 320 } | |
| 321 | |
| 322 void RootView::SchedulePaintInRect(const gfx::Rect& rect) { | |
| 323 if (layer()) { | |
| 324 layer()->SchedulePaint(rect); | |
| 325 } else { | |
| 326 gfx::Rect xrect = ConvertRectToParent(rect); | |
| 327 gfx::Rect invalid_rect = gfx::IntersectRects(GetLocalBounds(), xrect); | |
| 328 if (!invalid_rect.IsEmpty()) | |
| 329 widget_->SchedulePaintInRect(invalid_rect); | |
| 330 } | |
| 331 } | |
| 332 | |
| 333 bool RootView::OnMousePressed(const ui::MouseEvent& event) { | |
| 334 UpdateCursor(event); | |
| 335 SetMouseLocationAndFlags(event); | |
| 336 | |
| 337 // If mouse_pressed_handler_ is non null, we are currently processing | |
| 338 // a pressed -> drag -> released session. In that case we send the | |
| 339 // event to mouse_pressed_handler_ | |
| 340 if (mouse_pressed_handler_) { | |
| 341 ui::MouseEvent mouse_pressed_event(event, static_cast<View*>(this), | |
| 342 mouse_pressed_handler_); | |
| 343 drag_info_.Reset(); | |
| 344 ui::EventDispatchDetails dispatch_details = | |
| 345 DispatchEvent(mouse_pressed_handler_, &mouse_pressed_event); | |
| 346 if (dispatch_details.dispatcher_destroyed) | |
| 347 return true; | |
| 348 return true; | |
| 349 } | |
| 350 DCHECK(!explicit_mouse_handler_); | |
| 351 | |
| 352 bool hit_disabled_view = false; | |
| 353 // Walk up the tree until we find a view that wants the mouse event. | |
| 354 for (mouse_pressed_handler_ = GetEventHandlerForPoint(event.location()); | |
| 355 mouse_pressed_handler_ && (mouse_pressed_handler_ != this); | |
| 356 mouse_pressed_handler_ = mouse_pressed_handler_->parent()) { | |
| 357 DVLOG(1) << "OnMousePressed testing " | |
| 358 << mouse_pressed_handler_->GetClassName(); | |
| 359 if (!mouse_pressed_handler_->enabled()) { | |
| 360 // Disabled views should eat events instead of propagating them upwards. | |
| 361 hit_disabled_view = true; | |
| 362 break; | |
| 363 } | |
| 364 | |
| 365 // See if this view wants to handle the mouse press. | |
| 366 ui::MouseEvent mouse_pressed_event(event, static_cast<View*>(this), | |
| 367 mouse_pressed_handler_); | |
| 368 | |
| 369 // Remove the double-click flag if the handler is different than the | |
| 370 // one which got the first click part of the double-click. | |
| 371 if (mouse_pressed_handler_ != last_click_handler_) | |
| 372 mouse_pressed_event.set_flags(event.flags() & ~ui::EF_IS_DOUBLE_CLICK); | |
| 373 | |
| 374 drag_info_.Reset(); | |
| 375 ui::EventDispatchDetails dispatch_details = | |
| 376 DispatchEvent(mouse_pressed_handler_, &mouse_pressed_event); | |
| 377 if (dispatch_details.dispatcher_destroyed) | |
| 378 return mouse_pressed_event.handled(); | |
| 379 | |
| 380 // The view could have removed itself from the tree when handling | |
| 381 // OnMousePressed(). In this case, the removal notification will have | |
| 382 // reset mouse_pressed_handler_ to NULL out from under us. Detect this | |
| 383 // case and stop. (See comments in view.h.) | |
| 384 // | |
| 385 // NOTE: Don't return true here, because we don't want the frame to | |
| 386 // forward future events to us when there's no handler. | |
| 387 if (!mouse_pressed_handler_) | |
| 388 break; | |
| 389 | |
| 390 // If the view handled the event, leave mouse_pressed_handler_ set and | |
| 391 // return true, which will cause subsequent drag/release events to get | |
| 392 // forwarded to that view. | |
| 393 if (mouse_pressed_event.handled()) { | |
| 394 last_click_handler_ = mouse_pressed_handler_; | |
| 395 DVLOG(1) << "OnMousePressed handled by " | |
| 396 << mouse_pressed_handler_->GetClassName(); | |
| 397 return true; | |
| 398 } | |
| 399 } | |
| 400 | |
| 401 // Reset mouse_pressed_handler_ to indicate that no processing is occurring. | |
| 402 mouse_pressed_handler_ = NULL; | |
| 403 | |
| 404 // In the event that a double-click is not handled after traversing the | |
| 405 // entire hierarchy (even as a single-click when sent to a different view), | |
| 406 // it must be marked as handled to avoid anything happening from default | |
| 407 // processing if it the first click-part was handled by us. | |
| 408 if (last_click_handler_ && (event.flags() & ui::EF_IS_DOUBLE_CLICK)) | |
| 409 hit_disabled_view = true; | |
| 410 | |
| 411 last_click_handler_ = NULL; | |
| 412 return hit_disabled_view; | |
| 413 } | |
| 414 | |
| 415 bool RootView::OnMouseDragged(const ui::MouseEvent& event) { | |
| 416 CHECK_EQ(ui::ET_MOUSE_DRAGGED, event.type()); | |
| 417 if (mouse_pressed_handler_) { | |
| 418 SetMouseLocationAndFlags(event); | |
| 419 | |
| 420 ui::MouseEvent mouse_event(event, static_cast<View*>(this), | |
| 421 mouse_pressed_handler_); | |
| 422 ui::EventDispatchDetails dispatch_details = | |
| 423 DispatchEvent(mouse_pressed_handler_, &mouse_event); | |
| 424 if (dispatch_details.dispatcher_destroyed) | |
| 425 return false; | |
| 426 } | |
| 427 return false; | |
| 428 } | |
| 429 | |
| 430 void RootView::OnMouseReleased(const ui::MouseEvent& event) { | |
| 431 UpdateCursor(event); | |
| 432 | |
| 433 if (mouse_pressed_handler_) { | |
| 434 ui::MouseEvent mouse_released(event, static_cast<View*>(this), | |
| 435 mouse_pressed_handler_); | |
| 436 // We allow the view to delete us from the event dispatch callback. As such, | |
| 437 // configure state such that we're done first, then call View. | |
| 438 View* mouse_pressed_handler = mouse_pressed_handler_; | |
| 439 SetMouseHandler(NULL); | |
| 440 ui::EventDispatchDetails dispatch_details = | |
| 441 DispatchEvent(mouse_pressed_handler, &mouse_released); | |
| 442 if (dispatch_details.dispatcher_destroyed) | |
| 443 return; | |
| 444 } | |
| 445 } | |
| 446 | |
| 447 void RootView::OnMouseCaptureLost() { | |
| 448 // TODO: this likely needs to reset touch handler too. | |
| 449 | |
| 450 if (mouse_pressed_handler_ || gesture_handler_) { | |
| 451 // Synthesize a release event for UpdateCursor. | |
| 452 if (mouse_pressed_handler_) { | |
| 453 gfx::Point last_point(last_mouse_event_x_, last_mouse_event_y_); | |
| 454 ui::MouseEvent release_event(ui::ET_MOUSE_RELEASED, | |
| 455 last_point, last_point, | |
| 456 last_mouse_event_flags_, | |
| 457 0); | |
| 458 UpdateCursor(release_event); | |
| 459 } | |
| 460 // We allow the view to delete us from OnMouseCaptureLost. As such, | |
| 461 // configure state such that we're done first, then call View. | |
| 462 View* mouse_pressed_handler = mouse_pressed_handler_; | |
| 463 View* gesture_handler = gesture_handler_; | |
| 464 SetMouseHandler(NULL); | |
| 465 if (mouse_pressed_handler) | |
| 466 mouse_pressed_handler->OnMouseCaptureLost(); | |
| 467 else | |
| 468 gesture_handler->OnMouseCaptureLost(); | |
| 469 // WARNING: we may have been deleted. | |
| 470 } | |
| 471 } | |
| 472 | |
| 473 void RootView::OnMouseMoved(const ui::MouseEvent& event) { | |
| 474 CHECK_EQ(ui::ET_MOUSE_MOVED, event.type()); | |
| 475 View* v = GetEventHandlerForPoint(event.location()); | |
| 476 // Find the first enabled view, or the existing move handler, whichever comes | |
| 477 // first. The check for the existing handler is because if a view becomes | |
| 478 // disabled while handling moves, it's wrong to suddenly send ET_MOUSE_EXITED | |
| 479 // and ET_MOUSE_ENTERED events, because the mouse hasn't actually exited yet. | |
| 480 while (v && !v->enabled() && (v != mouse_move_handler_)) | |
| 481 v = v->parent(); | |
| 482 if (v && v != this) { | |
| 483 if (v != mouse_move_handler_) { | |
| 484 if (mouse_move_handler_ != NULL && | |
| 485 (!mouse_move_handler_->notify_enter_exit_on_child() || | |
| 486 !mouse_move_handler_->Contains(v))) { | |
| 487 MouseEnterExitEvent exit(event, ui::ET_MOUSE_EXITED); | |
| 488 exit.ConvertLocationToTarget(static_cast<View*>(this), | |
| 489 mouse_move_handler_); | |
| 490 ui::EventDispatchDetails dispatch_details = | |
| 491 DispatchEvent(mouse_move_handler_, &exit); | |
| 492 if (dispatch_details.dispatcher_destroyed) | |
| 493 return; | |
| 494 NotifyEnterExitOfDescendant(event, ui::ET_MOUSE_EXITED, | |
| 495 mouse_move_handler_, v); | |
| 496 } | |
| 497 View* old_handler = mouse_move_handler_; | |
| 498 mouse_move_handler_ = v; | |
| 499 if (!mouse_move_handler_->notify_enter_exit_on_child() || | |
| 500 !mouse_move_handler_->Contains(old_handler)) { | |
| 501 MouseEnterExitEvent entered(event, ui::ET_MOUSE_ENTERED); | |
| 502 entered.ConvertLocationToTarget(static_cast<View*>(this), | |
| 503 mouse_move_handler_); | |
| 504 ui::EventDispatchDetails dispatch_details = | |
| 505 DispatchEvent(mouse_move_handler_, &entered); | |
| 506 if (dispatch_details.dispatcher_destroyed) | |
| 507 return; | |
| 508 NotifyEnterExitOfDescendant(event, ui::ET_MOUSE_ENTERED, | |
| 509 mouse_move_handler_, old_handler); | |
| 510 } | |
| 511 } | |
| 512 ui::MouseEvent moved_event(event, static_cast<View*>(this), | |
| 513 mouse_move_handler_); | |
| 514 mouse_move_handler_->OnMouseMoved(moved_event); | |
| 515 // TODO(tdanderson): It may be possible to avoid setting the cursor twice | |
| 516 // (once here and once from CompoundEventFilter) on a | |
| 517 // mousemove. See crbug.com/351469. | |
| 518 if (!(moved_event.flags() & ui::EF_IS_NON_CLIENT)) | |
| 519 widget_->SetCursor(mouse_move_handler_->GetCursor(moved_event)); | |
| 520 } else if (mouse_move_handler_ != NULL) { | |
| 521 MouseEnterExitEvent exited(event, ui::ET_MOUSE_EXITED); | |
| 522 ui::EventDispatchDetails dispatch_details = | |
| 523 DispatchEvent(mouse_move_handler_, &exited); | |
| 524 if (dispatch_details.dispatcher_destroyed) | |
| 525 return; | |
| 526 NotifyEnterExitOfDescendant(event, ui::ET_MOUSE_EXITED, | |
| 527 mouse_move_handler_, v); | |
| 528 // On Aura the non-client area extends slightly outside the root view for | |
| 529 // some windows. Let the non-client cursor handling code set the cursor | |
| 530 // as we do above. | |
| 531 if (!(event.flags() & ui::EF_IS_NON_CLIENT)) | |
| 532 widget_->SetCursor(gfx::kNullCursor); | |
| 533 mouse_move_handler_ = NULL; | |
| 534 } | |
| 535 } | |
| 536 | |
| 537 void RootView::OnMouseExited(const ui::MouseEvent& event) { | |
| 538 if (mouse_move_handler_ != NULL) { | |
| 539 MouseEnterExitEvent exited(event, ui::ET_MOUSE_EXITED); | |
| 540 ui::EventDispatchDetails dispatch_details = | |
| 541 DispatchEvent(mouse_move_handler_, &exited); | |
| 542 if (dispatch_details.dispatcher_destroyed) | |
| 543 return; | |
| 544 NotifyEnterExitOfDescendant(event, ui::ET_MOUSE_EXITED, | |
| 545 mouse_move_handler_, NULL); | |
| 546 mouse_move_handler_ = NULL; | |
| 547 } | |
| 548 } | |
| 549 | |
| 550 bool RootView::OnMouseWheel(const ui::MouseWheelEvent& event) { | |
| 551 for (View* v = GetEventHandlerForPoint(event.location()); | |
| 552 v && v != this && !event.handled(); v = v->parent()) { | |
| 553 ui::EventDispatchDetails dispatch_details = | |
| 554 DispatchEvent(v, const_cast<ui::MouseWheelEvent*>(&event)); | |
| 555 if (dispatch_details.dispatcher_destroyed || | |
| 556 dispatch_details.target_destroyed) { | |
| 557 return event.handled(); | |
| 558 } | |
| 559 } | |
| 560 return event.handled(); | |
| 561 } | |
| 562 | |
| 563 void RootView::SetMouseHandler(View* new_mh) { | |
| 564 // If we're clearing the mouse handler, clear explicit_mouse_handler_ as well. | |
| 565 explicit_mouse_handler_ = (new_mh != NULL); | |
| 566 mouse_pressed_handler_ = new_mh; | |
| 567 gesture_handler_ = new_mh; | |
| 568 drag_info_.Reset(); | |
| 569 } | |
| 570 | |
| 571 void RootView::GetAccessibleState(ui::AXViewState* state) { | |
| 572 state->name = widget_->widget_delegate()->GetAccessibleWindowTitle(); | |
| 573 state->role = widget_->widget_delegate()->GetAccessibleWindowRole(); | |
| 574 } | |
| 575 | |
| 576 void RootView::UpdateParentLayer() { | |
| 577 if (layer()) | |
| 578 ReparentLayer(gfx::Vector2d(GetMirroredX(), y()), widget_->GetLayer()); | |
| 579 } | |
| 580 | |
| 581 //////////////////////////////////////////////////////////////////////////////// | |
| 582 // RootView, protected: | |
| 583 | |
| 584 void RootView::ViewHierarchyChanged( | |
| 585 const ViewHierarchyChangedDetails& details) { | |
| 586 widget_->ViewHierarchyChanged(details); | |
| 587 | |
| 588 if (!details.is_add) { | |
| 589 if (!explicit_mouse_handler_ && mouse_pressed_handler_ == details.child) | |
| 590 mouse_pressed_handler_ = NULL; | |
| 591 if (mouse_move_handler_ == details.child) | |
| 592 mouse_move_handler_ = NULL; | |
| 593 if (gesture_handler_ == details.child) | |
| 594 gesture_handler_ = NULL; | |
| 595 if (event_dispatch_target_ == details.child) | |
| 596 event_dispatch_target_ = NULL; | |
| 597 if (old_dispatch_target_ == details.child) | |
| 598 old_dispatch_target_ = NULL; | |
| 599 } | |
| 600 } | |
| 601 | |
| 602 void RootView::VisibilityChanged(View* /*starting_from*/, bool is_visible) { | |
| 603 if (!is_visible) { | |
| 604 // When the root view is being hidden (e.g. when widget is minimized) | |
| 605 // handlers are reset, so that after it is reshown, events are not captured | |
| 606 // by old handlers. | |
| 607 explicit_mouse_handler_ = false; | |
| 608 mouse_pressed_handler_ = NULL; | |
| 609 mouse_move_handler_ = NULL; | |
| 610 gesture_handler_ = NULL; | |
| 611 event_dispatch_target_ = NULL; | |
| 612 old_dispatch_target_ = NULL; | |
| 613 } | |
| 614 } | |
| 615 | |
| 616 void RootView::OnPaint(gfx::Canvas* canvas) { | |
| 617 if (!layer() || !layer()->fills_bounds_opaquely()) | |
| 618 canvas->DrawColor(SK_ColorBLACK, SkXfermode::kClear_Mode); | |
| 619 | |
| 620 View::OnPaint(canvas); | |
| 621 } | |
| 622 | |
| 623 gfx::Vector2d RootView::CalculateOffsetToAncestorWithLayer( | |
| 624 ui::Layer** layer_parent) { | |
| 625 gfx::Vector2d offset(View::CalculateOffsetToAncestorWithLayer(layer_parent)); | |
| 626 if (!layer() && layer_parent) | |
| 627 *layer_parent = widget_->GetLayer(); | |
| 628 return offset; | |
| 629 } | |
| 630 | |
| 631 View::DragInfo* RootView::GetDragInfo() { | |
| 632 return &drag_info_; | |
| 633 } | |
| 634 | |
| 635 //////////////////////////////////////////////////////////////////////////////// | |
| 636 // RootView, private: | |
| 637 | |
| 638 // Input ----------------------------------------------------------------------- | |
| 639 | |
| 640 void RootView::UpdateCursor(const ui::MouseEvent& event) { | |
| 641 if (!(event.flags() & ui::EF_IS_NON_CLIENT)) { | |
| 642 View* v = GetEventHandlerForPoint(event.location()); | |
| 643 ui::MouseEvent me(event, static_cast<View*>(this), v); | |
| 644 widget_->SetCursor(v->GetCursor(me)); | |
| 645 } | |
| 646 } | |
| 647 | |
| 648 void RootView::SetMouseLocationAndFlags(const ui::MouseEvent& event) { | |
| 649 last_mouse_event_flags_ = event.flags(); | |
| 650 last_mouse_event_x_ = event.x(); | |
| 651 last_mouse_event_y_ = event.y(); | |
| 652 } | |
| 653 | |
| 654 void RootView::NotifyEnterExitOfDescendant(const ui::MouseEvent& event, | |
| 655 ui::EventType type, | |
| 656 View* view, | |
| 657 View* sibling) { | |
| 658 for (View* p = view->parent(); p; p = p->parent()) { | |
| 659 if (!p->notify_enter_exit_on_child()) | |
| 660 continue; | |
| 661 if (sibling && p->Contains(sibling)) | |
| 662 break; | |
| 663 // It is necessary to recreate the notify-event for each dispatch, since one | |
| 664 // of the callbacks can mark the event as handled, and that would cause | |
| 665 // incorrect event dispatch. | |
| 666 MouseEnterExitEvent notify_event(event, type); | |
| 667 ui::EventDispatchDetails dispatch_details = DispatchEvent(p, ¬ify_event); | |
| 668 if (dispatch_details.dispatcher_destroyed || | |
| 669 dispatch_details.target_destroyed) { | |
| 670 return; | |
| 671 } | |
| 672 } | |
| 673 } | |
| 674 | |
| 675 bool RootView::CanDispatchToTarget(ui::EventTarget* target) { | |
| 676 return event_dispatch_target_ == target; | |
| 677 } | |
| 678 | |
| 679 ui::EventDispatchDetails RootView::PreDispatchEvent(ui::EventTarget* target, | |
| 680 ui::Event* event) { | |
| 681 View* view = static_cast<View*>(target); | |
| 682 if (event->IsGestureEvent()) { | |
| 683 // Update |gesture_handler_| to indicate which View is currently handling | |
| 684 // gesture events. | |
| 685 // TODO(tdanderson): Look into moving this to PostDispatchEvent() and | |
| 686 // using |event_dispatch_target_| instead of | |
| 687 // |gesture_handler_| to detect if the view has been | |
| 688 // removed from the tree. | |
| 689 gesture_handler_ = view; | |
| 690 | |
| 691 // Disabled views are permitted to be targets of gesture events, but | |
| 692 // gesture events should never actually be dispatched to them. Prevent | |
| 693 // dispatch by marking the event as handled. | |
| 694 if (!view->enabled()) | |
| 695 event->SetHandled(); | |
| 696 } | |
| 697 | |
| 698 old_dispatch_target_ = event_dispatch_target_; | |
| 699 event_dispatch_target_ = view; | |
| 700 return DispatchDetails(); | |
| 701 } | |
| 702 | |
| 703 ui::EventDispatchDetails RootView::PostDispatchEvent(ui::EventTarget* target, | |
| 704 const ui::Event& event) { | |
| 705 // The GESTURE_END event corresponding to the removal of the final touch | |
| 706 // point marks the end of a gesture sequence, so reset |gesture_handler_| | |
| 707 // to NULL. | |
| 708 if (event.type() == ui::ET_GESTURE_END) { | |
| 709 // In case a drag was in progress, reset all the handlers. Otherwise, just | |
| 710 // reset the gesture handler. | |
| 711 if (gesture_handler_ && gesture_handler_ == mouse_pressed_handler_) | |
| 712 SetMouseHandler(NULL); | |
| 713 else | |
| 714 gesture_handler_ = NULL; | |
| 715 } | |
| 716 | |
| 717 DispatchDetails details; | |
| 718 if (target != event_dispatch_target_) | |
| 719 details.target_destroyed = true; | |
| 720 | |
| 721 event_dispatch_target_ = old_dispatch_target_; | |
| 722 old_dispatch_target_ = NULL; | |
| 723 | |
| 724 #ifndef NDEBUG | |
| 725 DCHECK(!event_dispatch_target_ || Contains(event_dispatch_target_)); | |
| 726 #endif | |
| 727 | |
| 728 return details; | |
| 729 } | |
| 730 | |
| 731 } // namespace internal | |
| 732 } // namespace views | |
| OLD | NEW |