可动态识别属性

This commit is contained in:
YHH
2025-10-15 17:15:05 +08:00
parent b69b81f63a
commit fb7a1b1282
30 changed files with 2069 additions and 461 deletions

View File

@@ -1385,6 +1385,12 @@ dependencies = [
"pin-project-lite",
]
[[package]]
name = "http-range"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "21dec9db110f5f872ed9699c3ecf50cf16f423502706ba5c72462e28d3157573"
[[package]]
name = "httparse"
version = "1.10.1"
@@ -3592,6 +3598,7 @@ dependencies = [
"gtk",
"heck 0.5.0",
"http",
"http-range",
"jni",
"libc",
"log",

View File

@@ -13,7 +13,7 @@ crate-type = ["staticlib", "cdylib", "rlib"]
tauri-build = { version = "2.0", features = [] }
[dependencies]
tauri = { version = "2.0", features = [] }
tauri = { version = "2.0", features = ["protocol-asset"] }
tauri-plugin-shell = "2.0"
tauri-plugin-dialog = "2.0"
serde = { version = "1", features = ["derive"] }

View File

@@ -3,6 +3,8 @@
use tauri::Manager;
use tauri::AppHandle;
use std::sync::{Arc, Mutex};
use std::collections::HashMap;
// IPC Commands
#[tauri::command]
@@ -143,17 +145,77 @@ fn list_directory(path: String) -> Result<Vec<DirectoryEntry>, String> {
Ok(entries)
}
#[tauri::command]
fn set_project_base_path(
path: String,
state: tauri::State<Arc<Mutex<HashMap<String, String>>>>
) -> Result<(), String> {
let mut paths = state.lock().map_err(|e| format!("Failed to lock state: {}", e))?;
paths.insert("current".to_string(), path);
Ok(())
}
fn main() {
let project_paths: Arc<Mutex<HashMap<String, String>>> = Arc::new(Mutex::new(HashMap::new()));
let project_paths_clone = Arc::clone(&project_paths);
tauri::Builder::default()
.plugin(tauri_plugin_shell::init())
.plugin(tauri_plugin_dialog::init())
.setup(|app| {
// 应用启动时的初始化逻辑
.register_uri_scheme_protocol("project", move |_app, request| {
let project_paths = Arc::clone(&project_paths_clone);
let uri = request.uri();
let path = uri.path();
let file_path = {
let paths = project_paths.lock().unwrap();
if let Some(base_path) = paths.get("current") {
format!("{}{}", base_path, path)
} else {
return tauri::http::Response::builder()
.status(404)
.body(Vec::new())
.unwrap();
}
};
match std::fs::read(&file_path) {
Ok(content) => {
let mime_type = if file_path.ends_with(".ts") || file_path.ends_with(".tsx") {
"application/javascript"
} else if file_path.ends_with(".js") {
"application/javascript"
} else if file_path.ends_with(".json") {
"application/json"
} else {
"text/plain"
};
tauri::http::Response::builder()
.status(200)
.header("Content-Type", mime_type)
.header("Access-Control-Allow-Origin", "*")
.body(content)
.unwrap()
}
Err(e) => {
eprintln!("Failed to read file {}: {}", file_path, e);
tauri::http::Response::builder()
.status(404)
.body(Vec::new())
.unwrap()
}
}
})
.setup(move |app| {
#[cfg(debug_assertions)]
{
let window = app.get_webview_window("main").unwrap();
window.open_devtools();
}
app.manage(project_paths);
Ok(())
})
.invoke_handler(tauri::generate_handler![
@@ -164,7 +226,8 @@ fn main() {
open_project_dialog,
scan_directory,
read_file_content,
list_directory
list_directory,
set_project_base_path
])
.run(tauri::generate_context!())
.expect("error while running tauri application");

View File

@@ -29,7 +29,13 @@
}
],
"security": {
"csp": null
"csp": null,
"assetProtocol": {
"enable": true,
"scope": {
"allow": ["**"]
}
}
}
},
"plugins": {