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

Side by Side Diff: ui/views/view.cc

Issue 22891016: Add support for rect-based event targeting in views (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: patch for landing Created 7 years, 1 month 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
« no previous file with comments | « ui/views/view.h ('k') | ui/views/view_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 #define _USE_MATH_DEFINES // For VC++ to get M_PI. This has to be first. 5 #define _USE_MATH_DEFINES // For VC++ to get M_PI. This has to be first.
6 6
7 #include "ui/views/view.h" 7 #include "ui/views/view.h"
8 8
9 #include <algorithm> 9 #include <algorithm>
10 #include <cmath> 10 #include <cmath>
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
62 #else 62 #else
63 bool use_acceleration_when_possible = false; 63 bool use_acceleration_when_possible = false;
64 #endif 64 #endif
65 65
66 #if defined(OS_WIN) 66 #if defined(OS_WIN)
67 const bool kContextMenuOnMousePress = false; 67 const bool kContextMenuOnMousePress = false;
68 #else 68 #else
69 const bool kContextMenuOnMousePress = true; 69 const bool kContextMenuOnMousePress = true;
70 #endif 70 #endif
71 71
72 // The minimum percentage of a view's area that needs to be covered by a rect
73 // representing a touch region in order for that view to be considered by the
74 // rect-based targeting algorithm.
75 static const float kRectTargetOverlap = 0.6f;
76
72 // Returns the top view in |view|'s hierarchy. 77 // Returns the top view in |view|'s hierarchy.
73 const views::View* GetHierarchyRoot(const views::View* view) { 78 const views::View* GetHierarchyRoot(const views::View* view) {
74 const views::View* root = view; 79 const views::View* root = view;
75 while (root && root->parent()) 80 while (root && root->parent())
76 root = root->parent(); 81 root = root->parent();
77 return root; 82 return root;
78 } 83 }
79 84
80 } // namespace 85 } // namespace
81 86
(...skipping 745 matching lines...) Expand 10 before | Expand all | Expand 10 after
827 } 832 }
828 833
829 // static 834 // static
830 bool View::get_use_acceleration_when_possible() { 835 bool View::get_use_acceleration_when_possible() {
831 return use_acceleration_when_possible; 836 return use_acceleration_when_possible;
832 } 837 }
833 838
834 // Input ----------------------------------------------------------------------- 839 // Input -----------------------------------------------------------------------
835 840
836 View* View::GetEventHandlerForPoint(const gfx::Point& point) { 841 View* View::GetEventHandlerForPoint(const gfx::Point& point) {
837 // Walk the child Views recursively looking for the View that most 842 return GetEventHandlerForRect(gfx::Rect(point, gfx::Size(1, 1)));
838 // tightly encloses the specified point. 843 }
844
845 View* View::GetEventHandlerForRect(const gfx::Rect& rect) {
846 // |rect_view| represents the current best candidate to return
847 // if rect-based targeting (i.e., fuzzing) is used.
848 // |rect_view_distance| is used to keep track of the distance
849 // between the center point of |rect_view| and the center
850 // point of |rect|.
851 View* rect_view = NULL;
852 int rect_view_distance = INT_MAX;
853
854 // |point_view| represents the view that would have been returned
855 // from this function call if point-based targeting were used.
856 View* point_view = NULL;
857
839 for (int i = child_count() - 1; i >= 0; --i) { 858 for (int i = child_count() - 1; i >= 0; --i) {
840 View* child = child_at(i); 859 View* child = child_at(i);
860
861 // Ignore any children which are invisible or do not intersect |rect|.
841 if (!child->visible()) 862 if (!child->visible())
842 continue; 863 continue;
864 gfx::RectF rect_in_child_coords_f(rect);
865 ConvertRectToTarget(this, child, &rect_in_child_coords_f);
866 gfx::Rect rect_in_child_coords = gfx::ToEnclosingRect(
867 rect_in_child_coords_f);
868 if (!child->HitTestRect(rect_in_child_coords))
869 continue;
843 870
844 gfx::Point point_in_child_coords(point); 871 View* cur_view = child->GetEventHandlerForRect(rect_in_child_coords);
845 ConvertPointToTarget(this, child, &point_in_child_coords); 872
846 if (child->HitTestPoint(point_in_child_coords)) 873 if (views::UsePointBasedTargeting(rect))
847 return child->GetEventHandlerForPoint(point_in_child_coords); 874 return cur_view;
875
876 gfx::RectF cur_view_bounds_f(cur_view->GetLocalBounds());
877 ConvertRectToTarget(cur_view, this, &cur_view_bounds_f);
878 gfx::Rect cur_view_bounds = gfx::ToEnclosingRect(
879 cur_view_bounds_f);
880 if (views::PercentCoveredBy(cur_view_bounds, rect) >= kRectTargetOverlap) {
881 // |cur_view| is a suitable candidate for rect-based targeting.
882 // Check to see if it is the closest suitable candidate so far.
883 gfx::Point touch_center(rect.CenterPoint());
884 int cur_dist = views::DistanceSquaredFromCenterLineToPoint(
885 touch_center, cur_view_bounds);
886 if (!rect_view || cur_dist < rect_view_distance) {
887 rect_view = cur_view;
888 rect_view_distance = cur_dist;
889 }
890 } else if (!rect_view && !point_view) {
891 // Rect-based targeting has not yielded any candidates so far. Check
892 // if point-based targeting would have selected |cur_view|.
893 gfx::Point point_in_child_coords(rect_in_child_coords.CenterPoint());
894 if (child->HitTestPoint(point_in_child_coords))
895 point_view = child->GetEventHandlerForPoint(point_in_child_coords);
896 }
848 } 897 }
849 return this; 898
899 if (views::UsePointBasedTargeting(rect) || (!rect_view && !point_view))
900 return this;
901
902 // If |this| is a suitable candidate for rect-based targeting, check to
903 // see if it is closer than the current best suitable candidate so far.
904 gfx::Rect local_bounds(GetLocalBounds());
905 if (views::PercentCoveredBy(local_bounds, rect) >= kRectTargetOverlap) {
906 gfx::Point touch_center(rect.CenterPoint());
907 int cur_dist = views::DistanceSquaredFromCenterLineToPoint(touch_center,
908 local_bounds);
909 if (!rect_view || cur_dist < rect_view_distance)
910 rect_view = this;
911 }
912
913 return rect_view ? rect_view : point_view;
850 } 914 }
851 915
852 View* View::GetTooltipHandlerForPoint(const gfx::Point& point) { 916 View* View::GetTooltipHandlerForPoint(const gfx::Point& point) {
853 if (!HitTestPoint(point)) 917 if (!HitTestPoint(point))
854 return NULL; 918 return NULL;
855 919
856 // Walk the child Views recursively looking for the View that most 920 // Walk the child Views recursively looking for the View that most
857 // tightly encloses the specified point. 921 // tightly encloses the specified point.
858 for (int i = child_count() - 1; i >= 0; --i) { 922 for (int i = child_count() - 1; i >= 0; --i) {
859 View* child = child_at(i); 923 View* child = child_at(i);
(...skipping 1467 matching lines...) Expand 10 before | Expand all | Expand 10 after
2327 // Message the RootView to do the drag and drop. That way if we're removed 2391 // Message the RootView to do the drag and drop. That way if we're removed
2328 // the RootView can detect it and avoid calling us back. 2392 // the RootView can detect it and avoid calling us back.
2329 gfx::Point widget_location(event.location()); 2393 gfx::Point widget_location(event.location());
2330 ConvertPointToWidget(this, &widget_location); 2394 ConvertPointToWidget(this, &widget_location);
2331 widget->RunShellDrag(this, data, widget_location, drag_operations, source); 2395 widget->RunShellDrag(this, data, widget_location, drag_operations, source);
2332 // WARNING: we may have been deleted. 2396 // WARNING: we may have been deleted.
2333 return true; 2397 return true;
2334 } 2398 }
2335 2399
2336 } // namespace views 2400 } // namespace views
OLDNEW
« no previous file with comments | « ui/views/view.h ('k') | ui/views/view_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698