添加组件,实现选择路径功能

This commit is contained in:
iorebuild 2025-08-06 23:37:04 +08:00
parent 3e346314a1
commit 66a08a9c05
3 changed files with 104 additions and 4 deletions

View File

@ -7,6 +7,7 @@ edition = "2024"
smol = "1" # or any other runtime/executor
async-tftp = "0.3.6"
slint = "1.12.1"
rfd = "0.15.4"
[build-dependencies]
slint-build = "1.12"

View File

@ -1,5 +1,74 @@
import { Button, VerticalBox } from "std-widgets.slint";
export component MainWindow inherits Window {
height: 180px;
width: 320px;
title: "VoileTftpTools";
default-font-family: "Segoe UI";
default-font-size: 16px;
default-font-weight: 700;
//选择路径回调
callback BrowserFolder() -> string;
property <string> SelectFolder: "";
//启动回调
callback StartTftp();
//停止回调
callback StopTftp();
Rectangle {
height: 35px;
width: 95px;
x: 10px;
y: 10px;
Button {
height: parent.height;
width: parent.width;
text: "选择路径";
clicked => {
SelectFolder = BrowserFolder();
}
}
}
Rectangle {
height: 35px;
width: 95px;
x: 110px;
y: 10px;
Button {
height: parent.height;
width: parent.width;
text: "启动";
clicked => {
StartTftp();
}
}
}
Rectangle {
height: 35px;
width: 95px;
x: 210px;
y: 10px;
Button {
height: parent.height;
width: parent.width;
text: "停止";
clicked => {
StopTftp();
}
}
}
Text {
x: 13px;
y: 55px;
text: "路径:";
}
Text {
x: 60px;
y: 55px;
width: 200px;
wrap: word-wrap; // 自动换行关键属性
text: SelectFolder;
}
}

View File

@ -1,5 +1,7 @@
use async_tftp::server::TftpServerBuilder;
use async_tftp::Result;
// use async_tftp::server::TftpServerBuilder;
// use async_tftp::Result;
use rfd::FileDialog;
use slint::SharedString;
slint::include_modules!();
// fn main() -> Result<()> {
// smol::block_on(async {
@ -10,5 +12,33 @@ slint::include_modules!();
// }
fn main() {
MainWindow::new().unwrap().run().unwrap();
let ui = MainWindow::new().unwrap();
//选择路径按钮的回调函数
ui.on_BrowserFolder(move || {
let result = FileDialog::new()
.set_directory("/")
.pick_folder();
SharedString::from(
result.map(|p| p.to_string_lossy().into_owned())
.unwrap_or_default()
)
});
//启动按钮的回调函数
ui.on_StartTftp(move || {
println!("Start Tftp Dir");
});
//停止按钮的回调函数
ui.on_StopTftp(move || {
println!("Stop Tftp Dir");
});
ui.run().unwrap();
// // 实现 Slint 中定义的 select_path 函数
// ui.global::<MainWindow>().invoke_select_path(move || {
// // 使用 rfd 库打开文件选择对话框
// if let Some(path) = FileDialog::new().pick_folder() {
// // 将选择的路径设置到 Slint 的 selected_path 属性中
// ui.global::<MainWindow>().set_selected_path(path.to_str().unwrap_or(""));
// }
// });
}