Files
esengine/packages/editor-app/src-tauri/src/main.rs

172 lines
4.9 KiB
Rust
Raw Normal View History

2025-10-14 22:53:26 +08:00
// Prevents additional console window on Windows in release
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
use tauri::Manager;
2025-10-15 00:23:19 +08:00
use tauri::AppHandle;
2025-10-14 22:53:26 +08:00
// IPC Commands
#[tauri::command]
fn greet(name: &str) -> String {
format!("Hello, {}! Welcome to ECS Framework Editor.", name)
}
#[tauri::command]
fn open_project(path: String) -> Result<String, String> {
// 项目打开逻辑
Ok(format!("Project opened: {}", path))
}
#[tauri::command]
fn save_project(path: String, data: String) -> Result<(), String> {
// 项目保存逻辑
std::fs::write(&path, data)
.map_err(|e| format!("Failed to save project: {}", e))?;
Ok(())
}
#[tauri::command]
fn export_binary(data: Vec<u8>, output_path: String) -> Result<(), String> {
std::fs::write(&output_path, data)
.map_err(|e| format!("Failed to export binary: {}", e))?;
Ok(())
}
2025-10-15 00:23:19 +08:00
#[tauri::command]
async fn open_project_dialog(app: AppHandle) -> Result<Option<String>, String> {
2025-10-15 00:40:27 +08:00
use tauri_plugin_dialog::DialogExt;
2025-10-15 00:23:19 +08:00
2025-10-15 00:40:27 +08:00
let folder = app.dialog()
.file()
2025-10-15 00:23:19 +08:00
.set_title("Select Project Directory")
2025-10-15 00:40:27 +08:00
.blocking_pick_folder();
2025-10-15 00:23:19 +08:00
2025-10-15 00:40:27 +08:00
Ok(folder.map(|path| path.to_string()))
}
#[tauri::command]
fn scan_directory(path: String, pattern: String) -> Result<Vec<String>, String> {
use glob::glob;
2025-10-15 09:19:30 +08:00
use std::path::{Path, MAIN_SEPARATOR};
2025-10-15 00:40:27 +08:00
let base_path = Path::new(&path);
if !base_path.exists() {
return Err(format!("Directory does not exist: {}", path));
}
2025-10-15 09:19:30 +08:00
let separator = if path.contains('\\') { '\\' } else { '/' };
let glob_pattern = format!("{}{}{}", path.trim_end_matches(&['/', '\\'][..]), separator, pattern);
let normalized_pattern = if cfg!(windows) {
glob_pattern.replace('/', "\\")
} else {
glob_pattern.replace('\\', "/")
};
2025-10-15 00:40:27 +08:00
let mut files = Vec::new();
2025-10-15 09:19:30 +08:00
match glob(&normalized_pattern) {
2025-10-15 00:40:27 +08:00
Ok(entries) => {
for entry in entries {
match entry {
Ok(path) => {
if path.is_file() {
files.push(path.to_string_lossy().to_string());
}
}
Err(e) => eprintln!("Error reading entry: {}", e),
}
}
}
Err(e) => return Err(format!("Failed to scan directory: {}", e)),
}
Ok(files)
}
#[tauri::command]
fn read_file_content(path: String) -> Result<String, String> {
std::fs::read_to_string(&path)
.map_err(|e| format!("Failed to read file {}: {}", path, e))
2025-10-15 00:23:19 +08:00
}
2025-10-15 09:43:48 +08:00
#[derive(serde::Serialize)]
struct DirectoryEntry {
name: String,
path: String,
is_dir: bool,
}
#[tauri::command]
fn list_directory(path: String) -> Result<Vec<DirectoryEntry>, String> {
use std::fs;
use std::path::Path;
let dir_path = Path::new(&path);
if !dir_path.exists() {
return Err(format!("Directory does not exist: {}", path));
}
if !dir_path.is_dir() {
return Err(format!("Path is not a directory: {}", path));
}
let mut entries = Vec::new();
match fs::read_dir(dir_path) {
Ok(read_dir) => {
for entry in read_dir {
match entry {
Ok(entry) => {
let entry_path = entry.path();
if let Some(name) = entry_path.file_name() {
entries.push(DirectoryEntry {
name: name.to_string_lossy().to_string(),
path: entry_path.to_string_lossy().to_string(),
is_dir: entry_path.is_dir(),
});
}
}
Err(e) => eprintln!("Error reading directory entry: {}", e),
}
}
}
Err(e) => return Err(format!("Failed to read directory: {}", e)),
}
entries.sort_by(|a, b| {
match (a.is_dir, b.is_dir) {
(true, false) => std::cmp::Ordering::Less,
(false, true) => std::cmp::Ordering::Greater,
_ => a.name.to_lowercase().cmp(&b.name.to_lowercase()),
}
});
Ok(entries)
}
2025-10-14 22:53:26 +08:00
fn main() {
tauri::Builder::default()
.plugin(tauri_plugin_shell::init())
2025-10-15 00:40:27 +08:00
.plugin(tauri_plugin_dialog::init())
2025-10-14 22:53:26 +08:00
.setup(|app| {
// 应用启动时的初始化逻辑
#[cfg(debug_assertions)]
{
let window = app.get_webview_window("main").unwrap();
window.open_devtools();
}
Ok(())
})
.invoke_handler(tauri::generate_handler![
greet,
open_project,
save_project,
2025-10-15 00:23:19 +08:00
export_binary,
2025-10-15 00:40:27 +08:00
open_project_dialog,
scan_directory,
2025-10-15 09:43:48 +08:00
read_file_content,
list_directory
2025-10-14 22:53:26 +08:00
])
.run(tauri::generate_context!())
.expect("error while running tauri application");
}