macOS: Fix changing colors from dark->light not working correctly

As of Qt 5.15.0 if you set any colors in the application palette
they are not reset when the system colors are changed. So we cannot use
that to fix colors. Instead set a widget specific override palette.

Also, Qt appears to have fixed link and alternating row colors, so no
need for those override anymore.
This commit is contained in:
Kovid Goyal 2020-09-09 12:23:38 +05:30
parent 730fe50516
commit 1935d31d4e
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -15,7 +15,7 @@ from threading import Lock, RLock
from PyQt5.Qt import ( from PyQt5.Qt import (
QT_VERSION, QApplication, QBuffer, QByteArray, QCoreApplication, QDateTime, QT_VERSION, QApplication, QBuffer, QByteArray, QCoreApplication, QDateTime,
QDesktopServices, QDialog, QEvent, QFileDialog, QFileIconProvider, QFileInfo, QDesktopServices, QDialog, QEvent, QFileDialog, QFileIconProvider, QFileInfo, QPalette,
QFont, QFontDatabase, QFontInfo, QFontMetrics, QIcon, QLocale, QColor, QFont, QFontDatabase, QFontInfo, QFontMetrics, QIcon, QLocale, QColor,
QNetworkProxyFactory, QObject, QSettings, QSocketNotifier, QStringListModel, Qt, QNetworkProxyFactory, QObject, QSettings, QSocketNotifier, QStringListModel, Qt,
QThread, QTimer, QTranslator, QUrl, pyqtSignal QThread, QTimer, QTranslator, QUrl, pyqtSignal
@ -1063,21 +1063,16 @@ class Application(QApplication):
self.paletteChanged.connect(self.on_palette_change) self.paletteChanged.connect(self.on_palette_change)
self.on_palette_change() self.on_palette_change()
def fix_dark_theme_colors(self): def fix_combobox_text_color(self):
from calibre.gui2.palette import dark_link_color # Workaround for https://bugreports.qt.io/browse/QTBUG-75321
pal = self.palette() # Buttontext is set to black for some reason
# dark blue is unreadable when using dark backgrounds pal = QPalette(self.palette())
pal.setColor(pal.Link, dark_link_color) pal.setColor(pal.ButtonText, pal.color(pal.WindowText))
# alternating row colors look awful in most dark mode themes self.ignore_palette_changes = True
pal.setColor(pal.AlternateBase, pal.color(pal.Base)) self.setPalette(pal, 'QComboBox')
if self.using_calibre_style: self.ignore_palette_changes = False
# Workaround for https://bugreports.qt.io/browse/QTBUG-75321
# Buttontext is set to black for some reason
pal.setColor(pal.ButtonText, pal.color(pal.WindowText))
self.set_palette(pal)
def set_palette(self, pal): def set_palette(self, pal):
self.is_dark_mode_palette = False
self.ignore_palette_changes = True self.ignore_palette_changes = True
self.setPalette(pal) self.setPalette(pal)
# Needed otherwise Qt does not emit the paletteChanged signal when # Needed otherwise Qt does not emit the paletteChanged signal when
@ -1092,8 +1087,8 @@ class Application(QApplication):
return return
self.is_dark_theme = is_dark_theme() self.is_dark_theme = is_dark_theme()
self.setProperty('is_dark_theme', self.is_dark_theme) self.setProperty('is_dark_theme', self.is_dark_theme)
if isosx and self.is_dark_theme: if isosx and self.is_dark_theme and self.using_calibre_style:
self.fix_dark_theme_colors() QTimer.singleShot(0, self.fix_combobox_text_color)
if self.using_calibre_style: if self.using_calibre_style:
ss = 'QTabBar::tab:selected { font-style: italic }\n\n' ss = 'QTabBar::tab:selected { font-style: italic }\n\n'
if self.is_dark_theme: if self.is_dark_theme: