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 565 forks source link

Render loop hangs with using Arc<Mutex<>> in repeated timer #1137

Closed feelingnothing closed 2 years ago

feelingnothing commented 2 years ago

What I like when starting a new GUI framework or lib is to try to make a simple CPS counter that edits count in interval Thats my code:

use std::sync::{Arc, Mutex};
use std::time::Duration;

slint::slint! {
    import { Button } from "std-widgets.slint";

    MainWindow := Window {
        callback button_press();
        property <int> clicks;

        Button {
            text: parent.clicks;
            clicked => {
                root.button_press();
            }
        }
    }
}

#[tokio::main]
async fn main() {
    let get_timestamp = || std::time::SystemTime::now().duration_since(std::time::SystemTime::UNIX_EPOCH).expect("").as_millis();

    let app = MainWindow::new();
    let clicks = Arc::new(Mutex::new(vec![]));
    let weak = app.as_weak();

    let timer_clicks = Arc::clone(&clicks);
    let timer = slint::Timer::default();
    timer.start(slint::TimerMode::Repeated, Duration::from_millis(100), move || {
        let mut clicks = timer_clicks.lock().expect("");
        weak.unwrap().set_clicks(clicks.len() as i32);
        clicks.retain(|f| (get_timestamp() - 1000) < *f);
    });

    let button_clicks = Arc::clone(&clicks);
    app.on_button_press(move || {
        let mut clicks = button_clicks.lock().expect("");
        println!("Pressed {}", clicks.len());
        clicks.push(get_timestamp());
    });

    app.run();
}

And the behavior I'm getting: result

ogoffart commented 2 years ago

Thanks for your bug report. Are you using the Qt backend or the GL backend? This is probably the same as https://github.com/slint-ui/slint/issues/418

feelingnothing commented 2 years ago

Thanks for your bug report. Are you using the Qt backend or the GL backend? This is probably the same as #418

Yes, I'm using GL. Unfortunate I guess

tronical commented 2 years ago

If this was similar to #418 then commit 575665994aa348abec097178f3cc0b57333c110e may fix this. That said, I can't reproduce this issue - neither before that commit nor with current git. @feelingnothing , can you still reproduce it?

feelingnothing commented 2 years ago

Couldn't reproduce, fixed.

tronical commented 2 years ago

Lovely, thanks for checking - much appreciated!