aboutsummaryrefslogtreecommitdiff
path: root/include/cru/ui/ShortcutHub.hpp
blob: 9995a4e12206d6abfd61da4939fed10d0a9b61ae (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
#pragma once
#include "Base.hpp"

#include "cru/platform/native/Keyboard.hpp"

#include <functional>
#include <optional>
#include <string>
#include <string_view>
#include <vector>

namespace cru::ui {

class ShortcutKeyBind {
 public:
  ShortcutKeyBind(platform::native::KeyCode key,
                  platform::native::KeyModifier modifier)
      : key_(key), modifier_(modifier) {}

  CRU_DEFAULT_COPY(ShortcutKeyBind)
  CRU_DEFAULT_MOVE(ShortcutKeyBind)

  ~ShortcutKeyBind() = default;

  platform::native::KeyCode GetKey() const { return key_; }
  platform::native::KeyModifier GetModifier() const { return modifier_; }

  bool Is(platform::native::KeyCode key,
          platform::native::KeyModifier modifier) const {
    return key == key_ && modifier == modifier_;
  }

  bool operator==(const ShortcutKeyBind& other) const {
    return this->key_ == other.key_ && this->modifier_ == other.modifier_;
  }

  bool operator!=(const ShortcutKeyBind& other) const {
    return !this->operator==(other);
  }

 private:
  platform::native::KeyCode key_;
  platform::native::KeyModifier modifier_;
};

struct ShortcutInfo {
  std::u16string name;
  ShortcutKeyBind key_bind;
  std::function<bool()> handler;
};

class ShortcutHub : public Object {
 public:
  ShortcutHub();

  CRU_DELETE_COPY(ShortcutHub)
  CRU_DELETE_MOVE(ShortcutHub)

  ~ShortcutHub() override;

  // Handler return true if it consumes the shortcut. Or return false if it does
  // not handle the shortcut. Name is just for debug.
  int RegisterShortcut(std::u16string name, ShortcutKeyBind bind,
                       std::function<bool()> handler);

  void UnregisterShortcut(int id);

  std::vector<ShortcutInfo> GetAllShortcuts() const;
  std::optional<ShortcutInfo> GetShortcut(int id) const;
  std::vector<ShortcutInfo> GetShortcutByKeyBind(
      const ShortcutKeyBind& key_bind) const;

  void Install(Control* control);
  void Uninstall();

 private:
};
}  // namespace cru::ui