slint-ui / slint

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

key-pressed no animation #1255

Closed FloVanGH closed 2 years ago

FloVanGH commented 2 years ago

I use a FocusScope to change a selected-index by left or right arrow key pressed. I navigate translate x of my layout to show the element on the selected-index. The change of x is animated. If I change the selected-index by a clicked handler the animation works fine but if i press left or right arrow key, x is changed but the animation does not work.

 focus-scope:= FocusScope {
        key-pressed(event) => {
            if(event.text == Keys.RightArrow) {
                root.selected-index = Math.min(root.selected-index + 1, 3);
                return accept;
            }

            if(event.text == Keys.LeftArrow) {
                root.selected-index = Math.max(root.selected-index - 1, 0);
                return accept;
            }

            return reject;
        }
    }

    HorizontalLayout { 
        animate x { duration: 300ms; easing: ease-in; }
        x: root.selected-index * -136px;
        spacing: 8px;
        height: parent.height;

        @children
    }
FloVanGH commented 2 years ago

Playing a little bit with fast pressing, the animation sometimes works. If I press slow no animation.

ogoffart commented 2 years ago

Thanks for your bug report. I can reproduce the bug in the wasm code editor But i cannot really reproduce it on the X11 desktop (both backend).

I'll have to investigate why the animation don't work. (Maybe something related to the time computation on wasm? (wild guess))

What platform/backend are you using?

FloVanGH commented 2 years ago

@ogoffart thank you for the answer.

I tested it on macOS. My guess is, that the key event blocks the animation until the key is released. But in the meantime the x position is set through the binding.

ogoffart commented 2 years ago

I found out the cause: we were not registering time update before key events, causing the runtime to think that the time was still earlier than it was when the event occurs, making the starting time of the animation too much in the past.

Fix in https://github.com/slint-ui/slint/pull/1256

FloVanGH commented 2 years ago

@ogoffart: nice thank you :-)

FloVanGH commented 2 years ago

I checked your PR, it works well now :-).