Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Timer example does not work correctly on current branch #3875

Closed
ogoffart opened this issue Nov 7, 2023 Discussed in #3873 · 3 comments · Fixed by #3876
Closed

Timer example does not work correctly on current branch #3875

ogoffart opened this issue Nov 7, 2023 Discussed in #3873 · 3 comments · Fixed by #3876
Labels
a:platform-windows Issue specific to Windows (mT,bS) bug Something isn't working priority:blocker Highest priority for issues needed to be done as fast as possibe
Milestone

Comments

@ogoffart
Copy link
Member

ogoffart commented Nov 7, 2023

Discussed in #3873

Originally posted by HitecExports November 7, 2023
Hello!

Platform: Windows 11
Rust 1.73
slint = { git = "https://github.com/slint-ui/slint.git", branch = "master" }

Timer example does not work properly in current slint branch

"Elapsed Time" progress bar updates only while moving mouse in any area (even outside the main window)

So timer does not fire event automatically.

I noticed this problem for a week ago.

Before there were no such problem.

On Slint 1.2.2 same Timer Example works fine

image

Code (copied from Timer example)

// Copyright © SixtyFPS GmbH <info@slint.dev>
// SPDX-License-Identifier: MIT
use slint::{Timer, TimerMode};

slint::slint!(

    import { LineEdit, Button, Slider, HorizontalBox, VerticalBox } from "std-widgets.slint";

    export component MainWindow inherits Window {
        in-out property <duration> total-time: slider.value * 1s;
        in-out property <duration> elapsed-time;

        callback tick(duration);
        tick(passed-time) => {
            root.elapsed-time += passed-time;
            root.elapsed-time = min(root.elapsed-time, root.total-time);
        }

        VerticalBox {
            HorizontalBox {
                padding-left: 0;
                Text { text: "Elapsed Time:"; }
                Rectangle {
                    min-width: 200px;
                    max-height: 30px;
                    background: gray;
                    Rectangle {
                        x:0;
                        height: 100%;
                        width: parent.width * (root.elapsed-time/root.total-time);
                        background: lightblue;
                    }
                }
            }
            Text{
                text: (root.total-time / 1s) + "s";
            }
            HorizontalBox {
                padding-left: 0;
                Text {
                    text: "Duration:";
                    vertical-alignment: center;
                }
                slider := Slider {
                    maximum: 30s / 1s;
                    value: 10s / 1s;
                    changed(new-duration) => {
                        root.total-time = new-duration * 1s;
                        root.elapsed-time = min(root.elapsed-time, root.total-time);
                    }
                }
            }
            Button {
                text: "Reset";
                clicked => {
                    root.elapsed-time = 0
                }
            }
        }
    }
);

pub fn main() {
    let main_window = MainWindow::new().unwrap();
    let timer = Timer::default();
    {
        let main_window_weak = main_window.as_weak();
        timer.start(
            TimerMode::Repeated,
            std::time::Duration::from_millis(10),
            move || {
                let main_window = main_window_weak.unwrap();
                main_window.invoke_tick(10);
            },
        );
    }
    main_window.run().unwrap();
}
@ogoffart ogoffart added this to the 1.3 milestone Nov 7, 2023
@ogoffart ogoffart added a:platform-windows Issue specific to Windows (mT,bS) bug Something isn't working priority:blocker Highest priority for issues needed to be done as fast as possibe labels Nov 7, 2023
@ogoffart
Copy link
Member Author

ogoffart commented Nov 7, 2023

I've tried to debug to figure out what's wrong.

In the event loop, we get a AboutToWait event, and the next_timer is about to fire so the next_timer is very low value:

https://github.com/slint-ui/slint/blob/d82e3d4e5ce4e67a4d417ebcaf204680bf432072/internal/backends/winit/event_loop.rs#L571-573

We set control flow to ControlFlow with a wait duration of a a few, or 0ms. But we're never woken up.
Changing that to ControlFlow::Poll doesn't help.

ogoffart added a commit that referenced this issue Nov 7, 2023
If we set the control flow to Wait, winit will always wait even if we
set the control flow later to some smaller timeout in the AboutToWait
event.
Wait is the default anyway in winit 0.29 so this is not required

I'd say this is a bug in winit, but we can easily work it around

Fix #3875
@tronical
Copy link
Member

tronical commented Nov 7, 2023

I think winit uses SetTimer and the minimum resolution of that is around 16ms (USER_TIMER_MINIMUM). It might make sense for winit to use multimedia timers instead (timeSetEvent with a callback that wakes up winit), they offer a higher resolution. (That’s what Qt defaults to)

@ogoffart
Copy link
Member Author

ogoffart commented Nov 7, 2023

Filled rust-windowing/winit#3215

ogoffart added a commit that referenced this issue Nov 7, 2023
On Windows, winit doesn't respect the set_control_flow from the
AboutToWait event.
This is a bug in winit, but we can easily work it around

Fix #3875
ogoffart added a commit that referenced this issue Nov 8, 2023
On Windows, winit doesn't respect the set_control_flow from the
AboutToWait event.
This is a bug in winit, but we can easily work it around

Fix #3875
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
a:platform-windows Issue specific to Windows (mT,bS) bug Something isn't working priority:blocker Highest priority for issues needed to be done as fast as possibe
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants