slint-ui / slint

Slint is a declarative GUI toolkit to build native user interfaces for Rust, C++, or JavaScript apps.
https://slint.dev
Other
16.94k stars 568 forks source link

Get the mouse position without dragging inside a Window component #2770

Closed ben-cottrell-nz closed 9 months ago

ben-cottrell-nz commented 1 year ago

Discussed in https://github.com/slint-ui/slint/discussions/2769

Originally posted by **ben-cottrell-nz** May 24, 2023 Within a component, I would like to get the mouse position always, not just when dragging. In the below snippet, I only see cursor position when clicking and moving. ``` export component TanksGameView inherits Window { width: 1024px; height: 768px; forward-focus: player-key-handler; touchArea := TouchArea { width: parent.width; height: parent.height; moved => { debug(touchArea.mouse-x / 1px + "," + touchArea.mouse-y / 1px) } } } ```
FloVanGH commented 1 year ago

Hey,

it should be possible without pressed. This code snippet works for me:

 i-touch-area := TouchArea { }

    Text {
        color: white;

        text: i-touch-area.mouse-x / 1px;
    }

Documentation: https://slint-ui.com/releases/1.0.2/docs/slint/src/builtins/elements.html#id23

ogoffart commented 1 year ago

I think the issue is then that the moved callback is only called when the cursor is pressed.

We could change that to be called all the time. I wonder if that would be a breaking change.

tronical commented 1 year ago

I saw a similar request for this (always invoke, not only when pressed). I think it's a good idea.

MahitMehta commented 9 months ago

Has there been any progress on this? I am also in need of this feature and it doesn't seem very complicated to add as the moved callback already exists.

hunger commented 9 months ago

@MahitMehta: I took the liberty to create a PR for this. Is that something that works for you? Any testing would be welcome.

MahitMehta commented 9 months ago

@MahitMehta: I took the liberty to create a PR for this. Is that something that works for you? Any testing would be welcome.

@hunger Wow, thank you so much! This is perfect, I just cloned your branch, and it works flawlessly for my needs. However, would making these changes remove the functionality to detect mouse-dragging events (the current functionality)?

hunger commented 9 months ago

Yes, this is a incompatibility. @ogoffart also pointed that out in review.

We could add a new callback, maybe even providing button information and modifiers pressed? So that people can easily implement shift-control-middle-mouse-button drags... but that might just be overdoing it.

Would you need more complex drag operations? My impression is that UIs even on the desktop are heavily inspired by phone UIs these days, so maybe it is a good thing to not expose all those knobs to UI designers anymore? It will at least push towards more finger-friendly UIs that way.

MahitMehta commented 9 months ago

I was also thinking of making a separate callback to retain the current functionality. However, I agree that it may be unnecessary and even better not to expose too many "knobs". For my purposes, I just need the mouse move events, so the behavior change is fine by me.