summaryrefslogtreecommitdiffstats
path: root/ash/accelerators/key_hold_detector.h
blob: cad7e9f0b177db851da563bf01e3a8f267c2c05c (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
// 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.

#ifndef ASH_ACCELERATOR_KEY_HOLD_DETECTOR_H_
#define ASH_ACCELERATOR_KEY_HOLD_DETECTOR_H_

#include "ash/ash_export.h"
#include "base/memory/scoped_ptr.h"
#include "ui/events/event_handler.h"

namespace ui {
class KeyEvent;
}

namespace ash {

// This class is used to implement action when a user press and hold the key.
// When a user just pressed and released a key, normal pressed event gets
// generated upon release.
class ASH_EXPORT KeyHoldDetector : public ui::EventHandler {
 public:
  class Delegate {
   public:
    virtual ~Delegate() {}

    // If this return false, the event handler does not process
    // the event at all.
    virtual bool ShouldProcessEvent(const ui::KeyEvent* event) const = 0;

    // This should return true if the event should start the state transition.
    virtual bool IsStartEvent(const ui::KeyEvent* event) const = 0;

    // Called when the key is held.
    virtual void OnKeyHold(const ui::KeyEvent* event) = 0;

    // Called when the key is release after hold.
    virtual void OnKeyUnhold(const ui::KeyEvent* event) = 0;
  };

  explicit KeyHoldDetector(scoped_ptr<Delegate> delegate);
  ~KeyHoldDetector() override;

  // ui::EventHandler overrides:
  void OnKeyEvent(ui::KeyEvent* key_event) override;

 private:
  // A state to keep track of one click and click and hold operation.
  //
  // One click:
  // INITIAL --(first press)--> PRESSED --(release)--> INITIAL[SEND PRESS]
  //
  // Click and hold:
  // INITIAL --(first press)--> PRESSED --(press)-->
  //   HOLD[OnKeyHold] --(press)--> HOLD[OnKeyHold] --(release)-->
  //   INITIAL[OnKeyUnhold]
  enum State {
    INITIAL,
    PRESSED,
    HOLD
  };

  State state_;
  scoped_ptr<Delegate> delegate_;

  DISALLOW_COPY_AND_ASSIGN(KeyHoldDetector);
};

}  // namespace ash

#endif  // ASH_ACCELERATOR_KEY_HOLD_DETECTOR_H_