diff --git a/gui/canvas/connection.py b/gui/canvas/connection.py index be5db73..0195c23 100644 --- a/gui/canvas/connection.py +++ b/gui/canvas/connection.py @@ -10,6 +10,8 @@ SPDX-License-Identifier: GPL-2.0-or-later from argparse import Namespace from math import pi +import cairo + from . import colors from .drawable import Drawable from .. import Utils @@ -45,8 +47,11 @@ class Connection(CoreConnection, Drawable): self._rel_points = None # connection coordinates relative to sink/source self._arrow_rotation = 0.0 # rotation of the arrow in radians - self._current_cr = None # for what_is_selected() of curved line self._line_path = None + # simple cairo context for curved line and computing what_is_selected + cr = cairo.Context(cairo.RecordingSurface(cairo.CONTENT_ALPHA, None)) + cr.set_line_width(cr.get_line_width() * LINE_SELECT_SENSITIVITY) + self._line_path_cr = cr @nop_write @property @@ -125,12 +130,13 @@ class Connection(CoreConnection, Drawable): cr.curve_to(*(p2 + p3 + p4)) cr.line_to(*p5) self._line_path = cr.copy_path() + self._line_path_cr.new_path() + self._line_path_cr.append_path(self._line_path) def draw(self, cr): """ Draw the connection. """ - self._current_cr = cr sink = self.sink_port source = self.source_port @@ -192,18 +198,11 @@ class Connection(CoreConnection, Drawable): if coor_m: return Drawable.what_is_selected(self, coor, coor_m) - x, y = [a - b for a, b in zip(coor, self.coordinate)] - - cr = self._current_cr - - if cr is None: + if self._line_path is None: return - cr.save() - cr.new_path() - cr.append_path(self._line_path) - cr.set_line_width(cr.get_line_width() * LINE_SELECT_SENSITIVITY) - hit = cr.in_stroke(x, y) - cr.restore() + + x, y = [a - b for a, b in zip(coor, self.coordinate)] + hit = self._line_path_cr.in_stroke(x, y) if hit: return self