Reload stylesheet when CSS file changes (#6331)

* Reload stylesheet when CSS file changes

---------

Co-authored-by: Tres Finocchiaro <tres.finocchiaro@gmail.com>
This commit is contained in:
Dominic Clark
2025-02-09 04:33:44 +00:00
committed by GitHub
parent 786088baec
commit cb7c6d16cb
2 changed files with 29 additions and 1 deletions

View File

@@ -26,6 +26,7 @@
#ifndef LMMS_GUI_LMMS_STYLE_H
#define LMMS_GUI_LMMS_STYLE_H
#include <QFileSystemWatcher>
#include <QProxyStyle>
@@ -60,6 +61,7 @@ public:
private:
QImage colorizeXpm( const char * const * xpm, const QBrush& fill ) const;
void hoverColors( bool sunken, bool hover, bool active, QColor& color, QColor& blend ) const;
QFileSystemWatcher m_styleReloader;
};

View File

@@ -25,14 +25,17 @@
#include <array>
#include <QFile>
#include <QApplication>
#include <QFile>
#include <QFileInfo>
#include <QPainter>
#include <QPainterPath>
#include <QStyleFactory>
#include <QStyleOption>
#include "embed.h"
#include "LmmsStyle.h"
#include "TextFloat.h"
namespace lmms::gui
@@ -137,6 +140,29 @@ LmmsStyle::LmmsStyle() :
file.open( QIODevice::ReadOnly );
qApp->setStyleSheet( file.readAll() );
m_styleReloader.addPath(QFileInfo{file}.absoluteFilePath());
connect(&m_styleReloader, &QFileSystemWatcher::fileChanged, this,
[this](const QString& path)
{
if (auto file = QFile{path}; file.exists())
{
file.open(QIODevice::ReadOnly);
qApp->setStyleSheet(file.readAll());
TextFloat::displayMessage(
tr("Theme updated"),
tr("LMMS theme file %1 has been reloaded.").arg(file.fileName()),
embed::getIconPixmap("colorize"),
3000
);
// Handle delete + overwrite events
if (!m_styleReloader.files().contains(path))
{
m_styleReloader.addPath(path);
}
}
}
);
if( s_palette != nullptr ) { qApp->setPalette( *s_palette ); }
setBaseStyle( QStyleFactory::create( "Fusion" ) );