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.56k stars 604 forks source link

hide() directly closes the program #5161

Closed han1548772930 closed 6 months ago

han1548772930 commented 6 months ago

On windows, the program hangs directly after hide(). How to run in the background


use std::thread;

slint::include_modules!();

fn main() -> Result<(), slint::PlatformError> {
    let ui = AppWindow::new()?;
    let handle_weak = ui.as_weak();
    ui.on_request_increase_value({
        let ui_handle = ui.as_weak();
        move || {
            let ui = ui_handle.unwrap();
            ui.set_counter(ui.get_counter() + 1);
        }
    });
    let _ = std::thread::spawn(move || {
        thread::sleep(std::time::Duration::from_secs(2));
        let _ = handle_weak.upgrade_in_event_loop(move |handle| {
            let _ = handle.window().hide();

        });

    });

    ui.run()
}
han1548772930 commented 6 months ago

ok , I've tried it so far but I can't confirm if it's the way it should be. But there is no such program in the tray in the lower right corner.


use std::thread;

use slint::run_event_loop_until_quit;

slint::include_modules!();

fn main() -> Result<(), slint::PlatformError> {
    let ui = AppWindow::new()?;

    ui.on_request_increase_value({
        let ui_handle = ui.as_weak();
        move || {
            let ui = ui_handle.unwrap();
            ui.set_counter(ui.get_counter() + 1);
            let _ = run_event_loop_until_quit();
            let _ = ui.window().hide();
            thread::sleep(std::time::Duration::from_secs(3));
            let _ = ui.window().show();
        }
    });

    // let _ = std::thread::spawn(move || {
    //     let _ = run_event_loop_until_quit();
    // });

    ui.run()
}
han1548772930 commented 6 months ago

https://github.com/Guiguiprim/slint_systry_app

hunger commented 6 months ago

Try

let _ = ui.show();
run_event_loop_until_quit();

instead of ui.run().