backend_gtk4cairo.py 1.1 KB

1234567891011121314151617181920212223242526272829303132
  1. from contextlib import nullcontext
  2. from .backend_cairo import FigureCanvasCairo
  3. from .backend_gtk4 import GLib, Gtk, FigureCanvasGTK4, _BackendGTK4
  4. class FigureCanvasGTK4Cairo(FigureCanvasCairo, FigureCanvasGTK4):
  5. def _set_device_pixel_ratio(self, ratio):
  6. # Cairo in GTK4 always uses logical pixels, so we don't need to do anything for
  7. # changes to the device pixel ratio.
  8. return False
  9. def on_draw_event(self, widget, ctx):
  10. if self._idle_draw_id:
  11. GLib.source_remove(self._idle_draw_id)
  12. self._idle_draw_id = 0
  13. self.draw()
  14. with (self.toolbar._wait_cursor_for_draw_cm() if self.toolbar
  15. else nullcontext()):
  16. self._renderer.set_context(ctx)
  17. allocation = self.get_allocation()
  18. Gtk.render_background(
  19. self.get_style_context(), ctx,
  20. allocation.x, allocation.y,
  21. allocation.width, allocation.height)
  22. self.figure.draw(self._renderer)
  23. @_BackendGTK4.export
  24. class _BackendGTK4Cairo(_BackendGTK4):
  25. FigureCanvas = FigureCanvasGTK4Cairo