textual icon indicating copy to clipboard operation
textual copied to clipboard

Fix erroneous ButtonDown mouse event reporting.

Open EricWF opened this issue 2 years ago • 10 comments

After v0.38.0, I was seeing Input fields lose focus whenever the mouse left the input field area (but without having clicked anywhere). This behavior started after the release v0.38.0 release when Mouse ButtonDown began to trigger loss of focus (This change seemed intentional). However, I wasn't pressing the mouse button down.

The erronious ButtonDown events were actually MouseMove events with no keys held down. According to the docs for xterm escape sequences, when SGR (1006) mode is enabled, the encoded button value isn't always incremented by 32 for move movements events. [1]

I believe the correct fix to this issue is to detect mouse movement events with no button down by checking if the "pressed button" is 0. Which appears to indicate that no mouse button is pressed (in this specific case). Whereas, the button value will be set to 1 when left click is pressed for example.

I'm not sure if this change is fully correct for terminals which don't support SGR mode.

[1] https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h3-Extended-coordinates

Please review the following checklist.

  • [x] Docstrings on all new or modified functions / classes
  • [x] Updated documentation
  • [x] Updated CHANGELOG.md (where appropriate)

EricWF avatar Nov 07 '23 20:11 EricWF

I'm not seeing the effect you mention in my usual environment (macOS, kitty); so we can recreate and then test out this PR, what environment are you seeing this in?

davep avatar Nov 09 '23 09:11 davep

Oh, good question. After some testing this appears to only reproduce inside Intellij's builtin terminal implementation. It does not seem to reproduce with gnome's terminal implementation. I'm not sure exactly what to provide, so let me know if you would like something in particular. Here are some relevant environment variables:

JEDITERM_SOURCE_ARGS=
FIG_JETBRAINS_SHELL_INTEGRATION=1
TERM=xterm-256color
LANG=en_US.UTF-8
XMODIFIERS=@im=ibus
XDG_SESSION_TYPE=x11
TERMINAL_EMULATOR=JetBrains-JediTerm
XDG_VTNR=2
VTE_VERSION=7400
SHELL=/usr/bin/fish
COLORTERM=truecolor
PLATFORM_TYPE=Linux

I could dig more into whether the bug is in Intellij's terminal. But the behavior seems potentially inline with the very hard to understand documentation about terminal modes.

Testing with my fix applied causes mouse events in TextArea an Input to match across both terminal implementations, and brings the behavior with the Intellij terminal into line with Gnome terminals existing behavior.

EricWF avatar Nov 10 '23 13:11 EricWF

Ah, sorry I just saw your commit. Here's some of the information you need...

You can find a video of the bug & fix here

Here's the code I used to generate the video:

from textual.app import App, ComposeResult
from textual.widgets import Input, TextArea


class InputApp(App):
    def compose(self) -> ComposeResult:
        yield TextArea("I'm not touching the mouse buttons.")


if __name__ == "__main__":
    app = InputApp()
    app.run()

Textual Diagnostics

Versions

Name Value
Textual 0.41.0
Rich 13.6.0

Python

Name Value
Version 3.11.6
Implementation CPython
Compiler GCC 13.2.0
Executable /home/eric/.pyenvs/idea/bin/python3

Operating System

Name Value
System Linux
Release 6.5.0-2-amd64
Version #1 SMP PREEMPT_DYNAMIC Debian 6.5.6-1 (2023-10-07)

Terminal

Name Value
Terminal Application PyCharm
TERM xterm-256color
COLORTERM truecolor
FORCE_COLOR Not set
NO_COLOR Not set

Rich Console options

Name Value
size width=112, height=22
legacy_windows False
min_width 1
max_width 112
is_terminal False
encoding utf-8
max_height 22
justify None
overflow None
no_wrap False
highlight None
markup None
height None

Additionally, the output captured from _xterm_parser.py can be found below. mousecodes.log

EricWF avatar Nov 11 '23 02:11 EricWF

@davep Let me know if I can provide more.

EricWF avatar Nov 17 '23 06:11 EricWF

I suspect everything necessary is here; just needs one of us to find the time to recreate the problem and review (personally I'm on holiday at the moment).

Might take a wee while (lots of holiday all around heading into this time of year, plus lots going on around Textual), but we'll get to it.

davep avatar Nov 17 '23 06:11 davep

@davep Sounds good. Enjoy your time away.

I would love to land this before the next release. Because I use textual everywhere, having to patch it is :-(

PS. I fixed the link for the video.

EricWF avatar Nov 18 '23 17:11 EricWF

Hey friends, Happy Holidays!

I see there have been a couple of releases since this PR has been created. Unfortunately none of them contain a fix for the issues described here.

How can I help this land?

EricWF avatar Jan 04 '24 15:01 EricWF

We have a backlog. But I will be looking at this soon...

willmcgugan avatar Jan 04 '24 15:01 willmcgugan

@willmcgugan Please let me know if I can help move things along.

EricWF avatar Jan 30 '24 19:01 EricWF

Would you mind creating an issue for this. Or linking to it if it already exists.

willmcgugan avatar Jan 30 '24 19:01 willmcgugan

I haven't manage to reproduce this, but it doesn't break anything. Thanks for the PR, and sorry it took so long to go in.

willmcgugan avatar Jun 17 '24 14:06 willmcgugan