Skip to content

Commit

Permalink
GUI - pass theme information to phx webview on theme change
Browse files Browse the repository at this point in the history
Currently this is used to style the scrollbars, but should be integrated more deeply with colour theming once that has been implemented.
  • Loading branch information
samaaron committed Jan 7, 2023
1 parent 506c52e commit bc7c6c1
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 1 deletion.
3 changes: 3 additions & 0 deletions app/gui/qt/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2308,6 +2308,9 @@ void MainWindow::updateColourTheme()
scopeWindow->SetColor2(theme->color("Scope_2"));
lexer->unhighlightAll();
metroPane->updateColourTheme();
#ifdef WITH_WEBENGINE
phxWidget->setTheme(theme);
#endif
}

void MainWindow::showLineNumbersMenuChanged()
Expand Down
53 changes: 53 additions & 0 deletions app/gui/qt/widgets/phxwebview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
//++

#include <QWebEngineView>
#include <QWebEngineScript>
#include <QWebEngineScriptCollection>
#include "phxwebview.h"
#include "phxurlinterceptor.h"

Expand All @@ -30,5 +32,56 @@ PhxWebView::PhxWebView(QWidget *parent)
setContextMenuPolicy(Qt::NoContextMenu);
setZoomFactor(2.0);

setAttribute(Qt::WA_TranslucentBackground);
setStyleSheet("background:transparent");
setScrollbarColours("#5e5e5e", "black", "#1e90ff");

}

void PhxWebView::setScrollbarColours(QColor foreground, QColor background, QColor hover)
{
insertStyleSheet("scrollbar",
QString("/* Width */"
"::-webkit-scrollbar {"
" width: 5px;"
"}"
"/* Track */"
"::-webkit-scrollbar-track {"
" background: %1;"
"}"
"/* Thumb */"
"::-webkit-scrollbar-thumb {"
" border-radius: 8px;"
" background: %2;"
"}"
"/* Thumb on hover */"
"::-webkit-scrollbar-thumb:hover {"
" background: %3;"
"}"
).arg(background.name()).arg(foreground.name()).arg(hover.name()));

}

void PhxWebView::insertStyleSheet(const QString& name, const QString& source)
{
QWebEngineScript script;
QString s = QString::fromLatin1("(function() {"\
" css = document.createElement('style');"\
" css.type = 'text/css';"\
" css.id = '%1';"\
" document.head.appendChild(css);"\
" css.innerText = '%2';"\
"})()").arg(name).arg(source.simplified());

this->page()->runJavaScript(s, QWebEngineScript::ApplicationWorld);

script.setName(name);
script.setSourceCode(s);
script.setInjectionPoint(QWebEngineScript::DocumentReady);
script.setRunsOnSubFrames(true);
script.setWorldId(QWebEngineScript::ApplicationWorld);
this->page()->scripts().insert(script);
}



5 changes: 5 additions & 0 deletions app/gui/qt/widgets/phxwebview.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,15 @@ class PhxWebView : public QWebEngineView

public:
PhxWebView(QWidget *parent = nullptr);
void setScrollbarColours(QColor foreground, QColor background, QColor hover);

private:
void insertStyleSheet(const QString& name, const QString& source);

QWebEngineProfile *phxProfile;
QWebEnginePage *phxPage;


};

#endif // PHXWEBVIEW_H
10 changes: 9 additions & 1 deletion app/gui/qt/widgets/phxwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,15 @@ void PhxWidget::handleOpenExternalBrowser()
QDesktopServices::openUrl(phxView->url());
}

void PhxWidget::connectToTauPhx(QUrl url)
void PhxWidget::setTheme(SonicPiTheme* theme)
{
m_theme = theme;
phxView->setScrollbarColours(theme->color("ScrollBar"),
theme->color("ScrollBarBackground"),
theme->color("ScrollBarBorder"));
}

void PhxWidget::connectToTauPhx(QUrl url)
{
defaultUrl = url;
std::cout << "[PHX] - connecting to: " << url.toString().toStdString() << std::endl;
Expand Down
4 changes: 4 additions & 0 deletions app/gui/qt/widgets/phxwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#define PHXWIDGET_H
#include <QWidget>
#include "phxwebview.h"
#include "../model/sonicpitheme.h"

class QVBoxLayout;
class QHBoxLayout;
Expand All @@ -29,6 +30,7 @@ class PhxWidget : public QWidget
public:
PhxWidget(QWidget* parent = 0);
void connectToTauPhx(QUrl url);
void setTheme(SonicPiTheme* theme);

private:
QHBoxLayout *mainLayout;
Expand All @@ -40,13 +42,15 @@ class PhxWidget : public QWidget
PhxWebView *phxView;
bool phxAlive;
QUrl defaultUrl;
SonicPiTheme* m_theme;

private slots:
void handleSizeDown();
void handleOpenExternalBrowser();
void handleSizeUp();
void handleResetBrowser();
void handleLoadFinished(bool ok);

};

#endif // PHXWIDGET_H

0 comments on commit bc7c6c1

Please sign in to comment.