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 virtual ~DragHandleView();
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 delegate_
->HandleScrollEnd();
117 SetColor(kDragHandleColorNormal
);
118 SetIsScrolling(false);
120 } else if (event
->type() == ui::ET_GESTURE_SCROLL_UPDATE
) {
121 if (!scroll_in_progress_
)
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
);
133 views::View
* CreateDragHandleView(DragHandleScrollDirection scroll_direction
,
134 DragHandleScrollDelegate
* delegate
,
136 int preferred_height
) {
137 views::View
* view
= new DragHandleView(
138 scroll_direction
, delegate
, preferred_width
, preferred_height
);
142 } // namespace athena