程序写完
This commit is contained in:
parent
01b37916c1
commit
69469ee1f4
@ -1,12 +1,22 @@
|
|||||||
import { Button, VerticalBox } from "std-widgets.slint";
|
import { Button, VerticalBox } from "std-widgets.slint";
|
||||||
|
|
||||||
|
export global Globals {
|
||||||
|
//启动按钮状态
|
||||||
|
in-out property <bool> StartEnable: false;
|
||||||
|
//停止按钮状态
|
||||||
|
in-out property <bool> StopEnable: false;
|
||||||
|
}
|
||||||
|
|
||||||
export component MainWindow inherits Window {
|
export component MainWindow inherits Window {
|
||||||
|
title: "VoileTftpTools";
|
||||||
|
icon: @image-url("../tftp.png");
|
||||||
height: 180px;
|
height: 180px;
|
||||||
width: 320px;
|
width: 320px;
|
||||||
title: "VoileTftpTools";
|
|
||||||
default-font-family: "Segoe UI";
|
default-font-family: "Segoe UI";
|
||||||
default-font-size: 16px;
|
default-font-size: 16px;
|
||||||
default-font-weight: 700;
|
default-font-weight: 700;
|
||||||
|
in-out property <bool> StartEnable <=> Globals.StartEnable;
|
||||||
|
in-out property <bool> StopEnable <=> Globals.StopEnable;
|
||||||
//选择路径回调
|
//选择路径回调
|
||||||
callback BrowserFolder() -> string;
|
callback BrowserFolder() -> string;
|
||||||
property <string> SelectFolder: "";
|
property <string> SelectFolder: "";
|
||||||
@ -39,6 +49,7 @@ export component MainWindow inherits Window {
|
|||||||
height: parent.height;
|
height: parent.height;
|
||||||
width: parent.width;
|
width: parent.width;
|
||||||
text: "启动";
|
text: "启动";
|
||||||
|
enabled: StartEnable;
|
||||||
clicked => {
|
clicked => {
|
||||||
StartTftp();
|
StartTftp();
|
||||||
}
|
}
|
||||||
@ -54,6 +65,7 @@ export component MainWindow inherits Window {
|
|||||||
height: parent.height;
|
height: parent.height;
|
||||||
width: parent.width;
|
width: parent.width;
|
||||||
text: "停止";
|
text: "停止";
|
||||||
|
enabled: StopEnable;
|
||||||
clicked => {
|
clicked => {
|
||||||
StopTftp();
|
StopTftp();
|
||||||
}
|
}
|
||||||
|
2
config.toml
Normal file
2
config.toml
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
[build]
|
||||||
|
rustflags = ["-C", "link-arg=resource.res"]
|
1
resource.rc
Normal file
1
resource.rc
Normal file
@ -0,0 +1 @@
|
|||||||
|
0001 ICON "logo.ico"
|
BIN
resource.res
Normal file
BIN
resource.res
Normal file
Binary file not shown.
59
src/main.rs
59
src/main.rs
@ -1,3 +1,4 @@
|
|||||||
|
#![windows_subsystem = "windows"]
|
||||||
use async_tftp::server::TftpServerBuilder;
|
use async_tftp::server::TftpServerBuilder;
|
||||||
use async_tftp::Result;
|
use async_tftp::Result;
|
||||||
use rfd::FileDialog;
|
use rfd::FileDialog;
|
||||||
@ -10,7 +11,6 @@ slint::include_modules!();
|
|||||||
struct ServerState {
|
struct ServerState {
|
||||||
abort_handle: Option<AbortHandle>,
|
abort_handle: Option<AbortHandle>,
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn start_tftp_server(dir: PathBuf) -> Result<(JoinHandle<()>, AbortHandle)> {
|
async fn start_tftp_server(dir: PathBuf) -> Result<(JoinHandle<()>, AbortHandle)> {
|
||||||
let task = tokio::spawn(async move {
|
let task = tokio::spawn(async move {
|
||||||
println!("Starting TFTP server at: {:?}", dir);
|
println!("Starting TFTP server at: {:?}", dir);
|
||||||
@ -34,39 +34,70 @@ async fn main() {
|
|||||||
let state = Arc::new(Mutex::new(ServerState { abort_handle: None }));
|
let state = Arc::new(Mutex::new(ServerState { abort_handle: None }));
|
||||||
let folder_path = Arc::new(Mutex::new(String::new()));
|
let folder_path = Arc::new(Mutex::new(String::new()));
|
||||||
let path_clone = folder_path.clone();
|
let path_clone = folder_path.clone();
|
||||||
|
let ui_weak = ui.as_weak();
|
||||||
|
|
||||||
//选择路径按钮的回调函数
|
//选择路径按钮的回调函数
|
||||||
ui.on_BrowserFolder(move || {
|
ui.on_BrowserFolder(move || {
|
||||||
let result = FileDialog::new()
|
let result = FileDialog::new()
|
||||||
.set_directory("/")
|
.set_directory("/")
|
||||||
.pick_folder();
|
.pick_folder();
|
||||||
let path = result.map(|p| p.to_string_lossy().into_owned())
|
let mut path = result.map(|p| p.to_string_lossy().into_owned())
|
||||||
.unwrap_or_default();
|
.unwrap_or_default();
|
||||||
|
if path == "" {
|
||||||
|
println!("TFTP Dir Empty !");
|
||||||
|
if let Some(ui) = ui_weak.upgrade() {
|
||||||
|
ui.set_StartEnable(false);
|
||||||
|
}
|
||||||
|
path = String::from("");
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
if let Some(ui) = ui_weak.upgrade() {
|
||||||
|
ui.set_StartEnable(true);
|
||||||
|
}
|
||||||
|
}
|
||||||
*path_clone.lock().unwrap() = path.clone(); // 存储到变量
|
*path_clone.lock().unwrap() = path.clone(); // 存储到变量
|
||||||
SharedString::from(path)
|
SharedString::from(path)
|
||||||
});
|
});
|
||||||
//启动按钮的回调函数
|
//启动按钮的回调函数
|
||||||
let state_clone = state.clone();
|
let state_clone = state.clone();
|
||||||
let path_clone = Arc::clone(&folder_path);
|
let path_clone = Arc::clone(&folder_path);
|
||||||
|
let ui_weak = ui.as_weak();
|
||||||
ui.on_StartTftp(move || {
|
ui.on_StartTftp(move || {
|
||||||
// let Dir = PathBuf::from("C:/Users/anonymous/Desktop/Rust/VoiletTftp/target/release");
|
|
||||||
let dir = PathBuf::from(&*path_clone.lock().unwrap());
|
let dir = PathBuf::from(&*path_clone.lock().unwrap());
|
||||||
println!("{:?}",dir);
|
if dir.iter().count() == 0 {
|
||||||
let state = state_clone.clone();
|
println!("TFTP Dir Empty !");
|
||||||
tokio::spawn(async move {
|
if let Some(ui) = ui_weak.upgrade() {
|
||||||
match start_tftp_server(dir).await {
|
ui.set_StartEnable(true);
|
||||||
Ok((_handle, abort_handle)) => {
|
ui.set_StopEnable(false);
|
||||||
println!("Server started");
|
|
||||||
// 存储 abort_handle 以便停止
|
|
||||||
state.lock().unwrap().abort_handle = Some(abort_handle);
|
|
||||||
}
|
|
||||||
Err(e) => eprintln!("Error: {}", e),
|
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
|
else {
|
||||||
|
if let Some(ui) = ui_weak.upgrade() {
|
||||||
|
ui.set_StartEnable(false);
|
||||||
|
ui.set_StopEnable(true);
|
||||||
|
}
|
||||||
|
println!("{:?}",dir);
|
||||||
|
let state = state_clone.clone();
|
||||||
|
tokio::spawn(async move {
|
||||||
|
match start_tftp_server(dir).await {
|
||||||
|
Ok((_handle, abort_handle)) => {
|
||||||
|
println!("Server started");
|
||||||
|
// 存储 abort_handle 以便停止
|
||||||
|
state.lock().unwrap().abort_handle = Some(abort_handle);
|
||||||
|
}
|
||||||
|
Err(e) => eprintln!("Error: {}", e),
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
});
|
});
|
||||||
//停止按钮的回调函数
|
//停止按钮的回调函数
|
||||||
|
let ui_weak = ui.as_weak();
|
||||||
let state_clone = state.clone();
|
let state_clone = state.clone();
|
||||||
ui.on_StopTftp(move || {
|
ui.on_StopTftp(move || {
|
||||||
|
if let Some(ui) = ui_weak.upgrade() {
|
||||||
|
ui.set_StartEnable(true);
|
||||||
|
ui.set_StopEnable(false);
|
||||||
|
}
|
||||||
if let Some(handle) = state_clone.lock().unwrap().abort_handle.take() {
|
if let Some(handle) = state_clone.lock().unwrap().abort_handle.take() {
|
||||||
handle.abort();
|
handle.abort();
|
||||||
println!("Server stopped");
|
println!("Server stopped");
|
||||||
|
Loading…
x
Reference in New Issue
Block a user