aboutsummaryrefslogtreecommitdiff
path: root/src/MainWindow.cpp
blob: 809e5de05909b414e09bbf4b857ab8486f52c27d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <QKeyEvent>
#include <QStackedLayout>
#include <QVBoxLayout>
#include <QWebEngineView>
#include <iostream>
#include <lua.hpp>

#include "CommandInput.hpp"
#include "MainWindow.hpp"

MainWindow::MainWindow() {
  setStyleSheet("background-color: #000; color: #fff;");

  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);

  // 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) {
  if (!command.isEmpty())
    web->setUrl(command);
  hideInput();
}

void MainWindow::keyPressEvent(QKeyEvent *event) {
  auto combo = event->keyCombination();
  if (combo.key() == Qt::Key_L &&
      combo.keyboardModifiers().testFlag(Qt::ControlModifier)) {
    toggleURLInput();
  }
}

void MainWindow::toggleURLInput() {
  auto shouldShow = commandInput->isHidden();
  if (shouldShow) {
    commandInput->setInputText(web->url().toString());
    commandInput->raise();
  }
  commandInput->setHidden(!shouldShow);
  commandInput->setInputFocus(shouldShow);
}