添加显示图片和视频功能
This commit is contained in:
@@ -1,5 +1,8 @@
|
||||
// Learn more about Tauri commands at https://tauri.app/develop/calling-rust/
|
||||
use notify::{RecursiveMode, Watcher};
|
||||
use std::fs;
|
||||
use std::path::PathBuf;
|
||||
use std::sync::Mutex;
|
||||
use tauri::menu::{MenuBuilder, MenuItemBuilder, SubmenuBuilder};
|
||||
use tauri::{Emitter, Manager};
|
||||
use tauri_plugin_dialog::DialogExt;
|
||||
@@ -11,6 +14,24 @@ struct DirEntry {
|
||||
is_dir: bool,
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
struct WorkspaceWatcherState {
|
||||
watcher: Mutex<Option<notify::RecommendedWatcher>>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, serde::Serialize)]
|
||||
struct WorkspaceChangedPayload {
|
||||
root: String,
|
||||
paths: Vec<String>,
|
||||
kind: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, serde::Serialize)]
|
||||
struct WorkspaceWatchErrorPayload {
|
||||
root: String,
|
||||
message: String,
|
||||
}
|
||||
|
||||
fn is_hidden_entry(entry: &fs::DirEntry) -> bool {
|
||||
let name = entry.file_name();
|
||||
let is_dot_hidden = name.to_string_lossy().starts_with('.');
|
||||
@@ -94,6 +115,73 @@ fn write_file(path: String, content: String) -> Result<(), String> {
|
||||
fs::write(&path, &content).map_err(|e| format!("无法保存文件: {}", e))
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
fn watch_workspace(
|
||||
app: tauri::AppHandle,
|
||||
state: tauri::State<'_, WorkspaceWatcherState>,
|
||||
path: String,
|
||||
) -> Result<(), String> {
|
||||
let watch_path = PathBuf::from(&path);
|
||||
if !watch_path.is_dir() {
|
||||
return Err("工作目录不存在或不是文件夹".to_string());
|
||||
}
|
||||
|
||||
let root = watch_path.to_string_lossy().to_string();
|
||||
let event_root = root.clone();
|
||||
let error_root = root.clone();
|
||||
let app_handle = app.clone();
|
||||
let mut watcher =
|
||||
notify::recommended_watcher(move |result: notify::Result<notify::Event>| match result {
|
||||
Ok(event) => {
|
||||
if event.kind.is_access() {
|
||||
return;
|
||||
}
|
||||
|
||||
let payload = WorkspaceChangedPayload {
|
||||
root: event_root.clone(),
|
||||
paths: event
|
||||
.paths
|
||||
.iter()
|
||||
.map(|path| path.to_string_lossy().to_string())
|
||||
.collect(),
|
||||
kind: format!("{:?}", event.kind),
|
||||
};
|
||||
let _ = app_handle.emit("workspace-changed", payload);
|
||||
}
|
||||
Err(error) => {
|
||||
let _ = app_handle.emit(
|
||||
"workspace-watch-error",
|
||||
WorkspaceWatchErrorPayload {
|
||||
root: error_root.clone(),
|
||||
message: error.to_string(),
|
||||
},
|
||||
);
|
||||
}
|
||||
})
|
||||
.map_err(|e| format!("无法监听工作目录: {}", e))?;
|
||||
|
||||
watcher
|
||||
.watch(&watch_path, RecursiveMode::Recursive)
|
||||
.map_err(|e| format!("无法监听工作目录: {}", e))?;
|
||||
|
||||
let mut current = state
|
||||
.watcher
|
||||
.lock()
|
||||
.map_err(|_| "无法更新工作目录监听状态".to_string())?;
|
||||
*current = Some(watcher);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
fn unwatch_workspace(state: tauri::State<'_, WorkspaceWatcherState>) -> Result<(), String> {
|
||||
let mut current = state
|
||||
.watcher
|
||||
.lock()
|
||||
.map_err(|_| "无法更新工作目录监听状态".to_string())?;
|
||||
*current = None;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
fn pick_save_file(app: tauri::AppHandle) -> Option<String> {
|
||||
app.dialog()
|
||||
@@ -112,12 +200,15 @@ pub fn run() {
|
||||
tauri::Builder::default()
|
||||
.plugin(tauri_plugin_opener::init())
|
||||
.plugin(tauri_plugin_dialog::init())
|
||||
.manage(WorkspaceWatcherState::default())
|
||||
.invoke_handler(tauri::generate_handler![
|
||||
greet,
|
||||
pick_folder,
|
||||
read_dir,
|
||||
read_file,
|
||||
write_file,
|
||||
watch_workspace,
|
||||
unwatch_workspace,
|
||||
pick_save_file
|
||||
])
|
||||
.setup(|app| {
|
||||
|
||||
Reference in New Issue
Block a user