Skip to content

Commit 65f462d

Browse files
committed
qt: Improved GUIUtil::loadStyleSheet modification checks for -debug-ui
Avoid loading the stylesheets every 200ms to check if there are changes by just looking at the last modified timestamp.
1 parent d41a690 commit 65f462d

File tree

1 file changed

+44
-24
lines changed

1 file changed

+44
-24
lines changed

src/qt/guiutil.cpp

Lines changed: 44 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1043,23 +1043,48 @@ void loadStyleSheet(QWidget* widget, bool fForceUpdate)
10431043
LOCK(cs_css);
10441044

10451045
static std::unique_ptr<QString> stylesheet;
1046-
static uint256 hashStyle;
10471046
static std::set<QWidget*> setWidgets;
10481047

10491048
bool fDebugUI = gArgs.GetBoolArg("-debug-ui", false) && GUIUtil::isStyleSheetDirectoryCustom();
10501049
bool fStyleSheetChanged = false;
10511050

10521051
if (stylesheet == nullptr || fForceUpdate || fDebugUI) {
10531052

1054-
stylesheet = std::make_unique<QString>();
1053+
auto hasModified = [](const std::vector<QString>& vecFiles) -> bool
1054+
{
1055+
static std::map<const QString, QDateTime> mapLastModified;
1056+
1057+
bool fModified = false;
1058+
for (auto file : vecFiles) {
1059+
QFileInfo info(file);
1060+
QDateTime lastModified = info.lastModified(), prevLastModified;
1061+
auto it = mapLastModified.emplace(std::make_pair(file, lastModified));
1062+
prevLastModified = it.second ? QDateTime() : it.first->second;
1063+
it.first->second = lastModified;
1064+
fModified = prevLastModified != lastModified;
1065+
if (fModified) {
1066+
return true;
1067+
}
1068+
}
1069+
return fModified;
1070+
};
10551071

1056-
auto loadFile = [](const QString& name)
1072+
auto loadFiles = [fForceUpdate, fDebugUI, hasModified](const std::vector<QString>& vecFiles) -> bool
10571073
{
1058-
QFile qFile(QString(stylesheetDirectory + "/" + "%1%2").arg(name).arg(isStyleSheetDirectoryCustom() ? ".css" : ""));
1059-
if (qFile.open(QFile::ReadOnly)) {
1074+
if (!fForceUpdate && fDebugUI && !hasModified(vecFiles)) {
1075+
return false;
1076+
}
1077+
1078+
stylesheet = std::make_unique<QString>();
1079+
1080+
for (auto file : vecFiles) {
1081+
QFile qFile(file);
1082+
if (!qFile.open(QFile::ReadOnly)) {
1083+
throw std::runtime_error(strprintf("%s: Failed to open file: %s", __func__, file.toStdString()));
1084+
}
10601085
QString strStyle = QLatin1String(qFile.readAll());
10611086

1062-
// RegEx to match all <os=...></os> groups in the stylesheet
1087+
// Process all <os=...></os> groups in the stylesheet first
10631088
QRegularExpressionMatch osStyleMatch;
10641089
QRegularExpression osStyleExp("^(<os=(?:'|\").+(?:'|\")>)((?:.|\n)+?)(</os>?)$");
10651090
osStyleExp.setPatternOptions(QRegularExpression::MultilineOption);
@@ -1072,8 +1097,7 @@ void loadStyleSheet(QWidget* widget, bool fForceUpdate)
10721097

10731098
// Full match + 3 group matches
10741099
if (listMatches.size() % 4) {
1075-
qDebug() << "Invalid OS specific stylesheet section.";
1076-
return;
1100+
throw std::runtime_error(strprintf("%s: Invalid <os=...></os> section in file %s", __func__, file.toStdString()));
10771101
}
10781102

10791103
for (int i = 0; i < listMatches.size(); i+=4) {
@@ -1088,37 +1112,33 @@ void loadStyleSheet(QWidget* widget, bool fForceUpdate)
10881112
}
10891113
}
10901114
}
1091-
10921115
stylesheet->append(strStyle);
10931116
}
1117+
return true;
10941118
};
10951119

1120+
auto pathToFile = [](const QString& file) -> QString
1121+
{
1122+
return QString(stylesheetDirectory + "/" + "%1%2").arg(file).arg(isStyleSheetDirectoryCustom() ? ".css" : "");
1123+
};
1124+
1125+
std::vector<QString> vecFiles;
10961126
// If light/dark theme is used load general styles first
10971127
if (dashThemeActive()) {
1098-
loadFile("general");
1128+
vecFiles.push_back(pathToFile("general"));
10991129
}
1130+
vecFiles.push_back(pathToFile(GUIUtil::getActiveTheme()));
11001131

1101-
loadFile(GUIUtil::getActiveTheme());
1102-
1103-
if (hashStyle.IsNull()) {
1104-
hashStyle = Hash(stylesheet->begin(), stylesheet->end());
1105-
} else {
1106-
uint256 hashStyleTmp = Hash(stylesheet->begin(), stylesheet->end());
1107-
if (hashStyle != hashStyleTmp) {
1108-
hashStyle = hashStyleTmp;
1109-
fStyleSheetChanged = true;
1110-
}
1111-
}
1132+
fStyleSheetChanged = loadFiles(vecFiles);
11121133
}
11131134

1135+
bool fUpdateStyleSheet = fForceUpdate || (fDebugUI && fStyleSheetChanged);
1136+
11141137
if (widget) {
1115-
widget->setStyleSheet(*stylesheet);
11161138
setWidgets.insert(widget);
11171139
widget->setStyleSheet(*stylesheet);
11181140
}
11191141

1120-
bool fUpdateStyleSheet = (fForceUpdate || fDebugUI) && fStyleSheetChanged;
1121-
11221142
QWidgetList allWidgets = QApplication::allWidgets();
11231143
auto it = setWidgets.begin();
11241144
while (it != setWidgets.end()) {

0 commit comments

Comments
 (0)