micro icon indicating copy to clipboard operation
micro copied to clipboard

Add event argument to onAnyEvent callback

Open cutelisp opened this issue 6 months ago • 9 comments

This will enhance onAnyEvent, allowing it to capture event data that's otherwise inaccessible.

cutelisp avatar Jun 15 '25 01:06 cutelisp

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?

dmaluka avatar Jun 15 '25 13:06 dmaluka

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

cutelisp avatar Jun 15 '25 14:06 cutelisp

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.

dmaluka avatar Jun 15 '25 15:06 dmaluka

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.

JoeKar avatar Jun 15 '25 16:06 JoeKar

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

cutelisp avatar Jun 15 '25 16:06 cutelisp

We could expose them.

Right, I think this would be a great addition anyway

cutelisp avatar Jun 15 '25 16:06 cutelisp

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

Andriamanitra avatar Jun 15 '25 17:06 Andriamanitra

[...] eg. something along the lines of onMouseDrag(x, y, button) and onMouseClick(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...

JoeKar avatar Jun 15 '25 18:06 JoeKar

[...] eg. something along the lines of onMouseDrag(x, y, button) and onMouseClick(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

cutelisp avatar Jun 15 '25 19:06 cutelisp

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.

cutelisp avatar Jun 19 '25 17:06 cutelisp