show userstyles status on the bottom of the page

This commit is contained in:
Malte Jürgens 2023-02-17 16:20:29 +01:00
parent 63180f5d53
commit 3119e1df19
No known key found for this signature in database
GPG Key ID: D29FBD5F93C0CFC3
10 changed files with 134 additions and 68 deletions

View File

@ -44,6 +44,7 @@ set(discord-screenaudio_SRC
src/streamdialog.cpp
src/log.cpp
src/userscript.cpp
src/centralwidget.cpp
resources.qrc
)

View File

@ -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);

83
src/centralwidget.cpp Normal file
View File

@ -0,0 +1,83 @@
#include "centralwidget.h"
#include "discordpage.h"
#include "mainwindow.h"
#include <QWebEngineNotification>
#include <QWebEngineProfile>
#include <QWebEngineScript>
#include <QWebEngineScriptCollection>
#include <QWebEngineSettings>
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<QWebEngineNotification> 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;
}
}
}

29
src/centralwidget.h Normal file
View File

@ -0,0 +1,29 @@
#pragma once
#include <QLabel>
#include <QVBoxLayout>
#include <QWebEnginePage>
#include <QWebEngineProfile>
#include <QWebEngineView>
#include <QWidget>
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);
};

View File

@ -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; }

View File

@ -13,6 +13,7 @@ class DiscordPage : public QWebEnginePage {
public:
explicit DiscordPage(QWidget *parent = nullptr);
UserScript *userScript();
private:
UserScript m_userScript;

View File

@ -16,11 +16,7 @@
#include <QThread>
#include <QTimer>
#include <QUrl>
#include <QWebEngineNotification>
#include <QWebEngineProfile>
#include <QWebEngineScript>
#include <QWebEngineScriptCollection>
#include <QWebEngineSettings>
#include <QWebEngineFullScreenRequest>
#include <QWidget>
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<QWebEngineNotification> 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;
};

View File

@ -1,6 +1,6 @@
#pragma once
#include "discordpage.h"
#include "centralwidget.h"
#include <QMainWindow>
#include <QMenu>
@ -8,10 +8,8 @@
#include <QSettings>
#include <QString>
#include <QSystemTrayIcon>
#include <QVBoxLayout>
#include <QVector>
#include <QWebEnginePage>
#include <QWebEngineProfile>
#include <QWebEngineView>
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);
};

View File

@ -15,8 +15,6 @@ UserScript::UserScript() : QObject() {
setupShortcutsDialog();
setupStreamDialog();
setupVirtmic();
// connect(this, &UserScript::loadingMessageChanged, MainWindow::instance(),
// &MainWindow::setLoadingIndicator);
}
void UserScript::setupHelpMenu() {

View File

@ -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);