From b0b51570b0633b681306a564d3357ed3ad8927dc Mon Sep 17 00:00:00 2001 From: Le Tan Date: Wed, 2 Apr 2025 22:00:29 +0800 Subject: [PATCH] init stylesheet before mainwindow is shown --- src/core/theme.cpp | 35 ------------------- src/core/theme.h | 2 -- src/data/extra/themes/moonlight/interface.qss | 3 +- src/data/extra/themes/native/interface.qss | 1 - src/data/extra/themes/pure/interface.qss | 3 +- .../extra/themes/solarized-dark/interface.qss | 3 +- .../themes/solarized-light/interface.qss | 3 +- .../extra/themes/vscode-dark/interface.qss | 3 +- src/data/extra/themes/vue-dark/interface.qss | 3 +- src/data/extra/themes/vue-light/interface.qss | 3 +- src/data/extra/themes/vx-idea/interface.qss | 3 +- src/main.cpp | 15 ++++---- 12 files changed, 15 insertions(+), 62 deletions(-) diff --git a/src/core/theme.cpp b/src/core/theme.cpp index 8fd9727d..bb4cc75d 100644 --- a/src/core/theme.cpp +++ b/src/core/theme.cpp @@ -258,7 +258,6 @@ QString Theme::fetchQtStyleSheet() const translateStyleByPalette(m_palette, style); translateUrlToAbsolute(m_themeFolderPath, style); translateFontFamilyList(style); - translateScaledSize(WidgetUtils::calculateScaleFactor(), style); return style; } @@ -355,40 +354,6 @@ void Theme::translateFontFamilyList(QString &p_style) } } -void Theme::translateScaledSize(qreal p_factor, QString &p_style) -{ - QRegularExpression scaleRe("(\\s|:)\\$([+-]?)(\\d+)(?=\\D)"); - const int prefixCapturedIdx = 1; - const int signCapturedIdx = 2; - const int numCapturedIdx = 3; - - qDebug() << "translateScaledSize of Qt style sheet" << p_factor; - - int pos = 0; - QRegularExpressionMatch match; - while (pos < p_style.size()) { - int idx = p_style.indexOf(scaleRe, pos, &match); - if (idx == -1) { - break; - } - - auto numStr = match.captured(numCapturedIdx); - bool ok = false; - int val = numStr.toInt(&ok); - if (!ok) { - pos = idx + match.capturedLength(); - continue; - } - - val = val * p_factor + 0.5; - auto newStr = QStringLiteral("%1%2%3").arg(match.captured(prefixCapturedIdx), - match.captured(signCapturedIdx), - QString::number(val)); - p_style.replace(idx, match.capturedLength(), newStr); - pos = idx + newStr.size(); - } -} - QString Theme::paletteColor(const QString &p_name) const { auto val = findValueByKeyPath(m_palette, p_name).toString(); diff --git a/src/core/theme.h b/src/core/theme.h index cf293977..589e0f76 100644 --- a/src/core/theme.h +++ b/src/core/theme.h @@ -113,8 +113,6 @@ namespace vnotex // Thus we need to choose one available font from the list. static void translateFontFamilyList(QString &p_style); - static void translateScaledSize(qreal p_factor, QString &p_style); - static QJsonObject readJsonFile(const QString &p_filePath); static QJsonObject readPaletteFile(const QString &p_folder); diff --git a/src/data/extra/themes/moonlight/interface.qss b/src/data/extra/themes/moonlight/interface.qss index 22b8bd3d..27eefe89 100644 --- a/src/data/extra/themes/moonlight/interface.qss +++ b/src/data/extra/themes/moonlight/interface.qss @@ -5,7 +5,6 @@ * same specificity and the rule that appears last takes precedence. * VNote specific syntax: * - @widgets#toolbox#title#border: reference to a color defined in palette.ini; - * - $2px: 2 will be scaled by multiplying current display scaled factor. */ QWidget { @@ -586,7 +585,7 @@ QTabBar::close-button:hover { } QTabBar::scroller { - width: $20px; + width: 20px; } QTabBar QToolButton { diff --git a/src/data/extra/themes/native/interface.qss b/src/data/extra/themes/native/interface.qss index 7d48afb9..4173be03 100644 --- a/src/data/extra/themes/native/interface.qss +++ b/src/data/extra/themes/native/interface.qss @@ -5,7 +5,6 @@ * same specificity and the rule that appears last takes precedence. * VNote specific syntax: * - @widgets#toolbox#title#border: reference to a color defined in palette.ini; - * - $2px: 2 will be scaled by multiplying current display scaled factor. */ QWidget[HitSettingWidget="true"] { diff --git a/src/data/extra/themes/pure/interface.qss b/src/data/extra/themes/pure/interface.qss index 17611ab6..101241ed 100644 --- a/src/data/extra/themes/pure/interface.qss +++ b/src/data/extra/themes/pure/interface.qss @@ -5,7 +5,6 @@ * same specificity and the rule that appears last takes precedence. * VNote specific syntax: * - @widgets#toolbox#title#border: reference to a color defined in palette.ini; - * - $2px: 2 will be scaled by multiplying current display scaled factor. */ QWidget { @@ -624,7 +623,7 @@ QTabBar::close-button:hover { } QTabBar::scroller { - width: $20px; + width: 20px; } QTabBar QToolButton { diff --git a/src/data/extra/themes/solarized-dark/interface.qss b/src/data/extra/themes/solarized-dark/interface.qss index ad306a12..1064f36d 100644 --- a/src/data/extra/themes/solarized-dark/interface.qss +++ b/src/data/extra/themes/solarized-dark/interface.qss @@ -5,7 +5,6 @@ * same specificity and the rule that appears last takes precedence. * VNote specific syntax: * - @widgets#toolbox#title#border: reference to a color defined in palette.ini; - * - $2px: 2 will be scaled by multiplying current display scaled factor. */ QWidget { @@ -582,7 +581,7 @@ QTabBar::close-button:hover { } QTabBar::scroller { - width: $20px; + width: 20px; } QTabBar QToolButton { diff --git a/src/data/extra/themes/solarized-light/interface.qss b/src/data/extra/themes/solarized-light/interface.qss index ad306a12..1064f36d 100644 --- a/src/data/extra/themes/solarized-light/interface.qss +++ b/src/data/extra/themes/solarized-light/interface.qss @@ -5,7 +5,6 @@ * same specificity and the rule that appears last takes precedence. * VNote specific syntax: * - @widgets#toolbox#title#border: reference to a color defined in palette.ini; - * - $2px: 2 will be scaled by multiplying current display scaled factor. */ QWidget { @@ -582,7 +581,7 @@ QTabBar::close-button:hover { } QTabBar::scroller { - width: $20px; + width: 20px; } QTabBar QToolButton { diff --git a/src/data/extra/themes/vscode-dark/interface.qss b/src/data/extra/themes/vscode-dark/interface.qss index ad306a12..1064f36d 100644 --- a/src/data/extra/themes/vscode-dark/interface.qss +++ b/src/data/extra/themes/vscode-dark/interface.qss @@ -5,7 +5,6 @@ * same specificity and the rule that appears last takes precedence. * VNote specific syntax: * - @widgets#toolbox#title#border: reference to a color defined in palette.ini; - * - $2px: 2 will be scaled by multiplying current display scaled factor. */ QWidget { @@ -582,7 +581,7 @@ QTabBar::close-button:hover { } QTabBar::scroller { - width: $20px; + width: 20px; } QTabBar QToolButton { diff --git a/src/data/extra/themes/vue-dark/interface.qss b/src/data/extra/themes/vue-dark/interface.qss index 8497b83b..87d4bb31 100644 --- a/src/data/extra/themes/vue-dark/interface.qss +++ b/src/data/extra/themes/vue-dark/interface.qss @@ -5,7 +5,6 @@ * same specificity and the rule that appears last takes precedence. * VNote specific syntax: * - @widgets#toolbox#title#border: reference to a color defined in palette.ini; - * - $2px: 2 will be scaled by multiplying current display scaled factor. */ QWidget { @@ -587,7 +586,7 @@ QTabBar::close-button:hover { } QTabBar::scroller { - width: $20px; + width: 20px; } QTabBar QToolButton { diff --git a/src/data/extra/themes/vue-light/interface.qss b/src/data/extra/themes/vue-light/interface.qss index 13a711f8..fccda537 100644 --- a/src/data/extra/themes/vue-light/interface.qss +++ b/src/data/extra/themes/vue-light/interface.qss @@ -5,7 +5,6 @@ * same specificity and the rule that appears last takes precedence. * VNote specific syntax: * - @widgets#toolbox#title#border: reference to a color defined in palette.ini; - * - $2px: 2 will be scaled by multiplying current display scaled factor. */ QWidget { @@ -624,7 +623,7 @@ QTabBar::close-button:hover { } QTabBar::scroller { - width: $20px; + width: 20px; } QTabBar QToolButton { diff --git a/src/data/extra/themes/vx-idea/interface.qss b/src/data/extra/themes/vx-idea/interface.qss index cfee303b..4c1a1a92 100644 --- a/src/data/extra/themes/vx-idea/interface.qss +++ b/src/data/extra/themes/vx-idea/interface.qss @@ -5,7 +5,6 @@ * same specificity and the rule that appears last takes precedence. * VNote specific syntax: * - @widgets#toolbox#title#border: reference to a color defined in palette.ini; - * - $2px: 2 will be scaled by multiplying current display scaled factor. */ QWidget { @@ -643,7 +642,7 @@ QTabBar::close-button:hover { } QTabBar::scroller { - width: $20px; + width: 20px; } QTabBar QToolButton { diff --git a/src/main.cpp b/src/main.cpp index a52d6345..f605d7fd 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -157,6 +157,13 @@ int main(int argc, char *argv[]) // Should set the correct locale before VNoteX::getInst(). loadTranslators(app); + if (app.styleSheet().isEmpty()) { + auto style = VNoteX::getInst().getThemeMgr().fetchQtStyleSheet(); + if (!style.isEmpty()) { + app.setStyleSheet(style); + } + } + MainWindow window; window.show(); @@ -173,14 +180,6 @@ int main(int argc, char *argv[]) // Let MainWindow show first to decide the screen on which app is running. WidgetUtils::calculateScaleFactor(window.windowHandle()->screen()); - if (app.styleSheet().isEmpty()) { - auto style = VNoteX::getInst().getThemeMgr().fetchQtStyleSheet(); - if (!style.isEmpty()) { - app.setStyleSheet(style); - WidgetUtils::updateStyle(&window); - } - } - VNoteX::getInst().getThemeMgr().setBaseBackground(window.palette().color(QPalette::Base)); window.kickOffOnStart(cmdOptions.m_pathsToOpen);