diff options
| author | Akshay Nair <phenax5@gmail.com> | 2025-03-11 11:31:14 +0530 |
|---|---|---|
| committer | Akshay Nair <phenax5@gmail.com> | 2025-03-11 11:44:31 +0530 |
| commit | 12d0ce37db7323e8efd56d6a698a98abcb8d680b (patch) | |
| tree | ec2102d40326a332b2ecb976869585f81cba8574 /src/widgets | |
| parent | 971854bfefad4c644ac3d5f5d6003f24d551bf1d (diff) | |
| download | null-browser-12d0ce37db7323e8efd56d6a698a98abcb8d680b.tar.gz null-browser-12d0ce37db7323e8efd56d6a698a98abcb8d680b.zip | |
Add command+url adapter and switching
Diffstat (limited to 'src/widgets')
| -rw-r--r-- | src/widgets/InputLine.cpp (renamed from src/widgets/CommandInput.cpp) | 51 | ||||
| -rw-r--r-- | src/widgets/MainWindow.cpp | 63 |
2 files changed, 68 insertions, 46 deletions
diff --git a/src/widgets/CommandInput.cpp b/src/widgets/InputLine.cpp index 3ea0f38..d577e1d 100644 --- a/src/widgets/CommandInput.cpp +++ b/src/widgets/InputLine.cpp @@ -1,20 +1,17 @@ +#include <QBoxLayout> #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 <QtCore> -#include "completion/CommandsModel.hpp" -#include "completion/Completer.hpp" -#include "widgets/CommandInput.hpp" +#include "completion/CommandsAdapter.hpp" +#include "widgets/InputLine.hpp" const char *rootStyles = R"( background-color: #000; @@ -22,7 +19,13 @@ const char *rootStyles = R"( border-radius: 0; )"; -CommandInput::CommandInput(QString defaultInput, QWidget *parentNode) +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); @@ -31,10 +34,6 @@ CommandInput::CommandInput(QString defaultInput, QWidget *parentNode) 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(); @@ -42,22 +41,30 @@ CommandInput::CommandInput(QString defaultInput, QWidget *parentNode) cmdLineBox->layout()->setContentsMargins(0, 0, 0, 0); cmdLineLayout->setSpacing(0); - auto promptPrefix = new QLabel(tr(":")); + promptPrefix = new QLabel(); + promptPrefix->setStyleSheet(promptStyles); 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()); + + 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 CommandInput::emitSubmit() { emit submitted(getInputCommand()); } +void InputLine::emitSubmit() { emit submitted(getInputCommand()); } -void CommandInput::keyPressEvent(QKeyEvent *event) { +void InputLine::keyPressEvent(QKeyEvent *event) { auto combo = event->keyCombination(); auto ctrlL = combo.key() == Qt::Key_L && combo.keyboardModifiers().testFlag(Qt::ControlModifier); @@ -70,15 +77,15 @@ void CommandInput::keyPressEvent(QKeyEvent *event) { emitSubmit(); } -void CommandInput::setInputText(QString text) { input->setText(text); } +void InputLine::setInputText(QString text) { input->setText(text); } -bool CommandInput::isInputFocussed() { return input->hasFocus(); } +bool InputLine::isInputFocussed() { return input->hasFocus(); } -void CommandInput::setInputFocus(bool focussed) { +void InputLine::setInputFocus(bool focussed) { if (focussed) input->setFocus(Qt::PopupFocusReason); else input->clearFocus(); } -QString CommandInput::getInputCommand() { return input->text(); } +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(); -} |
