Move workspace file work off the main thread and delete to the trash
Run the blocking workspace commands on the blocking thread pool. Tauri runs a synchronous command on the main thread, so a long search or copy froze every window. Command names, arguments, and results do not change. Send each workspace-event only to the windows that have its root open. The frontend listens with its window label as the target. Do not send access events, because the frontend ignores them. Drop watchers after the state lock is released, because a macOS watcher waits for its thread when it stops. Fix these defects. Each fix has a Rust test: - A rename that changes only the letter case works on case-insensitive volumes. - New files publish with a move that never replaces a file: MoveFileExW on Windows, renameat2 or renamex_np on Linux and macOS, then a hard link, then create_new. exFAT, FAT32, and many network mounts now work. The Windows save recovery uses the same move. - On macOS and Linux, existing names with a colon stay usable. New names reject the characters that Windows does not allow, on every platform. - workspace_read_data_url checks the size before it reads the file. - workspace_write_bytes uses the atomic writer. Its only caller writes new names, so it gets no conflict check. - Delete and rename act on a link itself, not on its target. Delete now moves items to the system trash. One file moves without a confirmation. A folder or several items need one. Shift+Delete and "Delete permanently" keep the permanent delete with its confirmation. Windows drives without a Recycle Bin give an error instead of a silent permanent delete. The smoke test for tree actions now checks this behavior. The user guide describes it and the new-name rule.
This commit is contained in:
@@ -3466,7 +3466,15 @@ export const messageRows: [string, string, string, string][] = [
|
||||
"{count} éléments supprimés sur {total}",
|
||||
"{total}項目中{count}項目を削除しました",
|
||||
"已删除 {total} 项中的 {count} 项"
|
||||
]
|
||||
],
|
||||
["Delete permanently", "Supprimer définitivement", "完全に削除", "永久删除"],
|
||||
["Delete permanently ({count})", "Supprimer définitivement ({count})", "完全に削除 ({count})", "永久删除 ({count})"],
|
||||
["Move to trash", "Placer dans la corbeille", "ごみ箱に移動", "移到回收站"],
|
||||
["Move {name} and everything inside it to the trash?", "Placer {name} et tout son contenu dans la corbeille ?", "{name}とその中のすべてをごみ箱に移動しますか?", "将 {name} 及其全部内容移到回收站?"],
|
||||
["Move {count} selected items to the trash?", "Placer les {count} éléments sélectionnés dans la corbeille ?", "選択した{count}項目をごみ箱に移動しますか?", "将所选的 {count} 项移到回收站?"],
|
||||
["Move {count} selected items and all contents of selected folders to the trash?", "Placer les {count} éléments et tout le contenu des dossiers sélectionnés dans la corbeille ?", "選択した{count}項目と選択フォルダー内のすべてをごみ箱に移動しますか?", "将所选的 {count} 项及所选文件夹的全部内容移到回收站?"],
|
||||
["Moved {count} of {total} items to the trash. Restore them from the system trash.", "{count} éléments sur {total} placés dans la corbeille. Restaurez-les depuis la corbeille du système.", "{total}項目中{count}項目をごみ箱に移動しました。システムのごみ箱から復元できます。", "已将 {total} 项中的 {count} 项移到回收站。可从系统回收站恢复。"],
|
||||
["To delete permanently instead, select the items and press Shift+Delete.", "Pour supprimer définitivement, sélectionnez les éléments et appuyez sur Maj+Suppr.", "完全に削除するには、項目を選択して Shift+Delete を押してください。", "要永久删除,请选择这些项目并按 Shift+Delete。"]
|
||||
];
|
||||
export const catalogs: Record<Exclude<Locale, 'en'>, Record<string, string>> = {
|
||||
fr: Object.fromEntries(messageRows.map(row => [row[0], row[1]])),
|
||||
|
||||
@@ -449,6 +449,18 @@
|
||||
return;
|
||||
}
|
||||
}
|
||||
// Delete moves the selection to the trash. Shift+Delete deletes it permanently.
|
||||
// macOS keyboards: Cmd+Backspace and Cmd+Option+Backspace, as in Finder.
|
||||
const deleteKey = event.key === "Delete" && !event.ctrlKey && !event.metaKey && !event.altKey;
|
||||
const macDeleteKey = event.key === "Backspace" && event.metaKey && !event.ctrlKey && !event.shiftKey;
|
||||
if ((deleteKey || macDeleteKey) && event.target instanceof Element && treeElement?.contains(event.target)
|
||||
&& !event.target.closest('input, textarea, [contenteditable="true"]')) {
|
||||
const entries = [...selection.paths].map(findEntry).filter((entry): entry is TreeEntry => !!entry);
|
||||
if (!entries.length) return;
|
||||
event.preventDefault(); event.stopPropagation();
|
||||
void deleteEntries(entries, deleteKey ? event.shiftKey : event.altKey);
|
||||
return;
|
||||
}
|
||||
if (event.key !== "F2" || event.ctrlKey || event.metaKey || event.altKey) {
|
||||
return;
|
||||
}
|
||||
@@ -682,21 +694,40 @@
|
||||
onStatus?.(`Copied ${result.completed.length} of ${roots.length} items`);
|
||||
}
|
||||
|
||||
async function deleteEntries(entries: TreeEntry[]) {
|
||||
// Delete moves items to the system trash. One file needs no confirmation, because the
|
||||
// trash can restore it. A folder or several items need one. A permanent delete
|
||||
// (Shift+Delete, or "Delete permanently" in the menu) always asks first.
|
||||
function deleteQuestion(entries: TreeEntry[], roots: TreeEntry[], permanent: boolean): string | null {
|
||||
if (entries.length === 1) {
|
||||
const name = entries[0].name;
|
||||
if (entries[0].kind === "folder") return $t(permanent ? "Permanently delete {name} and everything inside it?" : "Move {name} and everything inside it to the trash?", { name });
|
||||
return permanent ? $t("Permanently delete {name}?", { name }) : null;
|
||||
}
|
||||
const folders = entries.some(entry => entry.kind === "folder");
|
||||
const question = permanent
|
||||
? $t(folders ? "Permanently delete {count} selected items and all contents of selected folders?" : "Permanently delete {count} selected items?", { count: entries.length })
|
||||
: $t(folders ? "Move {count} selected items and all contents of selected folders to the trash?" : "Move {count} selected items to the trash?", { count: entries.length });
|
||||
return question + "\n\n" + roots.map(entry => entry.rel_path).join("\n");
|
||||
}
|
||||
|
||||
async function deleteEntries(entries: TreeEntry[], permanent = false) {
|
||||
if (!entries.length) return;
|
||||
const roots = selectionRoots(entries);
|
||||
const message = entries.length === 1
|
||||
? $t(entries[0].kind === "folder" ? "Permanently delete {name} and everything inside it?" : "Permanently delete {name}?", { name: entries[0].name })
|
||||
: $t(entries.some(entry => entry.kind === "folder") ? "Permanently delete {count} selected items and all contents of selected folders?" : "Permanently delete {count} selected items?", { count: entries.length }) + "\n\n" + roots.map(entry => entry.rel_path).join("\n");
|
||||
const question = deleteQuestion(entries, roots, permanent);
|
||||
try {
|
||||
if (!await confirm(message, { title: $t("Delete from disk"), kind: "warning" })) return;
|
||||
if (question && !await confirm(question, { title: $t(permanent ? "Delete from disk" : "Move to trash"), kind: "warning" })) return;
|
||||
const result = await runTreeActions(roots, async entry => {
|
||||
await workspaceDelete(entry.rel_path);
|
||||
await workspaceDelete(entry.rel_path, permanent);
|
||||
forgetFolder(entry.rel_path);
|
||||
onEntryGone?.(entry.rel_path);
|
||||
});
|
||||
for (const parent of new Set(roots.map(entry => parentRel(entry.rel_path)))) await load(parent);
|
||||
error = result.failures.length ? result.failures.join("\n") : null;
|
||||
onStatus?.($t("Deleted {count} of {total} items", { count: result.completed.length, total: roots.length }));
|
||||
// The trash can be unavailable, for example on a network drive. A permanent delete stays an explicit choice.
|
||||
const hint = !permanent && result.failures.length ? [$t("To delete permanently instead, select the items and press Shift+Delete.")] : [];
|
||||
error = result.failures.length ? [...result.failures, ...hint].join("\n") : null;
|
||||
onStatus?.(permanent
|
||||
? $t("Deleted {count} of {total} items", { count: result.completed.length, total: roots.length })
|
||||
: $t("Moved {count} of {total} items to the trash. Restore them from the system trash.", { count: result.completed.length, total: roots.length }));
|
||||
} catch (err) {
|
||||
error = err instanceof Error ? err.message : String(err);
|
||||
}
|
||||
@@ -875,6 +906,12 @@
|
||||
icon: "trash",
|
||||
run: () => { void deleteEntries(entries); },
|
||||
},
|
||||
{
|
||||
kind: "action",
|
||||
label: multiple ? $t("Delete permanently ({count})", { count: entries.length }) : $t("Delete permanently"),
|
||||
icon: "trash",
|
||||
run: () => { void deleteEntries(entries, true); },
|
||||
},
|
||||
];
|
||||
}
|
||||
</script>
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { invoke } from "@tauri-apps/api/core";
|
||||
import { listen, type UnlistenFn } from "@tauri-apps/api/event";
|
||||
import { getCurrentWindow } from "@tauri-apps/api/window";
|
||||
import { open as openDialog } from "@tauri-apps/plugin-dialog";
|
||||
import type { TreeEntry, WorkspaceEvent, WorkspaceInfo } from "./types";
|
||||
import { tr } from "../i18n/index.ts";
|
||||
@@ -64,8 +65,9 @@ export function workspaceRename(from: string, to: string, expectedRoot?: string)
|
||||
return invoke("workspace_rename", { from, to, expectedRoot });
|
||||
}
|
||||
|
||||
export function workspaceDelete(rel: string): Promise<void> {
|
||||
return invoke("workspace_delete", { rel });
|
||||
/** Move an item to the system trash, or delete it permanently with `permanent`. */
|
||||
export function workspaceDelete(rel: string, permanent = false): Promise<void> {
|
||||
return invoke("workspace_delete", { rel, permanent });
|
||||
}
|
||||
|
||||
export function workspaceReadDataUrl(rel: string): Promise<string> {
|
||||
@@ -107,10 +109,15 @@ export function workspaceSearch(query: string): Promise<SearchHit[]> {
|
||||
return invoke("workspace_search", { query });
|
||||
}
|
||||
|
||||
/**
|
||||
* Listen to file changes in the workspace of this window. The host sends each change only to
|
||||
* the windows that have its workspace open (src-tauri/src/workspace.rs `emit_watch_event`).
|
||||
* The target must be this window: a listener without a target receives the events of every window.
|
||||
*/
|
||||
export function onWorkspaceEvent(
|
||||
handler: (event: WorkspaceEvent) => void,
|
||||
): Promise<UnlistenFn> {
|
||||
return listen<WorkspaceEvent>("workspace-event", (event) => {
|
||||
handler(event.payload);
|
||||
});
|
||||
}, { target: getCurrentWindow().label });
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user