添加切换工作目录功能
This commit is contained in:
@@ -11,6 +11,29 @@ struct DirEntry {
|
||||
is_dir: bool,
|
||||
}
|
||||
|
||||
fn is_hidden_entry(entry: &fs::DirEntry) -> bool {
|
||||
let name = entry.file_name();
|
||||
let is_dot_hidden = name.to_string_lossy().starts_with('.');
|
||||
|
||||
#[cfg(windows)]
|
||||
{
|
||||
use std::os::windows::fs::MetadataExt;
|
||||
|
||||
const FILE_ATTRIBUTE_HIDDEN: u32 = 0x2;
|
||||
let is_windows_hidden = entry
|
||||
.metadata()
|
||||
.map(|metadata| metadata.file_attributes() & FILE_ATTRIBUTE_HIDDEN != 0)
|
||||
.unwrap_or(false);
|
||||
|
||||
return is_dot_hidden || is_windows_hidden;
|
||||
}
|
||||
|
||||
#[cfg(not(windows))]
|
||||
{
|
||||
is_dot_hidden
|
||||
}
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
fn pick_folder(app: tauri::AppHandle) -> Option<String> {
|
||||
app.dialog()
|
||||
@@ -22,13 +45,19 @@ fn pick_folder(app: tauri::AppHandle) -> Option<String> {
|
||||
#[tauri::command]
|
||||
fn read_dir(path: String) -> Result<Vec<DirEntry>, String> {
|
||||
let entries = fs::read_dir(&path).map_err(|e| format!("无法读取目录: {}", e))?;
|
||||
let allowed_extensions = ["md", "txt", "png", "jpg", "jpeg", "gif", "svg", "webp", "bmp"];
|
||||
let allowed_extensions = [
|
||||
"md", "png", "jpg", "jpeg", "gif", "webp", "avif", "mp4", "webm", "mov", "m4v", "ogv",
|
||||
];
|
||||
let mut result = Vec::new();
|
||||
for entry in entries {
|
||||
let entry = entry.map_err(|e| format!("读取条目失败: {}", e))?;
|
||||
if is_hidden_entry(&entry) {
|
||||
continue;
|
||||
}
|
||||
|
||||
let path = entry.path();
|
||||
let is_dir = path.is_dir();
|
||||
// Always include directories; filter files by extension
|
||||
// Keep the workspace tree focused on markdown documents and embeddable media.
|
||||
if !is_dir {
|
||||
let ext = path
|
||||
.extension()
|
||||
@@ -39,10 +68,7 @@ fn read_dir(path: String) -> Result<Vec<DirEntry>, String> {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
let name = entry
|
||||
.file_name()
|
||||
.to_string_lossy()
|
||||
.to_string();
|
||||
let name = entry.file_name().to_string_lossy().to_string();
|
||||
result.push(DirEntry {
|
||||
name,
|
||||
path: path.to_string_lossy().to_string(),
|
||||
@@ -82,7 +108,14 @@ pub fn run() {
|
||||
tauri::Builder::default()
|
||||
.plugin(tauri_plugin_opener::init())
|
||||
.plugin(tauri_plugin_dialog::init())
|
||||
.invoke_handler(tauri::generate_handler![greet, pick_folder, read_dir, read_file, write_file, pick_save_file])
|
||||
.invoke_handler(tauri::generate_handler![
|
||||
greet,
|
||||
pick_folder,
|
||||
read_dir,
|
||||
read_file,
|
||||
write_file,
|
||||
pick_save_file
|
||||
])
|
||||
.setup(|app| {
|
||||
// ---- File menu ----
|
||||
let new = MenuItemBuilder::with_id("new", "新建")
|
||||
@@ -151,12 +184,9 @@ pub fn run() {
|
||||
.build()?;
|
||||
|
||||
// ---- Help menu ----
|
||||
let about = MenuItemBuilder::with_id("about", "关于")
|
||||
.build(app)?;
|
||||
let about = MenuItemBuilder::with_id("about", "关于").build(app)?;
|
||||
|
||||
let help_menu = SubmenuBuilder::new(app, "帮助")
|
||||
.item(&about)
|
||||
.build()?;
|
||||
let help_menu = SubmenuBuilder::new(app, "帮助").item(&about).build()?;
|
||||
|
||||
// ---- Build the menu bar ----
|
||||
let menu = MenuBuilder::new(app)
|
||||
@@ -180,11 +210,7 @@ pub fn run() {
|
||||
let handle = app_handle.clone();
|
||||
// Spawn because dialog is blocking
|
||||
std::thread::spawn(move || {
|
||||
if let Some(folder) = handle
|
||||
.dialog()
|
||||
.file()
|
||||
.blocking_pick_folder()
|
||||
{
|
||||
if let Some(folder) = handle.dialog().file().blocking_pick_folder() {
|
||||
let path = folder.to_string();
|
||||
let _ = handle.emit("folder-opened", path);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user