aboutsummaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--include/InputMediator.hpp7
-rw-r--r--include/keymap/KeymapEvaluator.hpp62
-rw-r--r--include/utils.hpp6
-rw-r--r--include/widgets/MainWindow.hpp2
4 files changed, 29 insertions, 48 deletions
diff --git a/include/InputMediator.hpp b/include/InputMediator.hpp
index 4aaa6c1..7194ff9 100644
--- a/include/InputMediator.hpp
+++ b/include/InputMediator.hpp
@@ -4,6 +4,7 @@
#include <QtCore>
#include "LuaRuntime.hpp"
+#include "keymap/KeymapEvaluator.hpp"
#include "utils.hpp"
#include "widgets/WebViewStack.hpp"
@@ -11,13 +12,16 @@ class InputMediator : public QObject {
Q_OBJECT
public:
- InputMediator(WebViewStack *webViewStack, LuaRuntime *luaRuntime);
+ InputMediator(WebViewStack *webViewStack, LuaRuntime *luaRuntime,
+ KeymapEvaluator *keymapEvaluator);
+ ~InputMediator();
DELEGATE(webViewStack, openUrl, openUrl)
DELEGATE(webViewStack, currentUrl, currentUrl)
DELEGATE(webViewStack, next, nextWebView)
DELEGATE(webViewStack, previous, previousWebView)
DELEGATE(webViewStack, closeCurrent, closeCurrentWebView)
+ DELEGATE(keymapEvaluator, evaluate, evaluateKeymap)
private:
void evaluateCommand(QString command);
@@ -25,4 +29,5 @@ private:
private:
WebViewStack *webViewStack;
LuaRuntime *luaRuntime;
+ KeymapEvaluator *keymapEvaluator;
};
diff --git a/include/keymap/KeymapEvaluator.hpp b/include/keymap/KeymapEvaluator.hpp
index 54409b1..db93900 100644
--- a/include/keymap/KeymapEvaluator.hpp
+++ b/include/keymap/KeymapEvaluator.hpp
@@ -1,70 +1,42 @@
#pragma once
-#include "keymap/KeySeqParser.hpp"
#include <QWidget>
#include <QtCore/qmap.h>
#include <QtCore/qnamespace.h>
#include <QtCore>
+#include <functional>
+
+#include "keymap/KeySeqParser.hpp"
+#include "utils.hpp"
+
+typedef std::function<void()> KeyAction;
struct KeyMap {
KeySequence keySequence;
- QString action; // TODO: string for testing
+ KeyAction action;
};
+enum KeyMode { Normal, Insert };
+
class KeymapEvaluator : public QObject {
Q_OBJECT
public:
KeymapEvaluator();
- void addKeymap(QString mode, QString key, QString action) {
- if (!modalKeys.contains(mode))
- modalKeys.insert(mode, {});
-
- auto keySeq = keySeqParser.parse(key);
- // TODO: Add actions
- modalKeys[mode].append((KeyMap){.keySequence = keySeq, .action = action});
- }
-
- void evaluate(Qt::KeyboardModifiers modifiers, Qt::Key key) {
- if (key == Qt::Key_Control || key == Qt::Key_Shift || key == Qt::Key_Meta ||
- key == Qt::Key_Alt)
- return;
+ void addKeymap(KeyMode mode, QString key, KeyAction action);
+ bool evaluate(Qt::KeyboardModifiers modifiers, Qt::Key key);
- auto keymaps = currentModeKeys();
- auto foundPendingMatches = false;
-
- activeKeySequence.append((KeyChord){.mod = modifiers, .key = key});
-
- for (auto &keymap : *keymaps) {
- auto matchType =
- KeySeqParser::keySequenceMatch(keymap.keySequence, activeKeySequence);
-
- if (matchType == KeyMatchType::Match) {
- qDebug() << "Matched!" << keymap.action;
- activeKeySequence.clear();
- return;
- } else if (matchType == KeyMatchType::Pending) {
- foundPendingMatches = true;
- }
- }
-
- if (!foundPendingMatches)
- activeKeySequence.clear();
- }
+ DEFINE_SETTER(setCurrentMode, currentMode)
+ DEFINE_GETTER(getCurrentMode, currentMode)
private:
- const QList<KeyMap> *currentModeKeys() {
- if (!modalKeys.contains(currentMode))
- return new QList<KeyMap>();
-
- return &modalKeys[currentMode];
- }
+ const QList<KeyMap> *currentModeKeys();
+ bool isInsertableMode();
private:
- QMap<QString, QList<KeyMap>> modalKeys;
+ QMap<KeyMode, QList<KeyMap>> modalKeys;
KeySeqParser keySeqParser;
- QString currentMode = "default";
-
+ KeyMode currentMode = KeyMode::Normal;
KeySequence activeKeySequence;
};
diff --git a/include/utils.hpp b/include/utils.hpp
index 65667f7..0650ac2 100644
--- a/include/utils.hpp
+++ b/include/utils.hpp
@@ -3,3 +3,9 @@
template <typename... Args> decltype(auto) METHOD_AS(Args &&...args) { \
return OBJ->METHOD(std::forward<Args>(args)...); \
}
+
+#define DEFINE_SETTER(METHOD, PROPERTY) \
+ template <typename Arg> void METHOD(Arg val) { PROPERTY = val; }
+
+#define DEFINE_GETTER(METHOD, EXPR) \
+ template <typename Arg> decltype(auto) METHOD() { return EXPR; }
diff --git a/include/widgets/MainWindow.hpp b/include/widgets/MainWindow.hpp
index 3e3a6d6..2bbc42e 100644
--- a/include/widgets/MainWindow.hpp
+++ b/include/widgets/MainWindow.hpp
@@ -4,7 +4,6 @@
#include "Configuration.hpp"
#include "InputMediator.hpp"
-#include "keymap/KeymapEvaluator.hpp"
class MainWindow : public QMainWindow {
public:
@@ -17,5 +16,4 @@ private:
private:
InputMediator *inputMediator;
Configuration configuration;
- KeymapEvaluator keymapEvaluator;
};