From a0a5e722b3997ba3bc059d5bc09aafdc7a254035 Mon Sep 17 00:00:00 2001 From: "n loewen (aider)" Date: Sat, 7 Jun 2025 21:37:13 +0100 Subject: [PATCH] refactor: Improve mouse drag and selection event handling logic --- gtm2.py | 58 ++++++++++++++++++++++++++++++--------------------------- 1 file changed, 31 insertions(+), 27 deletions(-) diff --git a/gtm2.py b/gtm2.py index d636314..52ef134 100755 --- a/gtm2.py +++ b/gtm2.py @@ -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