-
Notifications
You must be signed in to change notification settings - Fork 82
silx.gui.icons: Improved silx icons to support dark color theme #4593
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -31,8 +31,9 @@ | |
| __date__ = "07/01/2019" | ||
|
|
||
|
|
||
| import os | ||
| import logging | ||
| import os | ||
| import re | ||
| import weakref | ||
| from . import qt | ||
| import silx.resources | ||
|
|
@@ -70,6 +71,86 @@ def cleanIconCache(): | |
| """Order of file format extension to check""" | ||
|
|
||
|
|
||
| class _SvgIconEngine(qt.QIconEngine): | ||
| """Icon engine for SVG icons which tweaks color according to color scheme.""" | ||
|
|
||
| def __init__(self, name: str): | ||
| super().__init__() | ||
| self._lightSvgPath = self._readSVGPath(name) | ||
| self._isDarkColorScheme = False | ||
| self._renderer = qt.QSvgRenderer(self._lightSvgPath) | ||
|
|
||
| @staticmethod | ||
| def _readSVGPath(name: str) -> bytes: | ||
| filename = silx.resources._resource_filename( | ||
| f"{name}.svg", default_directory="gui/icons" | ||
| ) | ||
| qfile = qt.QFile(filename) | ||
| if not qfile.exists(): | ||
| raise ValueError(f"SVG icon resource not found: {name}") | ||
|
|
||
| if not qfile.open(qt.QIODevice.ReadOnly | qt.QIODevice.Text): | ||
| raise ValueError(f"Cannot open SVG icon resource: {name}") | ||
|
|
||
| svgpath = bytes(qfile.readAll()) | ||
| qfile.close() | ||
| return svgpath | ||
|
|
||
| @property | ||
| def _darkSvgPath(self) -> bytes: | ||
| palette = qt.QApplication.palette() | ||
| foregroundQColor = palette.color(qt.QPalette.Active, qt.QPalette.Text) | ||
| foreground = foregroundQColor.name() | ||
|
|
||
| svgpath = self._lightSvgPath.replace( | ||
| b"<svg", f'<svg fill="{foreground}"'.encode() | ||
| ) | ||
| svgpath = re.sub( | ||
| b'fill="(#000000|#000)"', f'fill="{foreground}"'.encode(), svgpath | ||
| ) | ||
| svgpath = re.sub( | ||
| b'stroke="(#000000|#000)"', f'stroke="{foreground}"'.encode(), svgpath | ||
| ) | ||
|
Comment on lines
+111
to
+113
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. svg default |
||
| return svgpath | ||
|
|
||
| def _currentRenderer(self) -> qt.QSvgRenderer: | ||
| if qt.BINDING != "PyQt5": | ||
| isDark = ( | ||
| qt.QApplication.styleHints().colorScheme() == qt.Qt.ColorScheme.Dark | ||
| ) | ||
| else: | ||
| isDark = False | ||
|
|
||
| if isDark != self._isDarkColorScheme: | ||
| self._renderer.load(self._darkSvgPath if isDark else self._lightSvgPath) | ||
| self._isDarkColorScheme = isDark | ||
|
|
||
|
Comment on lines
+124
to
+127
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Reload icon only when needed |
||
| return self._renderer | ||
|
|
||
| def paint( | ||
| self, | ||
| painter: qt.QPainter, | ||
| rect: qt.QRect, | ||
| mode: qt.QIcon.Mode, | ||
| state: qt.QIcon.State, | ||
| ): | ||
| renderer = self._currentRenderer() | ||
| if renderer.isValid(): | ||
| renderer.render(painter, rect) | ||
|
|
||
| def pixmap(self, size: qt.QSize, mode: qt.QIcon.Mode, state: qt.QIcon.State): | ||
| pixmap = qt.QPixmap(size) | ||
| pixmap.fill(qt.Qt.transparent) | ||
|
|
||
| painter = qt.QPainter(pixmap) | ||
| self.paint(painter, qt.QRect(0, 0, size.width(), size.height()), mode, state) | ||
| painter.end() | ||
| return pixmap | ||
|
|
||
| def clone(self): | ||
| return _SvgIconEngine(self._lightSvgPath) | ||
|
|
||
|
|
||
| class AbstractAnimatedIcon(qt.QObject): | ||
| """Store an animated icon. | ||
|
|
||
|
|
@@ -333,8 +414,12 @@ def getQIcon(name): | |
| """ | ||
| cached_icons = getIconCache() | ||
| if name not in cached_icons: | ||
| qfile = getQFile(name) | ||
| icon = qt.QIcon(qfile.fileName()) | ||
| try: | ||
| icon = qt.QIcon(_SvgIconEngine(name)) | ||
| except ValueError: | ||
| _logger.warning("Failed to load SVG icon") | ||
| qfile = getQFile(name) | ||
| icon = qt.QIcon(qfile.fileName()) | ||
|
Comment on lines
+421
to
+422
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Kept previous implementation for other application using silx icon mechanism but in silx we can consider completely remove .png icons. |
||
| cached_icons[name] = icon | ||
| else: | ||
| icon = cached_icons[name] | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
svg default
fillis black and this can be overridden in withfilldeclared in the<svg>tag.To complete this PR, the idea is to patch existing icons to remove unnecessary
fill="#000"so this substitution is not needed. This would allow to explicitly set a fill color to black even in dark mode.