diff options
| author | Akshay Nair <phenax5@gmail.com> | 2025-04-20 12:57:07 +0530 |
|---|---|---|
| committer | Akshay Nair <phenax5@gmail.com> | 2025-05-02 16:15:17 +0530 |
| commit | b4b6a646b3a7a7f6c7e990c67f208b76f4b5f748 (patch) | |
| tree | 7f18775a63594acf0187668af63766dd72b9f66f /src/keymap | |
| parent | 75157006bf6f1176f64e87695343980b16908bb6 (diff) | |
| download | null-browser-b4b6a646b3a7a7f6c7e990c67f208b76f4b5f748.tar.gz null-browser-b4b6a646b3a7a7f6c7e990c67f208b76f4b5f748.zip | |
Refactor + expose managing mode to inside lua
Diffstat (limited to '')
| -rw-r--r-- | src/keymap/KeymapEvaluator.cpp | 31 | ||||
| -rw-r--r-- | src/keymap/KeymapEvaluator.hpp | 23 |
2 files changed, 33 insertions, 21 deletions
diff --git a/src/keymap/KeymapEvaluator.cpp b/src/keymap/KeymapEvaluator.cpp index e42ec3c..0bd6f93 100644 --- a/src/keymap/KeymapEvaluator.cpp +++ b/src/keymap/KeymapEvaluator.cpp @@ -6,15 +6,14 @@ #include "keymap/KeymapEvaluator.hpp" // TODO: Clear mapping after some time +// TODO: Forward current keysequence with partial key after timeout (only passthrough kind) -void KeymapEvaluator::add_keymap(KeyMode mode, const QString &key, KeyAction action) { +void KeymapEvaluator::add_keymap(const KeyMode &mode, const QString &key, KeyAction action) { if (!modal_keys.contains(mode)) - modal_keys.insert(mode, {}); - - qDebug() << " " << mode << key; + modal_keys[mode] = {.keymap = {}, .config = {.passthrough = false}}; auto key_seq = key_seq_parser.parse(key); - modal_keys[mode].append(KeyMap{.key_sequence = key_seq, .action = std::move(action)}); + modal_keys[mode].keymap.append(KeyMap{.key_sequence = key_seq, .action = std::move(action)}); } bool KeymapEvaluator::evaluate(Qt::KeyboardModifiers modifiers, Qt::Key key) { @@ -43,25 +42,29 @@ bool KeymapEvaluator::evaluate(Qt::KeyboardModifiers modifiers, Qt::Key key) { if (!found_pending_matches) active_key_sequence.clear(); - if (is_insertable_mode()) + if (is_passthrough_mode()) return found_pending_matches; return true; } -bool KeymapEvaluator::is_insertable_mode() { return current_mode == KeyMode::Insert; } +bool KeymapEvaluator::is_passthrough_mode() { + if (!modal_keys.contains(current_mode)) + return false; + + return modal_keys[current_mode].config.passthrough; +} const QList<KeyMap> *KeymapEvaluator::current_mode_keys() { if (!modal_keys.contains(current_mode)) return new QList<KeyMap>(); - return &modal_keys[current_mode]; + return &modal_keys[current_mode].keymap; } -KeyMode KeymapEvaluator::mode_from_string(const QString &mode_string) { - if (mode_string == "n") - return KeyMode::Normal; - if (mode_string == "i") - return KeyMode::Insert; - return KeyMode::Normal; +void KeymapEvaluator::define_mode(const KeyMode &mode, const KeyModeConfig &config) { + if (!modal_keys.contains(current_mode)) + modal_keys[current_mode] = {.keymap = {}, .config = config}; + else + modal_keys[mode].config = config; } diff --git a/src/keymap/KeymapEvaluator.hpp b/src/keymap/KeymapEvaluator.hpp index 34f4ae5..a7a100b 100644 --- a/src/keymap/KeymapEvaluator.hpp +++ b/src/keymap/KeymapEvaluator.hpp @@ -2,8 +2,8 @@ #include <QWidget> #include <QtCore> -#include <cstdint> #include <functional> +#include <unordered_map> #include "keymap/KeySeqParser.hpp" #include "utils.hpp" @@ -15,7 +15,16 @@ struct KeyMap { KeyAction action; }; -enum KeyMode : uint8_t { Normal, Insert }; +using KeyMode = QString; + +struct KeyModeConfig { + bool passthrough; +}; + +struct KeyModeState { + QList<KeyMap> keymap; + KeyModeConfig config; +}; class KeymapEvaluator : public QObject { Q_OBJECT @@ -28,20 +37,20 @@ public: return keymap_evaluator; } - void add_keymap(KeyMode mode, const QString &key, KeyAction action); + void add_keymap(const KeyMode &mode, const QString &key, KeyAction action); bool evaluate(Qt::KeyboardModifiers modifiers, Qt::Key key); - KeyMode mode_from_string(const QString &mode_string); + 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_insertable_mode(); + bool is_passthrough_mode(); private: - QMap<KeyMode, QList<KeyMap>> modal_keys; + std::unordered_map<KeyMode, KeyModeState> modal_keys; KeySeqParser key_seq_parser; - KeyMode current_mode = KeyMode::Normal; + KeyMode current_mode = "i"; KeySequence active_key_sequence; }; |
