Skip to content

Support fractional HiDPI in GTK4 backend #30344

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
18 changes: 16 additions & 2 deletions lib/matplotlib/backends/backend_gtk4.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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.
Expand Down Expand Up @@ -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):
Expand Down
Loading
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