From 51fd28ed4da42bb56c4febc306934d4023ea03db Mon Sep 17 00:00:00 2001 From: Juergen Spitzmueller Date: Tue, 5 Sep 2023 11:14:59 +0200 Subject: [PATCH] Update highlighting colors in runtime (dark/light) mode change Until now, this required a restart. --- src/frontends/qt/GuiDocument.cpp | 13 ++++++++++--- src/frontends/qt/GuiDocument.h | 4 ++++ src/frontends/qt/GuiViewSource.cpp | 9 +++++++++ src/frontends/qt/LaTeXHighlighter.cpp | 6 ++++++ src/frontends/qt/LaTeXHighlighter.h | 1 + 5 files changed, 30 insertions(+), 3 deletions(-) diff --git a/src/frontends/qt/GuiDocument.cpp b/src/frontends/qt/GuiDocument.cpp index 35c8b3b891..bd7219561c 100644 --- a/src/frontends/qt/GuiDocument.cpp +++ b/src/frontends/qt/GuiDocument.cpp @@ -21,7 +21,6 @@ #include "GuiIndices.h" #include "GuiView.h" #include "GuiSelectionManager.h" -#include "LaTeXHighlighter.h" #include "Validator.h" #include "LayoutFile.h" @@ -475,12 +474,12 @@ void ModuleSelectionManager::updateDelPB() ///////////////////////////////////////////////////////////////////// PreambleModule::PreambleModule(QWidget * parent) - : UiWidget(parent), current_id_(nullptr) + : UiWidget(parent), current_id_(nullptr), + highlighter_(new LaTeXHighlighter(preambleTE->document(), true)) { // This is not a memory leak. The object will be destroyed // with this. // @ is letter in the LyX user preamble - (void) new LaTeXHighlighter(preambleTE->document(), true); preambleTE->setFont(guiApp->typewriterSystemFont()); preambleTE->setWordWrapMode(QTextOption::NoWrap); setFocusProxy(preambleTE); @@ -516,6 +515,14 @@ bool PreambleModule::eventFilter(QObject * sender, QEvent * event) } } } + if (event->type() == QEvent::ApplicationPaletteChange) { + // mode switch: colors need to be updated + // and the highlighting redone + if (highlighter_) { + highlighter_->setupColors(); + highlighter_->rehighlight(); + } + } return QWidget::eventFilter(sender, event); } diff --git a/src/frontends/qt/GuiDocument.h b/src/frontends/qt/GuiDocument.h index dbdf34f47e..12f1cd75b0 100644 --- a/src/frontends/qt/GuiDocument.h +++ b/src/frontends/qt/GuiDocument.h @@ -17,6 +17,8 @@ #include "GuiDialog.h" #include "GuiIdListModel.h" +#include "LaTeXHighlighter.h" + #include "ui_BiblioUi.h" #include "ui_ColorUi.h" #include "ui_ChangeTrackingUi.h" @@ -385,6 +387,8 @@ private: Coords preamble_coords_; BufferId current_id_; unique_ptr tempfile_; + /// LaTeX syntax highlighter + LaTeXHighlighter * highlighter_; private Q_SLOTS: /// diff --git a/src/frontends/qt/GuiViewSource.cpp b/src/frontends/qt/GuiViewSource.cpp index bcc6da4bcc..2688d7fb08 100644 --- a/src/frontends/qt/GuiViewSource.cpp +++ b/src/frontends/qt/GuiViewSource.cpp @@ -320,6 +320,15 @@ bool ViewSourceWidget::eventFilter(QObject * obj, QEvent * ev) goToCursor(); return true; } + if (ev->type() == QEvent::ApplicationPaletteChange) { + // mode switch: colors need to be updated + // and the highlighting redone + if (highlighter_) { + highlighter_->setupColors(); + highlighter_->rehighlight(); + Q_EMIT needUpdate(); + } + } return false; } diff --git a/src/frontends/qt/LaTeXHighlighter.cpp b/src/frontends/qt/LaTeXHighlighter.cpp index 69ddff69d1..dba92704a3 100644 --- a/src/frontends/qt/LaTeXHighlighter.cpp +++ b/src/frontends/qt/LaTeXHighlighter.cpp @@ -22,6 +22,12 @@ namespace frontend { LaTeXHighlighter::LaTeXHighlighter(QTextDocument * parent, bool at_letter, bool keyval) : QSyntaxHighlighter(parent), at_letter_(at_letter), keyval_(keyval) +{ + setupColors(); +} + + +void LaTeXHighlighter::setupColors() { auto blend = [](QColor color1, QColor color2) { int r = 0.5 * (color1.red() + color2.red()); diff --git a/src/frontends/qt/LaTeXHighlighter.h b/src/frontends/qt/LaTeXHighlighter.h index 7a80d1fa26..4b31a2476c 100644 --- a/src/frontends/qt/LaTeXHighlighter.h +++ b/src/frontends/qt/LaTeXHighlighter.h @@ -28,6 +28,7 @@ public: explicit LaTeXHighlighter(QTextDocument * parent, bool at_letter = false, bool keyval = false); + void setupColors(); protected: void highlightBlock(QString const & text) override;