From 3119e1df19d11e28ddcb0a950dbcde23458e069f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Malte=20J=C3=BCrgens?= Date: Fri, 17 Feb 2023 16:20:29 +0100 Subject: [PATCH] show userstyles status on the bottom of the page --- CMakeLists.txt | 1 + assets/userscript.js | 2 ++ src/centralwidget.cpp | 83 +++++++++++++++++++++++++++++++++++++++++++ src/centralwidget.h | 29 +++++++++++++++ src/discordpage.cpp | 5 +++ src/discordpage.h | 1 + src/mainwindow.cpp | 58 +++++------------------------- src/mainwindow.h | 19 +++------- src/userscript.cpp | 2 -- src/userscript.h | 2 +- 10 files changed, 134 insertions(+), 68 deletions(-) create mode 100644 src/centralwidget.cpp create mode 100644 src/centralwidget.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 9feccc8..48544ac 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -44,6 +44,7 @@ set(discord-screenaudio_SRC src/streamdialog.cpp src/log.cpp src/userscript.cpp + src/centralwidget.cpp resources.qrc ) diff --git a/assets/userscript.js b/assets/userscript.js index bd0e25f..129d1a0 100644 --- a/assets/userscript.js +++ b/assets/userscript.js @@ -159,6 +159,7 @@ function main() { function updateUserstyles() { userscript.log("Loading userstyles..."); + userscript.loadingMessage = "Loading userstyles..."; let stylesheet = document.getElementById("discordScreenaudioUserstyles"); if (!stylesheet) { stylesheet = document.createElement("style"); @@ -168,6 +169,7 @@ function main() { } stylesheet.innerText = userscript.userstyles; userscript.log("Finished loading userstyles"); + userscript.loadingMessage = ""; } userscript.userstylesChanged.connect(updateUserstyles); diff --git a/src/centralwidget.cpp b/src/centralwidget.cpp new file mode 100644 index 0000000..81352ed --- /dev/null +++ b/src/centralwidget.cpp @@ -0,0 +1,83 @@ +#include "centralwidget.h" +#include "discordpage.h" +#include "mainwindow.h" + +#include +#include +#include +#include +#include + +CentralWidget::CentralWidget(QWidget *parent) : QWidget(parent) { + setStyleSheet("background-color:#202225;"); + m_layout = new QVBoxLayout(this); + m_layout->setMargin(0); + m_layout->setSpacing(0); + setupWebView(); +} + +void CentralWidget::setupWebView() { + auto page = new DiscordPage(this); + + m_webView = new QWebEngineView(this); + m_webView->setPage(page); + + bool useNotifySend = MainWindow::instance() + ->settings() + ->value("useNotifySend", false) + .toBool(); + if (m_useKF5Notifications || useNotifySend) + QWebEngineProfile::defaultProfile()->setNotificationPresenter( + [&](std::unique_ptr notificationInfo) { + if (useNotifySend) { + auto title = notificationInfo->title(); + auto message = notificationInfo->message(); + auto image_path = + QString("/tmp/discord-screenaudio-%1.png").arg(title); + notificationInfo->icon().save(image_path); + QProcess::execute("notify-send", + {"--icon", image_path, "--app-name", + "discord-screenaudio", title, message}); + } else if (m_useKF5Notifications) { +#ifdef KNOTIFICATIONS + KNotification *notification = + new KNotification("discordNotification"); + notification->setTitle(notificationInfo->title()); + notification->setText(notificationInfo->message()); + notification->setPixmap( + QPixmap::fromImage(notificationInfo->icon())); + notification->setDefaultAction("View"); + connect(notification, &KNotification::defaultActivated, + [&, notificationInfo = std::move(notificationInfo)]() { + notificationInfo->click(); + show(); + activateWindow(); + }); + notification->sendEvent(); +#endif + } + }); + + connect(page->userScript(), &UserScript::loadingMessageChanged, this, + &CentralWidget::setLoadingIndicator); + + m_layout->addWidget(m_webView); +} + +void CentralWidget::setLoadingIndicator(QString text) { + if (text != "") { + if (m_loadingLabel == nullptr) { + m_loadingLabel = new QLabel(this); + m_loadingLabel->setMaximumHeight(20); + m_loadingLabel->setAlignment(Qt::AlignHCenter); + m_layout->addWidget(m_loadingLabel); + } + m_loadingLabel->setText(text); + } else { + if (m_loadingLabel != nullptr) { + m_layout->removeWidget(m_loadingLabel); + m_loadingLabel->deleteLater(); + m_loadingLabel = nullptr; + } + } +} diff --git a/src/centralwidget.h b/src/centralwidget.h new file mode 100644 index 0000000..a9c63d0 --- /dev/null +++ b/src/centralwidget.h @@ -0,0 +1,29 @@ +#pragma once + +#include +#include +#include +#include +#include +#include + +class CentralWidget : public QWidget { + Q_OBJECT + +public: + CentralWidget(QWidget *parent = nullptr); + +private: + void setupWebView(); + QVBoxLayout *m_layout; + QWebEngineView *m_webView; +#ifdef KNOTIFICATIONS + bool m_useKF5Notifications = true; +#else + bool m_useKF5Notifications = false; +#endif + QLabel *m_loadingLabel = nullptr; + +public Q_SLOTS: + void setLoadingIndicator(QString text); +}; diff --git a/src/discordpage.cpp b/src/discordpage.cpp index 6d07d36..f4cfb6e 100644 --- a/src/discordpage.cpp +++ b/src/discordpage.cpp @@ -85,6 +85,9 @@ void DiscordPage::fetchUserStyles(QFile *file) { << "Skipping" << url << "because it we can't prefetch it"; } else { qDebug(userstylesLog) << "Fetching" << url; + m_userScript.setProperty( + "loadingMessage", + QString("Loading userstyles: Fetching %1").arg(url)); QNetworkRequest request(url); auto reply = m_networkAccessManager.get(request); connect(reply, &QNetworkReply::finished, [=]() { @@ -281,3 +284,5 @@ void DiscordPage::javaScriptConsoleMessage( } } } + +UserScript *DiscordPage::userScript() { return &m_userScript; } diff --git a/src/discordpage.h b/src/discordpage.h index 39a4026..e42d484 100644 --- a/src/discordpage.h +++ b/src/discordpage.h @@ -13,6 +13,7 @@ class DiscordPage : public QWebEnginePage { public: explicit DiscordPage(QWidget *parent = nullptr); + UserScript *userScript(); private: UserScript m_userScript; diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 092ec18..93dc766 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -16,11 +16,7 @@ #include #include #include -#include -#include -#include -#include -#include +#include #include MainWindow *MainWindow::m_instance = nullptr; @@ -29,9 +25,10 @@ MainWindow::MainWindow(bool useNotifySend, QWidget *parent) : QMainWindow(parent) { assert(MainWindow::m_instance == nullptr); MainWindow::m_instance = this; - m_useNotifySend = useNotifySend; setupSettings(); - setupWebView(); + m_settings->setValue("useNotifySend", useNotifySend); + m_centralWidget = new CentralWidget(this); + setCentralWidget(m_centralWidget); setupTrayIcon(); resize(1000, 700); showMaximized(); @@ -42,49 +39,6 @@ MainWindow::MainWindow(bool useNotifySend, QWidget *parent) } } -void MainWindow::setupWebView() { - auto page = new DiscordPage(this); - connect(page, &QWebEnginePage::fullScreenRequested, this, - &MainWindow::fullScreenRequested); - - m_webView = new QWebEngineView(this); - m_webView->setPage(page); - - if (m_useKF5Notifications || m_useNotifySend) - QWebEngineProfile::defaultProfile()->setNotificationPresenter( - [&](std::unique_ptr notificationInfo) { - if (m_useNotifySend) { - auto title = notificationInfo->title(); - auto message = notificationInfo->message(); - auto image_path = - QString("/tmp/discord-screenaudio-%1.png").arg(title); - notificationInfo->icon().save(image_path); - QProcess::execute("notify-send", - {"--icon", image_path, "--app-name", - "discord-screenaudio", title, message}); - } else if (m_useKF5Notifications) { -#ifdef KNOTIFICATIONS - KNotification *notification = - new KNotification("discordNotification"); - notification->setTitle(notificationInfo->title()); - notification->setText(notificationInfo->message()); - notification->setPixmap( - QPixmap::fromImage(notificationInfo->icon())); - notification->setDefaultAction("View"); - connect(notification, &KNotification::defaultActivated, - [&, notificationInfo = std::move(notificationInfo)]() { - notificationInfo->click(); - show(); - activateWindow(); - }); - notification->sendEvent(); -#endif - } - }); - - setCentralWidget(m_webView); -} - void MainWindow::fullScreenRequested( QWebEngineFullScreenRequest fullScreenRequest) { fullScreenRequest.accept(); @@ -165,3 +119,7 @@ void MainWindow::closeEvent(QCloseEvent *event) { } MainWindow *MainWindow::instance() { return m_instance; } + +CentralWidget *MainWindow::centralWidget() { + return instance()->m_centralWidget; +}; diff --git a/src/mainwindow.h b/src/mainwindow.h index 0a50d62..d85c249 100644 --- a/src/mainwindow.h +++ b/src/mainwindow.h @@ -1,6 +1,6 @@ #pragma once -#include "discordpage.h" +#include "centralwidget.h" #include #include @@ -8,10 +8,8 @@ #include #include #include +#include #include -#include -#include -#include class MainWindow : public QMainWindow { Q_OBJECT @@ -20,31 +18,22 @@ public: explicit MainWindow(bool useNotifySend = false, QWidget *parent = nullptr); static MainWindow *instance(); QSettings *settings() const; + static CentralWidget *centralWidget(); private: - void setupWebView(); void setupTrayIcon(); void cleanTrayIcon(); void setupSettings(); - QWebEngineView *m_webView; QWebEngineProfile *prepareProfile(); - DiscordPage *m_discordPage; void closeEvent(QCloseEvent *event) override; QSystemTrayIcon *m_trayIcon = nullptr; QMenu *m_trayIconMenu; QSettings *m_settings; bool m_wasMaximized; static MainWindow *m_instance; - bool m_useNotifySend; -#ifdef KNOTIFICATIONS - bool m_useKF5Notifications = true; -#else - bool m_useKF5Notifications = false; -#endif + CentralWidget *m_centralWidget; public Q_SLOTS: void setTrayIcon(bool enabled); - -private Q_SLOTS: void fullScreenRequested(QWebEngineFullScreenRequest fullScreenRequest); }; diff --git a/src/userscript.cpp b/src/userscript.cpp index db345e5..b8a6171 100644 --- a/src/userscript.cpp +++ b/src/userscript.cpp @@ -15,8 +15,6 @@ UserScript::UserScript() : QObject() { setupShortcutsDialog(); setupStreamDialog(); setupVirtmic(); - // connect(this, &UserScript::loadingMessageChanged, MainWindow::instance(), - // &MainWindow::setLoadingIndicator); } void UserScript::setupHelpMenu() { diff --git a/src/userscript.h b/src/userscript.h index f632f40..8464b3a 100644 --- a/src/userscript.h +++ b/src/userscript.h @@ -64,7 +64,7 @@ Q_SIGNALS: void deafenToggled(); void streamStarted(bool video, int width, int height, int frameRate); void userstylesChanged(); - void loadingMessageChanged(); + void loadingMessageChanged(QString message); public Q_SLOTS: void log(QString message);