awesome-gocui / gocui

Minimalist Go package aimed at creating Console User Interfaces.
BSD 3-Clause "New" or "Revised" License
344 stars 39 forks source link

[BUG] Mouse handling delayed in neovim terminal #99

Closed Kavantix closed 2 years ago

Kavantix commented 2 years ago

Describe the bug Mouse clicks inside neovim terminal are delayed until the next system event. Mouse handling works fine with jroimartin/gocui

To Reproduce Steps to reproduce the behavior:

  1. Open nvim
  2. Open the nvim terminal with :term
  3. Run a gocui project that uses the MouseLeft keybinding
  4. Notice that nothing happened
  5. Press a random key on the keyboard
  6. Notice that the mouse press is now detected

Expected behavior The mouse click should be detected immediately

Environment (please complete the following information):

Context

mjarkk commented 2 years ago

I suspect this has something to do with the move over from termbox to tcell for the terminal rendering library

dankox commented 2 years ago

I was trying to look into this a bit and only found this issue which kinda correspond in tcell: https://github.com/gdamore/tcell/issues/480

However, when I tried to upgrade tcell to the latest version, it didn't really help :/.

Was testing it in neovim and in vim (the same problem). But actually when I was trying _examples/mouse.go application it didn't really get the mouse event even if any key press happen.
Not sure if this is because of the specific of mouse.go demo app, or anything else. All the other examples worked well, so the mouse is the only affected.

I will try to debug it later.

Kavantix commented 2 years ago

@dankox anything I can specifically test?

dankox commented 2 years ago

@Kavantix I'm sorry, I kinda forgot about this issue. However, I was doing some tests and I'm not really sure if this works at all in any case anywhere.

What I was using initialy was vim with :term command. But I couldn't make it work even with original gocui which uses termbox, so I tried it also in nvim and the result is the same.

Mouse doesn't work for me in vim or nvim either using termbox-go or tcell.

My environment is:

Can you please provide a source code or example of application using original gocui which uses mouse and works as you described? I want to try to know if it is just my environment or something else. Thanks...

Kavantix commented 2 years ago

@dankox you probably don’t have mouse mode enabled in vim then (it’s off by default) You can enable it using set mouse=a

mjarkk commented 2 years ago

Just tried what you said and my mouse input has no delay:

  1. Open nvim
  2. Ran set mouse=a
  3. Ran :term
  4. Ran _examples/mouse.go (I also tested lazygit and there the mouse input is also supper fast)

Envourment:

May i ask what terminal emulator you are using?

Kavantix commented 2 years ago

Weird, I have tried on several terminal emulators (iTerm, kitty, alacritty, macos terminal) and all behave the same on my system. I am using neovim nightly though.

Lazygit is indeed fast but that is still using the termbox version right?

mjarkk commented 2 years ago

Just tried out the same thing using Neovim nightly (v0.6.0-dev+551-ged2573b7e from aur/neovim-nightly-bin) but it's still as fast.

Lazygit is indeed fast but that is still using the termbox version right?

I think so, this was a tui program i knew has mouse support so i tested it also. tough now looking at it back it was a bit pointless.

mjarkk commented 2 years ago

Beside gnome-terminal also just tested Alacritty and Tilix but no difference both seem to be as responsive as running directly in the terminal, (this was with neovim v0.6.0)

dankox commented 2 years ago

Ooooooooooh... now I feel stupid 🤦‍♀️ my bad :)

So I've tried it with set mouse=a, and I see the difference.

But I have to say, this is a bit weird, because for vim it works ok... no delay whatsoever. For nvim however, I can see the delay by one event mentioned in the issue.
If I try the original gocui from jroimartin, there is no delay. So it's definitely tcell.

Looking at it more precisely, I think there is something "wrong" with nvim. I have a guess on what is the main difference (keep it mind this is from top of my head, didn't have time to look into code and debug, will do later).

tcell register two events, mouse press and mouse release (from what I remember). And I can see in vim when I try tcell demo app _demos/mouse.go the Button1 test appears just shortly and disapear right away after the mouse release.
However in nvim, this text appears and does not disapear until next event (if next is again mouse click, it will stay).

From this I can guess that nvim just doesn't propagate mouse release event to terminal (or does it later with next event). Why it works for original gocui is because termbox-go does not have mouse release. gocui does not have mouse release either, this is just implementation of tcell mouse events made to be as similar to original gocui as possible.

What we can do:

  1. register mouse click on mouse click (not on mouse release)
  2. introduce new mouse event which is mouse release (could be beneficial if somebody wants to implement drag and drop in TUI apps)
  3. report to nvim that the event is not send (however, this has to be debugged first to be sure)

Anyway, regardless of next action, I want to do some more debugging on this in nvim because now I have at least an idea what might go wrong.
The first option shouldn't be that hard to implement, as we are in charge of what to do with mouse events, but I'm not sure if it's the best.

dankox commented 2 years ago

@mjarkk can you try tcell demo application from _demos/mouse.go if it has similar behaviour on linux as I desribed in the post above? This could also narrow down a problem that maybe only macOS version of nvim has this problem, or maybe it's general.

Kavantix commented 2 years ago

@dankox thanks for the extensive explanation! Let me know if I can help in any way

Kavantix commented 2 years ago

Btw, my preference would be adding the mouse release, a drag feature is something I'm planning on adding to one of my projects so would be very welcome

mjarkk commented 2 years ago

@dankox Just tried out the _demos/mouse.go and now i see the problem the issue describes also on linux with neovim v0.6.0

dankox commented 2 years ago

@Kavantix if you want, you can try to debug it in tcell_driver.go program (in polling function) to see what events are provided and at what time. That's kinda what I want to do (to see when the mouse release is passed from nvim).

I want to look at it later this day when I will get the time to get better idea of what is going on.

Kavantix commented 2 years ago

@dankox @mjarkk I finally go around to testing this. It seems neovim is simply not sending mouse release events

However, the old driver that didnt use tcell seems to handle click events on mouse down not mouse release which is why it's not really a problem with the older version.

Kavantix commented 2 years ago

I opened neovim/neovim#16371 at their side

dankox commented 2 years ago

@Kavantix thanks for letting us know. I can see that in neovim they are fixing propagation of that event to terminal so that's cool.

I didn't have much time recently to look more into this, but I think we definitely should add the MouseRelease event and implement it properly. Keeping this issue open until it's done.
It should be done correctly so other apps are not affected by it and want to be sure to be compatible with original gocui implementation. So it might take some time, but will try to get to it as soon as possible.