Skip to content

Commit

Permalink
feat(browser): apply dark mode without restart with Qt 6.7+
Browse files Browse the repository at this point in the history
  • Loading branch information
trollixx committed Jun 16, 2024
1 parent f0ce90c commit 2df0378
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 14 deletions.
13 changes: 7 additions & 6 deletions src/libs/browser/settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 16 additions & 7 deletions src/libs/core/settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions src/libs/core/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ class Settings final : public QObject
~Settings() override;

// Helper functions.
bool isDarkModeEnabled() const;

#if QT_VERSION >= QT_VERSION_CHECK(6, 5, 0)
typedef Qt::ColorScheme ColorScheme;
#else
Expand Down
5 changes: 5 additions & 0 deletions src/libs/ui/settingsdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion src/libs/ui/settingsdialog.ui
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@
<item>
<widget class="QLabel" name="appearanceLabel">
<property name="text">
<string>Appearance (requires restart):</string>
<string>Appearance:</string>
</property>
</widget>
</item>
Expand Down

0 comments on commit 2df0378

Please sign in to comment.