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

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

Issue 265713007: views: Update event-related API to use PointF/RectF instead of Point/Rect. Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 7 months 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
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 800 matching lines...) Expand 10 before | Expand all | Expand 10 after
811 return widget ? widget->GetThemeProvider() : NULL; 811 return widget ? widget->GetThemeProvider() : NULL;
812 } 812 }
813 813
814 const ui::NativeTheme* View::GetNativeTheme() const { 814 const ui::NativeTheme* View::GetNativeTheme() const {
815 const Widget* widget = GetWidget(); 815 const Widget* widget = GetWidget();
816 return widget ? widget->GetNativeTheme() : ui::NativeTheme::instance(); 816 return widget ? widget->GetNativeTheme() : ui::NativeTheme::instance();
817 } 817 }
818 818
819 // Input ----------------------------------------------------------------------- 819 // Input -----------------------------------------------------------------------
820 820
821 View* View::GetEventHandlerForPoint(const gfx::Point& point) { 821 View* View::GetEventHandlerForPoint(const gfx::PointF& point) {
822 return GetEventHandlerForRect(gfx::Rect(point, gfx::Size(1, 1))); 822 return GetEventHandlerForRect(gfx::RectF(point, gfx::Size(1, 1)));
823 } 823 }
824 824
825 View* View::GetEventHandlerForRect(const gfx::Rect& rect) { 825 View* View::GetEventHandlerForRect(const gfx::RectF& rect) {
826 // |rect_view| represents the current best candidate to return 826 // |rect_view| represents the current best candidate to return
827 // if rect-based targeting (i.e., fuzzing) is used. 827 // if rect-based targeting (i.e., fuzzing) is used.
828 // |rect_view_distance| is used to keep track of the distance 828 // |rect_view_distance| is used to keep track of the distance
829 // between the center point of |rect_view| and the center 829 // between the center point of |rect_view| and the center
830 // point of |rect|. 830 // point of |rect|.
831 View* rect_view = NULL; 831 View* rect_view = NULL;
832 int rect_view_distance = INT_MAX; 832 float rect_view_distance = std::numeric_limits<float>::max();
833 833
834 // |point_view| represents the view that would have been returned 834 // |point_view| represents the view that would have been returned
835 // from this function call if point-based targeting were used. 835 // from this function call if point-based targeting were used.
836 View* point_view = NULL; 836 View* point_view = NULL;
837 837
838 for (int i = child_count() - 1; i >= 0; --i) { 838 for (int i = child_count() - 1; i >= 0; --i) {
839 View* child = child_at(i); 839 View* child = child_at(i);
840 840
841 // Ignore any children which are invisible or do not intersect |rect|. 841 // Ignore any children which are invisible or do not intersect |rect|.
842 if (!child->visible()) 842 if (!child->visible())
843 continue; 843 continue;
844 gfx::RectF rect_in_child_coords_f(rect); 844 gfx::RectF rect_in_child_coords(rect);
845 ConvertRectToTarget(this, child, &rect_in_child_coords_f); 845 ConvertRectToTarget(this, child, &rect_in_child_coords);
846 gfx::Rect rect_in_child_coords = gfx::ToEnclosingRect(
847 rect_in_child_coords_f);
848 if (!child->HitTestRect(rect_in_child_coords)) 846 if (!child->HitTestRect(rect_in_child_coords))
849 continue; 847 continue;
850 848
851 View* cur_view = child->GetEventHandlerForRect(rect_in_child_coords); 849 View* cur_view = child->GetEventHandlerForRect(rect_in_child_coords);
852 850
853 if (views::UsePointBasedTargeting(rect)) 851 if (views::UsePointBasedTargeting(rect))
854 return cur_view; 852 return cur_view;
855 853
856 gfx::RectF cur_view_bounds_f(cur_view->GetLocalBounds()); 854 gfx::RectF cur_view_bounds_f(cur_view->GetLocalBounds());
857 ConvertRectToTarget(cur_view, this, &cur_view_bounds_f); 855 ConvertRectToTarget(cur_view, this, &cur_view_bounds_f);
858 gfx::Rect cur_view_bounds = gfx::ToEnclosingRect( 856 gfx::Rect cur_view_bounds = gfx::ToEnclosingRect(
859 cur_view_bounds_f); 857 cur_view_bounds_f);
860 if (views::PercentCoveredBy(cur_view_bounds, rect) >= kRectTargetOverlap) { 858 if (views::PercentCoveredBy(cur_view_bounds, rect) >= kRectTargetOverlap) {
861 // |cur_view| is a suitable candidate for rect-based targeting. 859 // |cur_view| is a suitable candidate for rect-based targeting.
862 // Check to see if it is the closest suitable candidate so far. 860 // Check to see if it is the closest suitable candidate so far.
863 gfx::Point touch_center(rect.CenterPoint()); 861 gfx::PointF touch_center(rect.CenterPoint());
864 int cur_dist = views::DistanceSquaredFromCenterToPoint(touch_center, 862 float cur_dist = views::DistanceSquaredFromCenterToPoint(touch_center,
865 cur_view_bounds); 863 cur_view_bounds);
866 if (!rect_view || cur_dist < rect_view_distance) { 864 if (!rect_view || cur_dist < rect_view_distance) {
867 rect_view = cur_view; 865 rect_view = cur_view;
868 rect_view_distance = cur_dist; 866 rect_view_distance = cur_dist;
869 } 867 }
870 } else if (!rect_view && !point_view) { 868 } else if (!rect_view && !point_view) {
871 // Rect-based targeting has not yielded any candidates so far. Check 869 // Rect-based targeting has not yielded any candidates so far. Check
872 // if point-based targeting would have selected |cur_view|. 870 // if point-based targeting would have selected |cur_view|.
873 gfx::Point point_in_child_coords(rect_in_child_coords.CenterPoint()); 871 gfx::PointF point_in_child_coords(rect_in_child_coords.CenterPoint());
874 if (child->HitTestPoint(point_in_child_coords)) 872 if (child->HitTestPoint(point_in_child_coords))
875 point_view = child->GetEventHandlerForPoint(point_in_child_coords); 873 point_view = child->GetEventHandlerForPoint(point_in_child_coords);
876 } 874 }
877 } 875 }
878 876
879 if (views::UsePointBasedTargeting(rect) || (!rect_view && !point_view)) 877 if (views::UsePointBasedTargeting(rect) || (!rect_view && !point_view))
880 return this; 878 return this;
881 879
882 // If |this| is a suitable candidate for rect-based targeting, check to 880 // If |this| is a suitable candidate for rect-based targeting, check to
883 // see if it is closer than the current best suitable candidate so far. 881 // see if it is closer than the current best suitable candidate so far.
884 gfx::Rect local_bounds(GetLocalBounds()); 882 gfx::Rect local_bounds(GetLocalBounds());
885 if (views::PercentCoveredBy(local_bounds, rect) >= kRectTargetOverlap) { 883 if (views::PercentCoveredBy(local_bounds, rect) >= kRectTargetOverlap) {
886 gfx::Point touch_center(rect.CenterPoint()); 884 gfx::PointF touch_center(rect.CenterPoint());
887 int cur_dist = views::DistanceSquaredFromCenterToPoint(touch_center, 885 int cur_dist = views::DistanceSquaredFromCenterToPoint(touch_center,
888 local_bounds); 886 local_bounds);
889 if (!rect_view || cur_dist < rect_view_distance) 887 if (!rect_view || cur_dist < rect_view_distance)
890 rect_view = this; 888 rect_view = this;
891 } 889 }
892 890
893 return rect_view ? rect_view : point_view; 891 return rect_view ? rect_view : point_view;
894 } 892 }
895 893
896 View* View::GetTooltipHandlerForPoint(const gfx::Point& point) { 894 View* View::GetTooltipHandlerForPoint(const gfx::Point& point) {
(...skipping 20 matching lines...) Expand all
917 #if defined(OS_WIN) 915 #if defined(OS_WIN)
918 static ui::Cursor arrow; 916 static ui::Cursor arrow;
919 if (!arrow.platform()) 917 if (!arrow.platform())
920 arrow.SetPlatformCursor(LoadCursor(NULL, IDC_ARROW)); 918 arrow.SetPlatformCursor(LoadCursor(NULL, IDC_ARROW));
921 return arrow; 919 return arrow;
922 #else 920 #else
923 return gfx::kNullCursor; 921 return gfx::kNullCursor;
924 #endif 922 #endif
925 } 923 }
926 924
927 bool View::HitTestPoint(const gfx::Point& point) const { 925 bool View::HitTestPoint(const gfx::PointF& point) const {
928 return HitTestRect(gfx::Rect(point, gfx::Size(1, 1))); 926 return HitTestRect(gfx::RectF(point, gfx::Size(1, 1)));
929 } 927 }
930 928
931 bool View::HitTestRect(const gfx::Rect& rect) const { 929 bool View::HitTestRect(const gfx::RectF& rect) const {
932 if (GetLocalBounds().Intersects(rect)) { 930 gfx::RectF local_bounds(GetLocalBounds());
931 if (local_bounds.Intersects(rect)) {
933 if (HasHitTestMask()) { 932 if (HasHitTestMask()) {
934 gfx::Path mask; 933 gfx::Path mask;
935 HitTestSource source = HIT_TEST_SOURCE_MOUSE; 934 HitTestSource source = HIT_TEST_SOURCE_MOUSE;
936 if (!views::UsePointBasedTargeting(rect)) 935 if (!views::UsePointBasedTargeting(rect))
937 source = HIT_TEST_SOURCE_TOUCH; 936 source = HIT_TEST_SOURCE_TOUCH;
938 GetHitTestMask(source, &mask); 937 GetHitTestMask(source, &mask);
939 SkRegion clip_region; 938 SkRegion clip_region;
940 clip_region.setRect(0, 0, width(), height()); 939 clip_region.setRect(0, 0, width(), height());
941 SkRegion mask_region; 940 SkRegion mask_region;
942 return mask_region.setPath(mask, clip_region) && 941 return mask_region.setPath(mask, clip_region) &&
(...skipping 342 matching lines...) Expand 10 before | Expand all | Expand 10 after
1285 } 1284 }
1286 1285
1287 int View::OnPerformDrop(const ui::DropTargetEvent& event) { 1286 int View::OnPerformDrop(const ui::DropTargetEvent& event) {
1288 return ui::DragDropTypes::DRAG_NONE; 1287 return ui::DragDropTypes::DRAG_NONE;
1289 } 1288 }
1290 1289
1291 void View::OnDragDone() { 1290 void View::OnDragDone() {
1292 } 1291 }
1293 1292
1294 // static 1293 // static
1295 bool View::ExceededDragThreshold(const gfx::Vector2d& delta) { 1294 bool View::ExceededDragThreshold(const gfx::Vector2dF& delta) {
1296 return (abs(delta.x()) > GetHorizontalDragThreshold() || 1295 return (fabs(delta.x()) > GetHorizontalDragThreshold() ||
1297 abs(delta.y()) > GetVerticalDragThreshold()); 1296 fabs(delta.y()) > GetVerticalDragThreshold());
1298 } 1297 }
1299 1298
1300 // Accessibility---------------------------------------------------------------- 1299 // Accessibility----------------------------------------------------------------
1301 1300
1302 gfx::NativeViewAccessible View::GetNativeViewAccessible() { 1301 gfx::NativeViewAccessible View::GetNativeViewAccessible() {
1303 if (!native_view_accessibility_) 1302 if (!native_view_accessibility_)
1304 native_view_accessibility_ = NativeViewAccessibility::Create(this); 1303 native_view_accessibility_ = NativeViewAccessibility::Create(this);
1305 if (native_view_accessibility_) 1304 if (native_view_accessibility_)
1306 return native_view_accessibility_->GetNativeObject(); 1305 return native_view_accessibility_->GetNativeObject();
1307 return NULL; 1306 return NULL;
(...skipping 297 matching lines...) Expand 10 before | Expand all | Expand 10 after
1605 gfx::Point View::GetKeyboardContextMenuLocation() { 1604 gfx::Point View::GetKeyboardContextMenuLocation() {
1606 gfx::Rect vis_bounds = GetVisibleBounds(); 1605 gfx::Rect vis_bounds = GetVisibleBounds();
1607 gfx::Point screen_point(vis_bounds.x() + vis_bounds.width() / 2, 1606 gfx::Point screen_point(vis_bounds.x() + vis_bounds.width() / 2,
1608 vis_bounds.y() + vis_bounds.height() / 2); 1607 vis_bounds.y() + vis_bounds.height() / 2);
1609 ConvertPointToScreen(this, &screen_point); 1608 ConvertPointToScreen(this, &screen_point);
1610 return screen_point; 1609 return screen_point;
1611 } 1610 }
1612 1611
1613 // Drag and drop --------------------------------------------------------------- 1612 // Drag and drop ---------------------------------------------------------------
1614 1613
1615 int View::GetDragOperations(const gfx::Point& press_pt) { 1614 int View::GetDragOperations(const gfx::PointF& press_pt) {
1616 return drag_controller_ ? 1615 return drag_controller_ ?
1617 drag_controller_->GetDragOperationsForView(this, press_pt) : 1616 drag_controller_->GetDragOperationsForView(this, press_pt) :
1618 ui::DragDropTypes::DRAG_NONE; 1617 ui::DragDropTypes::DRAG_NONE;
1619 } 1618 }
1620 1619
1621 void View::WriteDragData(const gfx::Point& press_pt, OSExchangeData* data) { 1620 void View::WriteDragData(const gfx::PointF& press_pt, OSExchangeData* data) {
1622 DCHECK(drag_controller_); 1621 DCHECK(drag_controller_);
1623 drag_controller_->WriteDragDataForView(this, press_pt, data); 1622 drag_controller_->WriteDragDataForView(this, press_pt, data);
1624 } 1623 }
1625 1624
1626 bool View::InDrag() { 1625 bool View::InDrag() {
1627 Widget* widget = GetWidget(); 1626 Widget* widget = GetWidget();
1628 return widget ? widget->dragged_view() == this : false; 1627 return widget ? widget->dragged_view() == this : false;
1629 } 1628 }
1630 1629
1631 int View::GetHorizontalDragThreshold() { 1630 int View::GetHorizontalDragThreshold() {
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after
1748 1747
1749 #endif 1748 #endif
1750 1749
1751 //////////////////////////////////////////////////////////////////////////////// 1750 ////////////////////////////////////////////////////////////////////////////////
1752 // View, private: 1751 // View, private:
1753 1752
1754 // DropInfo -------------------------------------------------------------------- 1753 // DropInfo --------------------------------------------------------------------
1755 1754
1756 void View::DragInfo::Reset() { 1755 void View::DragInfo::Reset() {
1757 possible_drag = false; 1756 possible_drag = false;
1758 start_pt = gfx::Point(); 1757 start_pt = gfx::PointF();
1759 } 1758 }
1760 1759
1761 void View::DragInfo::PossibleDrag(const gfx::Point& p) { 1760 void View::DragInfo::PossibleDrag(const gfx::PointF& p) {
1762 possible_drag = true; 1761 possible_drag = true;
1763 start_pt = p; 1762 start_pt = p;
1764 } 1763 }
1765 1764
1766 // Painting -------------------------------------------------------------------- 1765 // Painting --------------------------------------------------------------------
1767 1766
1768 void View::SchedulePaintBoundsChanged(SchedulePaintType type) { 1767 void View::SchedulePaintBoundsChanged(SchedulePaintType type) {
1769 // If we have a layer and the View's size did not change, we do not need to 1768 // If we have a layer and the View's size did not change, we do not need to
1770 // schedule any paints since the layer will be redrawn at its new location 1769 // schedule any paints since the layer will be redrawn at its new location
1771 // during the next Draw() cycle in the compositor. 1770 // during the next Draw() cycle in the compositor.
(...skipping 588 matching lines...) Expand 10 before | Expand all | Expand 10 after
2360 // TODO(beng): The TooltipManager NULL check can be removed when we 2359 // TODO(beng): The TooltipManager NULL check can be removed when we
2361 // consolidate Init() methods and make views_unittests Init() all 2360 // consolidate Init() methods and make views_unittests Init() all
2362 // Widgets that it uses. 2361 // Widgets that it uses.
2363 if (widget && widget->GetTooltipManager()) 2362 if (widget && widget->GetTooltipManager())
2364 widget->GetTooltipManager()->UpdateTooltip(); 2363 widget->GetTooltipManager()->UpdateTooltip();
2365 } 2364 }
2366 2365
2367 // Drag and drop --------------------------------------------------------------- 2366 // Drag and drop ---------------------------------------------------------------
2368 2367
2369 bool View::DoDrag(const ui::LocatedEvent& event, 2368 bool View::DoDrag(const ui::LocatedEvent& event,
2370 const gfx::Point& press_pt, 2369 const gfx::PointF& press_pt,
2371 ui::DragDropTypes::DragEventSource source) { 2370 ui::DragDropTypes::DragEventSource source) {
2372 int drag_operations = GetDragOperations(press_pt); 2371 int drag_operations = GetDragOperations(press_pt);
2373 if (drag_operations == ui::DragDropTypes::DRAG_NONE) 2372 if (drag_operations == ui::DragDropTypes::DRAG_NONE)
2374 return false; 2373 return false;
2375 2374
2376 Widget* widget = GetWidget(); 2375 Widget* widget = GetWidget();
2377 // We should only start a drag from an event, implying we have a widget. 2376 // We should only start a drag from an event, implying we have a widget.
2378 DCHECK(widget); 2377 DCHECK(widget);
2379 2378
2380 // Don't attempt to start a drag while in the process of dragging. This is 2379 // Don't attempt to start a drag while in the process of dragging. This is
2381 // especially important on X where we can get multiple mouse move events when 2380 // especially important on X where we can get multiple mouse move events when
2382 // we start the drag. 2381 // we start the drag.
2383 if (widget->dragged_view()) 2382 if (widget->dragged_view())
2384 return false; 2383 return false;
2385 2384
2386 OSExchangeData data; 2385 OSExchangeData data;
2387 WriteDragData(press_pt, &data); 2386 WriteDragData(press_pt, &data);
2388 2387
2389 // Message the RootView to do the drag and drop. That way if we're removed 2388 // Message the RootView to do the drag and drop. That way if we're removed
2390 // the RootView can detect it and avoid calling us back. 2389 // the RootView can detect it and avoid calling us back.
2391 gfx::Point widget_location(event.location()); 2390 gfx::Point widget_location(event.location());
2392 ConvertPointToWidget(this, &widget_location); 2391 ConvertPointToWidget(this, &widget_location);
2393 widget->RunShellDrag(this, data, widget_location, drag_operations, source); 2392 widget->RunShellDrag(this, data, widget_location, drag_operations, source);
2394 // WARNING: we may have been deleted. 2393 // WARNING: we may have been deleted.
2395 return true; 2394 return true;
2396 } 2395 }
2397 2396
2398 } // namespace views 2397 } // namespace views
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698