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.55k stars 601 forks source link

Cannot show files under `C:\Users\[User]\Downloads` when integrate thirdpart file-dialog[native-dialog and rfd] libs into slint. #4773

Closed daydream123 closed 8 months ago

daydream123 commented 8 months ago

slint version: 4.1

I tried to integrate native-dialog/0.7.0 into my slint project to allow user pick file from local disk, and I found that I cannot pick any file under C:\Users\[User]\Downloads, the file dialog shows Working on it... always. ThenI tried anther thirdpart lib called rfd/0.14.0, it has the same result, I tried to run their examples, they work without problem, I don't known why it cannot work in my slint project:

微信截图_20240305223227

I created a simple demo project, blow is my test code:

// ===================== rust file ===================== //
slint::include_modules!();

fn main() -> Result<(), slint::PlatformError> {
    let ui = AppWindow::new()?;
    ui.on_open_dialog({
        move || {
            show_dialog();
        }
    });
    ui.run()
}

fn show_dialog() {
    let current_dir = std::env::current_dir().unwrap();
    let path = rfd::FileDialog::new()
        .set_title("Select file")
        .set_directory(current_dir)
        .pick_file();
    let path = match path {
        Some(path) => path,
        None => return,
    };

    print!("{}", path.to_str().unwrap());
}

// ===================== slint file ===================== //

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

export component AppWindow inherits Window {
    width: 400px;
    height: 200px;

    callback open_dialog();

    Rectangle {
        Button {
            text: "OpenFleDialog";
            clicked => {
                root.open_dialog();
            }
        }
    }
}

// ===================== cargo.toml ===================== //

[package]
name = "test-native-dialog"
version = "0.1.0"
authors = ["xxxx@gmail.com"]
edition = "2021"
build = "build.rs"

[dependencies]
slint = "1.4.1"
slint-interpreter = "1.4.1"
rfd = "0.14.0"

[build-dependencies]
slint-build = "1.4.1"
lifeRobot commented 8 months ago

感觉是线程阻塞问题,你使用tokio::spawn(async{show_dialog()})或者thread::spawn(||{show_dialog()})试一下 我感觉是你调用pick_file之后,会等待你去选择文件,而slint中的渲染由于选择文件被阻断了或者slint的渲染影响了pick_file的线程

daydream123 commented 8 months ago

感觉是线程阻塞问题,你使用tokio::spawn(async{show_dialog()})或者thread::spawn(||{show_dialog()})试一下 我感觉是你调用pick_file之后,会等待你去选择文件,而slint中的渲染由于选择文件被阻断了或者slint的渲染影响了pick_file的线程

I tested, you're right, but other folders like Documents, Images, Videos can show files..., very strange!