summaryrefslogtreecommitdiffstats
path: root/ui/touch_selection/longpress_drag_selector.h
blob: 4ebdd675a875d489b13a66569f0b04804586b29d (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
// Copyright 2015 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.

#ifndef UI_TOUCH_SELECTION_LONGPRESS_DRAG_SELECTOR_H_
#define UI_TOUCH_SELECTION_LONGPRESS_DRAG_SELECTOR_H_

#include "base/time/time.h"
#include "ui/gfx/geometry/point_f.h"
#include "ui/gfx/geometry/vector2d_f.h"
#include "ui/touch_selection/touch_selection_draggable.h"
#include "ui/touch_selection/ui_touch_selection_export.h"

namespace ui {

class MotionEvent;

class UI_TOUCH_SELECTION_EXPORT LongPressDragSelectorClient
    : public TouchSelectionDraggableClient {
 public:
  ~LongPressDragSelectorClient() override {}
  virtual void OnLongPressDragActiveStateChanged() = 0;
  virtual gfx::PointF GetSelectionStart() const = 0;
  virtual gfx::PointF GetSelectionEnd() const = 0;
};

// Supports text selection via touch dragging after a longpress-initiated
// selection.
class UI_TOUCH_SELECTION_EXPORT LongPressDragSelector
    : public TouchSelectionDraggable {
 public:
  explicit LongPressDragSelector(LongPressDragSelectorClient* client);
  ~LongPressDragSelector() override;

  // TouchSelectionDraggable implementation.
  bool WillHandleTouchEvent(const MotionEvent& event) override;
  bool IsActive() const override;

  // Called just prior to a longpress event being handled.
  void OnLongPressEvent(base::TimeTicks event_time,
                        const gfx::PointF& position);

  // Called when a scroll is going to happen to cancel longpress-drag gesture.
  void OnScrollBeginEvent();

  // Called when the active selection changes.
  void OnSelectionActivated();
  void OnSelectionDeactivated();

 private:
  enum SelectionState {
    INACTIVE,
    LONGPRESS_PENDING,
    SELECTION_PENDING,
    DRAG_PENDING,
    DRAGGING
  };

  void SetState(SelectionState state);

  LongPressDragSelectorClient* const client_;

  SelectionState state_;

  base::TimeTicks touch_down_time_;
  gfx::PointF touch_down_position_;

  gfx::Vector2dF longpress_drag_selection_offset_;
  gfx::PointF longpress_drag_start_anchor_;
  bool has_longpress_drag_start_anchor_;
};

}  // namespace ui

#endif  // UI_TOUCH_SELECTION_LONGPRESS_DRAG_SELECTOR_H_