OLD | NEW |
(Empty) | |
| 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/layout/box_layout.h" |
| 9 #include "ui/views/view.h" |
| 10 #include "ui/views/widget/widget.h" |
| 11 |
| 12 namespace athena { |
| 13 namespace { |
| 14 |
| 15 const SkColor kDragHandleColorNormal = SK_ColorGRAY; |
| 16 const SkColor kDragHandleColorHot = SK_ColorWHITE; |
| 17 |
| 18 // This view notifies its delegate of the touch scroll gestures performed on it. |
| 19 class DragHandleView : public views::View { |
| 20 public: |
| 21 DragHandleView(DragHandle::ScrollDirection scroll_direction, |
| 22 DragHandle::ScrollDelegate* delegate, |
| 23 int preferred_width, |
| 24 int preferred_height); |
| 25 virtual ~DragHandleView(); |
| 26 |
| 27 private: |
| 28 enum State { |
| 29 DEFAULT, |
| 30 SCROLLING, |
| 31 }; |
| 32 |
| 33 void SetColor(SkColor color); |
| 34 |
| 35 void SetState(State state); |
| 36 |
| 37 // views::View: |
| 38 virtual gfx::Size GetPreferredSize() const OVERRIDE; |
| 39 virtual void OnMouseEntered(const ui::MouseEvent& event) OVERRIDE; |
| 40 virtual void OnMouseExited(const ui::MouseEvent& event) OVERRIDE; |
| 41 virtual void OnGestureEvent(ui::GestureEvent* event) OVERRIDE; |
| 42 |
| 43 State state_; |
| 44 DragHandle::ScrollDelegate* delegate_; |
| 45 DragHandle::ScrollDirection scroll_direction_; |
| 46 SkColor color_; |
| 47 float scroll_start_location_; |
| 48 const int preferred_width_; |
| 49 const int preferred_height_; |
| 50 |
| 51 DISALLOW_COPY_AND_ASSIGN(DragHandleView); |
| 52 }; |
| 53 |
| 54 DragHandleView::DragHandleView(DragHandle::ScrollDirection scroll_direction, |
| 55 DragHandle::ScrollDelegate* delegate, |
| 56 int preferred_width, |
| 57 int preferred_height) |
| 58 : state_(DEFAULT), |
| 59 delegate_(delegate), |
| 60 scroll_direction_(scroll_direction), |
| 61 color_(SK_ColorTRANSPARENT), |
| 62 preferred_width_(preferred_width), |
| 63 preferred_height_(preferred_height) { |
| 64 SetColor(kDragHandleColorNormal); |
| 65 } |
| 66 |
| 67 DragHandleView::~DragHandleView() { |
| 68 } |
| 69 |
| 70 void DragHandleView::SetColor(SkColor color) { |
| 71 if (color_ == color) |
| 72 return; |
| 73 color_ = color; |
| 74 set_background(views::Background::CreateSolidBackground(color_)); |
| 75 SchedulePaint(); |
| 76 } |
| 77 |
| 78 void DragHandleView::SetState(State state) { |
| 79 if (state_ == state) |
| 80 return; |
| 81 state_ = state; |
| 82 if (state == DEFAULT) |
| 83 scroll_start_location_ = 0; |
| 84 } |
| 85 |
| 86 // views::View: |
| 87 gfx::Size DragHandleView::GetPreferredSize() const { |
| 88 return gfx::Size(preferred_width_, preferred_height_); |
| 89 } |
| 90 |
| 91 void DragHandleView::OnMouseEntered(const ui::MouseEvent& event) { |
| 92 SetColor(kDragHandleColorHot); |
| 93 } |
| 94 |
| 95 void DragHandleView::OnMouseExited(const ui::MouseEvent& event) { |
| 96 SetColor(kDragHandleColorNormal); |
| 97 } |
| 98 |
| 99 void DragHandleView::OnGestureEvent(ui::GestureEvent* event) { |
| 100 SkColor change_color = SK_ColorTRANSPARENT; |
| 101 if (event->type() == ui::ET_GESTURE_BEGIN && |
| 102 event->details().touch_points() == 1) { |
| 103 change_color = kDragHandleColorHot; |
| 104 } else if (event->type() == ui::ET_GESTURE_END && |
| 105 event->details().touch_points() == 1) { |
| 106 change_color = kDragHandleColorNormal; |
| 107 } |
| 108 |
| 109 if (change_color != SK_ColorTRANSPARENT) { |
| 110 SetColor(change_color); |
| 111 event->SetHandled(); |
| 112 return; |
| 113 } |
| 114 |
| 115 if (event->type() == ui::ET_GESTURE_SCROLL_BEGIN) { |
| 116 if (!delegate_->HandleCanScroll() || state_ != DEFAULT) |
| 117 return; |
| 118 float delta; |
| 119 if (scroll_direction_ == DragHandle::VERTICAL) { |
| 120 delta = event->details().scroll_y_hint(); |
| 121 scroll_start_location_ = event->root_location().y(); |
| 122 } else { |
| 123 delta = event->details().scroll_x_hint(); |
| 124 scroll_start_location_ = event->root_location().x(); |
| 125 } |
| 126 delegate_->HandleScrollBegin(delta); |
| 127 SetState(SCROLLING); |
| 128 event->SetHandled(); |
| 129 } else if (event->type() == ui::ET_GESTURE_SCROLL_END || |
| 130 event->type() == ui::ET_SCROLL_FLING_START) { |
| 131 if (state_ != SCROLLING) |
| 132 return; |
| 133 delegate_->HandleScrollEnd(); |
| 134 SetColor(kDragHandleColorNormal); |
| 135 SetState(DEFAULT); |
| 136 event->SetHandled(); |
| 137 } else if (event->type() == ui::ET_GESTURE_SCROLL_UPDATE) { |
| 138 if (state_ != SCROLLING) |
| 139 return; |
| 140 float delta = scroll_direction_ == DragHandle::VERTICAL |
| 141 ? event->root_location().y() - scroll_start_location_ |
| 142 : event->root_location().x() - scroll_start_location_; |
| 143 delegate_->HandleScrollUpdate(delta); |
| 144 event->SetHandled(); |
| 145 } |
| 146 } |
| 147 |
| 148 } // namespace |
| 149 |
| 150 views::View* CreateDragHandleView(DragHandle::ScrollDirection scroll_direction, |
| 151 DragHandle::ScrollDelegate* delegate, |
| 152 int preferred_width, |
| 153 int preferred_height) { |
| 154 views::View* view = new DragHandleView( |
| 155 scroll_direction, delegate, preferred_width, preferred_height); |
| 156 return view; |
| 157 } |
| 158 |
| 159 } // namespace athena |
OLD | NEW |