diff options
Diffstat (limited to '')
| -rw-r--r-- | src/widgets/MainWindow.cpp | 54 | ||||
| -rw-r--r-- | src/widgets/MainWindow.hpp | 4 | ||||
| -rw-r--r-- | src/widgets/WebView.cpp | 5 | ||||
| -rw-r--r-- | src/widgets/WebView.hpp | 4 | ||||
| -rw-r--r-- | src/widgets/WebViewStack.cpp | 103 | ||||
| -rw-r--r-- | src/widgets/WebViewStack.hpp | 39 |
6 files changed, 105 insertions, 104 deletions
diff --git a/src/widgets/MainWindow.cpp b/src/widgets/MainWindow.cpp index df8da53..c3c392e 100644 --- a/src/widgets/MainWindow.cpp +++ b/src/widgets/MainWindow.cpp @@ -1,10 +1,9 @@ #include <QKeyEvent> #include <QStackedLayout> #include <QVBoxLayout> -#include <QtCore/qcoreevent.h> -#include <QtCore/qnamespace.h> -#include <QtWidgets/qapplication.h> +#include <QtCore> +#include "Configuration.hpp" #include "InputMediator.hpp" #include "keymap/KeymapEvaluator.hpp" #include "widgets/MainWindow.hpp" @@ -18,50 +17,51 @@ MainWindow::MainWindow() { qApp->installEventFilter(this); // Root stacked layout - auto layout = new QStackedLayout(); + auto *layout = new QStackedLayout(); layout->setContentsMargins(0, 0, 0, 0); layout->setSpacing(0); layout->setStackingMode(QStackedLayout::StackAll); centralWidget()->setLayout(layout); // Web engine - auto webViewStack = new WebViewStack((const Configuration *)&configuration, - new QWebEngineProfile("null-browser")); - layout->addWidget(webViewStack); + auto *web_view_stack = + new WebViewStack((const Configuration *)&configuration, + new QWebEngineProfile("null-browser")); + layout->addWidget(web_view_stack); // TODO: remoev - webViewStack->openUrl(QUrl("https://duckduckgo.com"), OpenType::OpenUrl); - webViewStack->openUrl(QUrl("https://ediblemonad.dev"), - OpenType::OpenUrlInBgTab); - webViewStack->openUrl(QUrl("https://github.com/trending"), - OpenType::OpenUrlInBgTab); + web_view_stack->open_url(QUrl("https://duckduckgo.com"), OpenType::OpenUrl); + web_view_stack->open_url(QUrl("https://ediblemonad.dev"), + OpenType::OpenUrlInBgTab); + web_view_stack->open_url(QUrl("https://github.com/trending"), + OpenType::OpenUrlInBgTab); - auto keymapEvaluator = new KeymapEvaluator; + auto *keymap_evaluator = new KeymapEvaluator; - inputMediator = - new InputMediator(webViewStack, LuaRuntime::instance(), keymapEvaluator); + input_mediator = new InputMediator(web_view_stack, LuaRuntime::instance(), + keymap_evaluator); // NOTE: TMP - LuaRuntime::instance()->loadFile("./config.lua"); + LuaRuntime::instance()->load_file("./config.lua"); // TODO: remove - keymapEvaluator->addKeymap(KeyMode::Normal, "i", [keymapEvaluator]() { - keymapEvaluator->setCurrentMode(KeyMode::Insert); + keymap_evaluator->add_keymap(KeyMode::Normal, "i", [keymap_evaluator]() { + keymap_evaluator->set_current_mode(KeyMode::Insert); }); - keymapEvaluator->addKeymap(KeyMode::Insert, "<esc>", [keymapEvaluator]() { - keymapEvaluator->setCurrentMode(KeyMode::Normal); + keymap_evaluator->add_keymap(KeyMode::Insert, "<esc>", [keymap_evaluator]() { + keymap_evaluator->set_current_mode(KeyMode::Normal); }); - keymapEvaluator->addKeymap(KeyMode::Normal, "<c-t>a", - []() { qDebug() << "Stuff"; }); + keymap_evaluator->add_keymap(KeyMode::Normal, "<c-t>a", + []() { qDebug() << "Stuff"; }); } -bool MainWindow::eventFilter(QObject *, QEvent *event) { +bool MainWindow::eventFilter(QObject * /*watched*/, QEvent *event) { if (event->type() != QEvent::KeyPress) return false; - auto keyEvent = static_cast<QKeyEvent *>(event); - bool shouldSkip = inputMediator->evaluateKeymap(keyEvent->modifiers(), - (Qt::Key)keyEvent->key()); + auto *key_event = static_cast<QKeyEvent *>(event); + const bool should_skip = input_mediator->evaluate_keymap( + key_event->modifiers(), (Qt::Key)key_event->key()); - return shouldSkip; + return should_skip; } diff --git a/src/widgets/MainWindow.hpp b/src/widgets/MainWindow.hpp index c3118fc..26bd15b 100644 --- a/src/widgets/MainWindow.hpp +++ b/src/widgets/MainWindow.hpp @@ -9,10 +9,10 @@ class MainWindow : public QMainWindow { public: MainWindow(); -private: +protected: bool eventFilter(QObject *object, QEvent *event) override; private: - InputMediator *inputMediator; + InputMediator *input_mediator; Configuration configuration; }; diff --git a/src/widgets/WebView.cpp b/src/widgets/WebView.cpp index e00be1f..9c542f8 100644 --- a/src/widgets/WebView.cpp +++ b/src/widgets/WebView.cpp @@ -1,6 +1,7 @@ -#include <QWidget> +#include <QWebEngineView> #include <QtCore> #include "widgets/WebView.hpp" -WebView::WebView(QWebEngineProfile *profile) : QWebEngineView(profile) {} +WebView::WebView(QWebEngineProfile *profile, QWidget *parent_node) + : QWebEngineView(profile, parent_node) {} diff --git a/src/widgets/WebView.hpp b/src/widgets/WebView.hpp index 8a95139..f0457fd 100644 --- a/src/widgets/WebView.hpp +++ b/src/widgets/WebView.hpp @@ -1,13 +1,11 @@ #pragma once -#include <QKeyEvent> #include <QWebEngineView> -#include <QWidget> #include <QtCore> class WebView : public QWebEngineView { Q_OBJECT public: - WebView(QWebEngineProfile *profile); + WebView(QWebEngineProfile *profile, QWidget *parent_node = nullptr); }; diff --git a/src/widgets/WebViewStack.cpp b/src/widgets/WebViewStack.cpp index fa5d3ab..19e472e 100644 --- a/src/widgets/WebViewStack.cpp +++ b/src/widgets/WebViewStack.cpp @@ -1,6 +1,6 @@ #include <QStackedLayout> #include <QWebEngineNewWindowRequest> -#include <algorithm> +#include <QWebEngineProfile> #include <vector> #include "widgets/WebViewStack.hpp" @@ -12,135 +12,134 @@ WebViewStack::WebViewStack(const Configuration *configuration, layout->setContentsMargins(0, 0, 0, 0); layout->setStackingMode(QStackedLayout::StackOne); - createNewWebView(configuration->newTabUrl, true); + create_new_web_view(configuration->new_tab_url, true); } -void WebViewStack::openUrl(QUrl url, OpenType openType) { - switch (openType) { +void WebViewStack::open_url(const QUrl &url, OpenType open_type) { + switch (open_type) { case OpenType::OpenUrl: - setCurrentUrl(url); + set_current_url(url); break; case OpenType::OpenUrlInTab: - createNewWebView(url, true); + create_new_web_view(url, true); break; case OpenType::OpenUrlInBgTab: - createNewWebView(url, false); + create_new_web_view(url, false); break; case OpenType::OpenUrlInWindow: - createNewWebView(url, true); + create_new_web_view(url, true); break; } } -WebView *WebViewStack::createNewWebView(QUrl url, bool focus) { - auto webview = new WebView(profile); +WebView *WebViewStack::create_new_web_view(const QUrl &url, bool focus) { + auto *webview = new WebView(profile); webview->setUrl(url); layout->addWidget(webview); - webViewList.append(webview); + web_view_list.append(webview); connect(webview->page(), &QWebEnginePage::newWindowRequested, this, - &WebViewStack::onNewWebViewRequest); + &WebViewStack::on_new_web_view_request); if (focus) - focusWebView(webViewList.length() - 1); + focus_web_view(web_view_list.length() - 1); return webview; } -QList<Tab> WebViewStack::getTabList() { +QList<Tab> WebViewStack::get_tab_list() { QList<Tab> urls; - for (auto &view : webViewList) + for (auto &view : web_view_list) urls.append(Tab{.url = view->url().toString(), .title = view->title()}); return urls; } -void WebViewStack::onNewWebViewRequest( +void WebViewStack::on_new_web_view_request( const QWebEngineNewWindowRequest &request) { switch (request.destination()) { case QWebEngineNewWindowRequest::InNewTab: - createNewWebView(request.requestedUrl(), true); + create_new_web_view(request.requestedUrl(), true); break; case QWebEngineNewWindowRequest::InNewBackgroundTab: - createNewWebView(request.requestedUrl(), false); + create_new_web_view(request.requestedUrl(), false); break; case QWebEngineNewWindowRequest::InNewWindow: - // TODO: Impl - createNewWebView(request.requestedUrl(), true); - break; case QWebEngineNewWindowRequest::InNewDialog: // TODO: Impl - createNewWebView(request.requestedUrl(), true); + create_new_web_view(request.requestedUrl(), true); break; } } void WebViewStack::next() { - if (webViewList.isEmpty()) + if (web_view_list.isEmpty()) return; - auto index = currentWebViewIndex() + 1; - auto total = webViewList.length(); + auto index = current_web_view_index() + 1; + auto total = web_view_list.length(); index = index >= total ? index % total : index; - focusWebView(index); + focus_web_view(index); } void WebViewStack::previous() { - if (webViewList.isEmpty()) + if (web_view_list.isEmpty()) return; - int index = ((int)currentWebViewIndex()) - 1; - qsizetype total = webViewList.length(); + auto index = current_web_view_index() - 1; + auto total = web_view_list.length(); index = index < 0 ? total + index : index; - focusWebView(index); + focus_web_view(index); } -void WebViewStack::closeCurrent() { close(currentWebViewIndex()); } +void WebViewStack::close_current() { close(current_web_view_index()); } -void WebViewStack::close(int32_t index) { - if (index < 0 || index >= webViewList.length()) +void WebViewStack::close(qsizetype index) { + if (index < 0 || index >= web_view_list.length()) return; - auto webview = webViewList.at(index); + auto *webview = web_view_list.at(index); layout->removeWidget(webview); - webViewList.removeAt(index); + web_view_list.removeAt(index); disconnect(webview->page()); webview->deleteLater(); - focusWebView(currentWebViewIndex()); + focus_web_view(current_web_view_index()); - if (webViewList.isEmpty()) { - createNewWebView(configuration->newTabUrl, true); + if (web_view_list.isEmpty()) { + create_new_web_view(configuration->new_tab_url, true); } } std::vector<QUrl> WebViewStack::urls() { std::vector<QUrl> urls; - for (auto &view : webViewList) + for (auto &view : web_view_list) urls.push_back(view->url()); return urls; } -u_int32_t WebViewStack::currentWebViewIndex() { return layout->currentIndex(); } +uint32_t WebViewStack::current_web_view_index() { + return layout->currentIndex(); +} -u_int32_t WebViewStack::count() { return webViewList.length(); } +uint32_t WebViewStack::count() { return web_view_list.length(); } -void WebViewStack::focusWebView(int32_t index) { - if (webViewList.isEmpty()) +void WebViewStack::focus_web_view(qsizetype index) { + if (web_view_list.isEmpty()) return; index = std::max((long long)0, - std::min((long long)index, webViewList.length() - 1)); - layout->setCurrentIndex(index); + std::min((long long)index, web_view_list.length() - 1)); + layout->setCurrentIndex((int)index); } -QUrl WebViewStack::currentUrl() { - if (currentWebViewIndex() >= webViewList.length()) - return QUrl(""); +QUrl WebViewStack::current_url() { + if (current_web_view_index() >= web_view_list.length()) + return QUrl{}; - return webViewList.at(currentWebViewIndex())->url(); + return web_view_list.at(current_web_view_index())->url(); } -void WebViewStack::setCurrentUrl(QUrl url) { - if (currentWebViewIndex() >= webViewList.length()) +void WebViewStack::set_current_url(const QUrl &url) { + if (current_web_view_index() >= web_view_list.length()) return; - webViewList.at(currentWebViewIndex())->setUrl(url); + web_view_list.at(current_web_view_index())->setUrl(url); } diff --git a/src/widgets/WebViewStack.hpp b/src/widgets/WebViewStack.hpp index 473f699..aa463cc 100644 --- a/src/widgets/WebViewStack.hpp +++ b/src/widgets/WebViewStack.hpp @@ -2,12 +2,18 @@ #include <QStackedLayout> #include <QWebEngineProfile> +#include <cstdint> #include <vector> #include "Configuration.hpp" #include "widgets/WebView.hpp" -enum OpenType { OpenUrl, OpenUrlInTab, OpenUrlInBgTab, OpenUrlInWindow }; +enum OpenType : uint8_t { + OpenUrl, + OpenUrlInTab, + OpenUrlInBgTab, + OpenUrlInWindow +}; struct Tab { QString url; @@ -18,38 +24,35 @@ class WebViewStack : public QWidget { Q_OBJECT public: - inline static const QUrl NewtabURL = QUrl("about:blank"); - -public: WebViewStack(const Configuration *configuration, QWebEngineProfile *profile = new QWebEngineProfile, QWidget *parent = nullptr); - void openUrl(QUrl url, OpenType openType = OpenType::OpenUrl); + void open_url(const QUrl &url, OpenType open_type = OpenType::OpenUrl); std::vector<QUrl> urls(); - QList<Tab> getTabList(); - u_int32_t currentWebViewIndex(); - u_int32_t count(); - QUrl currentUrl(); + QList<Tab> get_tab_list(); + uint32_t current_web_view_index(); + uint32_t count(); + QUrl current_url(); - void focusWebView(int32_t index); + void focus_web_view(qsizetype index); void next(); void previous(); - void close(int32_t index); - void closeCurrent(); - -private: - void setCurrentUrl(QUrl url); - WebView *createNewWebView(QUrl url, bool focus = false); + void close(qsizetype index); + void close_current(); private slots: - void onNewWebViewRequest(const QWebEngineNewWindowRequest &request); + void on_new_web_view_request(const QWebEngineNewWindowRequest &request); + +protected: + void set_current_url(const QUrl &url); + WebView *create_new_web_view(const QUrl &url, bool focus = false); private: const Configuration *configuration; QWebEngineProfile *profile; QStackedLayout *layout; - QList<WebView *> webViewList; + QList<WebView *> web_view_list; }; |
