8000 gtk4: Fix Cairo backend on HiDPI screens · QuLogic/matplotlib@5c55265 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5c55265

Browse files
committed
gtk4: Fix Cairo backend on HiDPI screens
With GTK4, the Cairo context we get is always in logical pixels, and is automatically scaled to the right size, without any worry about blurriness. So in that case, we can ignore all scale factor changes, and assume it's always 1. The remaining effect of tracking scale factor changes is to trigger a re-draw, but GTK will send a resize event to go along with it, which will do that for us. Fixes matplotlib#25847 Replaces matplotlib#25861
1 parent d7d1bba commit 5c55265

File tree

2 files changed

+6
-13
lines changed

2 files changed

+6
-13
lines changed

lib/matplotlib/backends/backend_gtk4.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ class FigureCanvasGTK4(_FigureCanvasGTK, Gtk.DrawingArea):
3434
required_interactive_framework = "gtk4"
3535
supports_blit = False
3636
manager_class = _api.classproperty(lambda cls: FigureManagerGTK4)
37-
_context_is_scaled = False
3837

3938
def __init__(self, figure=None):
4039
super().__init__(figure=figure)
@@ -228,13 +227,8 @@ def _post_draw(self, widget, ctx):
228227

229228
lw = 1
230229
dash = 3
231-
if not self._context_is_scaled:
232-
x0, y0, w, h = (dim / self.device_pixel_ratio
233-
for dim in self._rubberband_rect)
234-
else:
235-
x0, y0, w, h = self._rubberband_rect
236-
lw *= self.device_pixel_ratio
237-
dash *= self.device_pixel_ratio
230+
x0, y0, w, h = (dim / self.device_pixel_ratio
231+
for dim in self._rubberband_rect)
238232
x1 = x0 + w
239233
y1 = y0 + h
240234

lib/matplotlib/backends/backend_gtk4cairo.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55

66

77
class FigureCanvasGTK4Cairo(FigureCanvasCairo, FigureCanvasGTK4):
8-
_context_is_scaled = True
8+
def _set_device_pixel_ratio(self, ratio):
9+
# Cairo in GTK4 always uses logical pixels, so we don't need to do anything for
10+
# changes to the device pixel ratio.
11+
return False
912

1013
def on_draw_event(self, widget, ctx):
1114
if self._idle_draw_id:
@@ -16,15 +19,11 @@ def on_draw_event(self, widget, ctx):
1619
with (self.toolbar._wait_cursor_for_draw_cm() if self.toolbar
1720
else nullcontext()):
1821
self._renderer.set_context(ctx)
19-
scale = self.device_pixel_ratio
20-
# Scale physical drawing to logical size.
21-
ctx.scale(1 / scale, 1 / scale)
2222
allocation = self.get_allocation()
2323
Gtk.render_background(
2424
self.get_style_context(), ctx,
2525
allocation.x, allocation.y,
2626
allocation.width, allocation.height)
27-
self._renderer.dpi = self.figure.dpi
2827
self.figure.draw(self._renderer)
2928

3029

0 commit comments

Comments
 (0)
0