Файли
openidea/src/platform/dragOut.test.ts
T
Bert Verhelstтаbertyhell 212ee03d71 feat(project-view): drag files out to other apps (#4245)
When a drag from the Project view leaves the window it becomes an OS file drag (copy only) of the selected local files and folders: AppKit dragging session on macOS, OLE CF_HDROP on Windows, GTK on Linux, in the new ide-drag-out crate behind the window_drag_files command. A drop back on the window itself is ignored, and remote projects keep the in-window drag only.

Closes #2867

Co-authored-by: bertyhell <[email protected]>
2026-11-20 22:10:02 +08:00

56 рядки
2.2 KiB
TypeScript

import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { commands, type RemoteSession } from "./ipc";
import { useRemote } from "./remote";
import { canDragOut, dragOut, isDraggingOut } from "./dragOut";
beforeEach(() => vi.stubGlobal("__TAURI_INTERNALS__", {}));
afterEach(() => {
vi.restoreAllMocks();
vi.unstubAllGlobals();
useRemote.setState({ session: null });
});
describe("dragging files out of the window", () => {
it("is offered for a local project in the desktop app only", () => {
expect(canDragOut()).toBe(true);
useRemote.setState({ session: { id: "remote" } as unknown as RemoteSession });
expect(canDragOut()).toBe(false);
useRemote.setState({ session: null });
vi.unstubAllGlobals();
expect(canDragOut()).toBe(false);
});
it("hands every path to one OS drag and reports whether another app took them", async () => {
let end!: (dropped: boolean) => void;
const drag = vi
.spyOn(commands, "windowDragFiles")
.mockReturnValue(new Promise((resolve) => (end = (data) => resolve({ status: "ok", data }))));
const paths = ["/w/a b/中文.txt", "/w/src"];
const result = dragOut(paths);
expect(drag).toHaveBeenCalledExactlyOnceWith(paths);
// Until it ends, OS drops on this window are the same files coming back.
expect(isDraggingOut()).toBe(true);
end(true);
expect(await result).toBe(true);
expect(isDraggingOut()).toBe(false);
});
it("stops ignoring drops at the next press when the drag never reports its end", () => {
vi.spyOn(commands, "windowDragFiles").mockReturnValue(new Promise(() => {}));
void dragOut(["/w/a.txt"]);
expect(isDraggingOut()).toBe(true);
window.dispatchEvent(new MouseEvent("pointerdown"));
expect(isDraggingOut()).toBe(false);
});
it("ends quietly when the drag can't start", async () => {
vi.spyOn(console, "warn").mockImplementation(() => {});
vi.spyOn(commands, "windowDragFiles").mockResolvedValue({
status: "error",
error: { kind: "not_found", message: "not a local file: /srv/x" } as never,
});
expect(await dragOut(["/srv/x"])).toBe(false);
expect(isDraggingOut()).toBe(false);
});
});