Skip to content

Avoid selecting more than one lines at a time #8

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
15 changes: 14 additions & 1 deletion mpl_draggable_line/_line.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from __future__ import annotations

import math
import threading
from numbers import Real

import numpy as np
Expand All @@ -17,6 +19,9 @@


class DraggableLine(AxesWidget):
active_line, min_dist = None, math.inf
lock = threading.Lock()

def __init__(
self,
ax,
Expand Down Expand Up @@ -132,7 +137,13 @@ def _on_press(self, event: MouseEvent):
dist = np.hypot(*diff.T)
idx = np.argmin(dist)
if dist[idx] < self._grab_range:
self._handle_idx = idx
with DraggableLine.lock:
if dist[idx] < DraggableLine.min_dist:
if DraggableLine.active_line is not None:
DraggableLine.active_line._handle_idx = None
self._handle_idx = idx
DraggableLine.min_dist = dist[idx]
DraggableLine.active_line = self

else:
self._handle_idx = None
Expand Down Expand Up @@ -163,6 +174,8 @@ def _on_move(self, event: MouseEvent):

def _on_release(self, event: MouseEvent):
self._handle_idx = None
with DraggableLine.lock:
DraggableLine.active_line, DraggableLine.min_dist = None, math.inf

def on_line_changed(self, func):
"""
Expand Down