Home | History | Annotate | Download | only in widget
      1 // Copyright 2014 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_targeter.h"
      6 
      7 #include "ui/views/view.h"
      8 #include "ui/views/view_targeter_delegate.h"
      9 #include "ui/views/views_switches.h"
     10 #include "ui/views/widget/root_view.h"
     11 #include "ui/views/widget/widget.h"
     12 
     13 namespace views {
     14 
     15 RootViewTargeter::RootViewTargeter(ViewTargeterDelegate* delegate,
     16                                    internal::RootView* root_view)
     17     : ViewTargeter(delegate), root_view_(root_view) {
     18 }
     19 
     20 RootViewTargeter::~RootViewTargeter() {
     21 }
     22 
     23 View* RootViewTargeter::FindTargetForGestureEvent(
     24     View* root,
     25     const ui::GestureEvent& gesture) {
     26   CHECK_EQ(root, root_view_);
     27 
     28   // Return the default gesture handler if one is already set.
     29   if (root_view_->gesture_handler_) {
     30     CHECK(root_view_->gesture_handler_set_before_processing_);
     31     return root_view_->gesture_handler_;
     32   }
     33 
     34   // If no default gesture handler has already been set, do not perform any
     35   // targeting for a ET_GESTURE_END event.
     36   if (gesture.type() == ui::ET_GESTURE_END)
     37     return NULL;
     38 
     39   // If rect-based targeting is enabled, use the gesture's bounding box to
     40   // determine the target. Otherwise use the center point of the gesture's
     41   // bounding box to determine the target.
     42   gfx::Rect rect(gesture.location(), gfx::Size(1, 1));
     43   if (views::switches::IsRectBasedTargetingEnabled() &&
     44       !gesture.details().bounding_box().IsEmpty()) {
     45     // TODO(tdanderson): Pass in the bounding box to GetEventHandlerForRect()
     46     // once crbug.com/313392 is resolved.
     47     rect.set_size(gesture.details().bounding_box().size());
     48     rect.Offset(-rect.width() / 2, -rect.height() / 2);
     49   }
     50 
     51   return root->GetEffectiveViewTargeter()->TargetForRect(root, rect);
     52 }
     53 
     54 ui::EventTarget* RootViewTargeter::FindNextBestTargetForGestureEvent(
     55     ui::EventTarget* previous_target,
     56     const ui::GestureEvent& gesture) {
     57   // ET_GESTURE_END events should only ever be targeted to the default
     58   // gesture handler set by a previous gesture, if one exists. Thus we do not
     59   // permit any re-targeting of ET_GESTURE_END events.
     60   if (gesture.type() == ui::ET_GESTURE_END)
     61     return NULL;
     62 
     63   // Prohibit re-targeting of gesture events (except for GESTURE_SCROLL_BEGIN
     64   // events) if the default gesture handler was set by the dispatch of a
     65   // previous gesture event.
     66   if (root_view_->gesture_handler_set_before_processing_ &&
     67       gesture.type() != ui::ET_GESTURE_SCROLL_BEGIN) {
     68     return NULL;
     69   }
     70 
     71   // If |gesture_handler_| is NULL, it is either because the view was removed
     72   // from the tree by the previous dispatch of |gesture| or because |gesture| is
     73   // the GESTURE_END event corresponding to the removal of the last touch
     74   // point. In either case, no further re-targeting of |gesture| should be
     75   // permitted.
     76   if (!root_view_->gesture_handler_)
     77     return NULL;
     78 
     79   return previous_target->GetParentTarget();
     80 }
     81 
     82 }  // namespace views
     83