Skip to content

Commit 7e726b2

Browse files
PR: Automatic colorization of SVG icons according to the interface theme (#24199)
1 parent 74ce6c5 commit 7e726b2

File tree

165 files changed

+759
-361
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

165 files changed

+759
-361
lines changed

binder/environment.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ dependencies:
2424
- ipython >=8.15.0,<10.0.0,!=8.17.1,!=9.1.0,!=9.2.0,!=9.3.0,!=9.4.0
2525
- ipython_pygments_lexers >=1.0
2626
- jedi >=0.17.2,<0.20.0
27+
- lxml >=4.9.0
2728
- jellyfish >=0.7
2829
- jsonschema >=3.2.0
2930
- keyring >=17.0.0

requirements/main.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ dependencies:
2222
- ipython >=8.15.0,<10.0.0,!=8.17.1,!=9.1.0,!=9.2.0,!=9.3.0,!=9.4.0
2323
- ipython_pygments_lexers >=1.0
2424
- jedi >=0.17.2,<0.20.0
25+
- lxml >=4.9.0
2526
- jellyfish >=0.7
2627
- jsonschema >=3.2.0
2728
- keyring >=17.0.0

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,7 @@ def run(self):
285285
'ipython_pygments_lexers>=1.0',
286286
'jedi>=0.17.2,<0.20.0',
287287
'jellyfish>=0.7',
288+
'lxml>=4.9.0',
288289
'jsonschema>=3.2.0',
289290
'keyring>=17.0.0',
290291
'nbconvert>=4.0',

spyder/api/widgets/mixins.py

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,7 @@
2424
)
2525

2626
# Local imports
27-
from spyder.api.config.mixins import (
28-
SpyderConfigurationAccessor,
29-
SpyderConfigurationObserver
30-
)
27+
from spyder.api.config.mixins import SpyderConfigurationAccessor
3128
from spyder.api.exceptions import SpyderAPIError
3229
from spyder.api.shortcuts import SpyderShortcutsMixin
3330
from spyder.api.widgets.menus import SpyderMenu
@@ -39,6 +36,7 @@
3936
from spyder.utils.registries import (
4037
ACTION_REGISTRY, MENU_REGISTRY, TOOLBAR_REGISTRY, TOOLBUTTON_REGISTRY)
4138
from spyder.utils.stylesheet import PANES_TOOLBAR_STYLESHEET
39+
from spyder.utils.svg_colorizer import SVGColorize
4240

4341

4442
class SpyderToolButtonMixin:
@@ -757,7 +755,7 @@ class SvgToScaledPixmap(SpyderConfigurationAccessor):
757755
def svg_to_scaled_pixmap(self, svg_file, rescale=None, in_package=True):
758756
"""
759757
Transform svg to a QPixmap that is scaled according to the factor set
760-
by users in Preferences.
758+
by users in Preferences. Uses the icon manager for proper colorization.
761759
762760
Parameters
763761
----------
@@ -771,6 +769,7 @@ def svg_to_scaled_pixmap(self, svg_file, rescale=None, in_package=True):
771769
if in_package:
772770
image_path = get_image_path(svg_file)
773771

772+
# Get user's DPI scale factor
774773
if self.get_conf('high_dpi_custom_scale_factor', section='main'):
775774
scale_factors = self.get_conf(
776775
'high_dpi_custom_scale_factors',
@@ -780,6 +779,52 @@ def svg_to_scaled_pixmap(self, svg_file, rescale=None, in_package=True):
780779
else:
781780
scale_factor = 1
782781

782+
# Check if the SVG has colorization classes before colorization
783+
should_colorize = False
784+
try:
785+
svg_paths_data = SVGColorize.get_colored_paths(
786+
image_path, ima.ICON_COLORS
787+
)
788+
if svg_paths_data and svg_paths_data.get('paths'):
789+
# Check if any of the paths have colorization classes
790+
# (not just default colors)
791+
paths = svg_paths_data.get('paths', [])
792+
for path in paths:
793+
# If a path has a color that's not the default color,
794+
# it means it has a colorization class
795+
default_color = ima.ICON_COLORS.get(
796+
'ICON_1', '#FF0000' # Get default color from palette
797+
)
798+
if path.get('color') != default_color:
799+
should_colorize = True
800+
break
801+
except Exception:
802+
should_colorize = False
803+
804+
# Try to use the icon manager for colorization only if SVG supports it
805+
if should_colorize:
806+
icon = ima.get_icon(svg_file)
807+
if icon and not icon.isNull():
808+
# Get the original SVG dimensions
809+
pm = QPixmap(image_path)
810+
width = pm.width()
811+
height = pm.height()
812+
813+
# Apply rescale factor
814+
if rescale is not None:
815+
aspect_ratio = width / height
816+
width = int(width * rescale)
817+
height = int(width / aspect_ratio)
818+
819+
# Get a properly scaled pixmap from the icon
820+
# Use the maximum dimension to maintain aspect ratio
821+
max_dimension = max(
822+
int(width * scale_factor),
823+
int(height * scale_factor)
824+
)
825+
return icon.pixmap(max_dimension, max_dimension)
826+
827+
# Fallback to original method for icons without colorization classes.
783828
# Get width and height
784829
pm = QPixmap(image_path)
785830
width = pm.width()

spyder/dependencies.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
JELLYFISH_REQVER = '>=0.7'
4848
JSONSCHEMA_REQVER = '>=3.2.0'
4949
KEYRING_REQVER = '>=17.0.0'
50+
LXML_REQVER = ">=4.9.0"
5051
NBCONVERT_REQVER = '>=4.0'
5152
NUMPYDOC_REQVER = '>=0.6.0'
5253
PACKAGING_REQVER = '>=20.0'
@@ -166,6 +167,10 @@
166167
'features': _("Save Github credentials to report internal "
167168
"errors securely"),
168169
'required_version': KEYRING_REQVER},
170+
{'modname': "lxml",
171+
'package_name': "lxml",
172+
'features': _("Colorize SVG icons according to the interface theme"),
173+
'required_version': LXML_REQVER},
169174
{'modname': "nbconvert",
170175
'package_name': "nbconvert",
171176
'features': _("Manipulate Jupyter notebooks in the Editor"),

spyder/images/dark/ArchiveFileIcon.svg

Lines changed: 0 additions & 1 deletion
This file was deleted.

spyder/images/dark/binary.svg

Lines changed: 0 additions & 1 deletion
This file was deleted.

spyder/images/dark/clear_console.svg

Lines changed: 0 additions & 3 deletions
This file was deleted.

spyder/images/dark/connection_connected.svg

Lines changed: 0 additions & 3 deletions
This file was deleted.

spyder/images/dark/connection_disconnected.svg

Lines changed: 0 additions & 3 deletions
This file was deleted.

0 commit comments

Comments
 (0)