Chromium Code Reviews| 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" | |
|
pkotwicz
2014/09/23 05:13:39
Nit: remove include
mfomitchev
2014/09/23 15:46:02
Done.
| |
| 9 #include "ui/views/view.h" | |
| 10 #include "ui/views/widget/widget.h" | |
|
pkotwicz
2014/09/23 05:13:39
Nit: Remove include
mfomitchev
2014/09/23 15:46:02
Done.
| |
| 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(DragHandleScrollDirection scroll_direction, | |
| 22 DragHandleScrollDelegate* delegate, | |
| 23 int preferred_width, | |
| 24 int preferred_height); | |
| 25 virtual ~DragHandleView(); | |
| 26 | |
| 27 private: | |
| 28 void SetColor(SkColor color); | |
| 29 | |
| 30 void SetIsScrolling(bool scrolling); | |
| 31 | |
| 32 // views::View: | |
| 33 virtual gfx::Size GetPreferredSize() const OVERRIDE; | |
| 34 virtual void OnMouseEntered(const ui::MouseEvent& event) OVERRIDE; | |
| 35 virtual void OnMouseExited(const ui::MouseEvent& event) OVERRIDE; | |
| 36 virtual void OnGestureEvent(ui::GestureEvent* event) OVERRIDE; | |
| 37 | |
| 38 bool scroll_in_progress_; | |
| 39 DragHandleScrollDelegate* delegate_; | |
| 40 DragHandleScrollDirection scroll_direction_; | |
| 41 SkColor color_; | |
| 42 float scroll_start_location_; | |
| 43 const int preferred_width_; | |
| 44 const int preferred_height_; | |
| 45 | |
| 46 DISALLOW_COPY_AND_ASSIGN(DragHandleView); | |
| 47 }; | |
| 48 | |
| 49 DragHandleView::DragHandleView(DragHandleScrollDirection scroll_direction, | |
| 50 DragHandleScrollDelegate* delegate, | |
| 51 int preferred_width, | |
| 52 int preferred_height) | |
| 53 : scroll_in_progress_(false), | |
| 54 delegate_(delegate), | |
| 55 scroll_direction_(scroll_direction), | |
| 56 color_(SK_ColorTRANSPARENT), | |
| 57 preferred_width_(preferred_width), | |
| 58 preferred_height_(preferred_height) { | |
| 59 SetColor(kDragHandleColorNormal); | |
| 60 } | |
| 61 | |
| 62 DragHandleView::~DragHandleView() { | |
| 63 } | |
| 64 | |
| 65 void DragHandleView::SetColor(SkColor color) { | |
| 66 if (color_ == color) | |
| 67 return; | |
| 68 color_ = color; | |
| 69 set_background(views::Background::CreateSolidBackground(color_)); | |
| 70 SchedulePaint(); | |
| 71 } | |
| 72 | |
| 73 void DragHandleView::SetIsScrolling(bool scrolling) { | |
| 74 if (scroll_in_progress_ == scrolling) | |
| 75 return; | |
| 76 scroll_in_progress_ = scrolling; | |
| 77 if (!scroll_in_progress_) | |
| 78 scroll_start_location_ = 0; | |
| 79 } | |
| 80 | |
| 81 // views::View: | |
| 82 gfx::Size DragHandleView::GetPreferredSize() const { | |
| 83 return gfx::Size(preferred_width_, preferred_height_); | |
| 84 } | |
| 85 | |
| 86 void DragHandleView::OnMouseEntered(const ui::MouseEvent& event) { | |
|
pkotwicz
2014/09/23 05:13:39
Given that this class does not yet handle mouse dr
mfomitchev
2014/09/23 15:46:02
Done.
| |
| 87 SetColor(kDragHandleColorHot); | |
| 88 } | |
| 89 | |
| 90 void DragHandleView::OnMouseExited(const ui::MouseEvent& event) { | |
| 91 SetColor(kDragHandleColorNormal); | |
| 92 } | |
| 93 | |
| 94 void DragHandleView::OnGestureEvent(ui::GestureEvent* event) { | |
| 95 SkColor change_color = SK_ColorTRANSPARENT; | |
| 96 if (event->type() == ui::ET_GESTURE_BEGIN && | |
| 97 event->details().touch_points() == 1) { | |
| 98 change_color = kDragHandleColorHot; | |
| 99 } else if (event->type() == ui::ET_GESTURE_END && | |
| 100 event->details().touch_points() == 1) { | |
| 101 change_color = kDragHandleColorNormal; | |
| 102 } | |
| 103 | |
| 104 if (change_color != SK_ColorTRANSPARENT) { | |
| 105 SetColor(change_color); | |
| 106 event->SetHandled(); | |
| 107 return; | |
| 108 } | |
| 109 | |
| 110 if (event->type() == ui::ET_GESTURE_SCROLL_BEGIN) { | |
| 111 if (!delegate_->HandleCanScroll() || scroll_in_progress_) | |
| 112 return; | |
| 113 float delta; | |
| 114 if (scroll_direction_ == DRAG_HANDLE_VERTICAL) { | |
| 115 delta = event->details().scroll_y_hint(); | |
| 116 scroll_start_location_ = event->root_location().y(); | |
| 117 } else { | |
| 118 delta = event->details().scroll_x_hint(); | |
| 119 scroll_start_location_ = event->root_location().x(); | |
| 120 } | |
| 121 delegate_->HandleScrollBegin(delta); | |
| 122 SetIsScrolling(true); | |
| 123 event->SetHandled(); | |
| 124 } else if (event->type() == ui::ET_GESTURE_SCROLL_END || | |
| 125 event->type() == ui::ET_SCROLL_FLING_START) { | |
| 126 if (!scroll_in_progress_) | |
| 127 return; | |
| 128 delegate_->HandleScrollEnd(); | |
| 129 SetColor(kDragHandleColorNormal); | |
| 130 SetIsScrolling(false); | |
| 131 event->SetHandled(); | |
| 132 } else if (event->type() == ui::ET_GESTURE_SCROLL_UPDATE) { | |
| 133 if (!scroll_in_progress_) | |
| 134 return; | |
| 135 float delta = scroll_direction_ == DRAG_HANDLE_VERTICAL | |
| 136 ? event->root_location().y() - scroll_start_location_ | |
| 137 : event->root_location().x() - scroll_start_location_; | |
| 138 delegate_->HandleScrollUpdate(delta); | |
| 139 event->SetHandled(); | |
| 140 } | |
| 141 } | |
| 142 | |
| 143 } // namespace | |
| 144 | |
| 145 views::View* CreateDragHandleView(DragHandleScrollDirection scroll_direction, | |
| 146 DragHandleScrollDelegate* delegate, | |
| 147 int preferred_width, | |
| 148 int preferred_height) { | |
| 149 views::View* view = new DragHandleView( | |
| 150 scroll_direction, delegate, preferred_width, preferred_height); | |
| 151 return view; | |
| 152 } | |
| 153 | |
| 154 } // namespace athena | |
| OLD | NEW |