Skip to content

Commit

Permalink
Merge pull request #1 from fosslife/master
Browse files Browse the repository at this point in the history
  • Loading branch information
Sparkenstein authored Nov 19, 2024
2 parents 3af2105 + e36af5c commit a7c2350
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 17 deletions.
40 changes: 33 additions & 7 deletions src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ serde = { version = "1", features = ["derive"] }
serde_json = "1"
tauri-plugin-fs = { version = "2", features = ["watch"] }
tauri-plugin-clipboard-manager = "2.0.2"

trash = "3"
11 changes: 11 additions & 0 deletions src-tauri/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
use trash;

#[cfg_attr(mobile, tauri::mobile_entry_point)]
pub fn run() {
tauri::Builder::default()
.plugin(tauri_plugin_clipboard_manager::init())
.plugin(tauri_plugin_fs::init())
.plugin(tauri_plugin_shell::init())
.invoke_handler(tauri::generate_handler![move_to_trash])
.run(tauri::generate_context!())
.expect("error while running tauri application");
}

#[tauri::command]
async fn move_to_trash(paths: Vec<String>) -> Result<(), String> {
for path in paths {
trash::delete(&path).map_err(|e| e.to_string())?;
}
Ok(())
}
44 changes: 43 additions & 1 deletion src/components/FileExplorer/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import {
cutToClipboard,
deleteFiles,
FileItem,
moveToTrash,
pasteFromClipboard,
transformEntries,
} from "@/lib/fileUtils";
Expand Down Expand Up @@ -89,6 +90,16 @@ interface FileExplorerProps {
onPathChange: (newPath: string) => void;
}

type MenuAction =
| "copy"
| "cut"
| "paste"
| "delete"
| "moveToTrash"
| "rename"
| "newFolder"
| "newFile";

export function FileExplorer({ currentPath, onPathChange }: FileExplorerProps) {
const {
entries,
Expand Down Expand Up @@ -207,7 +218,7 @@ export function FileExplorer({ currentPath, onPathChange }: FileExplorerProps) {
}
};

const handleMenuAction = async (action: string) => {
const handleMenuAction = async (action: MenuAction) => {
const selectedFiles = fileItems.filter((item) =>
selectedItems.has(item.name)
);
Expand Down Expand Up @@ -252,6 +263,30 @@ export function FileExplorer({ currentPath, onPathChange }: FileExplorerProps) {
case "delete":
confirmDelete(selectedFiles);
break;
// In handleMenuAction:
case "moveToTrash":
try {
const success = await moveToTrash(selectedFiles, currentPath);
if (success) {
notifications.show({
title: "Moved to Trash",
message: `${selectedFiles.length} item(s) moved to trash`,
color: "blue",
});
setSelectedItems(new Set());
await refreshDirectory();
}
} catch (error) {
notifications.show({
title: "Operation Failed",
message:
error instanceof Error
? error.message
: "Could not move items to trash",
color: "red",
});
}
break;

case "rename":
if (selectedFiles.length === 1) {
Expand Down Expand Up @@ -502,6 +537,13 @@ export function FileExplorer({ currentPath, onPathChange }: FileExplorerProps) {
Rename
</Menu.Item>
<Menu.Divider />
<Menu.Item
leftSection={<IconTrash size={16} />}
color="red"
onClick={() => handleMenuAction("moveToTrash")}
>
Move to Trash
</Menu.Item>
<Menu.Item
onClick={() => handleMenuAction("delete")}
leftSection={<IconTrash size={16} />}
Expand Down
26 changes: 18 additions & 8 deletions src/lib/fileUtils.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
import {
DirEntry,
BaseDirectory,
copyFile,
mkdir,
exists,
remove,
} from "@tauri-apps/plugin-fs";
import { DirEntry, copyFile, exists, remove } from "@tauri-apps/plugin-fs";
import { stat } from "@tauri-apps/plugin-fs";
// import { BaseDirectory, copyFile, createDir, exists } from "@tauri-apps/api/fs";
import { writeText, readText } from "@tauri-apps/plugin-clipboard-manager";
import { invoke } from "@tauri-apps/api/core";

export interface FileItem extends DirEntry {
size: string;
Expand Down Expand Up @@ -215,3 +209,19 @@ export async function deleteFiles(
return false;
}
}

export async function moveToTrash(
files: FileItem[],
basePath: string
): Promise<boolean> {
try {
// We'll need to implement this in Rust
await invoke("move_to_trash", {
paths: files.map((file) => `${basePath}/${file.name}`),
});
return true;
} catch (error) {
console.error("Move to trash failed:", error);
return false;
}
}

0 comments on commit a7c2350

Please sign in to comment.