Mouse support not working on Windows - potential fix, and interaction with ScrollablePane

Open
#1,349 5 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
35/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
python
Domain
cli

Research direction

Start with the Windows output modules prompt_toolkit.output.win32 and prompt_toolkit.output.windows10, then trace the mouse dispatch code and its use of renderer.mouse_handlers. Reproduce clicks on Windows with ScrollablePane controls and compare reported positions. Done means mouse interactions work on both Windows output classes without incorrect coordinates or related handler errors.

Written by the indexing model from the issue text.

Description

bug High priority

I have a prompt_toolkit application which has mouse_support=True in its Application call. The mouse support works fine on OS X and Linux, but nothing happens when clicking on controls when running on Windows.

I've seen various issues on here about this (#1232, #1177 and #1059) and there are various fixes proposed.

The fix in #1177 partially worked for me, but also caused other issues. This fix changes a check from checking for an object being a Win32Output instance, to checking for either a Win32Output or Windows10_Output instance. Altering the code as suggested in that issue led to a few other errors being raised - one of which I think is related to the work in #1347, where the way that mouse handlers are stored is changed, and this bit of code hasn't been updated.

I think I've managed to fix it, with the code looking this in the end:

from prompt_toolkit.output.win32 import Win32Output
from prompt_toolkit.output.windows10 import Windows10_Output

if isinstance(output, Win32Output) or isinstance(output, Windows10_Output):
    screen_buffer_info = output.get_win32_screen_buffer_info()
    rows_above_cursor = (
        screen_buffer_info.dwCursorPosition.Y - event.app.renderer._cursor_pos.y
    )
    y -= rows_above_cursor

    # Call the mouse event handler.
    handler = event.app.renderer.mouse_handlers.mouse_handlers[x, y]
    handler(MouseEvent(position=Point(x=x, y=y), event_type=event_type))

I ran into a few issues where it seemed like the mouse positions weren't being reported quite correctly (possibly due to issues with the ScrollablePane?), but I can't seem to reproduce them properly - so it might not be a problem. I'd be grateful for your experienced eye over the code though.

Dominant language
Python
Stars
10.6k
Forks
815
PR merge metrics
No merged PRs in 30d

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from prompt-toolkit/python-prompt-toolkit

All issues in prompt-toolkit/python-prompt-toolkit

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.