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