aboutsummaryrefslogtreecommitdiff
path: root/src/widgets/MainWindow.cpp
blob: 03a4e6e0fd342311b6fe2ff67fbfcedfe65f642c (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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include <QKeyEvent>
#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/InputLine.hpp"
#include "widgets/MainWindow.hpp"

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

  // Root stacked layout
  layout = new QStackedLayout();
  layout->setContentsMargins(0, 0, 0, 0);
  layout->setSpacing(0);
  layout->setStackingMode(QStackedLayout::StackAll);
  centralWidget()->setLayout(layout);

  // Web engine
  browserManager = new BrowserManager(new QWebEngineProfile("web-browser"));
  layout->addWidget(browserManager);

  // Command input
  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();
  connect(luaRuntime, &LuaRuntime::urlOpenned, this,
          [this](QString url, OpenType openType) {
            browserManager->openUrl(QUrl(url), openType);
          });
}

void MainWindow::hideInputLine() {
  inputLine->setInputFocus(false);
  inputLine->setHidden(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) {
  hideInputLine();

  CommandParser parser;
  auto cmd = parser.parse(command);

  switch (cmd.command) {
  case CommandType::LuaEval:
    luaRuntime->evaluate(cmd.argsString);
    break;
  case CommandType::Open:
    if (cmd.argsString.trimmed().isEmpty()) {
      showURLInput();
    } else {
      browserManager->openUrl(cmd.argsString, OpenType::OpenUrl);
    }
    break;
  case CommandType::TabOpen:
    browserManager->openUrl(cmd.argsString, OpenType::OpenUrlInTab);
    break;
  case CommandType::TabNext:
    browserManager->nextWebView();
    break;
  case CommandType::TabPrev:
    browserManager->previousWebView();
    break;
  case CommandType::Noop:
    break;
  }
}

void MainWindow::keyPressEvent(QKeyEvent *event) {
  auto combo = event->keyCombination();
  if (combo.key() == Qt::Key_L &&
      combo.keyboardModifiers().testFlag(Qt::ControlModifier)) {
    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);
  } else if (combo.key() == Qt::Key_J &&
             combo.keyboardModifiers().testFlag(Qt::ControlModifier)) {
    browserManager->nextWebView();
  } else if (combo.key() == Qt::Key_K &&
             combo.keyboardModifiers().testFlag(Qt::ControlModifier)) {
    browserManager->previousWebView();
  } else if (combo.key() == Qt::Key_W &&
             combo.keyboardModifiers().testFlag(Qt::ControlModifier)) {
    browserManager->closeCurrentWebView();
  }
}