fix: Improve mouse click handling for pane switching

This commit is contained in:
n loewen (aider) 2025-06-07 22:56:51 +01:00
parent a035a350d4
commit b0a142e90c
1 changed files with 39 additions and 43 deletions

82
gtm2.py
View File

@ -121,6 +121,7 @@ class AppState:
self.is_selecting = False
self.selection_start_coord = None
self.selection_end_coord = None
self.click_position = None # Store click position to detect clicks vs. drags
self.last_bstate = 0
self.mouse_x = -1
self.mouse_y = -1
@ -442,63 +443,58 @@ def handle_mouse_input(stdscr, state):
state.last_bstate = bstate
state.mouse_x, state.mouse_y = mx, my
# If selection is in progress, only end it on mouse button release or press
if state.is_selecting and (bstate & curses.BUTTON1_RELEASED or bstate & curses.BUTTON1_PRESSED):
# End the current selection
state.is_selecting = False
state.selection_start_coord = None
state.selection_end_coord = None
state.focus = "left" if mx < state.divider_col else "right"
# Redraw immediately to clear selection highlight
draw_ui(stdscr, state)
# If this was a new click, start a new selection
if bstate & curses.BUTTON1_PRESSED:
state.is_selecting = True
state.selection_start_coord = (mx, my)
state.selection_end_coord = (mx, my)
# Redraw immediately to show selection highlight as soon as it starts
draw_ui(stdscr, state)
return
# If a drag/selection is in progress
if state.dragging_divider:
# Update coordinates for any event during drag
state.update_divider(mx)
if bstate & curses.BUTTON1_RELEASED:
# End of drag
state.dragging_divider = False
# If no drag/selection is in progress, check for a new one starting
elif bstate & curses.BUTTON1_PRESSED:
# Handle mouse button press
if bstate & curses.BUTTON1_PRESSED:
# Check if clicking near divider
if abs(mx - state.divider_col) <= 1:
state.dragging_divider = True
else:
# Start a new selection
state.is_selecting = True
state.selection_start_coord = (mx, my)
state.selection_end_coord = (mx, my)
# Redraw immediately to show selection highlight as soon as it starts
# Store the click position for later comparison
state.click_position = (mx, my)
# Redraw immediately to show selection highlight
draw_ui(stdscr, state)
# Update selection end coordinates during drag
elif state.is_selecting:
state.selection_end_coord = (mx, my)
# Redraw immediately to show selection highlight during drag
draw_ui(stdscr, state)
return
if bstate & curses.BUTTON1_RELEASED:
# End of selection
# A simple click is a press and release at the same spot
if state.selection_start_coord == state.selection_end_coord:
# Handle mouse button release
if bstate & curses.BUTTON1_RELEASED:
# End of divider drag
if state.dragging_divider:
state.dragging_divider = False
return
# End of selection
if state.is_selecting:
# Check if this was a click (press and release at same position)
# or very close to the same position (within 1-2 pixels)
if (state.click_position and
abs(state.click_position[0] - mx) <= 2 and
abs(state.click_position[1] - my) <= 2):
# This was a click, so switch panes
state.focus = "left" if mx < state.divider_col else "right"
else:
# This was a drag, so copy the selection
elif state.selection_start_coord and state.selection_end_coord:
# This was a drag selection, copy the text
copy_selection_to_clipboard(stdscr, state)
# Reset selection state
state.is_selecting = False
state.selection_start_coord = None
state.selection_end_coord = None
state.click_position = None
return
# Handle mouse movement during drag operations
if state.dragging_divider:
# Update divider position during drag
state.update_divider(mx)
elif state.is_selecting:
# Update selection end coordinates during drag
state.selection_end_coord = (mx, my)
# Redraw immediately to show selection highlight during drag
draw_ui(stdscr, state)
except curses.error:
pass