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.
5 #include "athena/util/drag_handle.h"
7 #include "ui/views/background.h"
8 #include "ui/views/view.h"
13 const SkColor kDragHandleColorNormal
= SK_ColorGRAY
;
14 const SkColor kDragHandleColorHot
= SK_ColorWHITE
;
16 // This view notifies its delegate of the touch scroll gestures performed on it.
17 class DragHandleView
: public views::View
{
19 DragHandleView(DragHandleScrollDirection scroll_direction
,
20 DragHandleScrollDelegate
* delegate
,
22 int preferred_height
);
23 ~DragHandleView() override
;
26 void SetColor(SkColor color
);
28 void SetIsScrolling(bool scrolling
);
31 virtual gfx::Size
GetPreferredSize() const override
;
32 virtual void OnGestureEvent(ui::GestureEvent
* event
) override
;
34 bool scroll_in_progress_
;
35 DragHandleScrollDelegate
* delegate_
;
36 DragHandleScrollDirection scroll_direction_
;
38 float scroll_start_location_
;
39 const int preferred_width_
;
40 const int preferred_height_
;
42 DISALLOW_COPY_AND_ASSIGN(DragHandleView
);
45 DragHandleView::DragHandleView(DragHandleScrollDirection scroll_direction
,
46 DragHandleScrollDelegate
* delegate
,
49 : scroll_in_progress_(false),
51 scroll_direction_(scroll_direction
),
52 color_(SK_ColorTRANSPARENT
),
53 preferred_width_(preferred_width
),
54 preferred_height_(preferred_height
) {
55 SetColor(kDragHandleColorNormal
);
58 DragHandleView::~DragHandleView() {
61 void DragHandleView::SetColor(SkColor color
) {
65 set_background(views::Background::CreateSolidBackground(color_
));
69 void DragHandleView::SetIsScrolling(bool scrolling
) {
70 if (scroll_in_progress_
== scrolling
)
72 scroll_in_progress_
= scrolling
;
73 if (!scroll_in_progress_
)
74 scroll_start_location_
= 0;
78 gfx::Size
DragHandleView::GetPreferredSize() const {
79 return gfx::Size(preferred_width_
, preferred_height_
);
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
;
92 if (change_color
!= SK_ColorTRANSPARENT
) {
93 SetColor(change_color
);
98 if (event
->type() == ui::ET_GESTURE_SCROLL_BEGIN
) {
99 if (scroll_in_progress_
)
102 if (scroll_direction_
== DRAG_HANDLE_VERTICAL
) {
103 delta
= event
->details().scroll_y_hint();
104 scroll_start_location_
= event
->root_location().y();
106 delta
= event
->details().scroll_x_hint();
107 scroll_start_location_
= event
->root_location().x();
109 delegate_
->HandleScrollBegin(delta
);
110 SetIsScrolling(true);
112 } else if (event
->type() == ui::ET_GESTURE_SCROLL_END
||
113 event
->type() == ui::ET_SCROLL_FLING_START
) {
114 if (!scroll_in_progress_
)
116 float velocity
= 0.0f
;
117 if (event
->type() == ui::ET_SCROLL_FLING_START
)
118 velocity
= event
->details().velocity_x();
119 delegate_
->HandleScrollEnd(velocity
);
120 SetColor(kDragHandleColorNormal
);
121 SetIsScrolling(false);
123 } else if (event
->type() == ui::ET_GESTURE_SCROLL_UPDATE
) {
124 if (!scroll_in_progress_
)
126 float delta
= scroll_direction_
== DRAG_HANDLE_VERTICAL
127 ? event
->root_location().y() - scroll_start_location_
128 : event
->root_location().x() - scroll_start_location_
;
129 delegate_
->HandleScrollUpdate(delta
);
136 views::View
* CreateDragHandleView(DragHandleScrollDirection scroll_direction
,
137 DragHandleScrollDelegate
* delegate
,
139 int preferred_height
) {
140 views::View
* view
= new DragHandleView(
141 scroll_direction
, delegate
, preferred_width
, preferred_height
);
145 } // namespace athena