diff options
| author | Akshay Nair <phenax5@gmail.com> | 2025-03-09 19:47:50 +0530 |
|---|---|---|
| committer | Akshay Nair <phenax5@gmail.com> | 2025-03-09 19:47:50 +0530 |
| commit | 61f99089288138989cf244d174882aa5088b738b (patch) | |
| tree | ea1202b098b92e85b16e670d1c3ea1b86fcaef58 /src/widgets/CommandInput.cpp | |
| parent | 1e2302be2b47fbeb340db7482ed8476c829a7f66 (diff) | |
| download | null-browser-61f99089288138989cf244d174882aa5088b738b.tar.gz null-browser-61f99089288138989cf244d174882aa5088b738b.zip | |
Refactor directory structure
Diffstat (limited to 'src/widgets/CommandInput.cpp')
| -rw-r--r-- | src/widgets/CommandInput.cpp | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/src/widgets/CommandInput.cpp b/src/widgets/CommandInput.cpp new file mode 100644 index 0000000..464065a --- /dev/null +++ b/src/widgets/CommandInput.cpp @@ -0,0 +1,50 @@ +#include <QHBoxLayout> +#include <QKeyEvent> +#include <QLineEdit> +#include <QVBoxLayout> +#include <QWidget> +#include <QWindow> + +#include "widgets/CommandInput.hpp" + +CommandInput::CommandInput(QString defaultInput, QWidget *parentNode) + : QWidget(parentNode) { + setContentsMargins(0, 0, 0, 0); + 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->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(); + 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(); } |
