From 319908623e47b4e73f9af4aa4369cb895bd8df8f Mon Sep 17 00:00:00 2001 From: Akshay Nair Date: Sun, 9 Mar 2025 00:15:18 +0530 Subject: Fix command input ui to fix to top --- include/CommandInput.hpp | 5 +++- include/MainWindow.hpp | 3 ++- spec/CommandInputSpec.cpp | 39 ++++++++++++++++++++++++++--- src/CommandInput.cpp | 28 +++++++++++++-------- src/MainWindow.cpp | 50 +++++++++++++++++++++++++++----------- src/main.cpp | 1 + src/play.cpp.old | 62 ----------------------------------------------- 7 files changed, 97 insertions(+), 91 deletions(-) delete mode 100644 src/play.cpp.old diff --git a/include/CommandInput.hpp b/include/CommandInput.hpp index 5cebcd1..a6ca0ed 100644 --- a/include/CommandInput.hpp +++ b/include/CommandInput.hpp @@ -4,7 +4,7 @@ #include #include -class CommandInput : public QFrame { +class CommandInput : public QWidget { Q_OBJECT public: @@ -12,12 +12,15 @@ public: void setInputFocus(bool focussed); bool isInputFocussed(); QString getInputCommand(); + void setInputText(QString text); signals: void submitted(QString command); + void cancelled(); protected: void keyPressEvent(QKeyEvent *event) override; + void emitSubmit(); private: QBoxLayout *layout; diff --git a/include/MainWindow.hpp b/include/MainWindow.hpp index 20e07c0..552aa25 100644 --- a/include/MainWindow.hpp +++ b/include/MainWindow.hpp @@ -14,8 +14,9 @@ protected: void keyPressEvent(QKeyEvent *event) override; void toggleURLInput(); void evaluateCommand(QString command); + void hideInput(); private: QWebEngineView *web; - CommandInput *urlInputUI; + CommandInput *commandInput; }; diff --git a/spec/CommandInputSpec.cpp b/spec/CommandInputSpec.cpp index 7d15f25..f6f7f3a 100644 --- a/spec/CommandInputSpec.cpp +++ b/spec/CommandInputSpec.cpp @@ -26,24 +26,57 @@ private slots: QCOMPARE(inputWidget.getInputCommand(), QString("Initial content updated")); } + + context("when setInputText is called"); + it("sets input command") { + CommandInput inputWidget("Initial content", new QWidget()); + + inputWidget.setInputText("New content"); + + QCOMPARE(inputWidget.getInputCommand(), QString("New content")); + } } void testSubmitSignalOnReturnKey() { context("when user hits return key"); it("emits the submitted signal with the input command") { CommandInput inputWidget("Initial content", new QWidget()); - QSignalSpy spy(&inputWidget, &CommandInput::submitted); + QSignalSpy submitSignal(&inputWidget, &CommandInput::submitted); auto input = inputWidget.findChild(); QTest::keyClicks(input, " updated"); QTest::keyClick(&inputWidget, Qt::Key_Return); - QCOMPARE(spy.count(), 1); - QCOMPARE(spy.takeFirst().at(0).toString(), + QCOMPARE(submitSignal.count(), 1); + QCOMPARE(submitSignal.takeFirst().at(0).toString(), QString("Initial content updated")); } } + void testCancelSignalOnEscapeKey() { + context("when user hits escape key"); + it("emits 'cancelled' signal") { + CommandInput inputWidget("Initial content", new QWidget()); + QSignalSpy cancelSignal(&inputWidget, &CommandInput::cancelled); + + auto input = inputWidget.findChild(); + QTest::keyClick(&inputWidget, Qt::Key_Escape); + + QCOMPARE(cancelSignal.count(), 1); + } + + context("when user hits ctrl+l key"); + it("emits 'cancelled' signal") { + CommandInput inputWidget("Initial content", new QWidget()); + QSignalSpy cancelSignal(&inputWidget, &CommandInput::cancelled); + + auto input = inputWidget.findChild(); + QTest::keyClick(&inputWidget, Qt::Key_L, Qt::ControlModifier); + + QCOMPARE(cancelSignal.count(), 1); + } + } + void testSetFocus() { context("when setInputFocus is called with true"); xit("focusses input field") { diff --git a/src/CommandInput.cpp b/src/CommandInput.cpp index cf7346c..d553d19 100644 --- a/src/CommandInput.cpp +++ b/src/CommandInput.cpp @@ -1,3 +1,4 @@ +#include #include #include #include @@ -8,29 +9,36 @@ #include "CommandInput.hpp" CommandInput::CommandInput(QString defaultInput, QWidget *parentNode) - : QFrame(parentNode) { + : QWidget(parentNode) { setContentsMargins(0, 0, 0, 0); - // setFrameRect(QRect(0, 0, parentWidget()->width(), 50)); - setFixedWidth(parentWidget()->width()); - setFrameShadow(QFrame::Shadow::Raised); - setFrameShape(QFrame::Box); - setStyleSheet("background-color: #333; color: #fff;"); + setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed); + setStyleSheet("background-color: #000; color: #fff; border-radius: 0;"); auto layout = new QVBoxLayout(this); layout->setContentsMargins(0, 0, 0, 0); input = new QLineEdit(defaultInput, this); - input->setContentsMargins(0, 0, 0, 0); input->setFocusPolicy(Qt::StrongFocus); layout->addWidget(input); + setFixedHeight(input->sizeHint().height()); } +void CommandInput::emitSubmit() { emit submitted(getInputCommand()); } + void CommandInput::keyPressEvent(QKeyEvent *event) { auto combo = event->keyCombination(); - if (combo.key() == Qt::Key_Return) { - emit submitted(getInputCommand()); - } + 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) { diff --git a/src/MainWindow.cpp b/src/MainWindow.cpp index 660d731..809e5de 100644 --- a/src/MainWindow.cpp +++ b/src/MainWindow.cpp @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -9,23 +10,41 @@ MainWindow::MainWindow() { setStyleSheet("background-color: #000; color: #fff;"); - // layout = new QVBoxLayout(this); - // layout->setContentsMargins(0, 0, 0, 0); - web = new QWebEngineView(this); - web->setGeometry(200, 10, 500, 500); + auto centralWidget = new QWidget(); + setCentralWidget(centralWidget); + + auto layout = new QStackedLayout(); + layout->setContentsMargins(0, 0, 0, 0); + layout->setSpacing(0); + centralWidget->setLayout(layout); + layout->setStackingMode(QStackedLayout::StackAll); + + // Webengine + web = new QWebEngineView(); web->setUrl(QUrl("https://ediblemonad.dev")); + layout->addWidget(web); - urlInputUI = new CommandInput(web->url().toString(), this); - urlInputUI->setHidden(true); - connect(urlInputUI, &CommandInput::submitted, this, + // Command input + commandInput = new CommandInput(web->url().toString()); + hideInput(); + commandInput->move(0, 0); + connect(commandInput, &CommandInput::submitted, this, &MainWindow::evaluateCommand); + connect(commandInput, &CommandInput::cancelled, this, &MainWindow::hideInput); + + layout->addWidget(commandInput); +} + +void MainWindow::hideInput() { + commandInput->setInputFocus(false); + commandInput->setHidden(true); } void MainWindow::evaluateCommand(QString command) { - web->setUrl(command); - urlInputUI->setInputFocus(false); - urlInputUI->setHidden(true); + if (!command.isEmpty()) + web->setUrl(command); + hideInput(); } void MainWindow::keyPressEvent(QKeyEvent *event) { @@ -37,8 +56,11 @@ void MainWindow::keyPressEvent(QKeyEvent *event) { } void MainWindow::toggleURLInput() { - auto hidden = urlInputUI->isHidden(); - auto shouldShow = hidden; - urlInputUI->setHidden(!shouldShow); - urlInputUI->setInputFocus(shouldShow); + auto shouldShow = commandInput->isHidden(); + if (shouldShow) { + commandInput->setInputText(web->url().toString()); + commandInput->raise(); + } + commandInput->setHidden(!shouldShow); + commandInput->setInputFocus(shouldShow); } diff --git a/src/main.cpp b/src/main.cpp index 4f23e38..91e605e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -5,6 +5,7 @@ #include "MainWindow.hpp" int main(int argc, char *argv[]) { + // QCoreApplication::setAttribute(Qt::AA_UseDesktopOpenGL); QApplication app(argc, argv); MainWindow mainWindow; diff --git a/src/play.cpp.old b/src/play.cpp.old deleted file mode 100644 index 4a61043..0000000 --- a/src/play.cpp.old +++ /dev/null @@ -1,62 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include - -class CEFWidget : public QWidget { -public: - explicit CEFWidget(QWidget *parent = nullptr) : QWidget(parent) { - setFixedSize(400, 400); - - // TODO: Args - char *argv[] = {}; - int argc = 0; - - std::cout << "ficlk" << std::endl; - - // Create browser - CefWindowInfo window_info; - CefBrowserSettings browser_settings; - window_info.SetAsChild(winId(), {0, 0, 400, 400}); - CefBrowserHost::CreateBrowser(window_info, nullptr, "https://www.qt.io", - browser_settings, nullptr, nullptr); - } - - ~CEFWidget() { CefShutdown(); } -}; - -int main(int argc, char *argv[]) { - CefMainArgs main_args(argc, argv); - CefRefPtr cefApp; - int exit_code = CefExecuteProcess(main_args, cefApp, nullptr); - if (exit_code >= 0) { - return exit_code; - } - - CefMainArgs args(argc, argv); - CefSettings settings; - CefString(&settings.resources_dir_path) - .FromWString(L"/home/imsohexy/dev/projects/web-browser/build/lib/"); - CefString(&settings.locales_dir_path) - .FromWString( - L"/home/imsohexy/dev/projects/web-browser/build/lib/locales/"); - CefString(&settings.root_cache_path) - .FromWString(L"/home/imsohexy/dev/projects/web-browser/.cache/cef"); - settings.no_sandbox = true; - settings.pack_loading_disabled = true; - CefInitialize(args, settings, nullptr, nullptr); - - QApplication app(argc, argv); - QWidget mainWindow; - mainWindow.setWindowTitle("web-browser"); - QVBoxLayout layout(&mainWindow); - // CEFWidget cefWidget(&mainWindow); - // layout.addWidget(&cefWidget); - mainWindow.show(); - return app.exec(); -} -- cgit v1.3.1