feat: added ability to delete files/folders
mellbacon committed
May 30, 2023 at 13:40 UTC
c1e7fbdc1b83462959333949c75a4b5ae802a0ab
6 files changed
+127
-9
src-tauri/Cargo.lock
+98
-2
@@ -32,6 +32,21 @@ dependencies = [
32
"alloc-no-stdlib",
33
]
34
35
+[[package]]
36
+name = "android-tzdata"
37
+version = "0.1.1"
38
+source = "registry+https://github.com/rust-lang/crates.io-index"
39
+checksum = "e999941b234f3131b00bc13c22d06e8c5ff726d1b6318ac7eb276997bbb4fef0"
40
+
41
+[[package]]
42
+name = "android_system_properties"
43
+version = "0.1.5"
44
+source = "registry+https://github.com/rust-lang/crates.io-index"
45
+checksum = "819e7219dbd41043ac279b19830f2efc897156490d7fd6ea916720117ee66311"
46
+dependencies = [
47
+ "libc",
48
+]
49
+
50
[[package]]
51
name = "anyhow"
52
version = "1.0.70"
@@ -236,6 +251,21 @@ version = "1.0.0"
251
source = "registry+https://github.com/rust-lang/crates.io-index"
252
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
253
254
+[[package]]
255
+name = "chrono"
256
+version = "0.4.25"
257
+source = "registry+https://github.com/rust-lang/crates.io-index"
258
+checksum = "fdbc37d37da9e5bce8173f3a41b71d9bf3c674deebbaceacd0ebdabde76efb03"
259
+dependencies = [
260
+ "android-tzdata",
261
+ "iana-time-zone",
262
+ "js-sys",
263
+ "num-traits",
264
+ "time 0.1.45",
265
+ "wasm-bindgen",
266
+ "winapi",
267
+]
268
+
269
[[package]]
270
name = "cocoa"
271
version = "0.24.1"
@@ -1071,6 +1101,29 @@ version = "0.1.5"
1101
source = "registry+https://github.com/rust-lang/crates.io-index"
1102
checksum = "21dec9db110f5f872ed9699c3ecf50cf16f423502706ba5c72462e28d3157573"
1103
1104
+[[package]]
1105
+name = "iana-time-zone"
1106
+version = "0.1.56"
1107
+source = "registry+https://github.com/rust-lang/crates.io-index"
1108
+checksum = "0722cd7114b7de04316e7ea5456a0bbb20e4adb46fd27a3697adb812cff0f37c"
1109
+dependencies = [
1110
+ "android_system_properties",
1111
+ "core-foundation-sys",
1112
+ "iana-time-zone-haiku",
1113
+ "js-sys",
1114
+ "wasm-bindgen",
1115
+ "windows 0.48.0",
1116
+]
1117
+
1118
+[[package]]
1119
+name = "iana-time-zone-haiku"
1120
+version = "0.1.2"
1121
+source = "registry+https://github.com/rust-lang/crates.io-index"
1122
+checksum = "f31827a206f56af32e590ba56d5d2d085f558508192593743f16b2306495269f"
1123
+dependencies = [
1124
+ "cc",
1125
+]
1126
+
1127
[[package]]
1128
name = "ico"
1129
version = "0.2.0"
@@ -1534,6 +1587,7 @@ dependencies = [
1587
"tauri",
1588
"tauri-build",
1589
"tauri-plugin-fs-watch",
1590
+ "trash",
1591
"windows 0.32.0",
1592
]
1593
@@ -1852,7 +1906,7 @@ dependencies = [
1906
"line-wrap",
1907
"quick-xml",
1908
"serde",
1855
- "time",
1909
+ "time 0.3.20",
1910
]
1911
1912
[[package]]
@@ -2621,7 +2675,7 @@ dependencies = [
2675
"sha2",
2676
"tauri-utils",
2677
"thiserror",
2624
- "time",
2678
+ "time 0.3.20",
2679
"uuid 1.3.1",
2680
"walkdir",
2681
]
@@ -2784,6 +2838,17 @@ dependencies = [
2838
"once_cell",
2839
]
2840
2841
+[[package]]
2842
+name = "time"
2843
+version = "0.1.45"
2844
+source = "registry+https://github.com/rust-lang/crates.io-index"
2845
+checksum = "1b797afad3f312d1c66a56d11d0316f916356d11bd158fbc6ca6389ff6bf805a"
2846
+dependencies = [
2847
+ "libc",
2848
+ "wasi 0.10.0+wasi-snapshot-preview1",
2849
+ "winapi",
2850
+]
2851
+
2852
[[package]]
2853
name = "time"
2854
version = "0.3.20"
@@ -2944,6 +3009,22 @@ dependencies = [
3009
"tracing-log",
3010
]
3011
3012
+[[package]]
3013
+name = "trash"
3014
+version = "3.0.2"
3015
+source = "registry+https://github.com/rust-lang/crates.io-index"
3016
+checksum = "6b782240476bca40f750a7f205510ac1449ab707d3ac3f451c4b356f557c0ecd"
3017
+dependencies = [
3018
+ "chrono",
3019
+ "libc",
3020
+ "log",
3021
+ "objc",
3022
+ "once_cell",
3023
+ "scopeguard",
3024
+ "url",
3025
+ "windows 0.44.0",
3026
+]
3027
+
3028
[[package]]
3029
name = "treediff"
3030
version = "3.0.2"
@@ -3059,6 +3140,12 @@ version = "0.9.0+wasi-snapshot-preview1"
3140
source = "registry+https://github.com/rust-lang/crates.io-index"
3141
checksum = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519"
3142
3143
+[[package]]
3144
+name = "wasi"
3145
+version = "0.10.0+wasi-snapshot-preview1"
3146
+source = "registry+https://github.com/rust-lang/crates.io-index"
3147
+checksum = "1a143597ca7c7793eff794def352d41792a93c481eb1042423ff7ff72ba2c31f"
3148
+
3149
[[package]]
3150
name = "wasi"
3151
version = "0.11.0+wasi-snapshot-preview1"
@@ -3306,6 +3393,15 @@ dependencies = [
3393
"windows-targets 0.42.2",
3394
]
3395
3396
+[[package]]
3397
+name = "windows"
3398
+version = "0.48.0"
3399
+source = "registry+https://github.com/rust-lang/crates.io-index"
3400
+checksum = "e686886bc078bc1b0b600cac0147aadb815089b6e4da64016cbd754b6342700f"
3401
+dependencies = [
3402
+ "windows-targets 0.48.0",
3403
+]
3404
+
3405
[[package]]
3406
name = "windows-bindgen"
3407
version = "0.39.0"
src-tauri/Cargo.toml
+1
@@ -17,6 +17,7 @@ serde_json = "1.0"
17
serde = { version = "1.0", features = ["derive"] }
18
tauri = { version = "1.2.5", features = ["clipboard-write-text", "dialog-ask", "dialog-confirm", "dialog-open", "dialog-save", "fs-read-dir", "fs-read-file", "fs-rename-file", "fs-write-file", "path-all", "shell-open", "window-center", "window-close", "window-create", "window-hide", "window-maximize", "window-minimize", "window-print", "window-set-decorations", "window-set-focus", "window-set-fullscreen", "window-set-icon", "window-show", "window-start-dragging", "window-unmaximize", "window-unminimize"] }
19
tauri-plugin-fs-watch = { git = "https://github.com/tauri-apps/plugins-workspace", branch = "v1" }
20
+trash = "3.0.2"
21
22
[dev-dependencies]
23
windows = "0.32.0"
src-tauri/src/main.rs
+17
-2
@@ -3,7 +3,7 @@
3
windows_subsystem = "windows"
4
)]
5
6
-use std::env;
6
+use std::{env, fs};
7
use std::process::Command;
8
9
#[tauri::command]
@@ -19,9 +19,24 @@ fn open_in_explorer(path: &str) {
19
}
20
}
21
22
+#[tauri::command]
23
+fn delete_file(path: &str, perm: bool, is_file: bool) {
24
+ if perm {
25
+ if is_file {
26
+ fs::remove_file(path).unwrap();
27
+ }
28
+ else {
29
+ fs::remove_dir_all(path).unwrap();
30
+ }
31
+ }
32
+ else {
33
+ trash::delete(path).unwrap();
34
+ }
35
+}
36
+
37
fn main() {
38
tauri::Builder::default()
24
- .invoke_handler(tauri::generate_handler![open_in_explorer])
39
+ .invoke_handler(tauri::generate_handler![open_in_explorer, delete_file])
40
.plugin(tauri_plugin_fs_watch::init())
41
.run(tauri::generate_context!())
42
.expect("error while running tauri application");
src/lib/File.ts
+7
-1
@@ -135,4 +135,10 @@ export function updateSaveState(saved = true) {
135
136
export async function openInExplorer(path: string) {
137
invoke("open_in_explorer",{ path: path});
138
-}
\ No newline at end of file
138
+}
139
+
140
+export async function moveToTrash(p: string) {
141
+ // open dialog to choose between recycling bin and perm delete
142
+ if (!await dialog.ask(`Are you sure you want to delete ${p.split(path.sep).pop()}?`)) return;
143
+ await invoke("delete_file", {path: p, perm: false, isFile: p.includes(path.sep)})
144
+}
src/lib/FileTree/FileTreeList.svelte
+2
-2
@@ -10,7 +10,7 @@
10
import Directory from "../../util/icons/Directory.svelte";
11
import {filetree} from "../FileTree.svelte";
12
import ContextMenu from "../utility/ContextMenu.svelte";
13
- import { moveFile, openInExplorer } from "../File";
13
+ import { moveFile, moveToTrash, openInExplorer } from "../File";
14
import { clipboard } from "@tauri-apps/api";
15
16
export let root = false;
@@ -38,7 +38,7 @@
38
{name: "Copy Filename", shortcut: "", action: async () => {await clipboard.writeText(name)}},
39
{name: "Copy Absolute Path", shortcut: "", action: async () => {await clipboard.writeText(path)}},
40
{name: "Rename...", shortcut: "F2", disabled: true, action: () => {console.warn("Feature not implemented yet.")}},
41
- {name: "Delete", shortcut: "Delete", action: () => {console.warn("Feature not implemented yet.")}}
41
+ {name: "Delete", shortcut: "Delete", action: async () => {await moveToTrash(path)}}
42
]
43
44
function findNode(nodeid, tree = null) {
src/lib/FileTree/FileTreeNode.svelte
+2
-2
@@ -18,7 +18,7 @@
18
import { createEventDispatcher } from "svelte";
19
import File from "../../util/icons/File.svelte";
20
import ContextMenu from "../utility/ContextMenu.svelte";
21
- import { openInExplorer } from "../File";
21
+ import { moveToTrash, openInExplorer } from "../File";
22
import { addEditorTab } from "../EditorTabList.svelte";
23
import { clipboard } from "@tauri-apps/api";
24
@@ -41,7 +41,7 @@
41
{name: "Copy Absolute Path", shortcut: "", action: async () => {await clipboard.writeText(path)}},
42
{name: "Edit", shortcut: "", action: () => {addEditorTab(path, name)}},
43
{name: "Rename...", shortcut: "F2", disabled: true, action: () => {console.warn("Feature not implemented yet.")}},
44
- {name: "Delete", shortcut: "Delete", action: () => {console.warn("Feature not implemented yet.")}}
44
+ {name: "Delete", shortcut: "Delete", action: async () => {await moveToTrash(path)}}
45
]
46
47
function dragstart(e) {