Repository: matplotlib/matplotlib
File: lib/matplotlib/widgets.py (Python)

Code before:
        # docstring inherited
        return super().ignore(event) or self.canvas is None

    def _set_cursor(self, cursor):
        """Update the canvas cursor."""
        self.ax.get_figure(root=True).canvas.set_cursor(cursor)

    def _save_blit_background(self, background):
        """
        Save a blit background.

        The background is stored on the canvas in a uniquely identifiable way.
        It should be read back via `._load_blit_background`. Be prepared that
        some events may invalidate the background, in which case
        `._load_blit_background` will return None.

        This currently allows at most one background per widget, which is
        good enough for all existing widgets.
        """
        if self._blit_background_id is None:
            self._blit_background_id = self.canvas._get_blit_background_id()
        self.canvas._blit_backgrounds[self._blit_background_id] = background

    def _load_blit_background(self):
        """Load a blit background; may be None at any time."""
        return self.canvas._blit_backgrounds.get(self._blit_background_id)


class Button(AxesWidget):
    """
    A GUI neutral button.

    For the button to remain responsive you must keep a reference to it.
    Call `.on_clicked` to connect to the button.

    Attributes
    ----------
    ax
        The `~.axes.Axes` the button renders into.
    label
        A `.Text` instance.
    color
        The color of the button when not hovering.
    hovercolor
        The color of the button when hovering.
    """

    def __init__(self, ax, label, image=None,
                 color='0.85', hovercolor='0.95', *, useblit=True):
        """
        Parameters

Review comment (performance):
Does this dictionary get cleared at any point?  I'm worried about an un-bounded cache.

It is more complex, but should `canvas._blit_background` be a weak-key dict keyed on the Widget and values being (blit_id, background)?

I think in most cases the lifecycle of widgets will be longer than that of the canvas, but I can imagine someone having a long-living UI (so long lived Canvas object) that adds/revmoves widgets dynamically.

Code after:
        return ((event.xdata, event.ydata) if event.inaxes is self.ax
                else self.ax.transData.inverted().transform((event.x, event.y)))

    def ignore(self, event):
        # docstring inherited
        return super().ignore(event) or self.canvas is None

    def _set_cursor(self, cursor):
        """Update the canvas cursor."""
        self.ax.get_figure(root=True).canvas.set_cursor(cursor)

    def _save_blit_background(self, background):
        """
        Save a blit background.

        The background is stored on the canvas in a uniquely identifiable way.
        It should be read back via `._load_blit_background`. Be prepared that
        some events may invalidate the background, in which case
        `._load_blit_background` will return None.

        This currently allows at most one background per widget, which is
        good enough for all existing widgets.
        """
        if self._blit_background_id is None:
            self._blit_background_id = self.canvas._get_blit_background_id()
        self.canvas._blit_backgrounds[self._blit_background_id] = background

    def _load_blit_background(self):
        """Load a blit background; may be None at any time."""
        return self.canvas._blit_backgrounds.get(self._blit_background_id)


class Button(AxesWidget):
    """
    A GUI neutral button.

    For the button to remain responsive you must keep a reference to it.
    Call `.on_clicked` to connect to the button.

    Attributes
    ----------
    ax
        The `~.axes.Axes` the button renders into.
    label
        A `.Text` instance.
    color
        The color of the button when not hovering.
    hovercolor
        The color of the button when hovering.
    """