HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
KeyboardBindings.hpp
1// Copyright 2020 Pokitec
2// All rights reserved.
3
4#pragma once
5
6#include "TTauri/GUI/KeyboardKey.hpp"
7#include "TTauri/Foundation/string_tag.hpp"
8#include "TTauri/Foundation/URL.hpp"
9#include "TTauri/Foundation/os_detect.hpp"
10#include <unordered_map>
11#include <tuple>
12
13namespace std {
14
15template<>
16struct hash<std::pair<tt::string_tag,tt::KeyboardKey>> {
17 [[nodiscard]] size_t operator() (std::pair<tt::string_tag,tt::KeyboardKey> const &rhs) const noexcept {
18 return tt::hash_mix(rhs.first, rhs.second);
19 }
20};
21
22}
23
24namespace tt {
25
27 struct commands_t {
29 std::vector<string_ltag> system = {};
30
32 std::vector<string_ltag> ignored = {};
33
36
38 std::vector<string_ltag> cache = {};
39
40 [[nodiscard]] std::vector<string_ltag> const &get_commands() const noexcept {
41 return cache;
42 }
43
44 void add_system_command(string_ltag cmd) noexcept {
45 ttlet i = std::find(system.cbegin(), system.cend(), cmd);
46 if (i == system.cend()) {
47 system.push_back(cmd);
48 update_cache();
49 }
50 }
51
52 void add_ignored_command(string_ltag cmd) noexcept {
53 ttlet i = std::find(ignored.cbegin(), ignored.cend(), cmd);
54 if (i == ignored.cend()) {
55 ignored.push_back(cmd);
56 update_cache();
57 }
58 }
59
60 void add_user_command(string_ltag cmd) noexcept {
61 ttlet i = std::find(user.cbegin(), user.cend(), cmd);
62 if (i == user.cend()) {
63 user.push_back(cmd);
64 update_cache();
65 }
66 }
67
68 void update_cache() noexcept {
69 cache.reserve(ssize(system) + ssize(user));
70
71 for (ttlet cmd: system) {
72 ttlet i = std::find(cache.cbegin(), cache.cend(), cmd);
73 if (i == cache.cend()) {
74 cache.push_back(cmd);
75 }
76 }
77
78 for (ttlet cmd: ignored) {
79 ttlet i = std::find(cache.cbegin(), cache.cend(), cmd);
80 if (i != cache.cend()) {
81 cache.erase(i);
82 }
83 }
84
85 for (ttlet cmd: user) {
86 ttlet i = std::find(cache.cbegin(), cache.cend(), cmd);
87 if (i == cache.cend()) {
88 cache.push_back(cmd);
89 }
90 }
91 }
92 };
93
97
98public:
99 KeyboardBindings() noexcept :
100 bindings() {}
101
102 void addSystemBinding(KeyboardKey key, string_ltag command) noexcept {
103 bindings[key].add_system_command(command);
104 }
105
106 void addIgnoredBinding(KeyboardKey key, string_ltag command) noexcept {
107 bindings[key].add_ignored_command(command);
108 }
109
110 void addUserBinding(KeyboardKey key, string_ltag command) noexcept {
111 bindings[key].add_user_command(command);
112 }
113
116 [[nodiscard]] std::vector<string_ltag> const &translate(KeyboardKey key) const noexcept {
117 static std::vector<string_ltag> empty_commands = {};
118
119 ttlet i = bindings.find(key);
120 if (i != bindings.cend()) {
121 return i->second.get_commands();
122 } else {
123 return empty_commands;
124 }
125 }
126
132 void clear() noexcept {
133 bindings.clear();
134 }
135
138 void loadBindings(URL url, bool system_binding);
139
143 if constexpr (OperatingSystem::current == OperatingSystem::Windows) {
144 return loadBindings(URL{"resource:win32.keybinds.json"}, true);
145 } else {
146 tt_no_default;
147 }
148 }
149
150 void loadUserBindings(URL url) {
151 clear();
153 loadBindings(url, false);
154 }
155
160};
161
162
163}
STL namespace.
Definition URL.hpp:45
Definition KeyboardBindings.hpp:26
void loadSystemBindings()
Load system bindings.
Definition KeyboardBindings.hpp:142
void loadBindings(URL url, bool system_binding)
Load bindings from a JSON file.
std::vector< string_ltag > const & translate(KeyboardKey key) const noexcept
translate a key press in the empty-context to a command.
Definition KeyboardBindings.hpp:116
void clear() noexcept
Clear all bindings.
Definition KeyboardBindings.hpp:132
void saveUserBindings(URL url)
Save user bindings This will save all bindings that are different from the system bindings.
A key in combination with modifiers.
Definition KeyboardKey.hpp:23
T cbegin(T... args)
T clear(T... args)
T cend(T... args)
T erase(T... args)
T find(T... args)
T operator()(T... args)
T push_back(T... args)
T reserve(T... args)