1 // Copyright 2014 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #include "athena/util/drag_handle.h" 6 7 #include "ui/views/background.h" 8 #include "ui/views/view.h" 9 10 namespace athena { 11 namespace { 12 13 const SkColor kDragHandleColorNormal = SK_ColorGRAY; 14 const SkColor kDragHandleColorHot = SK_ColorWHITE; 15 16 // This view notifies its delegate of the touch scroll gestures performed on it. 17 class DragHandleView : public views::View { 18 public: 19 DragHandleView(DragHandleScrollDirection scroll_direction, 20 DragHandleScrollDelegate* delegate, 21 int preferred_width, 22 int preferred_height); 23 virtual ~DragHandleView(); 24 25 private: 26 void SetColor(SkColor color); 27 28 void SetIsScrolling(bool scrolling); 29 30 // views::View: 31 virtual gfx::Size GetPreferredSize() const OVERRIDE; 32 virtual void OnGestureEvent(ui::GestureEvent* event) OVERRIDE; 33 34 bool scroll_in_progress_; 35 DragHandleScrollDelegate* delegate_; 36 DragHandleScrollDirection scroll_direction_; 37 SkColor color_; 38 float scroll_start_location_; 39 const int preferred_width_; 40 const int preferred_height_; 41 42 DISALLOW_COPY_AND_ASSIGN(DragHandleView); 43 }; 44 45 DragHandleView::DragHandleView(DragHandleScrollDirection scroll_direction, 46 DragHandleScrollDelegate* delegate, 47 int preferred_width, 48 int preferred_height) 49 : scroll_in_progress_(false), 50 delegate_(delegate), 51 scroll_direction_(scroll_direction), 52 color_(SK_ColorTRANSPARENT), 53 preferred_width_(preferred_width), 54 preferred_height_(preferred_height) { 55 SetColor(kDragHandleColorNormal); 56 } 57 58 DragHandleView::~DragHandleView() { 59 } 60 61 void DragHandleView::SetColor(SkColor color) { 62 if (color_ == color) 63 return; 64 color_ = color; 65 set_background(views::Background::CreateSolidBackground(color_)); 66 SchedulePaint(); 67 } 68 69 void DragHandleView::SetIsScrolling(bool scrolling) { 70 if (scroll_in_progress_ == scrolling) 71 return; 72 scroll_in_progress_ = scrolling; 73 if (!scroll_in_progress_) 74 scroll_start_location_ = 0; 75 } 76 77 // views::View: 78 gfx::Size DragHandleView::GetPreferredSize() const { 79 return gfx::Size(preferred_width_, preferred_height_); 80 } 81 82 void DragHandleView::OnGestureEvent(ui::GestureEvent* event) { 83 SkColor change_color = SK_ColorTRANSPARENT; 84 if (event->type() == ui::ET_GESTURE_BEGIN && 85 event->details().touch_points() == 1) { 86 change_color = kDragHandleColorHot; 87 } else if (event->type() == ui::ET_GESTURE_END && 88 event->details().touch_points() == 1) { 89 change_color = kDragHandleColorNormal; 90 } 91 92 if (change_color != SK_ColorTRANSPARENT) { 93 SetColor(change_color); 94 event->SetHandled(); 95 return; 96 } 97 98 if (event->type() == ui::ET_GESTURE_SCROLL_BEGIN) { 99 if (scroll_in_progress_) 100 return; 101 float delta; 102 if (scroll_direction_ == DRAG_HANDLE_VERTICAL) { 103 delta = event->details().scroll_y_hint(); 104 scroll_start_location_ = event->root_location().y(); 105 } else { 106 delta = event->details().scroll_x_hint(); 107 scroll_start_location_ = event->root_location().x(); 108 } 109 delegate_->HandleScrollBegin(delta); 110 SetIsScrolling(true); 111 event->SetHandled(); 112 } else if (event->type() == ui::ET_GESTURE_SCROLL_END || 113 event->type() == ui::ET_SCROLL_FLING_START) { 114 if (!scroll_in_progress_) 115 return; 116 delegate_->HandleScrollEnd(); 117 SetColor(kDragHandleColorNormal); 118 SetIsScrolling(false); 119 event->SetHandled(); 120 } else if (event->type() == ui::ET_GESTURE_SCROLL_UPDATE) { 121 if (!scroll_in_progress_) 122 return; 123 float delta = scroll_direction_ == DRAG_HANDLE_VERTICAL 124 ? event->root_location().y() - scroll_start_location_ 125 : event->root_location().x() - scroll_start_location_; 126 delegate_->HandleScrollUpdate(delta); 127 event->SetHandled(); 128 } 129 } 130 131 } // namespace 132 133 views::View* CreateDragHandleView(DragHandleScrollDirection scroll_direction, 134 DragHandleScrollDelegate* delegate, 135 int preferred_width, 136 int preferred_height) { 137 views::View* view = new DragHandleView( 138 scroll_direction, delegate, preferred_width, preferred_height); 139 return view; 140 } 141 142 } // namespace athena 143