OLD | NEW |
---|---|
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 20 matching lines...) Expand all Loading... | |
31 #include "ui/gfx/scoped_canvas.h" | 31 #include "ui/gfx/scoped_canvas.h" |
32 #include "ui/gfx/screen.h" | 32 #include "ui/gfx/screen.h" |
33 #include "ui/gfx/skia_util.h" | 33 #include "ui/gfx/skia_util.h" |
34 #include "ui/gfx/transform.h" | 34 #include "ui/gfx/transform.h" |
35 #include "ui/native_theme/native_theme.h" | 35 #include "ui/native_theme/native_theme.h" |
36 #include "ui/views/accessibility/native_view_accessibility.h" | 36 #include "ui/views/accessibility/native_view_accessibility.h" |
37 #include "ui/views/background.h" | 37 #include "ui/views/background.h" |
38 #include "ui/views/context_menu_controller.h" | 38 #include "ui/views/context_menu_controller.h" |
39 #include "ui/views/drag_controller.h" | 39 #include "ui/views/drag_controller.h" |
40 #include "ui/views/layout/layout_manager.h" | 40 #include "ui/views/layout/layout_manager.h" |
41 #include "ui/views/rect_based_targeting_utils.h" | |
41 #include "ui/views/views_delegate.h" | 42 #include "ui/views/views_delegate.h" |
42 #include "ui/views/widget/native_widget_private.h" | 43 #include "ui/views/widget/native_widget_private.h" |
43 #include "ui/views/widget/root_view.h" | 44 #include "ui/views/widget/root_view.h" |
44 #include "ui/views/widget/tooltip_manager.h" | 45 #include "ui/views/widget/tooltip_manager.h" |
45 #include "ui/views/widget/widget.h" | 46 #include "ui/views/widget/widget.h" |
46 | 47 |
47 #if defined(USE_AURA) | 48 #if defined(USE_AURA) |
48 #include "ui/base/cursor/cursor.h" | 49 #include "ui/base/cursor/cursor.h" |
49 #endif | 50 #endif |
50 | 51 |
(...skipping 10 matching lines...) Expand all Loading... | |
61 #else | 62 #else |
62 bool use_acceleration_when_possible = false; | 63 bool use_acceleration_when_possible = false; |
63 #endif | 64 #endif |
64 | 65 |
65 #if defined(OS_WIN) | 66 #if defined(OS_WIN) |
66 const bool kContextMenuOnMousePress = false; | 67 const bool kContextMenuOnMousePress = false; |
67 #else | 68 #else |
68 const bool kContextMenuOnMousePress = true; | 69 const bool kContextMenuOnMousePress = true; |
69 #endif | 70 #endif |
70 | 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 | |
71 // Returns the top view in |view|'s hierarchy. | 77 // Returns the top view in |view|'s hierarchy. |
72 const views::View* GetHierarchyRoot(const views::View* view) { | 78 const views::View* GetHierarchyRoot(const views::View* view) { |
73 const views::View* root = view; | 79 const views::View* root = view; |
74 while (root && root->parent()) | 80 while (root && root->parent()) |
75 root = root->parent(); | 81 root = root->parent(); |
76 return root; | 82 return root; |
77 } | 83 } |
78 | 84 |
79 } // namespace | 85 } // namespace |
80 | 86 |
(...skipping 745 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
826 } | 832 } |
827 | 833 |
828 // static | 834 // static |
829 bool View::get_use_acceleration_when_possible() { | 835 bool View::get_use_acceleration_when_possible() { |
830 return use_acceleration_when_possible; | 836 return use_acceleration_when_possible; |
831 } | 837 } |
832 | 838 |
833 // Input ----------------------------------------------------------------------- | 839 // Input ----------------------------------------------------------------------- |
834 | 840 |
835 View* View::GetEventHandlerForPoint(const gfx::Point& point) { | 841 View* View::GetEventHandlerForPoint(const gfx::Point& point) { |
836 // Walk the child Views recursively looking for the View that most | 842 return GetEventHandlerForRect(gfx::Rect(point, gfx::Size(1, 1))); |
837 // 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 | |
838 for (int i = child_count() - 1; i >= 0; --i) { | 858 for (int i = child_count() - 1; i >= 0; --i) { |
839 View* child = child_at(i); | 859 View* child = child_at(i); |
860 | |
861 // Ignore any children which are invisible or do not intersect |rect|. | |
840 if (!child->visible()) | 862 if (!child->visible()) |
841 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; | |
842 | 870 |
843 gfx::Point point_in_child_coords(point); | 871 View* cur_view = child->GetEventHandlerForRect(rect_in_child_coords); |
844 ConvertPointToTarget(this, child, &point_in_child_coords); | 872 |
845 if (child->HitTestPoint(point_in_child_coords)) | 873 if (views::UsePointBasedTargeting(rect)) |
846 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 } | |
847 } | 897 } |
848 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()); | |
tdanderson
2013/10/29 23:36:57
Lines 904-911 are a new addition since the last ti
| |
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; | |
849 } | 914 } |
850 | 915 |
851 View* View::GetTooltipHandlerForPoint(const gfx::Point& point) { | 916 View* View::GetTooltipHandlerForPoint(const gfx::Point& point) { |
852 if (!HitTestPoint(point)) | 917 if (!HitTestPoint(point)) |
853 return NULL; | 918 return NULL; |
854 | 919 |
855 // Walk the child Views recursively looking for the View that most | 920 // Walk the child Views recursively looking for the View that most |
856 // tightly encloses the specified point. | 921 // tightly encloses the specified point. |
857 for (int i = child_count() - 1; i >= 0; --i) { | 922 for (int i = child_count() - 1; i >= 0; --i) { |
858 View* child = child_at(i); | 923 View* child = child_at(i); |
(...skipping 26 matching lines...) Expand all Loading... | |
885 } | 950 } |
886 | 951 |
887 bool View::HitTestPoint(const gfx::Point& point) const { | 952 bool View::HitTestPoint(const gfx::Point& point) const { |
888 return HitTestRect(gfx::Rect(point, gfx::Size(1, 1))); | 953 return HitTestRect(gfx::Rect(point, gfx::Size(1, 1))); |
889 } | 954 } |
890 | 955 |
891 bool View::HitTestRect(const gfx::Rect& rect) const { | 956 bool View::HitTestRect(const gfx::Rect& rect) const { |
892 if (GetLocalBounds().Intersects(rect)) { | 957 if (GetLocalBounds().Intersects(rect)) { |
893 if (HasHitTestMask()) { | 958 if (HasHitTestMask()) { |
894 gfx::Path mask; | 959 gfx::Path mask; |
895 GetHitTestMask(&mask); | 960 HitTestSource source = HIT_TEST_SOURCE_MOUSE; |
961 if (!views::UsePointBasedTargeting(rect)) | |
962 source = HIT_TEST_SOURCE_TOUCH; | |
963 GetHitTestMask(&mask, source); | |
896 #if defined(USE_AURA) | 964 #if defined(USE_AURA) |
897 // TODO: should we use this every where? | 965 // TODO: should we use this every where? |
898 SkRegion clip_region; | 966 SkRegion clip_region; |
899 clip_region.setRect(0, 0, width(), height()); | 967 clip_region.setRect(0, 0, width(), height()); |
900 SkRegion mask_region; | 968 SkRegion mask_region; |
901 return mask_region.setPath(mask, clip_region) && | 969 return mask_region.setPath(mask, clip_region) && |
902 mask_region.intersects(RectToSkIRect(rect)); | 970 mask_region.intersects(RectToSkIRect(rect)); |
903 #elif defined(OS_WIN) | 971 #elif defined(OS_WIN) |
904 base::win::ScopedRegion rgn(mask.CreateNativeRegion()); | 972 base::win::ScopedRegion rgn(mask.CreateNativeRegion()); |
905 const RECT r(rect.ToRECT()); | 973 const RECT r(rect.ToRECT()); |
(...skipping 588 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1494 } | 1562 } |
1495 } | 1563 } |
1496 } | 1564 } |
1497 | 1565 |
1498 // Input ----------------------------------------------------------------------- | 1566 // Input ----------------------------------------------------------------------- |
1499 | 1567 |
1500 bool View::HasHitTestMask() const { | 1568 bool View::HasHitTestMask() const { |
1501 return false; | 1569 return false; |
1502 } | 1570 } |
1503 | 1571 |
1504 void View::GetHitTestMask(gfx::Path* mask) const { | 1572 void View::GetHitTestMask(gfx::Path* mask, HitTestSource source) const { |
1505 DCHECK(mask); | 1573 DCHECK(mask); |
1506 } | 1574 } |
1507 | 1575 |
1508 View::DragInfo* View::GetDragInfo() { | 1576 View::DragInfo* View::GetDragInfo() { |
1509 return parent_ ? parent_->GetDragInfo() : NULL; | 1577 return parent_ ? parent_->GetDragInfo() : NULL; |
1510 } | 1578 } |
1511 | 1579 |
1512 // Focus ----------------------------------------------------------------------- | 1580 // Focus ----------------------------------------------------------------------- |
1513 | 1581 |
1514 void View::OnFocus() { | 1582 void View::OnFocus() { |
(...skipping 808 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2323 // 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 |
2324 // the RootView can detect it and avoid calling us back. | 2392 // the RootView can detect it and avoid calling us back. |
2325 gfx::Point widget_location(event.location()); | 2393 gfx::Point widget_location(event.location()); |
2326 ConvertPointToWidget(this, &widget_location); | 2394 ConvertPointToWidget(this, &widget_location); |
2327 widget->RunShellDrag(this, data, widget_location, drag_operations, source); | 2395 widget->RunShellDrag(this, data, widget_location, drag_operations, source); |
2328 // WARNING: we may have been deleted. | 2396 // WARNING: we may have been deleted. |
2329 return true; | 2397 return true; |
2330 } | 2398 } |
2331 | 2399 |
2332 } // namespace views | 2400 } // namespace views |
OLD | NEW |