Skip to content

Commit d32fed7

Browse files
tacaswellmeeseeksmachine
authored andcommitted
Backport PR matplotlib#30344: Support fractional HiDPI in GTK4 backend
1 parent 8076615 commit d32fed7

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
@@ -44,10 +44,9 @@ def on_activate(app):
4444
toolbar = NavigationToolbar(canvas)
4545
vbox.append(toolbar)
4646

47-
win.show()
47+
win.present()
4848

4949

50-
app = Gtk.Application(
51-
application_id='org.matplotlib.examples.EmbeddingInGTK4PanZoom')
50+
app = Gtk.Application(application_id='org.matplotlib.examples.EmbeddingInGTK4PanZoom')
5251
app.connect('activate', on_activate)
5352
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
@@ -39,7 +39,7 @@ def on_activate(app):
3939
canvas.set_size_request(800, 600)
4040
sw.set_child(canvas)
4141

42-
win.show()
42+
win.present()
4343

4444

4545
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)
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