From d32fed784f5bf92eecf9c6b466ec5502697426ca Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Thu, 24 Jul 2025 11:52:30 -0400 Subject: [PATCH] Backport PR #30344: Support fractional HiDPI in GTK4 backend --- .../embedding_in_gtk4_panzoom_sgskip.py | 5 ++--- .../embedding_in_gtk4_sgskip.py | 2 +- lib/matplotlib/backends/backend_gtk4.py | 18 ++++++++++++++++-- 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/galleries/examples/user_interfaces/embedding_in_gtk4_panzoom_sgskip.py b/galleries/examples/user_interfaces/embedding_in_gtk4_panzoom_sgskip.py index e42e59459198..aeb5d7562f58 100644 --- a/galleries/examples/user_interfaces/embedding_in_gtk4_panzoom_sgskip.py +++ b/galleries/examples/user_interfaces/embedding_in_gtk4_panzoom_sgskip.py @@ -44,10 +44,9 @@ def on_activate(app): toolbar = NavigationToolbar(canvas) vbox.append(toolbar) - win.show() + win.present() -app = Gtk.Application( - application_id='org.matplotlib.examples.EmbeddingInGTK4PanZoom') +app = Gtk.Application(application_id='org.matplotlib.examples.EmbeddingInGTK4PanZoom') app.connect('activate', on_activate) app.run(None) diff --git a/galleries/examples/user_interfaces/embedding_in_gtk4_sgskip.py b/galleries/examples/user_interfaces/embedding_in_gtk4_sgskip.py index 197cd7971088..e393658c25e8 100644 --- a/galleries/examples/user_interfaces/embedding_in_gtk4_sgskip.py +++ b/galleries/examples/user_interfaces/embedding_in_gtk4_sgskip.py @@ -39,7 +39,7 @@ def on_activate(app): canvas.set_size_request(800, 600) sw.set_child(canvas) - win.show() + win.present() app = Gtk.Application(application_id='org.matplotlib.examples.EmbeddingInGTK4') diff --git a/lib/matplotlib/backends/backend_gtk4.py b/lib/matplotlib/backends/backend_gtk4.py index 620c9e5b94b6..cd38968779ed 100644 --- a/lib/matplotlib/backends/backend_gtk4.py +++ b/lib/matplotlib/backends/backend_gtk4.py @@ -30,6 +30,7 @@ ) _GOBJECT_GE_3_47 = gi.version_info >= (3, 47, 0) +_GTK_GE_4_12 = Gtk.check_version(4, 12, 0) is None class FigureCanvasGTK4(_FigureCanvasGTK, Gtk.DrawingArea): @@ -48,7 +49,10 @@ def __init__(self, figure=None): self.set_draw_func(self._draw_func) self.connect('resize', self.resize_event) - self.connect('notify::scale-factor', self._update_device_pixel_ratio) + if _GTK_GE_4_12: + self.connect('realize', self._realize_event) + else: + self.connect('notify::scale-factor', self._update_device_pixel_ratio) click = Gtk.GestureClick() click.set_button(0) # All buttons. @@ -237,10 +241,20 @@ def _get_key(self, keyval, keycode, state): and not (mod == "shift" and unikey.isprintable()))] return "+".join([*mods, key]) + def _realize_event(self, obj): + surface = self.get_native().get_surface() + surface.connect('notify::scale', self._update_device_pixel_ratio) + self._update_device_pixel_ratio() + def _update_device_pixel_ratio(self, *args, **kwargs): # We need to be careful in cases with mixed resolution displays if # device_pixel_ratio changes. - if self._set_device_pixel_ratio(self.get_scale_factor()): + if _GTK_GE_4_12: + scale = self.get_native().get_surface().get_scale() + else: + scale = self.get_scale_factor() + assert scale is not None + if self._set_device_pixel_ratio(scale): self.draw() def _draw_rubberband(self, rect):