refactor: Improve mouse drag and selection event handling logic

This commit is contained in:
n loewen (aider) 2025-06-07 21:37:13 +01:00
parent f527ba4c36
commit a0a5e722b3
1 changed files with 31 additions and 27 deletions

58
gtm2.py
View File

@ -377,35 +377,39 @@ def handle_mouse_input(stdscr, state):
try:
_, mx, my, _, bstate = curses.getmouse()
if bstate & curses.BUTTON1_PRESSED:
# Only start a new action if no action is in progress
if not state.dragging_divider and not state.is_selecting:
if abs(mx - state.divider_col) <= 1:
state.dragging_divider = True
else:
state.is_selecting = True
state.selection_start_coord = (mx, my)
state.selection_end_coord = (mx, my)
if state.dragging_divider:
state.update_divider(mx)
elif state.is_selecting:
state.selection_end_coord = (mx, my)
if bstate & curses.BUTTON1_RELEASED:
# If a drag/selection is in progress
if state.is_selecting or state.dragging_divider:
# Update coordinates for any event during drag
if state.dragging_divider:
state.dragging_divider = False
state.update_divider(mx)
elif state.is_selecting:
state.is_selecting = False
start_x, start_y = state.selection_start_coord
if start_x == mx and start_y == my:
state.focus = "left" if mx < state.divider_col else "right"
else:
copy_selection_to_clipboard(stdscr, state)
state.selection_start_coord = None
state.selection_end_coord = None
state.selection_end_coord = (mx, my)
if bstate & curses.BUTTON1_RELEASED:
# End of drag/selection
if state.dragging_divider:
state.dragging_divider = False
elif state.is_selecting:
# A simple click is a press and release at the same spot
if state.selection_start_coord == state.selection_end_coord:
state.focus = "left" if mx < state.divider_col else "right"
else:
# This was a drag, so copy the selection
copy_selection_to_clipboard(stdscr, state)
state.is_selecting = False
state.selection_start_coord = None
state.selection_end_coord = None
# If no drag/selection is in progress, check for a new one starting
elif bstate & curses.BUTTON1_PRESSED:
if abs(mx - state.divider_col) <= 1:
state.dragging_divider = True
else:
state.is_selecting = True
state.selection_start_coord = (mx, my)
state.selection_end_coord = (mx, my)
except curses.error:
pass