diff --git a/e2e/fileMoveLayout.spec.ts b/e2e/fileMoveLayout.spec.ts new file mode 100644 index 0000000000..6323571201 --- /dev/null +++ b/e2e/fileMoveLayout.spec.ts @@ -0,0 +1,86 @@ +import { expect, ipcCalls, stubDesktop, test } from "./fixtures"; + +const root = "/dev/acme-api"; +const name = `InternationalSettlement${"Reconciliation".repeat(10)}.txt`; +const destination = `archive/Approved${"Settlement".repeat(11)}`; + +test("keeps a long source name and destination readable while moving a file", async ({ workbench: page }) => { + await page.evaluate( + async ({ root, name, destination }) => { + const fsModule = "/src/features/project-tree/fs.ts"; + const { projectFs } = await import(/* @vite-ignore */ fsModule); + const storeModule = "/src/features/project-tree/store.ts"; + const { loadDir } = await import(/* @vite-ignore */ storeModule); + const fs = projectFs(root); + await fs.createFile(`${root}/${name}`); + await fs.createDir(`${root}/archive`); + await fs.createDir(`${root}/${destination}`); + await loadDir(root, true); + }, + { root, name, destination }, + ); + await page.getByRole("treeitem", { name, exact: true }).click(); + await page.keyboard.press("F6"); + + const dialog = page.getByRole("dialog", { name: "Move", exact: true }); + const field = dialog.getByRole("textbox", { name: "Target directory" }); + await expect(field).toBeFocused(); + await page.setViewportSize({ width: 470, height: 480 }); + await expect + .poll(() => dialog.locator("[data-dialog-body]").evaluate((el) => el.scrollWidth - el.clientWidth)) + .toBeLessThanOrEqual(1); + expect( + await field.evaluate((el: HTMLInputElement) => el.value.slice(el.selectionStart ?? 0, el.selectionEnd ?? 0)), + ).toBe("acme-api"); + const submit = dialog.getByRole("button", { name: "Refactor", exact: true }); + await expect(submit).toBeDisabled(); + await field.fill(destination); + await expect(dialog.getByText(destination, { exact: true })).toBeVisible(); + await expect(submit).toBeInViewport({ ratio: 1 }); + await dialog.getByRole("checkbox", { name: "Search for references" }).uncheck(); + await dialog.getByRole("checkbox", { name: "Search in comments and strings" }).uncheck(); + await submit.click(); + await expect(dialog).toBeHidden(); + await expect + .poll(() => + page.evaluate( + async ({ root, destination }) => { + const fsModule = "/src/features/project-tree/fs.ts"; + const { projectFs } = await import(/* @vite-ignore */ fsModule); + return (await projectFs(root).readDir(`${root}/${destination}`)).map((entry: { name: string }) => entry.name); + }, + { root, destination }, + ), + ) + .toEqual([name]); +}); + +test("browses for a destination and keeps the current path on cancel", async ({ workbench: page }) => { + await page.getByRole("treeitem", { name: "README.md", exact: true }).click(); + await stubDesktop(page); + await page.evaluate(() => { + window.__ipc.handlers.file_dialog_needs_gtk = () => false; + let chosen: string | null = null; + window.__ipc.handlers["plugin:dialog|open"] = () => { + const result = chosen; + chosen = "C:\\Projects\\Invoice Archive"; + return result; + }; + }); + await page.keyboard.press("F6"); + const dialog = page.getByRole("dialog", { name: "Move", exact: true }); + const field = dialog.getByRole("textbox", { name: "Target directory" }); + const browse = dialog.getByRole("button", { name: "Browse for target directory", exact: true }); + await browse.click(); + await expect.poll(() => ipcCalls(page, "plugin:dialog|open")).toHaveLength(1); + await expect(field).toHaveValue(root); + await browse.click(); + await expect(field).toHaveValue("C:/Projects/Invoice Archive"); + await expect(dialog.getByRole("button", { name: "Refactor", exact: true })).toBeEnabled(); + await field.fill("C:/"); + await expect(dialog.getByText("C:/", { exact: true })).toBeVisible(); + await field.fill("/"); + await expect(dialog.getByText("/", { exact: true })).toBeVisible(); + await page.keyboard.press("Escape"); + await expect(dialog).toBeHidden(); +}); diff --git a/src/features/project-tree/FileRefactor.module.css b/src/features/project-tree/FileRefactor.module.css index 9a93444458..160b139853 100644 --- a/src/features/project-tree/FileRefactor.module.css +++ b/src/features/project-tree/FileRefactor.module.css @@ -10,6 +10,60 @@ font-size: var(--fs-ui); } +.moveForm { + display: flex; + flex-direction: column; + gap: var(--space-3); + padding: var(--space-3) var(--space-4); +} + +.moveSubject { + color: var(--fg-emphasis); + font-size: var(--fs-ui); + font-weight: 600; + overflow-wrap: anywhere; +} + +.moveDestination { + display: flex; + flex-wrap: wrap; + align-items: flex-start; + gap: var(--space-1) var(--space-2); +} + +.moveDestination > .fieldLabel { + line-height: var(--h-field); +} + +.moveControls { + display: flex; + flex: 1 1 244px; + flex-direction: column; + gap: var(--space-2); + min-width: 0; +} + +.moveField { + display: flex; + align-items: center; + gap: var(--space-1); +} + +.moveField input { + flex: 1 1 0; +} + +.moveField button { + flex: none; +} + +.movePath { + color: var(--fg-subtle); + font-size: var(--fs-small); + font-family: var(--font-mono); + overflow-wrap: anywhere; +} + .fieldLabel { color: var(--fg); font-size: var(--fs-ui); diff --git a/src/features/project-tree/FileRefactorDialogs.tsx b/src/features/project-tree/FileRefactorDialogs.tsx index 8377de18e5..96af013edf 100644 --- a/src/features/project-tree/FileRefactorDialogs.tsx +++ b/src/features/project-tree/FileRefactorDialogs.tsx @@ -6,7 +6,10 @@ import { useEffect, useRef, useState } from "react"; import { create } from "zustand"; import type { FileMove } from "@/bindings"; -import { Checkbox, Dialog, Input, Spinner, T } from "@/ui"; +import { Checkbox, Dialog, IconButton, Input, Spinner, T } from "@/ui"; +import { choosePath } from "@/platform/choosePath"; +import { isTauri } from "@/platform/ipc"; +import { ipcPath } from "@/platform/paths"; import { runFileRefactoring, type FileRefactorOptions } from "./fileRefactor"; import { isSameOrInside, @@ -253,31 +256,59 @@ function RenameDialog({ request }: { request: Extract }) { const root = useProjectTree((s) => s.root) ?? ""; const [target, setTarget] = useState(request.target); const [options, setOptions] = useState(() => loadOptions("move")); + const [browsing, setBrowsing] = useState(false); + const [browseError, setBrowseError] = useState(null); + const field = useRef(null); const { busy, run, cancel } = useSearch(); + useEffect(() => { + const input = field.current; + const path = request.target.replace(/[\\/]+$/, ""); + if (!input || !path || /^[A-Za-z]:$/.test(path)) return; + const start = Math.max(path.lastIndexOf("/"), path.lastIndexOf("\\")) + 1; + input.setSelectionRange(start, path.length); + input.scrollLeft = input.scrollWidth; + }, [request.target]); const dest = resolveTarget(target, root); const moves: FileMove[] = request.paths - .filter((p) => parentOf(p) !== dest) - .map((p) => ({ from: p, to: joinPath(dest, nameOf(p)) })); + .map((from) => ({ from, to: joinPath(dest, nameOf(from)) })) + .filter((move) => move.from !== move.to); const error = request.paths.some((p) => isSameOrInside(dest, p)) - ? "Cannot move a folder into itself" + ? tx("Cannot move a folder into itself") : !moves.length - ? "Already in this directory" + ? tx("Already in this directory") : null; - const subject = - request.paths.length === 1 - ? `Move ${request.dir || useProjectTree.getState().listings.has(request.paths[0]!) ? "directory" : "file"} '${nameOf(request.paths[0]!)}'` - : `Move ${request.paths.length} items`; + const single = request.paths.length === 1 ? request.paths[0]! : null; + const subject = single + ? request.dir || useProjectTree.getState().listings.has(single) + ? tx`Move directory '${nameOf(single)}'` + : tx`Move file '${nameOf(single)}'` + : tx`Move ${request.paths.length} items`; + const browse = async () => { + setBrowsing(true); + setBrowseError(null); + try { + const picked = await choosePath({ directory: true, title: tx("Select Target Directory"), defaultPath: dest }); + if (picked !== null) setTarget(ipcPath(picked)); + } catch (e) { + setBrowseError(e instanceof Error ? e.message : String(e)); + } finally { + setBrowsing(false); + } + }; const submit = async () => { - if (busy || error) return; + if (busy || browsing || error) return; saveOptions("move", options); const status = await run(moves, options, "Move"); if (status !== "done") return; @@ -291,38 +322,59 @@ function MoveDialog({ request }: { request: Extract : undefined} buttons={[ - { label: "Refactor", role: "default", disabled: busy || !!error, onClick: () => void submit() }, + { label: "Refactor", role: "default", disabled: busy || browsing || !!error, onClick: () => void submit() }, { label: "Cancel", role: "cancel", onClick: cancel }, ]} >
{ e.preventDefault(); void submit(); }} > -
{subject}
- - setTarget(e.target.value)} - /> -
{relativeTo(root, dest) || (dest === root ? tx("Project root") : dest)}
- {error && ( -
- {error} +
{subject}
+
+ +
+
+ { + setBrowseError(null); + setTarget(e.target.value); + }} + /> + {isTauri() && ( + void browse()} + /> + )} +
+
+ {relativeTo(root, dest) || (dest === root ? tx("Project root") : dest)} +
+ {(browseError ?? error) && ( +
+ {browseError ?? error} +
+ )} +
- )} - +
); diff --git a/src/platform/locales/text/en.json b/src/platform/locales/text/en.json index 92a13c9093..2f49794b22 100644 --- a/src/platform/locales/text/en.json +++ b/src/platform/locales/text/en.json @@ -2738,5 +2738,12 @@ "{count} hover actions": "{count} hover actions", "{description}; JVM options: {flags}": "{description}; JVM options: {flags}", "Sorting Options": "Sorting Options", - "Sort by Duration": "Sort by Duration" + "Sort by Duration": "Sort by Duration", + "Move file '{name}'": "Move file '{name}'", + "Move directory '{name}'": "Move directory '{name}'", + "Move {count} items": "Move {count} items", + "Browse for target directory": "Browse for target directory", + "Select Target Directory": "Select Target Directory", + "Cannot move a folder into itself": "Cannot move a folder into itself", + "Already in this directory": "Already in this directory" } diff --git a/src/platform/locales/text/zh.json b/src/platform/locales/text/zh.json index 42830a83b3..41c9ea11be 100644 --- a/src/platform/locales/text/zh.json +++ b/src/platform/locales/text/zh.json @@ -9108,5 +9108,12 @@ "{count} hover actions": "{count} 个悬停操作", "{description}; JVM options: {flags}": "{description};JVM 选项:{flags}", "Sorting Options": "排序选项", - "Sort by Duration": "按持续时间排序" + "Sort by Duration": "按持续时间排序", + "Move file '{name}'": "移动文件「{name}」", + "Move directory '{name}'": "移动目录「{name}」", + "Move {count} items": "移动 {count} 个项目", + "Browse for target directory": "浏览目标目录", + "Select Target Directory": "选择目标目录", + "Cannot move a folder into itself": "不能将文件夹移动到自身目录中", + "Already in this directory": "已在此目录中" }