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 #include "ui/views/view.h" | 5 #include "ui/views/view.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/debug/trace_event.h" | 9 #include "base/debug/trace_event.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
12 #include "base/message_loop.h" | 12 #include "base/message_loop.h" |
13 #include "base/stringprintf.h" | 13 #include "base/stringprintf.h" |
14 #include "base/utf_string_conversions.h" | 14 #include "base/utf_string_conversions.h" |
15 #include "third_party/skia/include/core/SkRect.h" | 15 #include "third_party/skia/include/core/SkRect.h" |
16 #include "ui/base/accessibility/accessibility_types.h" | 16 #include "ui/base/accessibility/accessibility_types.h" |
17 #include "ui/base/dragdrop/drag_drop_types.h" | 17 #include "ui/base/dragdrop/drag_drop_types.h" |
18 #include "ui/compositor/compositor.h" | 18 #include "ui/compositor/compositor.h" |
19 #include "ui/compositor/layer.h" | 19 #include "ui/compositor/layer.h" |
20 #include "ui/compositor/layer_animator.h" | 20 #include "ui/compositor/layer_animator.h" |
21 #include "ui/gfx/canvas.h" | 21 #include "ui/gfx/canvas.h" |
22 #include "ui/gfx/interpolated_transform.h" | 22 #include "ui/gfx/interpolated_transform.h" |
23 #include "ui/gfx/path.h" | 23 #include "ui/gfx/path.h" |
24 #include "ui/gfx/point3.h" | 24 #include "ui/gfx/point3.h" |
25 #include "ui/gfx/transform.h" | 25 #include "ui/gfx/transform.h" |
26 #include "ui/views/background.h" | 26 #include "ui/views/background.h" |
27 #include "ui/views/context_menu_controller.h" | 27 #include "ui/views/context_menu_controller.h" |
28 #include "ui/views/drag_controller.h" | 28 #include "ui/views/drag_controller.h" |
29 #include "ui/views/layout/layout_manager.h" | 29 #include "ui/views/layout/layout_manager.h" |
30 #include "ui/views/view_constants.h" | |
30 #include "ui/views/views_delegate.h" | 31 #include "ui/views/views_delegate.h" |
31 #include "ui/views/widget/native_widget_private.h" | 32 #include "ui/views/widget/native_widget_private.h" |
32 #include "ui/views/widget/root_view.h" | 33 #include "ui/views/widget/root_view.h" |
33 #include "ui/views/widget/tooltip_manager.h" | 34 #include "ui/views/widget/tooltip_manager.h" |
34 #include "ui/views/widget/widget.h" | 35 #include "ui/views/widget/widget.h" |
35 | 36 |
36 #if defined(OS_WIN) | 37 #if defined(OS_WIN) |
37 #include "base/win/scoped_gdi_object.h" | 38 #include "base/win/scoped_gdi_object.h" |
38 #include "ui/views/accessibility/native_view_accessibility_win.h" | 39 #include "ui/views/accessibility/native_view_accessibility_win.h" |
39 #endif | 40 #endif |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
73 }; | 74 }; |
74 | 75 |
75 // Returns the top view in |view|'s hierarchy. | 76 // Returns the top view in |view|'s hierarchy. |
76 const views::View* GetHierarchyRoot(const views::View* view) { | 77 const views::View* GetHierarchyRoot(const views::View* view) { |
77 const views::View* root = view; | 78 const views::View* root = view; |
78 while (root && root->parent()) | 79 while (root && root->parent()) |
79 root = root->parent(); | 80 root = root->parent(); |
80 return root; | 81 return root; |
81 } | 82 } |
82 | 83 |
84 // Returns the percentage of |rect_1|'s area that is covered by |rect_2|. | |
85 float PercentCoveredBy(const gfx::Rect& rect_1, const gfx::Rect& rect_2) { | |
86 gfx::Rect intersection(rect_1.Intersect(rect_2)); | |
87 float intersection_area = intersection.size().GetArea(); | |
88 float rect_1_area = rect_1.size().GetArea(); | |
89 return intersection_area / rect_1_area; | |
90 } | |
91 | |
92 int DistanceToInterval(int pos, int start, int end) { | |
93 if (pos < start) | |
94 return start - pos; | |
95 if (pos > end) | |
96 return end - pos; | |
97 return 0; | |
98 } | |
99 | |
100 // Returns the distance from |point| to the center line of |target_rect|. | |
rjkroege
2012/07/19 00:51:57
square of the distance -- comment not consistent w
tdanderson
2012/07/25 23:08:57
Done.
| |
101 // The center line of a rectangle is obtained by repeatedly stripping away | |
102 // 1px borders around the rectangle until a line remains. | |
103 int DistanceSquaredFromCenterLineToPoint(const gfx::Point& point, | |
104 const gfx::Rect& target_rect) { | |
105 gfx::Point center_point = target_rect.CenterPoint(); | |
106 int dx = center_point.x() - point.x(); | |
107 int dy = center_point.y() - point.y(); | |
108 | |
109 if (target_rect.width() > target_rect.height()) { | |
110 dx = DistanceToInterval(point.x(), | |
111 target_rect.x() + (target_rect.height() / 2), | |
112 target_rect.right() - (target_rect.height() / 2)); | |
113 } else { | |
114 dy = DistanceToInterval(point.y(), | |
115 target_rect.y() + (target_rect.width() / 2), | |
116 target_rect.bottom() - (target_rect.width() / 2)); | |
117 } | |
118 | |
119 return (dx * dx) + (dy * dy); | |
120 } | |
121 | |
83 } // namespace | 122 } // namespace |
84 | 123 |
85 namespace views { | 124 namespace views { |
86 | 125 |
87 // static | 126 // static |
88 ViewsDelegate* ViewsDelegate::views_delegate = NULL; | 127 ViewsDelegate* ViewsDelegate::views_delegate = NULL; |
89 | 128 |
90 // static | 129 // static |
91 const char View::kViewClassName[] = "views/View"; | 130 const char View::kViewClassName[] = "views/View"; |
92 | 131 |
(...skipping 664 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
757 continue; | 796 continue; |
758 | 797 |
759 gfx::Point point_in_child_coords(point); | 798 gfx::Point point_in_child_coords(point); |
760 View::ConvertPointToView(this, child, &point_in_child_coords); | 799 View::ConvertPointToView(this, child, &point_in_child_coords); |
761 if (child->HitTest(point_in_child_coords)) | 800 if (child->HitTest(point_in_child_coords)) |
762 return child->GetEventHandlerForPoint(point_in_child_coords); | 801 return child->GetEventHandlerForPoint(point_in_child_coords); |
763 } | 802 } |
764 return this; | 803 return this; |
765 } | 804 } |
766 | 805 |
806 View* View::GetEventHandlerForRect(const gfx::Rect& touch_rect) { | |
807 if (!child_count()) { | |
808 gfx::Rect view_rect(GetScreenBounds()); | |
809 if (PercentCoveredBy(view_rect, touch_rect) >= kFuzzingOverlapPercentage) | |
810 return this; | |
811 return NULL; | |
sadrul
2012/07/18 23:42:31
I think as long as the intersection is non-empty,
tdanderson
2012/07/25 23:08:57
This is the behavior that I want since only small
| |
812 } | |
813 | |
814 View* closest_view = NULL; | |
815 for (int i = child_count() - 1; i >= 0; --i) { | |
816 View* child = child_at(i); | |
817 | |
818 if (!child->visible()) | |
819 continue; | |
820 | |
821 gfx::Rect child_rect(child->GetScreenBounds()); | |
822 if (!child_rect.Intersects(touch_rect)) | |
823 continue; | |
824 | |
825 View* cur_view = child->GetEventHandlerForRect(touch_rect); | |
826 | |
827 if (!closest_view) | |
sadrul
2012/07/18 23:42:31
braces (since else has them)
tdanderson
2012/07/25 23:08:57
Done.
| |
828 closest_view = cur_view; | |
829 else if (cur_view) { | |
830 gfx::Point touch_center(touch_rect.CenterPoint()); | |
831 gfx::Rect closest_view_rect(closest_view->GetScreenBounds()); | |
832 gfx::Rect cur_rect(cur_view->GetScreenBounds()); | |
833 | |
834 int best_dist_so_far = DistanceSquaredFromCenterLineToPoint( | |
835 touch_center, | |
836 closest_view_rect); | |
837 int cur_dist = DistanceSquaredFromCenterLineToPoint(touch_center, | |
838 cur_rect); | |
839 | |
840 if (cur_dist < best_dist_so_far) | |
841 closest_view = cur_view; | |
sadrul
2012/07/18 23:42:31
Can you remember best_dist_so_far instead of compu
tdanderson
2012/07/25 23:08:57
Done.
| |
842 } | |
843 } | |
844 | |
845 return closest_view; | |
846 } | |
847 | |
767 gfx::NativeCursor View::GetCursor(const MouseEvent& event) { | 848 gfx::NativeCursor View::GetCursor(const MouseEvent& event) { |
768 #if defined(OS_WIN) && !defined(USE_AURA) | 849 #if defined(OS_WIN) && !defined(USE_AURA) |
769 static HCURSOR arrow = LoadCursor(NULL, IDC_ARROW); | 850 static HCURSOR arrow = LoadCursor(NULL, IDC_ARROW); |
770 return arrow; | 851 return arrow; |
771 #else | 852 #else |
772 return gfx::kNullCursor; | 853 return gfx::kNullCursor; |
773 #endif | 854 #endif |
774 } | 855 } |
775 | 856 |
776 bool View::HitTest(const gfx::Point& l) const { | 857 bool View::HitTest(const gfx::Point& l) const { |
(...skipping 1328 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2105 gfx::Point widget_location(event.location()); | 2186 gfx::Point widget_location(event.location()); |
2106 ConvertPointToWidget(this, &widget_location); | 2187 ConvertPointToWidget(this, &widget_location); |
2107 GetWidget()->RunShellDrag(this, data, widget_location, drag_operations); | 2188 GetWidget()->RunShellDrag(this, data, widget_location, drag_operations); |
2108 return true; | 2189 return true; |
2109 #else | 2190 #else |
2110 return false; | 2191 return false; |
2111 #endif // !defined(OS_MACOSX) | 2192 #endif // !defined(OS_MACOSX) |
2112 } | 2193 } |
2113 | 2194 |
2114 } // namespace views | 2195 } // namespace views |
OLD | NEW |