Chromium Code Reviews| 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; | |
|
sky
2012/08/02 15:52:08
Is it possible for rect_a_area to be 0?
tdanderson
2012/08/02 18:11:37
I don't believe this is possible given our impleme
| |
| 90 } | |
| 91 | |
| 92 int DistanceToInterval(int pos, int start, int end) { | |
|
sky
2012/08/02 15:52:08
Add description.
tdanderson
2012/08/02 18:11:37
Done.
| |
| 93 if (pos < start) | |
| 94 return start - pos; | |
| 95 if (pos > end) | |
| 96 return end - pos; | |
| 97 return 0; | |
| 98 } | |
| 99 | |
| 100 // Returns the square of the distance from |point| to the center line of | |
| 101 // |target_rect|. The center line of a rectangle is obtained by repeatedly | |
| 102 // stripping away 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 663 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 756 continue; | 795 continue; |
| 757 | 796 |
| 758 gfx::Point point_in_child_coords(point); | 797 gfx::Point point_in_child_coords(point); |
| 759 View::ConvertPointToView(this, child, &point_in_child_coords); | 798 View::ConvertPointToView(this, child, &point_in_child_coords); |
| 760 if (child->HitTest(point_in_child_coords)) | 799 if (child->HitTest(point_in_child_coords)) |
| 761 return child->GetEventHandlerForPoint(point_in_child_coords); | 800 return child->GetEventHandlerForPoint(point_in_child_coords); |
| 762 } | 801 } |
| 763 return this; | 802 return this; |
| 764 } | 803 } |
| 765 | 804 |
| 805 View* View::GetEventHandlerForRect(const gfx::Rect& touch_rect) { | |
| 806 if (!child_count()) { | |
|
sky
2012/08/02 15:52:08
The structure of this should roughly match that of
tdanderson
2012/08/02 18:11:37
Done.
| |
| 807 gfx::Rect view_rect(GetBoundsInScreen()); | |
| 808 if (PercentCoveredBy(view_rect, touch_rect) >= kFuzzingOverlapPercentage) | |
| 809 return this; | |
| 810 return NULL; | |
| 811 } | |
| 812 | |
| 813 View* closest_view = NULL; | |
| 814 int best_dist_so_far = INT_MAX; | |
| 815 for (int i = child_count() - 1; i >= 0; --i) { | |
| 816 View* child = child_at(i); | |
| 817 if (!child->visible()) | |
| 818 continue; | |
| 819 | |
| 820 gfx::Rect child_rect(child->GetBoundsInScreen()); | |
| 821 if (!child_rect.Intersects(touch_rect)) | |
| 822 continue; | |
| 823 | |
| 824 View* cur_view = child->GetEventHandlerForRect(touch_rect); | |
| 825 if (!cur_view) | |
| 826 continue; | |
| 827 | |
| 828 gfx::Point touch_center(touch_rect.CenterPoint()); | |
| 829 gfx::Rect cur_rect(cur_view->GetBoundsInScreen()); | |
| 830 int cur_dist = DistanceSquaredFromCenterLineToPoint(touch_center, | |
| 831 cur_rect); | |
| 832 if (!closest_view || cur_dist < best_dist_so_far) { | |
| 833 closest_view = cur_view; | |
| 834 best_dist_so_far = cur_dist; | |
| 835 } | |
| 836 } | |
| 837 | |
| 838 return closest_view; | |
| 839 } | |
| 840 | |
| 766 gfx::NativeCursor View::GetCursor(const MouseEvent& event) { | 841 gfx::NativeCursor View::GetCursor(const MouseEvent& event) { |
| 767 #if defined(OS_WIN) && !defined(USE_AURA) | 842 #if defined(OS_WIN) && !defined(USE_AURA) |
| 768 static HCURSOR arrow = LoadCursor(NULL, IDC_ARROW); | 843 static HCURSOR arrow = LoadCursor(NULL, IDC_ARROW); |
| 769 return arrow; | 844 return arrow; |
| 770 #else | 845 #else |
| 771 return gfx::kNullCursor; | 846 return gfx::kNullCursor; |
| 772 #endif | 847 #endif |
| 773 } | 848 } |
| 774 | 849 |
| 775 bool View::HitTest(const gfx::Point& l) const { | 850 bool View::HitTest(const gfx::Point& l) const { |
| (...skipping 1330 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2106 gfx::Point widget_location(event.location()); | 2181 gfx::Point widget_location(event.location()); |
| 2107 ConvertPointToWidget(this, &widget_location); | 2182 ConvertPointToWidget(this, &widget_location); |
| 2108 GetWidget()->RunShellDrag(this, data, widget_location, drag_operations); | 2183 GetWidget()->RunShellDrag(this, data, widget_location, drag_operations); |
| 2109 return true; | 2184 return true; |
| 2110 #else | 2185 #else |
| 2111 return false; | 2186 return false; |
| 2112 #endif // !defined(OS_MACOSX) | 2187 #endif // !defined(OS_MACOSX) |
| 2113 } | 2188 } |
| 2114 | 2189 |
| 2115 } // namespace views | 2190 } // namespace views |
| OLD | NEW |