Skip to content

wx: Fix file extension for toolmanager-style toolbar #28007

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

Merged
merged 3 commits into from
Apr 3, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 24 additions & 6 deletions lib/matplotlib/backends/backend_wx.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
import sys
import weakref

import numpy as np
import PIL.Image

import matplotlib as mpl
from matplotlib.backend_bases import (
_Backend, FigureCanvasBase, FigureManagerBase,
Expand Down Expand Up @@ -1071,7 +1074,6 @@
*name*, including the extension and relative to Matplotlib's "images"
data directory.
"""
svg = cbook._get_data_path("images", name).read_bytes()
try:
dark = wx.SystemSettings.GetAppearance().IsDark()
except AttributeError: # wxpython < 4.1
Expand All @@ -1082,10 +1084,24 @@
bg_lum = (.299 * bg.red + .587 * bg.green + .114 * bg.blue) / 255
fg_lum = (.299 * fg.red + .587 * fg.green + .114 * fg.blue) / 255
dark = fg_lum - bg_lum > .2
if dark:
svg = svg.replace(b'fill:black;', b'fill:white;')
toolbarIconSize = wx.ArtProvider().GetDIPSizeHint(wx.ART_TOOLBAR)
return wx.BitmapBundle.FromSVG(svg, toolbarIconSize)

path = cbook._get_data_path('images', name)
if path.suffix == '.svg':
svg = path.read_bytes()
if dark:
svg = svg.replace(b'fill:black;', b'fill:white;')

Check warning on line 1092 in lib/matplotlib/backends/backend_wx.py

View check run for this annotation

Codecov / codecov/patch

lib/matplotlib/backends/backend_wx.py#L1092

Added line #L1092 was not covered by tests
toolbarIconSize = wx.ArtProvider().GetDIPSizeHint(wx.ART_TOOLBAR)
return wx.BitmapBundle.FromSVG(svg, toolbarIconSize)
else:
pilimg = PIL.Image.open(path)
# ensure RGBA as wx BitMap expects RGBA format
image = np.array(pilimg.convert("RGBA"))
if dark:
fg = wx.SystemSettings.GetColour(wx.SYS_COLOUR_WINDOWTEXT)
black_mask = (image[..., :3] == 0).all(axis=-1)
image[black_mask, :3] = (fg.Red(), fg.Green(), fg.Blue())

Check warning on line 1102 in lib/matplotlib/backends/backend_wx.py

View check run for this annotation

Codecov / codecov/patch

lib/matplotlib/backends/backend_wx.py#L1100-L1102

Added lines #L1100 - L1102 were not covered by tests
return wx.Bitmap.FromBufferRGBA(
image.shape[1], image.shape[0], image.tobytes())

def _update_buttons_checked(self):
if "Pan" in self.wx_ids:
Expand Down Expand Up @@ -1136,7 +1152,7 @@

def draw_rubberband(self, event, x0, y0, x1, y1):
height = self.canvas.figure.bbox.height
sf = 1 if wx.Platform == '__WXMSW__' else self.GetDPIScaleFactor()
sf = 1 if wx.Platform == '__WXMSW__' else self.canvas.GetDPIScaleFactor()
self.canvas._rubberband_rect = (x0/sf, (height - y0)/sf,
x1/sf, (height - y1)/sf)
self.canvas.Refresh()
Expand All @@ -1161,6 +1177,8 @@
# tools for matplotlib.backend_managers.ToolManager:

class ToolbarWx(ToolContainerBase, wx.ToolBar):
_icon_extension = '.svg'

def __init__(self, toolmanager, parent=None, style=wx.TB_BOTTOM):
if parent is None:
parent = toolmanager.canvas.GetParent()
Expand Down
Loading