[LyX/master] Update highlighting colors in runtime (dark/light) mode change

Juergen Spitzmueller spitz at lyx.org
Tue Sep 5 07:59:37 UTC 2023


commit 51fd28ed4da42bb56c4febc306934d4023ea03db
Author: Juergen Spitzmueller <spitz at lyx.org>
Date:   Tue Sep 5 11:14:59 2023 +0200

    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 35c8b3b..bd72195 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<Ui::PreambleUi>(parent), current_id_(nullptr)
+	: UiWidget<Ui::PreambleUi>(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 dbdf34f..12f1cd7 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<support::TempFile> tempfile_;
+	/// LaTeX syntax highlighter
+	LaTeXHighlighter * highlighter_;
 
 private Q_SLOTS:
 	///
diff --git a/src/frontends/qt/GuiViewSource.cpp b/src/frontends/qt/GuiViewSource.cpp
index bcc6da4..2688d7f 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 69ddff6..dba9270 100644
--- a/src/frontends/qt/LaTeXHighlighter.cpp
+++ b/src/frontends/qt/LaTeXHighlighter.cpp
@@ -23,6 +23,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());
 		int g = 0.5 * (color1.green() + color2.green());
diff --git a/src/frontends/qt/LaTeXHighlighter.h b/src/frontends/qt/LaTeXHighlighter.h
index 7a80d1f..4b31a24 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;


More information about the lyx-cvs mailing list