Skip to content

Commit 3444f0f

Browse files
authored
Merge pull request matplotlib#30344 from QuLogic/gtk4-fractional
Support fractional HiDPI in GTK4 backend
2 parents be5fd62 + b0ad3c1 commit 3444f0f

File tree

3 files changed

+19
-6
lines changed

3 files changed

+19
-6
lines changed

galleries/examples/user_interfaces/embedding_in_gtk4_panzoom_sgskip.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,9 @@ def on_activate(app):
4242
toolbar = NavigationToolbar(canvas)
4343
vbox.append(toolbar)
4444

45-
win.show()
45+
win.present()
4646

4747

48-
app = Gtk.Application(
49-
application_id='org.matplotlib.examples.EmbeddingInGTK4PanZoom')
48+
app = Gtk.Application(application_id='org.matplotlib.examples.EmbeddingInGTK4PanZoom')
5049
app.connect('activate', on_activate)
5150
app.run(None)

galleries/examples/user_interfaces/embedding_in_gtk4_sgskip.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def on_activate(app):
3838
canvas.set_size_request(800, 600)
3939
sw.set_child(canvas)
4040

41-
win.show()
41+
win.present()
4242

4343

4444
app = Gtk.Application(application_id='org.matplotlib.examples.EmbeddingInGTK4')

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)