Skip to content

Commit 53e38da

Browse files
committed
Simplify code for setting dark style for Web Engine previewer
1 parent 5d915f4 commit 53e38da

File tree

2 files changed

+12
-18
lines changed

2 files changed

+12
-18
lines changed

ReText/tab.py

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
from markups import find_markup_class_by_name, get_markup_for_file_name
2424
from markups.common import MODULE_HOME_PAGE
2525
from PyQt6.QtCore import QDir, QFile, QFileInfo, QPoint, Qt, QTimer, QUrl, pyqtSignal
26-
from PyQt6.QtGui import QPalette, QTextCursor, QTextDocument
27-
from PyQt6.QtWidgets import QApplication, QMessageBox, QSplitter, QTextEdit
26+
from PyQt6.QtGui import QTextCursor, QTextDocument
27+
from PyQt6.QtWidgets import QMessageBox, QSplitter, QTextEdit
2828

2929
from ReText import app_version, converterprocess, globalSettings
3030
from ReText.editor import ReTextEdit
@@ -192,21 +192,6 @@ def getHtmlFromConverted(self, converted, includeStyleSheet=True, webenv=False):
192192
style = 'td, th { border: 1px solid #c3c3c3; padding: 0 3px 0 3px; }\n'
193193
style += 'table { border-collapse: collapse; }\n'
194194
style += 'img { max-width: 100%; }\n'
195-
# QTextDocument seems to use media=screen even for printing
196-
if not isinstance(self.previewBox, QTextEdit) and not webenv:
197-
# https://github.com/retext-project/retext/pull/187
198-
palette = QApplication.palette()
199-
fgColor = palette.color(QPalette.ColorRole.Text).name()
200-
bgColor = palette.color(QPalette.ColorRole.Base).name()
201-
linkColor = palette.color(QPalette.ColorRole.Link).name()
202-
visitedLinkColor = palette.color(QPalette.ColorRole.LinkVisited).name()
203-
style += ('@media screen {\n'
204-
f' html {{ color: {fgColor}; background-color: {bgColor}; }}\n'
205-
f' a, a * {{ color: {linkColor}; }}\n'
206-
f' a:visited, a:visited * {{ color: {visitedLinkColor}; }}\n'
207-
'}\n')
208-
# https://github.com/retext-project/retext/issues/408
209-
style += '@media print { html { background-color: white; } }\n'
210195
headers += '<style type="text/css">\n' + style + '</style>\n'
211196
baseName = self.getBaseName()
212197
if self.cssFileExists:

ReText/webenginepreview.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
QDesktopServices,
2424
QFontInfo,
2525
QGuiApplication,
26+
QPalette,
2627
QTextDocument,
2728
)
2829
from PyQt6.QtWebEngineCore import (
@@ -32,7 +33,7 @@
3233
QWebEngineUrlRequestInterceptor,
3334
)
3435
from PyQt6.QtWebEngineWidgets import QWebEngineView
35-
from PyQt6.QtWidgets import QLabel
36+
from PyQt6.QtWidgets import QApplication, QLabel
3637

3738
from ReText import globalSettings
3839
from ReText.editor import getColor
@@ -157,6 +158,14 @@ def __init__(self, tab,
157158
settings = self.settings()
158159
settings.setDefaultTextEncoding('utf-8')
159160
settings.setAttribute(QWebEngineSettings.WebAttribute.LocalContentCanAccessRemoteUrls, True)
161+
if hasattr(QWebEngineSettings.WebAttribute, 'ForceDarkMode'): # Qt >= 6.7
162+
# QGuiApplication.instance().styleHints().colorScheme() does not seem to
163+
# work properly on KDE Plasma, so let's re-use the same approach as our
164+
# editor uses.
165+
palette = QApplication.palette()
166+
windowColor = palette.color(QPalette.ColorRole.Window)
167+
if windowColor.lightness() <= 150:
168+
settings.setAttribute(QWebEngineSettings.WebAttribute.ForceDarkMode, True)
160169

161170
# Events relevant to sync scrolling
162171
self.editBox.cursorPositionChanged.connect(self._handleCursorPositionChanged)

0 commit comments

Comments
 (0)