summaryrefslogtreecommitdiffstats
path: root/athena/util/drag_handle.cc
blob: ad19c9adb16ee375d4b6e53d9289dae2ec2ea966 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "athena/util/drag_handle.h"

#include "ui/views/background.h"
#include "ui/views/view.h"

namespace athena {
namespace {

const SkColor kDragHandleColorNormal = SK_ColorGRAY;
const SkColor kDragHandleColorHot = SK_ColorWHITE;

// This view notifies its delegate of the touch scroll gestures performed on it.
class DragHandleView : public views::View {
 public:
  DragHandleView(DragHandleScrollDirection scroll_direction,
                 DragHandleScrollDelegate* delegate,
                 int preferred_width,
                 int preferred_height);
  ~DragHandleView() override;

 private:
  void SetColor(SkColor color);

  void SetIsScrolling(bool scrolling);

  // views::View:
  virtual gfx::Size GetPreferredSize() const override;
  virtual void OnGestureEvent(ui::GestureEvent* event) override;

  bool scroll_in_progress_;
  DragHandleScrollDelegate* delegate_;
  DragHandleScrollDirection scroll_direction_;
  SkColor color_;
  float scroll_start_location_;
  const int preferred_width_;
  const int preferred_height_;

  DISALLOW_COPY_AND_ASSIGN(DragHandleView);
};

DragHandleView::DragHandleView(DragHandleScrollDirection scroll_direction,
                               DragHandleScrollDelegate* delegate,
                               int preferred_width,
                               int preferred_height)
    : scroll_in_progress_(false),
      delegate_(delegate),
      scroll_direction_(scroll_direction),
      color_(SK_ColorTRANSPARENT),
      preferred_width_(preferred_width),
      preferred_height_(preferred_height) {
  SetColor(kDragHandleColorNormal);
}

DragHandleView::~DragHandleView() {
}

void DragHandleView::SetColor(SkColor color) {
  if (color_ == color)
    return;
  color_ = color;
  set_background(views::Background::CreateSolidBackground(color_));
  SchedulePaint();
}

void DragHandleView::SetIsScrolling(bool scrolling) {
  if (scroll_in_progress_ == scrolling)
    return;
  scroll_in_progress_ = scrolling;
  if (!scroll_in_progress_)
    scroll_start_location_ = 0;
}

// views::View:
gfx::Size DragHandleView::GetPreferredSize() const {
  return gfx::Size(preferred_width_, preferred_height_);
}

void DragHandleView::OnGestureEvent(ui::GestureEvent* event) {
  SkColor change_color = SK_ColorTRANSPARENT;
  if (event->type() == ui::ET_GESTURE_BEGIN &&
      event->details().touch_points() == 1) {
    change_color = kDragHandleColorHot;
  } else if (event->type() == ui::ET_GESTURE_END &&
             event->details().touch_points() == 1) {
    change_color = kDragHandleColorNormal;
  }

  if (change_color != SK_ColorTRANSPARENT) {
    SetColor(change_color);
    event->SetHandled();
    return;
  }

  if (event->type() == ui::ET_GESTURE_SCROLL_BEGIN) {
    if (scroll_in_progress_)
      return;
    float delta;
    if (scroll_direction_ == DRAG_HANDLE_VERTICAL) {
      delta = event->details().scroll_y_hint();
      scroll_start_location_ = event->root_location().y();
    } else {
      delta = event->details().scroll_x_hint();
      scroll_start_location_ = event->root_location().x();
    }
    delegate_->HandleScrollBegin(delta);
    SetIsScrolling(true);
    event->SetHandled();
  } else if (event->type() == ui::ET_GESTURE_SCROLL_END ||
             event->type() == ui::ET_SCROLL_FLING_START) {
    if (!scroll_in_progress_)
      return;
    float velocity = 0.0f;
    if (event->type() == ui::ET_SCROLL_FLING_START)
      velocity = event->details().velocity_x();
    delegate_->HandleScrollEnd(velocity);
    SetColor(kDragHandleColorNormal);
    SetIsScrolling(false);
    event->SetHandled();
  } else if (event->type() == ui::ET_GESTURE_SCROLL_UPDATE) {
    if (!scroll_in_progress_)
      return;
    float delta = scroll_direction_ == DRAG_HANDLE_VERTICAL
                      ? event->root_location().y() - scroll_start_location_
                      : event->root_location().x() - scroll_start_location_;
    delegate_->HandleScrollUpdate(delta);
    event->SetHandled();
  }
}

}  // namespace

views::View* CreateDragHandleView(DragHandleScrollDirection scroll_direction,
                                  DragHandleScrollDelegate* delegate,
                                  int preferred_width,
                                  int preferred_height) {
  views::View* view = new DragHandleView(
      scroll_direction, delegate, preferred_width, preferred_height);
  return view;
}

}  // namespace athena