commit 4560cfd0126e11919a20a671944cad97363d8ba9
Author: Juergen Spitzmueller <[email protected]>
Date: Mon Oct 21 12:34:57 2024 +0200
Fix dark/light mode setting and run-time switch with Qt 6.8
The old method doesn't work any longer. This one is also announced to
work cross OSes -- verification on Mac and Win is needed!
---
src/frontends/qt/ColorCache.cpp | 7 +++----
src/frontends/qt/GuiApplication.cpp | 7 ++-----
src/frontends/qt/GuiView.cpp | 18 +++++++++++++++---
3 files changed, 20 insertions(+), 12 deletions(-)
diff --git a/src/frontends/qt/ColorCache.cpp b/src/frontends/qt/ColorCache.cpp
index 2566b75025..de8ff77a56 100644
--- a/src/frontends/qt/ColorCache.cpp
+++ b/src/frontends/qt/ColorCache.cpp
@@ -124,10 +124,9 @@ bool ColorCache::isSystem(ColorCode const color) const
bool ColorCache::isDarkMode() const
{
- QPalette palette = QPalette();
- QColor text_color = palette.color(QPalette::Active,
QPalette::WindowText);
- QColor bg_color = palette.color(QPalette::Active, QPalette::Window);
-
+ QColor text_color = pal_.color(QPalette::Active, QPalette::WindowText);
+ QColor bg_color = pal_.color(QPalette::Active, QPalette::Window);
+
return (text_color.black() < bg_color.black());
}
diff --git a/src/frontends/qt/GuiApplication.cpp
b/src/frontends/qt/GuiApplication.cpp
index 082ca57444..5685b1a0ef 100644
--- a/src/frontends/qt/GuiApplication.cpp
+++ b/src/frontends/qt/GuiApplication.cpp
@@ -639,17 +639,14 @@ IconInfo iconInfo(FuncRequest const & f, bool unknown,
bool rtl)
QPixmap prepareForDarkMode(QPixmap pixmap)
{
- QPalette palette = QPalette();
- QColor text_color = palette.color(QPalette::Active,
QPalette::WindowText);
- QColor bg_color = palette.color(QPalette::Active, QPalette::Window);
-
// guess whether we are in dark mode
- if (text_color.black() > bg_color.black())
+ if (!theGuiApp()->isInDarkMode())
// not in dark mode, do nothing
return pixmap;
// create a layer with black text turned to QPalette::WindowText
QPixmap black_overlay(pixmap.size());
+ QColor text_color =
theGuiApp()->style()->standardPalette().color(QPalette::Active,
QPalette::WindowText);
black_overlay.fill(text_color);
black_overlay.setMask(pixmap.createMaskFromColor(Qt::black,
Qt::MaskOutColor));
diff --git a/src/frontends/qt/GuiView.cpp b/src/frontends/qt/GuiView.cpp
index ae2a604b22..99278bcbd4 100644
--- a/src/frontends/qt/GuiView.cpp
+++ b/src/frontends/qt/GuiView.cpp
@@ -659,7 +659,6 @@ GuiView::GuiView(int id)
stat_counts_ = new GuiClickableLabel(statusBar());
stat_counts_->setAlignment(Qt::AlignCenter);
- stat_counts_->setStyleSheet("padding-left: 5px; padding-right: 5px;");
stat_counts_->hide();
statusBar()->addPermanentWidget(stat_counts_);
@@ -1509,7 +1508,9 @@ void GuiView::showStats()
else
stats << toqstr(bformat(_("%1$d Characters (no
Blanks)"), chars));
}
- stat_counts_->setText(stats.join(qt_(", [[stats separator]]")));
+ // we need to add space before and after manually, using stylesheet
+ // would break with dark mode on Qt >= 6.8.
+ stat_counts_->setText(" " + stats.join(qt_(", [[stats separator]]")) +
" ");
stat_counts_->show();
d.time_to_update = d.default_stats_rate;
@@ -1793,7 +1794,17 @@ bool GuiView::event(QEvent * e)
return QMainWindow::event(e);
}
- // dark/light mode runtime switch support, OS-dependent.
+ // dark/light mode runtime switch support
+#if QT_VERSION >= QT_VERSION_CHECK(6, 8, 0)
+ case QEvent::ThemeChange: {
+ guiApp->setPalette(guiApp->style()->standardPalette());
+ // We need to update metrics here to avoid a crash (#12786)
+ theBufferList().changed(true);
+ refillToolbars();
+ return QMainWindow::event(e);
+ }
+#else
+ // Pre 6.8: OS-dependent
// 1. Mac OS X
// Limit to Q_OS_MAC as this unnecessarily would also
// trigger on Linux with grave performance issues
@@ -1811,6 +1822,7 @@ bool GuiView::event(QEvent * e)
theBufferList().changed(true);
return QMainWindow::event(e);
}
+#endif
default:
return QMainWindow::event(e);
--
lyx-cvs mailing list
[email protected]
https://lists.lyx.org/mailman/listinfo/lyx-cvs