Explicitly add python-numpy dependency to install-build-deps.
[chromium-blink-merge.git] / ui / views / widget / root_view.cc
blobf282b9a4b009401fb88f0d995111d672bb94bc87
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.
5 #include "ui/views/widget/root_view.h"
7 #include <algorithm>
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"
27 typedef ui::EventDispatchDetails DispatchDetails;
29 namespace views {
30 namespace internal {
32 namespace {
34 enum EventType {
35 EVENT_ENTER,
36 EVENT_EXIT
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);
50 ~MouseEnterExitEvent() override {}
53 } // namespace
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) {
63 ~PreEventDispatchHandler() override {}
65 private:
66 // ui::EventHandler:
67 void OnKeyEvent(ui::KeyEvent* event) override {
68 CHECK_EQ(ui::EP_PRETARGET, event->phase());
69 if (event->handled())
70 return;
72 View* v = NULL;
73 if (owner_->GetFocusManager()) // Can be NULL in unittests.
74 v = owner_->GetFocusManager()->GetFocusedView();
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());
87 v->ShowContextMenu(location, ui::MENU_SOURCE_KEYBOARD);
88 event->StopPropagation();
92 View* owner_;
94 DISALLOW_COPY_AND_ASSIGN(PreEventDispatchHandler);
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()) {
105 ~PostEventDispatchHandler() override {}
107 private:
108 // Overridden from ui::EventHandler:
109 void OnGestureEvent(ui::GestureEvent* event) override {
110 DCHECK_EQ(ui::EP_POSTTARGET, event->phase());
111 if (event->handled())
112 return;
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;
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();
139 bool touch_dnd_enabled_;
141 DISALLOW_COPY_AND_ASSIGN(PostEventDispatchHandler);
144 // static
145 const char RootView::kViewClassName[] = "RootView";
147 ////////////////////////////////////////////////////////////////////////////////
148 // RootView, public:
150 // Creation and lifetime -------------------------------------------------------
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)));
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);
182 // Tree operations -------------------------------------------------------------
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);
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();
201 View* RootView::GetContentsView() {
202 return child_count() > 0 ? child_at(0) : NULL;
205 void RootView::NotifyNativeViewHierarchyChanged() {
206 PropagateNativeViewHierarchyChanged();
209 // Focus -----------------------------------------------------------------------
211 void RootView::SetFocusTraversableParent(FocusTraversable* focus_traversable) {
212 DCHECK(focus_traversable != this);
213 focus_traversable_parent_ = focus_traversable;
216 void RootView::SetFocusTraversableParentView(View* view) {
217 focus_traversable_parent_view_ = view;
220 // System events ---------------------------------------------------------------
222 void RootView::ThemeChanged() {
223 View::PropagateThemeChanged();
226 void RootView::LocaleChanged() {
227 View::PropagateLocaleChanged();
230 ////////////////////////////////////////////////////////////////////////////////
231 // RootView, FocusTraversable implementation:
233 FocusSearch* RootView::GetFocusSearch() {
234 return &focus_search_;
237 FocusTraversable* RootView::GetFocusTraversableParent() {
238 return focus_traversable_parent_;
241 View* RootView::GetFocusTraversableParentView() {
242 return focus_traversable_parent_view_;
245 ////////////////////////////////////////////////////////////////////////////////
246 // RootView, ui::EventProcessor overrides:
248 ui::EventTarget* RootView::GetRootTarget() {
249 return this;
252 void RootView::OnEventProcessingStarted(ui::Event* event) {
253 if (!event->IsGestureEvent())
254 return;
256 ui::GestureEvent* gesture_event = event->AsGestureEvent();
258 // Do not process ui::ET_GESTURE_BEGIN events.
259 if (gesture_event->type() == ui::ET_GESTURE_BEGIN) {
260 event->SetHandled();
261 return;
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;
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;
284 gesture_handler_set_before_processing_ = !!gesture_handler_;
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;
298 ////////////////////////////////////////////////////////////////////////////////
299 // RootView, View overrides:
301 const Widget* RootView::GetWidget() const {
302 return widget_;
305 Widget* RootView::GetWidget() {
306 return const_cast<Widget*>(const_cast<const RootView*>(this)->GetWidget());
309 bool RootView::IsDrawn() const {
310 return visible();
313 void RootView::Layout() {
314 View::Layout();
315 widget_->OnRootViewLayout();
318 const char* RootView::GetClassName() const {
319 return kViewClassName;
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);
333 bool RootView::OnMousePressed(const ui::MouseEvent& event) {
334 UpdateCursor(event);
335 SetMouseLocationAndFlags(event);
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;
350 DCHECK(!explicit_mouse_handler_);
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;
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_);
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);
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();
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.)
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;
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;
401 // Reset mouse_pressed_handler_ to indicate that no processing is occurring.
402 mouse_pressed_handler_ = NULL;
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;
411 last_click_handler_ = NULL;
412 return hit_disabled_view;
415 bool RootView::OnMouseDragged(const ui::MouseEvent& event) {
416 if (mouse_pressed_handler_) {
417 SetMouseLocationAndFlags(event);
419 ui::MouseEvent mouse_event(event, static_cast<View*>(this),
420 mouse_pressed_handler_);
421 ui::EventDispatchDetails dispatch_details =
422 DispatchEvent(mouse_pressed_handler_, &mouse_event);
423 if (dispatch_details.dispatcher_destroyed)
424 return false;
426 return false;
429 void RootView::OnMouseReleased(const ui::MouseEvent& event) {
430 UpdateCursor(event);
432 if (mouse_pressed_handler_) {
433 ui::MouseEvent mouse_released(event, static_cast<View*>(this),
434 mouse_pressed_handler_);
435 // We allow the view to delete us from the event dispatch callback. As such,
436 // configure state such that we're done first, then call View.
437 View* mouse_pressed_handler = mouse_pressed_handler_;
438 SetMouseHandler(NULL);
439 ui::EventDispatchDetails dispatch_details =
440 DispatchEvent(mouse_pressed_handler, &mouse_released);
441 if (dispatch_details.dispatcher_destroyed)
442 return;
446 void RootView::OnMouseCaptureLost() {
447 // TODO: this likely needs to reset touch handler too.
449 if (mouse_pressed_handler_ || gesture_handler_) {
450 // Synthesize a release event for UpdateCursor.
451 if (mouse_pressed_handler_) {
452 gfx::Point last_point(last_mouse_event_x_, last_mouse_event_y_);
453 ui::MouseEvent release_event(ui::ET_MOUSE_RELEASED,
454 last_point, last_point,
455 last_mouse_event_flags_,
457 UpdateCursor(release_event);
459 // We allow the view to delete us from OnMouseCaptureLost. As such,
460 // configure state such that we're done first, then call View.
461 View* mouse_pressed_handler = mouse_pressed_handler_;
462 View* gesture_handler = gesture_handler_;
463 SetMouseHandler(NULL);
464 if (mouse_pressed_handler)
465 mouse_pressed_handler->OnMouseCaptureLost();
466 else
467 gesture_handler->OnMouseCaptureLost();
468 // WARNING: we may have been deleted.
472 void RootView::OnMouseMoved(const ui::MouseEvent& event) {
473 View* v = GetEventHandlerForPoint(event.location());
474 // Find the first enabled view, or the existing move handler, whichever comes
475 // first. The check for the existing handler is because if a view becomes
476 // disabled while handling moves, it's wrong to suddenly send ET_MOUSE_EXITED
477 // and ET_MOUSE_ENTERED events, because the mouse hasn't actually exited yet.
478 while (v && !v->enabled() && (v != mouse_move_handler_))
479 v = v->parent();
480 if (v && v != this) {
481 if (v != mouse_move_handler_) {
482 if (mouse_move_handler_ != NULL &&
483 (!mouse_move_handler_->notify_enter_exit_on_child() ||
484 !mouse_move_handler_->Contains(v))) {
485 MouseEnterExitEvent exit(event, ui::ET_MOUSE_EXITED);
486 exit.ConvertLocationToTarget(static_cast<View*>(this),
487 mouse_move_handler_);
488 ui::EventDispatchDetails dispatch_details =
489 DispatchEvent(mouse_move_handler_, &exit);
490 if (dispatch_details.dispatcher_destroyed)
491 return;
492 NotifyEnterExitOfDescendant(event, ui::ET_MOUSE_EXITED,
493 mouse_move_handler_, v);
495 View* old_handler = mouse_move_handler_;
496 mouse_move_handler_ = v;
497 if (!mouse_move_handler_->notify_enter_exit_on_child() ||
498 !mouse_move_handler_->Contains(old_handler)) {
499 MouseEnterExitEvent entered(event, ui::ET_MOUSE_ENTERED);
500 entered.ConvertLocationToTarget(static_cast<View*>(this),
501 mouse_move_handler_);
502 ui::EventDispatchDetails dispatch_details =
503 DispatchEvent(mouse_move_handler_, &entered);
504 if (dispatch_details.dispatcher_destroyed)
505 return;
506 NotifyEnterExitOfDescendant(event, ui::ET_MOUSE_ENTERED,
507 mouse_move_handler_, old_handler);
510 ui::MouseEvent moved_event(event, static_cast<View*>(this),
511 mouse_move_handler_);
512 mouse_move_handler_->OnMouseMoved(moved_event);
513 // TODO(tdanderson): It may be possible to avoid setting the cursor twice
514 // (once here and once from CompoundEventFilter) on a
515 // mousemove. See crbug.com/351469.
516 if (!(moved_event.flags() & ui::EF_IS_NON_CLIENT))
517 widget_->SetCursor(mouse_move_handler_->GetCursor(moved_event));
518 } else if (mouse_move_handler_ != NULL) {
519 MouseEnterExitEvent exited(event, ui::ET_MOUSE_EXITED);
520 ui::EventDispatchDetails dispatch_details =
521 DispatchEvent(mouse_move_handler_, &exited);
522 if (dispatch_details.dispatcher_destroyed)
523 return;
524 NotifyEnterExitOfDescendant(event, ui::ET_MOUSE_EXITED,
525 mouse_move_handler_, v);
526 // On Aura the non-client area extends slightly outside the root view for
527 // some windows. Let the non-client cursor handling code set the cursor
528 // as we do above.
529 if (!(event.flags() & ui::EF_IS_NON_CLIENT))
530 widget_->SetCursor(gfx::kNullCursor);
531 mouse_move_handler_ = NULL;
535 void RootView::OnMouseExited(const ui::MouseEvent& event) {
536 if (mouse_move_handler_ != NULL) {
537 MouseEnterExitEvent exited(event, ui::ET_MOUSE_EXITED);
538 ui::EventDispatchDetails dispatch_details =
539 DispatchEvent(mouse_move_handler_, &exited);
540 if (dispatch_details.dispatcher_destroyed)
541 return;
542 NotifyEnterExitOfDescendant(event, ui::ET_MOUSE_EXITED,
543 mouse_move_handler_, NULL);
544 mouse_move_handler_ = NULL;
548 bool RootView::OnMouseWheel(const ui::MouseWheelEvent& event) {
549 for (View* v = GetEventHandlerForPoint(event.location());
550 v && v != this && !event.handled(); v = v->parent()) {
551 ui::EventDispatchDetails dispatch_details =
552 DispatchEvent(v, const_cast<ui::MouseWheelEvent*>(&event));
553 if (dispatch_details.dispatcher_destroyed ||
554 dispatch_details.target_destroyed) {
555 return event.handled();
558 return event.handled();
561 void RootView::SetMouseHandler(View* new_mh) {
562 // If we're clearing the mouse handler, clear explicit_mouse_handler_ as well.
563 explicit_mouse_handler_ = (new_mh != NULL);
564 mouse_pressed_handler_ = new_mh;
565 gesture_handler_ = new_mh;
566 drag_info_.Reset();
569 void RootView::GetAccessibleState(ui::AXViewState* state) {
570 state->name = widget_->widget_delegate()->GetAccessibleWindowTitle();
571 state->role = widget_->widget_delegate()->GetAccessibleWindowRole();
574 void RootView::UpdateParentLayer() {
575 if (layer())
576 ReparentLayer(gfx::Vector2d(GetMirroredX(), y()), widget_->GetLayer());
579 ////////////////////////////////////////////////////////////////////////////////
580 // RootView, protected:
582 void RootView::ViewHierarchyChanged(
583 const ViewHierarchyChangedDetails& details) {
584 widget_->ViewHierarchyChanged(details);
586 if (!details.is_add) {
587 if (!explicit_mouse_handler_ && mouse_pressed_handler_ == details.child)
588 mouse_pressed_handler_ = NULL;
589 if (mouse_move_handler_ == details.child)
590 mouse_move_handler_ = NULL;
591 if (gesture_handler_ == details.child)
592 gesture_handler_ = NULL;
593 if (event_dispatch_target_ == details.child)
594 event_dispatch_target_ = NULL;
595 if (old_dispatch_target_ == details.child)
596 old_dispatch_target_ = NULL;
600 void RootView::VisibilityChanged(View* /*starting_from*/, bool is_visible) {
601 if (!is_visible) {
602 // When the root view is being hidden (e.g. when widget is minimized)
603 // handlers are reset, so that after it is reshown, events are not captured
604 // by old handlers.
605 explicit_mouse_handler_ = false;
606 mouse_pressed_handler_ = NULL;
607 mouse_move_handler_ = NULL;
608 gesture_handler_ = NULL;
609 event_dispatch_target_ = NULL;
610 old_dispatch_target_ = NULL;
614 void RootView::OnPaint(gfx::Canvas* canvas) {
615 if (!layer() || !layer()->fills_bounds_opaquely())
616 canvas->DrawColor(SK_ColorBLACK, SkXfermode::kClear_Mode);
618 View::OnPaint(canvas);
621 gfx::Vector2d RootView::CalculateOffsetToAncestorWithLayer(
622 ui::Layer** layer_parent) {
623 gfx::Vector2d offset(View::CalculateOffsetToAncestorWithLayer(layer_parent));
624 if (!layer() && layer_parent)
625 *layer_parent = widget_->GetLayer();
626 return offset;
629 View::DragInfo* RootView::GetDragInfo() {
630 return &drag_info_;
633 ////////////////////////////////////////////////////////////////////////////////
634 // RootView, private:
636 // Input -----------------------------------------------------------------------
638 void RootView::UpdateCursor(const ui::MouseEvent& event) {
639 if (!(event.flags() & ui::EF_IS_NON_CLIENT)) {
640 View* v = GetEventHandlerForPoint(event.location());
641 ui::MouseEvent me(event, static_cast<View*>(this), v);
642 widget_->SetCursor(v->GetCursor(me));
646 void RootView::SetMouseLocationAndFlags(const ui::MouseEvent& event) {
647 last_mouse_event_flags_ = event.flags();
648 last_mouse_event_x_ = event.x();
649 last_mouse_event_y_ = event.y();
652 void RootView::NotifyEnterExitOfDescendant(const ui::MouseEvent& event,
653 ui::EventType type,
654 View* view,
655 View* sibling) {
656 for (View* p = view->parent(); p; p = p->parent()) {
657 if (!p->notify_enter_exit_on_child())
658 continue;
659 if (sibling && p->Contains(sibling))
660 break;
661 // It is necessary to recreate the notify-event for each dispatch, since one
662 // of the callbacks can mark the event as handled, and that would cause
663 // incorrect event dispatch.
664 MouseEnterExitEvent notify_event(event, type);
665 ui::EventDispatchDetails dispatch_details = DispatchEvent(p, &notify_event);
666 if (dispatch_details.dispatcher_destroyed ||
667 dispatch_details.target_destroyed) {
668 return;
673 bool RootView::CanDispatchToTarget(ui::EventTarget* target) {
674 return event_dispatch_target_ == target;
677 ui::EventDispatchDetails RootView::PreDispatchEvent(ui::EventTarget* target,
678 ui::Event* event) {
679 View* view = static_cast<View*>(target);
680 if (event->IsGestureEvent()) {
681 // Update |gesture_handler_| to indicate which View is currently handling
682 // gesture events.
683 // TODO(tdanderson): Look into moving this to PostDispatchEvent() and
684 // using |event_dispatch_target_| instead of
685 // |gesture_handler_| to detect if the view has been
686 // removed from the tree.
687 gesture_handler_ = view;
689 // Disabled views are permitted to be targets of gesture events, but
690 // gesture events should never actually be dispatched to them. Prevent
691 // dispatch by marking the event as handled.
692 if (!view->enabled())
693 event->SetHandled();
696 old_dispatch_target_ = event_dispatch_target_;
697 event_dispatch_target_ = view;
698 return DispatchDetails();
701 ui::EventDispatchDetails RootView::PostDispatchEvent(ui::EventTarget* target,
702 const ui::Event& event) {
703 // The GESTURE_END event corresponding to the removal of the final touch
704 // point marks the end of a gesture sequence, so reset |gesture_handler_|
705 // to NULL.
706 if (event.type() == ui::ET_GESTURE_END) {
707 // In case a drag was in progress, reset all the handlers. Otherwise, just
708 // reset the gesture handler.
709 if (gesture_handler_ && gesture_handler_ == mouse_pressed_handler_)
710 SetMouseHandler(NULL);
711 else
712 gesture_handler_ = NULL;
715 DispatchDetails details;
716 if (target != event_dispatch_target_)
717 details.target_destroyed = true;
719 event_dispatch_target_ = old_dispatch_target_;
720 old_dispatch_target_ = NULL;
722 #ifndef NDEBUG
723 DCHECK(!event_dispatch_target_ || Contains(event_dispatch_target_));
724 #endif
726 return details;
729 } // namespace internal
730 } // namespace views