Skip to content

Commit f4d9f6c

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 ea350fa commit f4d9f6c

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

lib/matplotlib/backends/backend_gtk4.py

Lines changed: 16 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,20 @@ 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+
if _GTK_GE_4_12:
253+
scale = self.get_native().get_surface().get_scale()
254+
else:
255+
scale = self.get_scale_factor()
256+
assert scale is not None
257+
if self._set_device_pixel_ratio(scale):
244258
self.draw()
245259

246260
def _draw_rubberband(self, rect):

0 commit comments

Comments
 (0)