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.93k stars 568 forks source link

How can I do something when widget or window lose focus? #3250

Closed Horbin-Magician closed 1 year ago

Horbin-Magician commented 1 year ago

My platform is windows11, and using Slint with Rust. I want to hide my window when it loss focus. But I can't find any callback or api to do so. Please help me. (ps: It is also difficult for me to change the backend of Slint. I try to change the SLINT_BACKEND environment variable of Windows11, but there is no change. How can I know what backend Slint is using?)

ogoffart commented 1 year ago

I think it's currently not possible to hide the window when it looses focus. This is a feature we'd need to add. When a widget loses focus, the has-focus property change. Again it is not possible to detect change, but there are workarounds: https://github.com/slint-ui/slint/issues/112#issuecomment-1065866865

(ps: It is also difficult for me to change the backend of Slint. I try to change the SLINT_BACKEND environment variable of Windows11, but there is no change. How can I know what backend Slint is using?)

One way to know the backend is to use SLINT_DEBUG_PERFORMANCE env variable. For example, set it to SLINT_DEBUG_PERFORMANCE=refresh_lazy,console,overlay and it should print in the console what backend is used.

Horbin-Magician commented 1 year ago

I think it's currently not possible to hide the window when it looses focus. This is a feature we'd need to add. When a widget loses focus, the has-focus property change. Again it is not possible to detect change, but there are workarounds: #112 (comment)

(ps: It is also difficult for me to change the backend of Slint. I try to change the SLINT_BACKEND environment variable of Windows11, but there is no change. How can I know what backend Slint is using?)

One way to know the backend is to use SLINT_DEBUG_PERFORMANCE env variable. For example, set it to SLINT_DEBUG_PERFORMANCE=refresh_lazy,console,overlay and it should print in the console what backend is used.

Thanks for your reply! SLINT_DEBUG_PERFORMANCE env variable is very helpful for me. However, the workarounds #112 (comment) do not work now.

    export component Demo inherits Window {
        callback my_callback(bool) -> color;
        my_callback(x) => { red; }
        background: my_callback(touch.has-hover);
        touch := TouchArea {  height: 50%;  }
    }

The 'my_callback' method causes an error: Call of impure callback 'my-callback'.

Now, I'm trying to implement this function with TouchArea, and really hope a "focus changing" callback (like ActivationChange event in Qt) can be added. (Maybe at least for FocusScope?)

ogoffart commented 1 year ago

The 'my_callback' method causes an error: Call of impure callback 'my-callback'.

right, you need to annotate my_callback as pure

Now, I'm trying to implement this function with TouchArea, and really hope a "focus changing" callback

Yes, this would be a good addition: https://github.com/slint-ui/slint/issues/1236