aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAkshay Nair <phenax5@gmail.com>2025-03-16 15:28:00 +0530
committerAkshay Nair <phenax5@gmail.com>2025-03-16 15:29:59 +0530
commit041f8933be28d0fcff1196792f30c5ca3176c155 (patch)
tree5fceaf5844f3312e8db63425c5c25825df594861
parent350d2bd7ccf80b91abdff154344f0ce88080a195 (diff)
downloadnull-browser-041f8933be28d0fcff1196792f30c5ca3176c155.tar.gz
null-browser-041f8933be28d0fcff1196792f30c5ca3176c155.zip
Add configuration class
-rw-r--r--flake.nix7
-rw-r--r--include/Configuration.hpp13
-rw-r--r--include/widgets/MainWindow.hpp2
-rw-r--r--include/widgets/WebViewStack.hpp11
-rw-r--r--spec/InputMediatorSpec.cpp47
-rw-r--r--spec/WebViewStackSpec.cpp77
-rw-r--r--src/main.cpp13
-rw-r--r--src/widgets/MainWindow.cpp6
-rw-r--r--src/widgets/WebViewStack.cpp10
9 files changed, 109 insertions, 77 deletions
diff --git a/flake.nix b/flake.nix
index ed3f539..d22ef72 100644
--- a/flake.nix
+++ b/flake.nix
@@ -8,7 +8,7 @@
flake-utils.lib.eachDefaultSystem (system:
let
pkgs = import nixpkgs { inherit system; };
- packages = with pkgs; [
+ dependencies = with pkgs; [
pkg-config
qt6.full
qt6.qtbase
@@ -22,7 +22,7 @@
version = "0.0.0";
src = ./.;
- buildInputs = packages;
+ buildInputs = dependencies;
nativeBuildInputs = with pkgs; [ cmake qt6.wrapQtAppsHook ];
};
@@ -31,7 +31,8 @@
cmake
gnumake
clang-tools
- ] ++ packages;
+ ] ++ dependencies;
+
LD_LIBRARY_PATH = "${pkgs.lib.makeLibraryPath buildInputs}";
};
});
diff --git a/include/Configuration.hpp b/include/Configuration.hpp
new file mode 100644
index 0000000..cc68803
--- /dev/null
+++ b/include/Configuration.hpp
@@ -0,0 +1,13 @@
+#pragma once
+
+#include <QtCore>
+#include <cstdio>
+
+class Configuration : public QObject {
+ Q_OBJECT
+
+public:
+ using QObject::QObject;
+
+ QUrl newTabUrl = QUrl("https://lite.duckduckgo.com");
+};
diff --git a/include/widgets/MainWindow.hpp b/include/widgets/MainWindow.hpp
index f77ed60..559a119 100644
--- a/include/widgets/MainWindow.hpp
+++ b/include/widgets/MainWindow.hpp
@@ -5,6 +5,7 @@
#include <QStackedLayout>
#include <QWebEngineView>
+#include "Configuration.hpp"
#include "InputMediator.hpp"
class MainWindow : public QMainWindow {
@@ -16,4 +17,5 @@ private:
private:
InputMediator *inputMediator;
+ Configuration configuration;
};
diff --git a/include/widgets/WebViewStack.hpp b/include/widgets/WebViewStack.hpp
index faf4562..34b1658 100644
--- a/include/widgets/WebViewStack.hpp
+++ b/include/widgets/WebViewStack.hpp
@@ -5,6 +5,7 @@
#include <QWebEngineView>
#include <sys/types.h>
+#include "Configuration.hpp"
#include "completion/TabsModel.hpp"
enum OpenType { OpenUrl, OpenUrlInTab, OpenUrlInBgTab, OpenUrlInWindow };
@@ -16,11 +17,11 @@ public:
inline static const QUrl NewtabURL = QUrl("about:blank");
public:
- WebViewStack(QWebEngineProfile *profile = new QWebEngineProfile,
+ WebViewStack(const Configuration *configuration,
+ QWebEngineProfile *profile = new QWebEngineProfile,
QWidget *parent = nullptr);
- void openUrl(QUrl url = WebViewStack::NewtabURL,
- OpenType openType = OpenType::OpenUrl);
+ void openUrl(QUrl url, OpenType openType = OpenType::OpenUrl);
std::vector<QUrl> urls();
u_int32_t currentWebViewIndex();
@@ -38,13 +39,13 @@ public:
private:
void setCurrentUrl(QUrl url);
- QWebEngineView *createNewWebView(QUrl url = WebViewStack::NewtabURL,
- bool focus = false);
+ QWebEngineView *createNewWebView(QUrl url, bool focus = false);
private slots:
void onNewWebViewRequest(QWebEngineNewWindowRequest &request);
private:
+ const Configuration *configuration;
QWebEngineProfile *profile;
QStackedLayout *layout;
QList<QWebEngineView *> webViewList;
diff --git a/spec/InputMediatorSpec.cpp b/spec/InputMediatorSpec.cpp
index 464e156..abc2232 100644
--- a/spec/InputMediatorSpec.cpp
+++ b/spec/InputMediatorSpec.cpp
@@ -11,8 +11,9 @@ private slots:
void testInputTypes() {
context("when showCommandInput is called");
it("shows url input with default command") {
- auto inputLine = InputLine();
- auto webViewStack = WebViewStack();
+ InputLine inputLine;
+ Configuration configuration;
+ WebViewStack webViewStack(&configuration);
auto luaRuntime = LuaRuntime::instance();
InputMediator inputMediator(&inputLine, &webViewStack, luaRuntime);
QCOMPARE(inputLine.isHidden(), true);
@@ -26,8 +27,9 @@ private slots:
context("when showURLInput is called");
it("shows url input with default url") {
- auto inputLine = InputLine();
- auto webViewStack = WebViewStack();
+ InputLine inputLine;
+ Configuration configuration;
+ WebViewStack webViewStack(&configuration);
auto luaRuntime = LuaRuntime::instance();
InputMediator inputMediator(&inputLine, &webViewStack, luaRuntime);
QCOMPARE(inputLine.isHidden(), true);
@@ -44,8 +46,9 @@ private slots:
context("when command open is executed");
context("- without args");
it("opens url input") {
- auto inputLine = InputLine();
- auto webViewStack = WebViewStack();
+ InputLine inputLine;
+ Configuration configuration;
+ WebViewStack webViewStack(&configuration);
auto luaRuntime = LuaRuntime::instance();
InputMediator inputMediator(&inputLine, &webViewStack, luaRuntime);
@@ -58,8 +61,9 @@ private slots:
context("when command open is executed");
context("- with url as an arg");
it("opens url in current tab") {
- auto inputLine = InputLine();
- auto webViewStack = WebViewStack();
+ InputLine inputLine;
+ Configuration configuration;
+ WebViewStack webViewStack(&configuration);
auto luaRuntime = LuaRuntime::instance();
InputMediator inputMediator(&inputLine, &webViewStack, luaRuntime);
@@ -76,8 +80,9 @@ private slots:
context("when command tabopen is run");
context("- without args");
it("opens url in a new tab") {
- auto inputLine = InputLine();
- auto webViewStack = WebViewStack();
+ InputLine inputLine;
+ Configuration configuration;
+ WebViewStack webViewStack(&configuration);
auto luaRuntime = LuaRuntime::instance();
InputMediator inputMediator(&inputLine, &webViewStack, luaRuntime);
@@ -90,15 +95,16 @@ private slots:
context("when command tabopen is executed");
context("- with url as an arg");
it("opens url in a new tab") {
- auto inputLine = InputLine();
- auto webViewStack = WebViewStack();
+ InputLine inputLine;
+ Configuration configuration;
+ WebViewStack webViewStack(&configuration);
auto luaRuntime = LuaRuntime::instance();
InputMediator inputMediator(&inputLine, &webViewStack, luaRuntime);
inputMediator.showCommandInput();
emit inputLine.submitted("tabopen http://a.com");
- std::vector<QUrl> urls = {QUrl(WebViewStack::NewtabURL),
+ std::vector<QUrl> urls = {QUrl(configuration.newTabUrl),
QUrl("http://a.com")};
QCOMPARE(webViewStack.urls(), urls);
QCOMPARE(webViewStack.currentWebViewIndex(), 1);
@@ -108,8 +114,9 @@ private slots:
void testCommandEvaluationTabNextPrev() {
context("when command tabnext is executed");
it("jumps to next tab") {
- auto inputLine = InputLine();
- auto webViewStack = WebViewStack();
+ InputLine inputLine;
+ Configuration configuration;
+ WebViewStack webViewStack(&configuration);
auto luaRuntime = LuaRuntime::instance();
InputMediator inputMediator(&inputLine, &webViewStack, luaRuntime);
webViewStack.openUrl(QUrl("https://a1.com"), OpenType::OpenUrl);
@@ -125,8 +132,9 @@ private slots:
context("when command tabprev is executed");
it("jumps to previous tab") {
- auto inputLine = InputLine();
- auto webViewStack = WebViewStack();
+ InputLine inputLine;
+ Configuration configuration;
+ WebViewStack webViewStack(&configuration);
auto luaRuntime = LuaRuntime::instance();
InputMediator inputMediator(&inputLine, &webViewStack, luaRuntime);
webViewStack.openUrl(QUrl("https://a1.com"), OpenType::OpenUrl);
@@ -144,8 +152,9 @@ private slots:
void testHideInputLine() {
context("when hideInputLine is called");
it("hides input") {
- auto inputLine = InputLine();
- auto webViewStack = WebViewStack();
+ InputLine inputLine;
+ Configuration configuration;
+ WebViewStack webViewStack(&configuration);
auto luaRuntime = LuaRuntime::instance();
InputMediator inputMediator(&inputLine, &webViewStack, luaRuntime);
inputMediator.showURLInput();
diff --git a/spec/WebViewStackSpec.cpp b/spec/WebViewStackSpec.cpp
index 5824437..e8df0c1 100644
--- a/spec/WebViewStackSpec.cpp
+++ b/spec/WebViewStackSpec.cpp
@@ -2,6 +2,7 @@
#include <QWebEngineProfile>
#include <vector>
+#include "Configuration.hpp"
#include "testUtils.h"
#include "widgets/WebViewStack.hpp"
@@ -19,10 +20,11 @@ private slots:
void testInitialState() {
context("when initialized");
it("opens a single tab") {
- WebViewStack webViewStack;
+ Configuration configuration;
+ WebViewStack webViewStack(&configuration);
QCOMPARE(webViewStack.count(), 1);
- QCOMPARE(webViewStack.currentUrl(), WebViewStack::NewtabURL);
+ QCOMPARE(webViewStack.currentUrl(), configuration.newTabUrl);
webViewStack.deleteLater();
}
}
@@ -30,42 +32,45 @@ private slots:
void testOpenUrl() {
context("when openUrl is called without an open type");
it("replaces current tab url with newtab url") {
- WebViewStack webViewStack;
+ Configuration configuration;
+ WebViewStack webViewStack(&configuration);
webViewStack.openUrl(QUrl("http://a.com"), OpenType::OpenUrl);
- webViewStack.openUrl();
+ webViewStack.openUrl(QUrl(configuration.newTabUrl));
QCOMPARE(webViewStack.count(), 1);
- std::vector<QUrl> urls = {QUrl(WebViewStack::NewtabURL)};
+ std::vector<QUrl> urls = {QUrl(configuration.newTabUrl)};
QCOMPARE(webViewStack.urls(), urls);
QCOMPARE(webViewStack.currentWebViewIndex(), 0);
- QCOMPARE(webViewStack.currentUrl(), WebViewStack::NewtabURL);
+ QCOMPARE(webViewStack.currentUrl(), configuration.newTabUrl);
}
context("when creating a new webview with a url and focus is false");
it("opens the given url in background") {
- WebViewStack webViewStack;
+ Configuration configuration;
+ WebViewStack webViewStack(&configuration);
webViewStack.openUrl(QUrl("https://duckduckgo.com"),
OpenType::OpenUrlInBgTab);
QCOMPARE(webViewStack.count(), 2);
- std::vector<QUrl> urls = {QUrl(WebViewStack::NewtabURL),
+ std::vector<QUrl> urls = {QUrl(configuration.newTabUrl),
QUrl("https://duckduckgo.com")};
QCOMPARE(webViewStack.urls(), urls);
QCOMPARE(webViewStack.currentWebViewIndex(), 0);
- QCOMPARE(webViewStack.currentUrl(), WebViewStack::NewtabURL);
+ QCOMPARE(webViewStack.currentUrl(), configuration.newTabUrl);
}
context("when creating a new webview with a url and focus is true");
it("opens the given url as current view") {
- WebViewStack webViewStack;
+ Configuration configuration;
+ WebViewStack webViewStack(&configuration);
webViewStack.openUrl(QUrl("https://duckduckgo.com"),
OpenType::OpenUrlInTab);
QCOMPARE(webViewStack.count(), 2);
- std::vector<QUrl> urls = {QUrl(WebViewStack::NewtabURL),
+ std::vector<QUrl> urls = {QUrl(configuration.newTabUrl),
QUrl("https://duckduckgo.com")};
QCOMPARE(webViewStack.urls(), urls);
QCOMPARE(webViewStack.currentWebViewIndex(), 1);
@@ -77,18 +82,20 @@ private slots:
context("when nextWebView is called");
context("- and there is only 1 tab");
it("does nothing") {
- WebViewStack webViewStack;
+ Configuration configuration;
+ WebViewStack webViewStack(&configuration);
webViewStack.next();
QCOMPARE(webViewStack.currentWebViewIndex(), 0);
- QCOMPARE(webViewStack.currentUrl(), WebViewStack::NewtabURL);
+ QCOMPARE(webViewStack.currentUrl(), configuration.newTabUrl);
}
context("when nextWebView is called");
context("- and there are tabs after the current tab");
it("goes to the next tab") {
- WebViewStack webViewStack;
+ Configuration configuration;
+ WebViewStack webViewStack(&configuration);
webViewStack.openUrl(QUrl("http://a1.com"), OpenType::OpenUrlInBgTab);
webViewStack.openUrl(QUrl("http://a2.com"), OpenType::OpenUrlInBgTab);
@@ -101,7 +108,8 @@ private slots:
context("when nextWebView is called");
context("- and current tab is the last tab");
it("jumps to the first tab") {
- WebViewStack webViewStack;
+ Configuration configuration;
+ WebViewStack webViewStack(&configuration);
webViewStack.openUrl(QUrl("http://a1.com"), OpenType::OpenUrlInBgTab);
webViewStack.openUrl(QUrl("http://a2.com"), OpenType::OpenUrlInTab);
QCOMPARE(webViewStack.currentWebViewIndex(), 2);
@@ -109,7 +117,7 @@ private slots:
webViewStack.next();
QCOMPARE(webViewStack.currentWebViewIndex(), 0);
- QCOMPARE(webViewStack.currentUrl(), WebViewStack::NewtabURL);
+ QCOMPARE(webViewStack.currentUrl(), configuration.newTabUrl);
}
}
@@ -117,18 +125,20 @@ private slots:
context("when previousWebView is called");
context("- and there is only 1 tab");
it("does nothing") {
- WebViewStack webViewStack;
+ Configuration configuration;
+ WebViewStack webViewStack(&configuration);
webViewStack.previous();
QCOMPARE(webViewStack.currentWebViewIndex(), 0);
- QCOMPARE(webViewStack.currentUrl(), WebViewStack::NewtabURL);
+ QCOMPARE(webViewStack.currentUrl(), configuration.newTabUrl);
}
context("when previousWebView is called");
context("- and there are tabs before the current tab");
it("goes to the next tab") {
- WebViewStack webViewStack;
+ Configuration configuration;
+ WebViewStack webViewStack(&configuration);
webViewStack.openUrl(QUrl("http://a1.com"), OpenType::OpenUrlInBgTab);
webViewStack.openUrl(QUrl("http://a2.com"), OpenType::OpenUrlInTab);
QCOMPARE(webViewStack.currentWebViewIndex(), 2);
@@ -142,7 +152,8 @@ private slots:
context("when previousWebView is called");
context("- and current tab is the last tab");
it("jumps to the last tab") {
- WebViewStack webViewStack;
+ Configuration configuration;
+ WebViewStack webViewStack(&configuration);
webViewStack.openUrl(QUrl("http://a1.com"), OpenType::OpenUrlInBgTab);
webViewStack.openUrl(QUrl("http://a2.com"), OpenType::OpenUrlInBgTab);
@@ -157,7 +168,8 @@ private slots:
context("when closeWebView is called");
context("- with out of bounds index");
it("does nothing") {
- WebViewStack webViewStack;
+ Configuration configuration;
+ WebViewStack webViewStack(&configuration);
webViewStack.openUrl(QUrl("https://a.com"), OpenType::OpenUrl);
webViewStack.close(1);
@@ -171,7 +183,8 @@ private slots:
context("when closeWebView is called");
context("- and there is only 1 tab");
it("closes the tab and opens empty tab in its place") {
- WebViewStack webViewStack;
+ Configuration configuration;
+ WebViewStack webViewStack(&configuration);
webViewStack.openUrl(QUrl("https://a.com"), OpenType::OpenUrl);
QCOMPARE(webViewStack.count(), 1);
@@ -179,16 +192,17 @@ private slots:
QCOMPARE(webViewStack.count(), 1);
QCOMPARE(webViewStack.urls(),
- (std::vector<QUrl>{WebViewStack::NewtabURL}));
+ (std::vector<QUrl>{configuration.newTabUrl}));
QCOMPARE(webViewStack.currentWebViewIndex(), 0);
- QCOMPARE(webViewStack.currentUrl(), WebViewStack::NewtabURL);
+ QCOMPARE(webViewStack.currentUrl(), configuration.newTabUrl);
}
context("when closeWebView is called");
context("- with the current tab index");
context("- and there are some tabs after");
it("closes the tab and focuses the next tab") {
- WebViewStack webViewStack;
+ Configuration configuration;
+ WebViewStack webViewStack(&configuration);
webViewStack.openUrl(QUrl("https://a1.com"), OpenType::OpenUrlInTab);
webViewStack.openUrl(QUrl("https://a2.com"), OpenType::OpenUrlInBgTab);
QCOMPARE(webViewStack.count(), 3);
@@ -199,7 +213,7 @@ private slots:
QCOMPARE(webViewStack.count(), 2);
QCOMPARE(
webViewStack.urls(),
- (std::vector<QUrl>{WebViewStack::NewtabURL, QUrl("https://a2.com")}));
+ (std::vector<QUrl>{configuration.newTabUrl, QUrl("https://a2.com")}));
QCOMPARE(webViewStack.currentWebViewIndex(), 1);
QCOMPARE(webViewStack.currentUrl(), QUrl("https://a2.com"));
}
@@ -208,7 +222,8 @@ private slots:
context("- with the current tab index");
context("- which is the last tab");
it("closes the tab and focusses previous tab") {
- WebViewStack webViewStack;
+ Configuration configuration;
+ WebViewStack webViewStack(&configuration);
webViewStack.openUrl(QUrl("https://a1.com"), OpenType::OpenUrlInBgTab);
webViewStack.openUrl(QUrl("https://a2.com"), OpenType::OpenUrlInTab);
QCOMPARE(webViewStack.count(), 3);
@@ -219,7 +234,7 @@ private slots:
QCOMPARE(webViewStack.count(), 2);
QCOMPARE(
webViewStack.urls(),
- (std::vector<QUrl>{WebViewStack::NewtabURL, QUrl("https://a1.com")}));
+ (std::vector<QUrl>{configuration.newTabUrl, QUrl("https://a1.com")}));
QCOMPARE(webViewStack.currentWebViewIndex(), 1);
QCOMPARE(webViewStack.currentUrl(), QUrl("https://a1.com"));
}
@@ -229,7 +244,8 @@ private slots:
context("when webview emits a newWindowRequested signal");
context("- of type new tab");
it("opens a new web view and focusses it") {
- WebViewStack webViewStack;
+ Configuration configuration;
+ WebViewStack webViewStack(&configuration);
webViewStack.openUrl(QUrl("https://a.com"), OpenType::OpenUrl);
auto webview = webViewStack.findChild<QWebEngineView *>();
QCOMPARE(webViewStack.count(), 1);
@@ -250,7 +266,8 @@ private slots:
context("when webview emits a newWindowRequested signal");
context("- of type new background tab");
it("opens a new web view in the background") {
- WebViewStack webViewStack;
+ Configuration configuration;
+ WebViewStack webViewStack(&configuration);
webViewStack.openUrl(QUrl("https://a.com"), OpenType::OpenUrl);
auto webview = webViewStack.findChild<QWebEngineView *>();
diff --git a/src/main.cpp b/src/main.cpp
index 3e2e2eb..c6c9a29 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -4,7 +4,6 @@
#include "widgets/MainWindow.hpp"
int main(int argc, char *argv[]) {
- // QCoreApplication::setAttribute(Qt::AA_UseDesktopOpenGL);
QApplication app(argc, argv);
MainWindow mainWindow;
@@ -13,15 +12,3 @@ int main(int argc, char *argv[]) {
return app.exec();
}
-
-// auto L = luaL_newstate();
-//
-// luaL_openlibs(L);
-//
-// auto status = luaL_dostring(L, "print(500 + 10 * 3)");
-// if (status) {
-// fprintf(stderr, "Couldn't load file: %s\n", lua_tostring(L, -1));
-// exit(1);
-// }
-// lua_close(L);
-// exit(0);
diff --git a/src/widgets/MainWindow.cpp b/src/widgets/MainWindow.cpp
index e2e54da..289e629 100644
--- a/src/widgets/MainWindow.cpp
+++ b/src/widgets/MainWindow.cpp
@@ -21,7 +21,8 @@ MainWindow::MainWindow() {
centralWidget()->setLayout(layout);
// Web engine
- auto webViewStack = new WebViewStack(new QWebEngineProfile("web-browser"));
+ auto webViewStack = new WebViewStack((const Configuration *)&configuration,
+ new QWebEngineProfile("web-browser"));
layout->addWidget(webViewStack);
// Command input
@@ -44,8 +45,7 @@ void MainWindow::keyPressEvent(QKeyEvent *event) {
inputMediator->showCommandInput("");
} else if (combo.key() == Qt::Key_T &&
combo.keyboardModifiers().testFlag(Qt::ControlModifier)) {
- inputMediator->openUrl(QUrl("https://lite.duckduckgo.com"),
- OpenType::OpenUrlInTab);
+ inputMediator->showTabsInput();
} else if (combo.key() == Qt::Key_J &&
combo.keyboardModifiers().testFlag(Qt::ControlModifier)) {
inputMediator->nextWebView();
diff --git a/src/widgets/WebViewStack.cpp b/src/widgets/WebViewStack.cpp
index b825623..d89db6e 100644
--- a/src/widgets/WebViewStack.cpp
+++ b/src/widgets/WebViewStack.cpp
@@ -4,13 +4,14 @@
#include "widgets/WebViewStack.hpp"
-WebViewStack::WebViewStack(QWebEngineProfile *profile, QWidget *parent)
- : QWidget(parent), profile(profile) {
+WebViewStack::WebViewStack(const Configuration *configuration,
+ QWebEngineProfile *profile, QWidget *parent)
+ : QWidget(parent), profile(profile), configuration(configuration) {
layout = new QStackedLayout(this);
layout->setContentsMargins(0, 0, 0, 0);
layout->setStackingMode(QStackedLayout::StackOne);
- createNewWebView(WebViewStack::NewtabURL, true);
+ createNewWebView(configuration->newTabUrl, true);
}
void WebViewStack::openUrl(QUrl url, OpenType openType) {
@@ -104,7 +105,7 @@ void WebViewStack::close(long index) {
focusWebView(currentWebViewIndex());
if (webViewList.isEmpty()) {
- createNewWebView(WebViewStack::NewtabURL, true);
+ createNewWebView(configuration->newTabUrl, true);
}
}
@@ -116,6 +117,7 @@ std::vector<QUrl> WebViewStack::urls() {
}
u_int32_t WebViewStack::currentWebViewIndex() { return layout->currentIndex(); }
+
u_int32_t WebViewStack::count() { return webViewList.length(); }
void WebViewStack::focusWebView(long index) {