aboutsummaryrefslogtreecommitdiff
path: root/src/keymap/KeymapEvaluator.hpp
blob: a7a100bccd1bd27e6a7356c346834a5746db5c46 (plain) (blame)
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
#pragma once

#include <QWidget>
#include <QtCore>
#include <functional>
#include <unordered_map>

#include "keymap/KeySeqParser.hpp"
#include "utils.hpp"

using KeyAction = std::function<void()>;

struct KeyMap {
  KeySequence key_sequence;
  KeyAction action;
};

using KeyMode = QString;

struct KeyModeConfig {
  bool passthrough;
};

struct KeyModeState {
  QList<KeyMap> keymap;
  KeyModeConfig config;
};

class KeymapEvaluator : public QObject {
  Q_OBJECT

public:
  KeymapEvaluator() = default;

  static KeymapEvaluator &instance() {
    static KeymapEvaluator keymap_evaluator;
    return keymap_evaluator;
  }

  void add_keymap(const KeyMode &mode, const QString &key, KeyAction action);
  bool evaluate(Qt::KeyboardModifiers modifiers, Qt::Key key);
  void define_mode(const KeyMode &mode, const KeyModeConfig &config);

  DEFINE_SETTER(set_current_mode, current_mode)
  DEFINE_GETTER(get_current_mode, current_mode)

protected:
  const QList<KeyMap> *current_mode_keys();
  bool is_passthrough_mode();

private:
  std::unordered_map<KeyMode, KeyModeState> modal_keys;
  KeySeqParser key_seq_parser;
  KeyMode current_mode = "i";
  KeySequence active_key_sequence;
};