aboutsummaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
authorAkshay Nair <phenax5@gmail.com>2025-03-18 20:52:36 +0530
committerAkshay Nair <phenax5@gmail.com>2025-03-18 20:52:36 +0530
commit056b0b16947ac19a0c425aa9d2ff8f589378d3bc (patch)
treed062dc2de09711c1f7e980fc8475c19a97ff7d99 /include
parentdd6590a0fda614fa2fb0ca9e4ad11b086393372e (diff)
downloadnull-browser-056b0b16947ac19a0c425aa9d2ff8f589378d3bc.tar.gz
null-browser-056b0b16947ac19a0c425aa9d2ff8f589378d3bc.zip
Add keymap evaluation with strict event filter
Diffstat (limited to '')
-rw-r--r--include/keymap/KeySeqParser.hpp26
-rw-r--r--include/keymap/KeymapEvaluator.hpp71
-rw-r--r--include/widgets/InputLine.hpp2
-rw-r--r--include/widgets/MainWindow.hpp6
-rw-r--r--include/widgets/WebView.hpp13
-rw-r--r--include/widgets/WebViewStack.hpp9
6 files changed, 116 insertions, 11 deletions
diff --git a/include/keymap/KeySeqParser.hpp b/include/keymap/KeySeqParser.hpp
index aefcb62..fac2546 100644
--- a/include/keymap/KeySeqParser.hpp
+++ b/include/keymap/KeySeqParser.hpp
@@ -2,6 +2,7 @@
#include <QtCore/qnamespace.h>
#include <QtCore>
+#include <cmath>
struct KeyChord {
Qt::KeyboardModifiers mod;
@@ -9,11 +10,34 @@ struct KeyChord {
};
bool operator==(const KeyChord a, const KeyChord b);
+typedef QList<KeyChord> KeySequence;
+
+enum KeyMatchType {
+ NoMatch,
+ Match,
+ Pending,
+};
+
class KeySeqParser {
public:
+ static const KeyMatchType keySequenceMatch(const KeySequence target,
+ const KeySequence current) {
+ for (int i = 0; i < target.length(); i++) {
+ if (current.length() <= i)
+ return KeyMatchType::Pending;
+
+ if (target[i].key != current[i].key ||
+ !target[i].mod.testFlags(current[i].mod))
+ return KeyMatchType::NoMatch;
+ }
+
+ return KeyMatchType::Match;
+ }
+
+public:
KeySeqParser();
- QList<KeyChord> parse(QString keySequence);
+ KeySequence parse(QString keySequence);
private:
Qt::Key parseKey(QString keyName);
diff --git a/include/keymap/KeymapEvaluator.hpp b/include/keymap/KeymapEvaluator.hpp
new file mode 100644
index 0000000..ed468d2
--- /dev/null
+++ b/include/keymap/KeymapEvaluator.hpp
@@ -0,0 +1,71 @@
+#pragma once
+
+#include "keymap/KeySeqParser.hpp"
+#include <QWidget>
+#include <QtCore/qmap.h>
+#include <QtCore/qnamespace.h>
+#include <QtCore>
+#include <algorithm>
+
+struct KeyMap {
+ KeySequence keySequence;
+ QString action; // TODO: string for testing
+};
+
+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;
+
+ 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();
+ }
+
+private:
+ const QList<KeyMap> *currentModeKeys() {
+ if (!modalKeys.contains(currentMode))
+ return new QList<KeyMap>();
+
+ return &modalKeys[currentMode];
+ }
+
+private:
+ QMap<QString, QList<KeyMap>> modalKeys;
+ KeySeqParser keySeqParser;
+ QString currentMode = "default";
+
+ KeySequence activeKeySequence;
+};
diff --git a/include/widgets/InputLine.hpp b/include/widgets/InputLine.hpp
index d5f65be..a382ff8 100644
--- a/include/widgets/InputLine.hpp
+++ b/include/widgets/InputLine.hpp
@@ -24,8 +24,6 @@ public:
DELEGATE(input, text, getInputText)
DELEGATE(promptPrefix, text, prompt)
- // bool eventFilter(QObject *obj, QEvent *event) override;
-
signals:
void submitted(QString text);
void cancelled();
diff --git a/include/widgets/MainWindow.hpp b/include/widgets/MainWindow.hpp
index 559a119..3e3a6d6 100644
--- a/include/widgets/MainWindow.hpp
+++ b/include/widgets/MainWindow.hpp
@@ -1,12 +1,10 @@
#pragma once
#include <QMainWindow>
-#include <QObject>
-#include <QStackedLayout>
-#include <QWebEngineView>
#include "Configuration.hpp"
#include "InputMediator.hpp"
+#include "keymap/KeymapEvaluator.hpp"
class MainWindow : public QMainWindow {
public:
@@ -14,8 +12,10 @@ public:
private:
void keyPressEvent(QKeyEvent *event) override;
+ bool eventFilter(QObject *object, QEvent *event) override;
private:
InputMediator *inputMediator;
Configuration configuration;
+ KeymapEvaluator keymapEvaluator;
};
diff --git a/include/widgets/WebView.hpp b/include/widgets/WebView.hpp
new file mode 100644
index 0000000..8a95139
--- /dev/null
+++ b/include/widgets/WebView.hpp
@@ -0,0 +1,13 @@
+#pragma once
+
+#include <QKeyEvent>
+#include <QWebEngineView>
+#include <QWidget>
+#include <QtCore>
+
+class WebView : public QWebEngineView {
+ Q_OBJECT
+
+public:
+ WebView(QWebEngineProfile *profile);
+};
diff --git a/include/widgets/WebViewStack.hpp b/include/widgets/WebViewStack.hpp
index 34b1658..c315251 100644
--- a/include/widgets/WebViewStack.hpp
+++ b/include/widgets/WebViewStack.hpp
@@ -2,11 +2,11 @@
#include <QStackedLayout>
#include <QWebEngineProfile>
-#include <QWebEngineView>
#include <sys/types.h>
#include "Configuration.hpp"
#include "completion/TabsModel.hpp"
+#include "widgets/WebView.hpp"
enum OpenType { OpenUrl, OpenUrlInTab, OpenUrlInBgTab, OpenUrlInWindow };
@@ -24,6 +24,7 @@ public:
void openUrl(QUrl url, OpenType openType = OpenType::OpenUrl);
std::vector<QUrl> urls();
+ QList<Tab> getTabList();
u_int32_t currentWebViewIndex();
u_int32_t count();
QUrl currentUrl();
@@ -35,11 +36,9 @@ public:
void close(long index);
void closeCurrent();
- QList<Tab> getTabList();
-
private:
void setCurrentUrl(QUrl url);
- QWebEngineView *createNewWebView(QUrl url, bool focus = false);
+ WebView *createNewWebView(QUrl url, bool focus = false);
private slots:
void onNewWebViewRequest(QWebEngineNewWindowRequest &request);
@@ -48,5 +47,5 @@ private:
const Configuration *configuration;
QWebEngineProfile *profile;
QStackedLayout *layout;
- QList<QWebEngineView *> webViewList;
+ QList<WebView *> webViewList;
};