Add event argument to onAnyEvent callback
This will enhance onAnyEvent, allowing it to capture event data that's otherwise inaccessible.
Ok, at least it wouldn't break compatibility with existing plugins (I thought it would, but I've just checked that it doesn't)...
I'm wondering what is the use case, and how exactly you are gonna use this tcell.Event type from Lua code. Could you share an example?
I wan't to be able to know the cords of the cursor while dragging. Afais, those values aren't exposed to lua side. I think it's more convenient to expose the event to onAnyEvent than on the actions callbacks.
function onAnyEvent(event)
local ex, ey = event:Position()
...
end
local ex, ey = event:Position()
You are assuming that the event is EventMouse?
Did you actually try to use that?
I wan't to be able to know the cords of the cursor while dragging. Afais, those values aren't exposed to lua side.
We could expose them.
Yes, the identification of all of the different event types could be quite hard in this case. With this PR you will have the access to all and everything, but need to take care to access the right type/memory.
You are assuming that the event is
EventMouse? Did you actually try to use that?
Yes, I've tried it and it works (not heavily tested tho). Maybe I should've sent a better snippet.
Maybe not the most neat logic, but this was my approach.
clicking = false
function onMousePress()
clicking = true
end
function onMouseRelease()
clicking = false
end
function onAnyEvent(event)
if clicking and event then
local ex, ey = event:Position()
...
end
end
We could expose them.
Right, I think this would be a great addition anyway
I don't think exposing tcell.Event directly is the best approach. To access anything useful plugins will need to somehow figure out the concrete type of the event which is not trivial to do in Lua (@cutelisp's implementation above crashes if the user happens to press a key while holding down a mouse button). It also poses difficulties for documentation/maintenance because the types live in a separate 3rd party package (although I guess micro-editor/tcell has already somewhat diverged from the upstream tcell).
As a plugin author I would rather have new "event callbacks" [^1] with more specific arguments and stronger backwards compatibility guarantees, eg. something along the lines of onMouseDrag(x, y, button) and onMouseClick(x, y, button).
[^1]: @dmaluka brought up the idea of adding more of these in https://github.com/zyedidia/micro/issues/3550#issuecomment-2508735791
[...] eg. something along the lines of
onMouseDrag(x, y, button)andonMouseClick(x, y, button).
I'd prefer something like onMouseEvent(MouseEvent), which should include key/button, press, release and positions etc.pp.
I'm no friend of having a (callback-)function for every single bit, but I'm not a plugin developer...
[...] eg. something along the lines of
onMouseDrag(x, y, button)andonMouseClick(x, y, button).I'd prefer something like
onMouseEvent(MouseEvent), which should include key/button, press, release and positions etc.pp. I'm no friend of having a (callback-)function for every single bit, but I'm not a plugin developer...
Since the use case by now is just for mouse events maybe we should go this way. I prefer that idea over @Andriamanitra one.
We could create onMouseEvent(EventMouse) since EventMouse already have methods for all those things, the only info it doesn't provide have is the state (drag, click or release) it can be achieved by logic on lua side tho.
MouseEvent have a state atribute, not sure if would be neat to have MouseEvent and EventMouse both as args
Changed the PR towards this suggestion https://github.com/zyedidia/micro/pull/3774#issuecomment-2974451484 Tried to make things simple and just pass the essential args. I'll appreciate feedback.