feat(browser): apply dark mode without restart with Qt 6.7+

This commit is contained in:
Oleg Shparber 2024-06-16 02:50:15 -04:00
parent f0ce90c485
commit 2df03780a3
5 changed files with 31 additions and 14 deletions

View File

@ -68,18 +68,19 @@ void Settings::applySettings()
m_webProfile->settings()->setAttribute(QWebEngineSettings::ScrollAnimatorEnabled,
m_appSettings->isSmoothScrollingEnabled);
// Qt 6.7+ does not require restart to enable dark mode.
#if QT_VERSION >= QT_VERSION_CHECK(6, 7, 0)
m_webProfile->settings()->setAttribute(QWebEngineSettings::ForceDarkMode,
m_appSettings->isDarkModeEnabled());
#endif
// Apply custom CSS.
// TODO: Apply to all open pages.
m_webProfile->scripts()->clear(); // Remove all scripts first.
// Qt 5.14+ uses native Chromium dark mode.
#if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
const bool enableDarkMode
= m_appSettings->contentAppearance == Core::Settings::ContentAppearance::Dark
|| (m_appSettings->contentAppearance == Core::Settings::ContentAppearance::Automatic
&& m_appSettings->colorScheme() == Core::Settings::ColorScheme::Dark);
if (enableDarkMode) {
if (m_appSettings->isDarkModeEnabled()) {
setCustomStyleSheet(QStringLiteral("_zeal_darkstylesheet"), DarkModeCssUrl);
}
#endif

View File

@ -73,6 +73,19 @@ Settings::~Settings()
save();
}
bool Settings::isDarkModeEnabled() const
{
if (contentAppearance == ContentAppearance::Dark) {
return true;
}
if (contentAppearance == ContentAppearance::Automatic && colorScheme() == ColorScheme::Dark) {
return true;
}
return false;
}
Zeal::Core::Settings::ColorScheme Settings::colorScheme()
{
#if QT_VERSION >= QT_VERSION_CHECK(6, 5, 0)
@ -115,16 +128,12 @@ void Settings::load()
settings->beginGroup(GroupContent);
// Dark mode needs to be applied before Qt WebEngine is initialized.
contentAppearance = settings->value(QStringLiteral("appearance"),
QVariant::fromValue(ContentAppearance::Automatic)).value<ContentAppearance>();
#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)
const bool enableDarkMode
= contentAppearance == ContentAppearance::Dark
|| (contentAppearance == ContentAppearance::Automatic && colorScheme() == ColorScheme::Dark);
if (enableDarkMode) {
#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0) && QT_VERSION < QT_VERSION_CHECK(6, 7, 0)
// Dark mode needs to be applied before Qt WebEngine is initialized.
if (isDarkModeEnabled()) {
qputenv("QTWEBENGINE_CHROMIUM_FLAGS", "--blink-settings=forceDarkModeEnabled=true,darkModeInversionAlgorithm=4");
}
#endif

View File

@ -127,6 +127,8 @@ public:
~Settings() override;
// Helper functions.
bool isDarkModeEnabled() const;
#if QT_VERSION >= QT_VERSION_CHECK(6, 5, 0)
typedef Qt::ColorScheme ColorScheme;
#else

View File

@ -285,6 +285,11 @@ void SettingsDialog::saveSettings()
settings->isFuzzySearchEnabled = ui->fuzzySearchCheckBox->isChecked();
// Content Tab
#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0) && QT_VERSION < QT_VERSION_CHECK(6, 7, 0)
// Applying dark mode requires restart.
ui->appearanceLabel->setText(tr("Appearance (requires restart):"));
#endif
settings->defaultFontFamily = ui->defaultFontComboBox->currentData().toString();
settings->serifFontFamily = ui->serifFontComboBox->currentText();
settings->sansSerifFontFamily = ui->sansSerifFontComboBox->currentText();

View File

@ -251,7 +251,7 @@
<item>
<widget class="QLabel" name="appearanceLabel">
<property name="text">
<string>Appearance (requires restart):</string>
<string>Appearance:</string>
</property>
</widget>
</item>