aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/CommandParser.cpp5
-rw-r--r--src/completion/CommandsAdapter.cpp22
-rw-r--r--src/completion/Completer.cpp4
-rw-r--r--src/completion/UrlAdapter.cpp20
-rw-r--r--src/completion/UrlModel.cpp48
-rw-r--r--src/widgets/CommandInput.cpp84
-rw-r--r--src/widgets/InputLine.cpp91
-rw-r--r--src/widgets/MainWindow.cpp63
8 files changed, 223 insertions, 114 deletions
diff --git a/src/CommandParser.cpp b/src/CommandParser.cpp
index 7a8eb00..8f83575 100644
--- a/src/CommandParser.cpp
+++ b/src/CommandParser.cpp
@@ -6,6 +6,7 @@
CommandParser::CommandParser() {}
Cmd CommandParser::parse(QString input) {
+ // TODO: simplify this to only parse the command
auto parts = input.split(QRegularExpression("\\s+"), Qt::SkipEmptyParts);
if (parts.isEmpty())
@@ -13,9 +14,7 @@ Cmd CommandParser::parse(QString input) {
auto cmdStr = parts.first();
auto cmd = toCommandType(cmdStr);
- auto rawArgs = input.slice(cmdStr.length());
-
- parts.removeFirst();
+ auto rawArgs = input.slice(cmdStr.length()).trimmed();
return {.command = cmd, .argsString = rawArgs, .rawInput = input};
}
diff --git a/src/completion/CommandsAdapter.cpp b/src/completion/CommandsAdapter.cpp
new file mode 100644
index 0000000..df71661
--- /dev/null
+++ b/src/completion/CommandsAdapter.cpp
@@ -0,0 +1,22 @@
+#include <QWidget>
+#include <QtCore>
+
+#include "completion/CommandsAdapter.hpp"
+#include "completion/CommandsModel.hpp"
+#include "completion/Completer.hpp"
+
+const QString cmdPrompt = "[exec]";
+
+CommandsAdapter::CommandsAdapter() : Adapter() {
+ completerInstance = new Completer(this);
+ completerInstance->setModel(new CommandsModel(this));
+}
+
+Completer *CommandsAdapter::completer() { return completerInstance; }
+
+QString CommandsAdapter::prompt() { return cmdPrompt; }
+
+CommandsAdapter::~CommandsAdapter() {
+ delete completerInstance->model();
+ delete completerInstance;
+}
diff --git a/src/completion/Completer.cpp b/src/completion/Completer.cpp
index 96a363e..a38d3f2 100644
--- a/src/completion/Completer.cpp
+++ b/src/completion/Completer.cpp
@@ -27,9 +27,7 @@ Completer::Completer(QObject *parent) : QCompleter(parent) {
treeView->setRootIsDecorated(false);
treeView->setUniformRowHeights(true);
treeView->header()->hide();
- treeView->setColumnWidth(1, 160);
-
- complete();
+ treeView->setColumnWidth(1, 300);
}
// bool Completer::eventFilter(QObject *watched, QEvent *event) {
diff --git a/src/completion/UrlAdapter.cpp b/src/completion/UrlAdapter.cpp
new file mode 100644
index 0000000..2f1bb08
--- /dev/null
+++ b/src/completion/UrlAdapter.cpp
@@ -0,0 +1,20 @@
+#include <QWidget>
+#include <QtCore>
+
+#include "completion/Completer.hpp"
+#include "completion/UrlAdapter.hpp"
+#include "completion/UrlModel.hpp"
+
+UrlAdapter::UrlAdapter() : Adapter() {
+ completerInstance = new Completer(this);
+ completerInstance->setModel(new UrlModel(this));
+}
+
+Completer *UrlAdapter::completer() { return completerInstance; }
+
+QString UrlAdapter::prompt() { return "[url]"; }
+
+UrlAdapter::~UrlAdapter() {
+ delete completerInstance->model();
+ delete completerInstance;
+}
diff --git a/src/completion/UrlModel.cpp b/src/completion/UrlModel.cpp
new file mode 100644
index 0000000..7671fc3
--- /dev/null
+++ b/src/completion/UrlModel.cpp
@@ -0,0 +1,48 @@
+#include <QAbstractListModel>
+#include <QWidget>
+#include <QtCore>
+
+#include "completion/UrlModel.hpp"
+
+UrlModel::UrlModel(QObject *parent) : QAbstractListModel(parent) {
+ items = {
+ {.url = "https://google.com", .description = "Google"},
+ {.url = "https://duckduckgo.com", .description = "DuckDuckGo"},
+ {.url = "https://ediblemonad.dev", .description = "EdibleMonad website"},
+ {.url = "https://lite.duckduckgo.com", .description = "DDG lite"},
+ {.url = "https://github.com/trending", .description = "Github trending"},
+ };
+}
+
+QVariant UrlModel::data(const QModelIndex &index, int role) const {
+ if (!index.isValid() || index.row() >= items.size())
+ return QVariant();
+
+ const Url &item = items.at(index.row());
+
+ if (role == Qt::DisplayRole)
+ return item.url;
+ else if (role == Qt::ToolTipRole)
+ return item.description;
+ else if (role == Qt::UserRole)
+ return item.description;
+
+ return QVariant();
+}
+
+QHash<int, QByteArray> UrlModel::roleNames() const {
+ QHash<int, QByteArray> roles;
+ roles[Qt::DisplayRole] = "url";
+ roles[Qt::UserRole] = "description";
+ return roles;
+}
+
+int UrlModel::rowCount(const QModelIndex &parent) const {
+ Q_UNUSED(parent);
+ return items.size();
+}
+
+int UrlModel::columnCount(const QModelIndex &parent) const {
+ Q_UNUSED(parent);
+ return 2; // name + description
+}
diff --git a/src/widgets/CommandInput.cpp b/src/widgets/CommandInput.cpp
deleted file mode 100644
index 3ea0f38..0000000
--- a/src/widgets/CommandInput.cpp
+++ /dev/null
@@ -1,84 +0,0 @@
-#include <QCompleter>
-#include <QHBoxLayout>
-#include <QKeyEvent>
-#include <QLabel>
-#include <QLineEdit>
-#include <QStringListModel>
-#include <QVBoxLayout>
-#include <QWidget>
-#include <QWidgetAction>
-#include <QWindow>
-#include <QtCore/qnamespace.h>
-#include <QtCore/qstringlistmodel.h>
-#include <QtWidgets/qboxlayout.h>
-
-#include "completion/CommandsModel.hpp"
-#include "completion/Completer.hpp"
-#include "widgets/CommandInput.hpp"
-
-const char *rootStyles = R"(
- background-color: #000;
- color: #fff;
- border-radius: 0;
-)";
-
-CommandInput::CommandInput(QString defaultInput, QWidget *parentNode)
- : QWidget(parentNode) {
- setContentsMargins(0, 0, 0, 0);
- setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
- setStyleSheet(rootStyles);
-
- auto layout = new QVBoxLayout(this);
- layout->setContentsMargins(0, 0, 0, 0);
-
- auto listModel = new CommandsModel();
- auto completer = new Completer(this);
- completer->setModel(listModel);
-
- auto cmdLineBox = new QWidget();
- layout->addWidget(cmdLineBox);
- auto cmdLineLayout = new QHBoxLayout();
- cmdLineBox->setLayout(cmdLineLayout);
- cmdLineBox->layout()->setContentsMargins(0, 0, 0, 0);
- cmdLineLayout->setSpacing(0);
-
- auto promptPrefix = new QLabel(tr(":"));
- promptPrefix->setContentsMargins(0, 0, 0, 0);
- input = new QLineEdit(defaultInput);
- input->setFocusPolicy(Qt::StrongFocus);
- input->setCompleter(completer);
- // input->installEventFilter(completer);
-
- cmdLineLayout->addWidget(promptPrefix);
- cmdLineLayout->addWidget(input);
-
- setFixedHeight(input->sizeHint().height());
-}
-
-void CommandInput::emitSubmit() { emit submitted(getInputCommand()); }
-
-void CommandInput::keyPressEvent(QKeyEvent *event) {
- auto combo = event->keyCombination();
- auto ctrlL = combo.key() == Qt::Key_L &&
- combo.keyboardModifiers().testFlag(Qt::ControlModifier);
- auto esc = combo.key() == Qt::Key_Escape;
- auto enter = combo.key() == Qt::Key_Return || combo.key() == Qt::Key_Enter;
-
- if (esc || ctrlL)
- emit cancelled();
- else if (enter)
- emitSubmit();
-}
-
-void CommandInput::setInputText(QString text) { input->setText(text); }
-
-bool CommandInput::isInputFocussed() { return input->hasFocus(); }
-
-void CommandInput::setInputFocus(bool focussed) {
- if (focussed)
- input->setFocus(Qt::PopupFocusReason);
- else
- input->clearFocus();
-}
-
-QString CommandInput::getInputCommand() { return input->text(); }
diff --git a/src/widgets/InputLine.cpp b/src/widgets/InputLine.cpp
new file mode 100644
index 0000000..d577e1d
--- /dev/null
+++ b/src/widgets/InputLine.cpp
@@ -0,0 +1,91 @@
+#include <QBoxLayout>
+#include <QCompleter>
+#include <QHBoxLayout>
+#include <QKeyEvent>
+#include <QLabel>
+#include <QLineEdit>
+#include <QStringListModel>
+#include <QWidget>
+#include <QWidgetAction>
+#include <QWindow>
+#include <QtCore>
+
+#include "completion/CommandsAdapter.hpp"
+#include "widgets/InputLine.hpp"
+
+const char *rootStyles = R"(
+ background-color: #000;
+ color: #fff;
+ border-radius: 0;
+)";
+
+const char *promptStyles = R"(
+ background-color: #aaa;
+ color: #555;
+ padding: 0 2px;
+)";
+
+InputLine::InputLine(QString defaultInput, QWidget *parentNode)
+ : QWidget(parentNode) {
+ setContentsMargins(0, 0, 0, 0);
+ setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
+ setStyleSheet(rootStyles);
+
+ auto layout = new QVBoxLayout(this);
+ layout->setContentsMargins(0, 0, 0, 0);
+
+ auto cmdLineBox = new QWidget();
+ layout->addWidget(cmdLineBox);
+ auto cmdLineLayout = new QHBoxLayout();
+ cmdLineBox->setLayout(cmdLineLayout);
+ cmdLineBox->layout()->setContentsMargins(0, 0, 0, 0);
+ cmdLineLayout->setSpacing(0);
+
+ promptPrefix = new QLabel();
+ promptPrefix->setStyleSheet(promptStyles);
+ promptPrefix->setContentsMargins(0, 0, 0, 0);
+ input = new QLineEdit(defaultInput);
+ input->setFocusPolicy(Qt::StrongFocus);
+
+ cmdLineLayout->addWidget(promptPrefix);
+ cmdLineLayout->addWidget(input);
+ setFixedHeight(input->sizeHint().height());
+
+ setAdapter(new CommandsAdapter());
+}
+
+void InputLine::setAdapter(Adapter *adapter) {
+ if (this->adapter)
+ delete this->adapter;
+ this->adapter = adapter;
+ promptPrefix->setText(adapter->prompt());
+ input->setCompleter(adapter->completer());
+}
+
+void InputLine::emitSubmit() { emit submitted(getInputCommand()); }
+
+void InputLine::keyPressEvent(QKeyEvent *event) {
+ auto combo = event->keyCombination();
+ auto ctrlL = combo.key() == Qt::Key_L &&
+ combo.keyboardModifiers().testFlag(Qt::ControlModifier);
+ auto esc = combo.key() == Qt::Key_Escape;
+ auto enter = combo.key() == Qt::Key_Return || combo.key() == Qt::Key_Enter;
+
+ if (esc || ctrlL)
+ emit cancelled();
+ else if (enter)
+ emitSubmit();
+}
+
+void InputLine::setInputText(QString text) { input->setText(text); }
+
+bool InputLine::isInputFocussed() { return input->hasFocus(); }
+
+void InputLine::setInputFocus(bool focussed) {
+ if (focussed)
+ input->setFocus(Qt::PopupFocusReason);
+ else
+ input->clearFocus();
+}
+
+QString InputLine::getInputCommand() { return input->text(); }
diff --git a/src/widgets/MainWindow.cpp b/src/widgets/MainWindow.cpp
index 3191719..03a4e6e 100644
--- a/src/widgets/MainWindow.cpp
+++ b/src/widgets/MainWindow.cpp
@@ -2,10 +2,13 @@
#include <QStackedLayout>
#include <QVBoxLayout>
#include <QWebEngineView>
+#include <QtCore/qnamespace.h>
#include "CommandParser.hpp"
+#include "completion/CommandsAdapter.hpp"
+#include "completion/UrlAdapter.hpp"
#include "widgets/BrowserManager.hpp"
-#include "widgets/CommandInput.hpp"
+#include "widgets/InputLine.hpp"
#include "widgets/MainWindow.hpp"
MainWindow::MainWindow() {
@@ -24,14 +27,12 @@ MainWindow::MainWindow() {
layout->addWidget(browserManager);
// Command input
- commandInput = new CommandInput;
- hideURLInput();
- commandInput->move(0, 0);
- connect(commandInput, &CommandInput::submitted, this,
- &MainWindow::evaluateCommand);
- connect(commandInput, &CommandInput::cancelled, this,
- &MainWindow::hideURLInput);
- layout->addWidget(commandInput);
+ inputLine = new InputLine;
+ inputLine->setHidden(true);
+ inputLine->move(0, 0);
+ connect(inputLine, &InputLine::submitted, this, &MainWindow::evaluateCommand);
+ connect(inputLine, &InputLine::cancelled, this, &MainWindow::hideInputLine);
+ layout->addWidget(inputLine);
// Lua runtime
luaRuntime = LuaRuntime::instance();
@@ -41,20 +42,31 @@ MainWindow::MainWindow() {
});
}
-void MainWindow::hideURLInput() {
- commandInput->setInputFocus(false);
- commandInput->setHidden(true);
+void MainWindow::hideInputLine() {
+ inputLine->setInputFocus(false);
+ inputLine->setHidden(true);
}
-void MainWindow::showURLInput() {
- commandInput->setInputText("open " + browserManager->currentUrl().toString());
- commandInput->setHidden(false);
- layout->setCurrentWidget(commandInput);
- commandInput->setInputFocus(true);
+void MainWindow::showInputLine() {
+ inputLine->setHidden(false);
+ layout->setCurrentWidget(inputLine);
+ inputLine->setInputFocus(true);
+}
+
+void MainWindow::showURLInput(QString url) {
+ showInputLine();
+ inputLine->setAdapter(new UrlAdapter);
+ inputLine->setInputText(url);
+}
+
+void MainWindow::showCommandInput(QString cmd) {
+ showInputLine();
+ inputLine->setAdapter(new CommandsAdapter);
+ inputLine->setInputText(cmd);
}
void MainWindow::evaluateCommand(QString command) {
- hideURLInput();
+ hideInputLine();
CommandParser parser;
auto cmd = parser.parse(command);
@@ -64,7 +76,11 @@ void MainWindow::evaluateCommand(QString command) {
luaRuntime->evaluate(cmd.argsString);
break;
case CommandType::Open:
- browserManager->openUrl(cmd.argsString, OpenType::OpenUrl);
+ if (cmd.argsString.trimmed().isEmpty()) {
+ showURLInput();
+ } else {
+ browserManager->openUrl(cmd.argsString, OpenType::OpenUrl);
+ }
break;
case CommandType::TabOpen:
browserManager->openUrl(cmd.argsString, OpenType::OpenUrlInTab);
@@ -84,7 +100,10 @@ void MainWindow::keyPressEvent(QKeyEvent *event) {
auto combo = event->keyCombination();
if (combo.key() == Qt::Key_L &&
combo.keyboardModifiers().testFlag(Qt::ControlModifier)) {
- toggleURLInput();
+ showURLInput(browserManager->currentUrl().toString());
+ } else if (combo.key() == Qt::Key_Semicolon &&
+ combo.keyboardModifiers().testFlag(Qt::ControlModifier)) {
+ showCommandInput();
} else if (combo.key() == Qt::Key_T &&
combo.keyboardModifiers().testFlag(Qt::ControlModifier)) {
browserManager->createNewWebView(QUrl("https://lite.duckduckgo.com"), true);
@@ -99,7 +118,3 @@ void MainWindow::keyPressEvent(QKeyEvent *event) {
browserManager->closeCurrentWebView();
}
}
-
-void MainWindow::toggleURLInput() {
- commandInput->isHidden() ? showURLInput() : hideURLInput();
-}