aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/BrowserManager.cpp124
-rw-r--r--src/MainWindow.cpp58
2 files changed, 158 insertions, 24 deletions
diff --git a/src/BrowserManager.cpp b/src/BrowserManager.cpp
new file mode 100644
index 0000000..ddc6105
--- /dev/null
+++ b/src/BrowserManager.cpp
@@ -0,0 +1,124 @@
+#include <QStackedLayout>
+#include <QWebEngineNewWindowRequest>
+#include <QWebEngineView>
+
+#include "BrowserManager.hpp"
+
+BrowserManager::BrowserManager(QWebEngineProfile *profile) : QWidget() {
+ setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
+
+ layout = new QStackedLayout(this);
+ layout->setStackingMode(QStackedLayout::StackAll);
+ layout->setContentsMargins(0, 0, 0, 0);
+
+ this->profile = profile;
+
+ createNewWebView(BrowserManager::NewtabURL, true);
+}
+
+QWebEngineView *BrowserManager::createNewWebView(QUrl url, bool focus) {
+ auto webview = new QWebEngineView(profile);
+ webview->setUrl(url);
+ layout->addWidget(webview);
+ webViewList.append(webview);
+
+ connect(webview->page(), &QWebEnginePage::newWindowRequested, this,
+ &BrowserManager::onNewWebViewRequest);
+
+ if (focus)
+ focusWebView(webViewList.length() - 1);
+
+ return webview;
+}
+
+void BrowserManager::onNewWebViewRequest(QWebEngineNewWindowRequest &request) {
+ switch (request.destination()) {
+ case QWebEngineNewWindowRequest::InNewTab:
+ createNewWebView(request.requestedUrl(), true);
+ break;
+ case QWebEngineNewWindowRequest::InNewBackgroundTab:
+ createNewWebView(request.requestedUrl(), false);
+ break;
+ case QWebEngineNewWindowRequest::InNewWindow:
+ // TODO: Impl
+ createNewWebView(request.requestedUrl(), true);
+ break;
+ case QWebEngineNewWindowRequest::InNewDialog:
+ // TODO: Impl
+ createNewWebView(request.requestedUrl(), true);
+ break;
+ }
+}
+
+void BrowserManager::nextWebView() {
+ if (webViewList.isEmpty())
+ return;
+ auto index = currentWebViewIndex() + 1;
+ auto total = webViewList.length();
+ index = index >= total ? index % total : index;
+ focusWebView(index);
+}
+
+void BrowserManager::previousWebView() {
+ if (webViewList.isEmpty())
+ return;
+ auto index = currentWebViewIndex() - 1;
+ auto total = webViewList.length();
+ index = index < 0 ? total + index : index;
+ focusWebView(index);
+}
+
+void BrowserManager::closeCurrentWebView() {
+ closeWebView(currentWebViewIndex());
+}
+
+void BrowserManager::closeWebView(long index) {
+ if (index < 0 || index >= webViewList.length())
+ return;
+
+ auto webview = webViewList.at(index);
+ layout->removeWidget(webview);
+ webViewList.removeAt(index);
+ disconnect(webview->page());
+ webview->deleteLater();
+
+ focusWebView(currentWebViewIndex());
+
+ if (webViewList.isEmpty()) {
+ createNewWebView(BrowserManager::NewtabURL, true);
+ }
+}
+
+std::vector<QUrl> BrowserManager::webViewUrls() {
+ std::vector<QUrl> urls;
+ for (auto &view : webViewList)
+ urls.push_back(view->url());
+ return urls;
+}
+
+u_int32_t BrowserManager::currentWebViewIndex() {
+ return layout->currentIndex();
+}
+u_int32_t BrowserManager::webViewCount() { return webViewList.length(); }
+
+void BrowserManager::focusWebView(long index) {
+ if (webViewList.isEmpty())
+ return;
+
+ index = std::max((long)0, std::min(index, (long)webViewList.length() - 1));
+ layout->setCurrentIndex(index);
+}
+
+QUrl BrowserManager::currentUrl() {
+ if (currentWebViewIndex() >= webViewList.length())
+ return QUrl("about:newtab");
+
+ return webViewList.at(currentWebViewIndex())->url();
+}
+
+void BrowserManager::setCurrentUrl(QUrl url) {
+ if (currentWebViewIndex() >= webViewList.length())
+ return;
+
+ webViewList.at(currentWebViewIndex())->setUrl(url);
+}
diff --git a/src/MainWindow.cpp b/src/MainWindow.cpp
index 809e5de..ee8d128 100644
--- a/src/MainWindow.cpp
+++ b/src/MainWindow.cpp
@@ -2,49 +2,53 @@
#include <QStackedLayout>
#include <QVBoxLayout>
#include <QWebEngineView>
-#include <iostream>
-#include <lua.hpp>
+#include "BrowserManager.hpp"
#include "CommandInput.hpp"
#include "MainWindow.hpp"
MainWindow::MainWindow() {
setStyleSheet("background-color: #000; color: #fff;");
+ setCentralWidget(new QWidget());
- auto centralWidget = new QWidget();
- setCentralWidget(centralWidget);
-
+ // Root stacked layout
auto layout = new QStackedLayout();
layout->setContentsMargins(0, 0, 0, 0);
layout->setSpacing(0);
- centralWidget->setLayout(layout);
layout->setStackingMode(QStackedLayout::StackAll);
+ centralWidget()->setLayout(layout);
- // Webengine
- web = new QWebEngineView();
- web->setUrl(QUrl("https://ediblemonad.dev"));
- layout->addWidget(web);
+ // Web engine
+ browserManager = new BrowserManager(new QWebEngineProfile("web-browser"));
+ layout->addWidget(browserManager);
// Command input
- commandInput = new CommandInput(web->url().toString());
- hideInput();
+ commandInput = new CommandInput(browserManager->currentUrl().toString());
+ hideURLInput();
commandInput->move(0, 0);
connect(commandInput, &CommandInput::submitted, this,
&MainWindow::evaluateCommand);
- connect(commandInput, &CommandInput::cancelled, this, &MainWindow::hideInput);
-
+ connect(commandInput, &CommandInput::cancelled, this,
+ &MainWindow::hideURLInput);
layout->addWidget(commandInput);
}
-void MainWindow::hideInput() {
+void MainWindow::hideURLInput() {
commandInput->setInputFocus(false);
commandInput->setHidden(true);
}
+void MainWindow::showURLInput() {
+ commandInput->setInputText(browserManager->currentUrl().toString());
+ commandInput->raise();
+ commandInput->setHidden(false);
+ commandInput->setInputFocus(true);
+}
+
void MainWindow::evaluateCommand(QString command) {
if (!command.isEmpty())
- web->setUrl(command);
- hideInput();
+ browserManager->setCurrentUrl(command);
+ hideURLInput();
}
void MainWindow::keyPressEvent(QKeyEvent *event) {
@@ -52,15 +56,21 @@ void MainWindow::keyPressEvent(QKeyEvent *event) {
if (combo.key() == Qt::Key_L &&
combo.keyboardModifiers().testFlag(Qt::ControlModifier)) {
toggleURLInput();
+ } else if (combo.key() == Qt::Key_T &&
+ combo.keyboardModifiers().testFlag(Qt::ControlModifier)) {
+ browserManager->createNewWebView(QUrl("https://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();
}
}
void MainWindow::toggleURLInput() {
- auto shouldShow = commandInput->isHidden();
- if (shouldShow) {
- commandInput->setInputText(web->url().toString());
- commandInput->raise();
- }
- commandInput->setHidden(!shouldShow);
- commandInput->setInputFocus(shouldShow);
+ commandInput->isHidden() ? showURLInput() : hideURLInput();
}