Skip to content

Commit d65f546

Browse files
committed
Support fractional HiDPI in GTK4 backend
Since GTK 4.12, fractional HiDPI is handled, but with a separate property on the backing surface due to it being a different type.
1 parent 95db12f commit d65f546

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

lib/matplotlib/backends/backend_gtk4.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
)
3131

3232
_GOBJECT_GE_3_47 = gi.version_info >= (3, 47, 0)
33+
_GTK_GE_4_12 = Gtk.check_version(4, 12, 0) is None
3334

3435

3536
class FigureCanvasGTK4(_FigureCanvasGTK, Gtk.DrawingArea):
@@ -48,7 +49,10 @@ def __init__(self, figure=None):
4849

4950
self.set_draw_func(self._draw_func)
5051
self.connect('resize', self.resize_event)
51-
self.connect('notify::scale-factor', self._update_device_pixel_ratio)
52+
if _GTK_GE_4_12:
53+
self.connect('realize', self._realize_event)
54+
else:
55+
self.connect('notify::scale-factor', self._update_device_pixel_ratio)
5256

5357
click = Gtk.GestureClick()
5458
click.set_button(0) # All buttons.
@@ -237,10 +241,22 @@ def _get_key(self, keyval, keycode, state):
237241
and not (mod == "shift" and unikey.isprintable()))]
238242
return "+".join([*mods, key])
239243

244+
def _realize_event(self, obj):
245+
surface = self.get_native().get_surface()
246+
surface.connect('notify::scale', self._update_device_pixel_ratio)
247+
self._update_device_pixel_ratio()
248+
240249
def _update_device_pixel_ratio(self, *args, **kwargs):
241250
# We need to be careful in cases with mixed resolution displays if
242251
# device_pixel_ratio changes.
243-
if self._set_device_pixel_ratio(self.get_scale_factor()):
252+
scale = None
253+
if _GTK_GE_4_12:
254+
if (native := self.get_native()) is not None:
255+
if (surface := native.get_surface()) is not None:
256+
scale = surface.get_scale()
257+
else:
258+
scale = self.get_scale_factor()
259+
if scale is not None and self._set_device_pixel_ratio(scale):
244260
self.draw()
245261

246262
def _draw_rubberband(self, rect):

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy