@devries-cameron20 / t3code Mirror · read-only / commits / fb89ce1680
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 09:45 UTC

feat(desktop): drag file pills into Finder on macOS (#37768)

Co-authored-by: Cursor <cursoragent@cursor.com>

Yash Singh committed Nov 16, 2026 at 15:14 UTC fb89ce1680bcaa16d5c564369543ffefda755529
12 files changed +164
apps/desktop/src/ipc/DesktopIpcHandlers.ts
+2
@@ -1,3 +1,4 @@
1 +import { startFileDrag } from "./methods/fileDrag.ts";
2 import * as Effect from "effect/Effect";
3
4 import { receiveProviderAuthCallback, cancelProviderAuthCallback } from "./methods/providerAuth.ts";
@@ -110,6 +111,7 @@ export const installDesktopIpcHandlers = Effect.fn("desktop.ipc.installHandlers"
111 yield* ipc.handle(getHostEnvironmentId);
112 yield* ipc.handle(getLocalEnvironmentBearerToken);
113
114 + yield* ipc.handle(startFileDrag);
115 yield* ipc.handle(restartAudioService);
116 yield* ipc.handle(getClientSettings);
117 yield* ipc.handle(setClientSettings);
apps/desktop/src/ipc/channels.ts
+2
@@ -142,3 +142,5 @@ export const TAKE_LEGACY_LOCAL_STORAGE_CHANNEL = "desktop:take-legacy-local-stor
142 export const COMPLETE_LEGACY_LOCAL_STORAGE_CHANNEL = "desktop:complete-legacy-local-storage";
143
144 export const RESTART_AUDIO_SERVICE_CHANNEL = "desktop:restart-audio-service";
145 +
146 +export const START_FILE_DRAG_CHANNEL = "desktop:start-file-drag";
apps/desktop/src/ipc/methods/fileDrag.test.ts new
+51
@@ -0,0 +1,51 @@
1 +import { assert, it } from "@effect/vitest";
2 +import * as NodeServices from "@effect/platform-node/NodeServices";
3 +import * as Effect from "effect/Effect";
4 +import * as FileSystem from "effect/FileSystem";
5 +import * as Path from "effect/Path";
6 +import { vi } from "vite-plus/test";
7 +
8 +const { startDrag, getFileIcon } = vi.hoisted(() => ({
9 + startDrag: vi.fn(),
10 + getFileIcon: vi.fn(async () => "file-icon"),
11 +}));
12 +vi.mock("electron", () => ({ app: { getFileIcon } }));
13 +
14 +import * as DesktopEnvironment from "../../app/DesktopEnvironment.ts";
15 +import * as ElectronWindow from "../../electron/ElectronWindow.ts";
16 +import { startFileDrag } from "./fileDrag.ts";
17 +
18 +it.effect("only drags existing absolute paths from the Mac host environment and app window", () =>
19 + Effect.gen(function* () {
20 + startDrag.mockClear();
21 + getFileIcon.mockClear();
22 + const fs = yield* FileSystem.FileSystem;
23 + const path = yield* Path.Path;
24 + const stateDir = yield* fs.makeTempDirectoryScoped();
25 + const filePath = path.join(stateDir, "file with spaces.txt");
26 + yield* fs.writeFileString(filePath, "file");
27 + yield* fs.writeFileString(path.join(stateDir, "environment-id"), "host\n");
28 + const run = (environmentId: string, target: string, platform = "darwin", sender = 1) =>
29 + startFileDrag.handler({ environmentId, path: target }, { sender: { id: sender } }).pipe(
30 + Effect.provideService(DesktopEnvironment.DesktopEnvironment, {
31 + path,
32 + stateDir,
33 + platform,
34 + } as DesktopEnvironment.DesktopEnvironment["Service"]),
35 + Effect.provideService(ElectronWindow.ElectronWindow, {
36 + main: Effect.succeedSome({ webContents: { id: 1, startDrag } }),
37 + } as unknown as ElectronWindow.ElectronWindow["Service"]),
38 + );
39 + assert.isFalse(yield* run("remote", filePath));
40 + assert.isFalse(yield* run("host", "relative.txt"));
41 + assert.isFalse(yield* run("host", `${filePath}:12`));
42 + assert.isFalse(yield* run("host", filePath, "linux"));
43 + assert.isFalse(yield* run("host", filePath, "darwin", 2));
44 + assert.strictEqual(startDrag.mock.calls.length, 0);
45 + assert.isTrue(yield* run("host", filePath));
46 + assert.deepEqual(startDrag.mock.calls, [[{ file: filePath, icon: "file-icon" }]]);
47 + yield* fs.remove(path.join(stateDir, "environment-id"));
48 + assert.isFalse(yield* run("host", filePath));
49 + assert.strictEqual(startDrag.mock.calls.length, 1);
50 + }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
51 +);
apps/desktop/src/ipc/methods/fileDrag.ts new
+36
@@ -0,0 +1,36 @@
1 +import * as Effect from "effect/Effect";
2 +import * as FileSystem from "effect/FileSystem";
3 +import * as Option from "effect/Option";
4 +import * as Schema from "effect/Schema";
5 +import * as Electron from "electron";
6 +
7 +import * as DesktopEnvironment from "../../app/DesktopEnvironment.ts";
8 +import * as ElectronWindow from "../../electron/ElectronWindow.ts";
9 +import * as IpcChannels from "../channels.ts";
10 +import { makeIpcMethod } from "../DesktopIpc.ts";
11 +import { getHostEnvironmentId } from "./localEnvironment.ts";
12 +
13 +export const startFileDrag = makeIpcMethod({
14 + channel: IpcChannels.START_FILE_DRAG_CHANNEL,
15 + payload: Schema.Struct({ environmentId: Schema.String, path: Schema.String }),
16 + result: Schema.Boolean,
17 + handler: Effect.fn("desktop.ipc.fileDrag.start")(function* (input, event) {
18 + const environment = yield* DesktopEnvironment.DesktopEnvironment;
19 + if (environment.platform !== "darwin" || !environment.path.isAbsolute(input.path)) return false;
20 + const hostEnvironmentId = yield* getHostEnvironmentId.handler(undefined);
21 + if (hostEnvironmentId === null || input.environmentId !== hostEnvironmentId) return false;
22 + const window = yield* (yield* ElectronWindow.ElectronWindow).main;
23 + if (Option.isNone(window) || event?.sender.id !== window.value.webContents.id) return false;
24 + const fs = yield* FileSystem.FileSystem;
25 + if (!(yield* fs.exists(input.path))) return false;
26 + return yield* Effect.promise(async () => {
27 + try {
28 + const icon = await Electron.app.getFileIcon(input.path, { size: "normal" });
29 + window.value.webContents.startDrag({ file: input.path, icon });
30 + return true;
31 + } catch {
32 + return false;
33 + }
34 + });
35 + }),
36 +});
apps/desktop/src/preload.ts
+1
@@ -255,6 +255,7 @@ contextBridge.exposeInMainWorld("desktopBridge", {
255 ipcRenderer.invoke(IpcChannels.RECEIVE_PROVIDER_AUTH_CALLBACK_CHANNEL, url),
256 cancelProviderAuthCallback: (url: string) =>
257 ipcRenderer.invoke(IpcChannels.CANCEL_PROVIDER_AUTH_CALLBACK_CHANNEL, url),
258 + startFileDrag: (input) => ipcRenderer.invoke(IpcChannels.START_FILE_DRAG_CHANNEL, input),
259 openExternal: (url: string) => ipcRenderer.invoke(IpcChannels.OPEN_EXTERNAL_CHANNEL, url),
260 readMedia: (url: string) => ipcRenderer.invoke(IpcChannels.READ_MEDIA_CHANNEL, url),
261 checkSystemPermission: (pane: string) =>
apps/web/src/components/ChatMarkdown.permissions.browser.test.tsx
+1
@@ -308,6 +308,7 @@ it("preserves keyboard activation of a file-options button", async () => {
308 copyMarkdown="[Readme](readme.md)"
309 theme="dark"
310 onOpenInPanel={vi.fn()}
311 + resolveDragPath={async () => "/work/readme.md"}
312 resolveCopyPaths={async () => ({ relativePath: "readme.md", fullPath: "/work/readme.md" })}
313 openInEditorMenuLabel="Open in editor"
314 />,
apps/web/src/components/ChatMarkdown.tsx
+4
@@ -1,3 +1,4 @@
1 +import { resolvePathLinkTarget } from "@t3tools/shared/fileLinks";
2 import { clientHostEnvironmentAtom } from "../state/clientHostEnvironment";
3 import { buildFileLinkParentSuffixByPath, fileLinkLabel } from "@t3tools/shared/fileLinks";
4 import {
@@ -941,6 +942,9 @@ function useChatMarkdownState({
942 )
943 : undefined
944 }
945 + resolveDragPath={async () =>
946 + resolvePathLinkTarget((await resolveHostPath()).absolutePath, cwd ?? "")
947 + }
948 resolveCopyPaths={async () =>
949 markdownFileCopyPaths(fileLinkMeta, await resolveHostPath(), cwd)
950 }
apps/web/src/components/ComposerPromptEditorTiptap.tsx
+6
@@ -1,3 +1,5 @@
1 +import { useNativeFileDrag } from "../useNativeFileDrag";
2 +import { resolvePathLinkTarget } from "@t3tools/shared/fileLinks";
3 import { useAtomValue } from "@effect/atom-react";
4 import { documentChipKindForFileName } from "~/pierre-icons";
5 import { isProviderSkillUserInvocable } from "@t3tools/client-runtime/providerSkills";
@@ -281,8 +283,12 @@ const ComposerMentionExtension = Node.create({
283 function ComposerMentionNodeView({ node }: NodeViewProps) {
284 const actions = use(ComposerContextActionsContext);
285 const path = (node.attrs.path as string) ?? "";
286 + const nativeDrag = useNativeFileDrag(actions.environmentId, () =>
287 + resolvePathLinkTarget(path, actions.cwd ?? ""),
288 + );
289 const chip = (
290 <ContextChip
291 + {...nativeDrag}
292 kind={documentChipKindForFileName(path) ?? "mention"}
293 dir="ltr"
294 render={<button type="button" />}
apps/web/src/components/chat/MarkdownFileLink.tsx
+7
@@ -1,3 +1,4 @@
1 +import { useNativeFileDrag } from "../../useNativeFileDrag";
2 import { useAssetUrlState } from "../../assets/assetUrls";
3 import { formatFilePathForClipboard } from "@t3tools/client-runtime/media-actions";
4 import {
@@ -42,6 +43,7 @@ interface MarkdownFileLinkProps {
43 threadRef?: ScopedThreadRef | undefined;
44 onOpen?: (() => Promise<AtomCommandResult<unknown, unknown>>) | undefined;
45 onOpenInPanel: (panelPath: string, line: number | undefined) => void;
46 + resolveDragPath: () => Promise<string>;
47 resolveCopyPaths: () => Promise<ReturnType<typeof markdownFileCopyPaths>>;
48 openInEditorMenuLabel: string;
49 onOpenInBrowser?: (() => Promise<AtomCommandResult<unknown, unknown>>) | undefined;
@@ -73,7 +75,9 @@ export const MarkdownFileLink = memo(function MarkdownFileLink({
75 onReveal,
76 revealLabel,
77 resolveCopyPaths,
78 + resolveDragPath,
79 }: MarkdownFileLinkProps) {
80 + const nativeDrag = useNativeFileDrag(environmentId, resolveDragPath);
81 const handleOpenInEditor = () => {
82 if (!onOpen) {
83 return;
@@ -328,6 +332,7 @@ export const MarkdownFileLink = memo(function MarkdownFileLink({
332 render={
333 hasPrimaryAction ? (
334 <ContextChip
335 + {...nativeDrag}
336 kind={chipKind}
337 dir="ltr"
338 render={<a href={href} />}
@@ -362,6 +367,7 @@ export const MarkdownFileLink = memo(function MarkdownFileLink({
367 </ContextChip>
368 ) : (
369 <ContextChip
370 + {...nativeDrag}
371 kind={chipKind}
372 dir="ltr"
373 render={<button type="button" />}
@@ -418,6 +424,7 @@ function areMarkdownFileLinkPropsEqual(
424 previous.onOpenMedia === next.onOpenMedia &&
425 previous.onReveal === next.onReveal &&
426 previous.resolveCopyPaths === next.resolveCopyPaths &&
427 + previous.resolveDragPath === next.resolveDragPath &&
428 previous.revealLabel === next.revealLabel
429 );
430 }
apps/web/src/components/chat/UserMessageContextChips.tsx
+6
@@ -1,3 +1,5 @@
1 +import { useNativeFileDrag } from "../../useNativeFileDrag";
2 +import { resolvePathLinkTarget } from "@t3tools/shared/fileLinks";
3 import { documentChipKindForFileName } from "~/pierre-icons";
4 import { isSvgAttachment } from "@t3tools/shared/image";
5 import { useRenderedAppearance } from "../../hooks/useTheme";
@@ -68,11 +70,15 @@ function UserMessageMentionChip(props: {
70 copyMarkdown: string;
71 }) {
72 const ctx = use(TimelineRowCtx);
73 + const nativeDrag = useNativeFileDrag(ctx.activeThreadEnvironmentId, () =>
74 + resolvePathLinkTarget(props.record.path, ctx.workspaceRoot ?? ""),
75 + );
76 return (
77 <Tooltip>
78 <TooltipTrigger
79 render={
80 <ContextChip
81 + {...nativeDrag}
82 kind={documentChipKindForFileName(props.record.path) ?? "mention"}
83 render={<button type="button" />}
84 aria-label={`Preview ${props.record.path}`}
apps/web/src/useNativeFileDrag.ts new
+47
@@ -0,0 +1,47 @@
1 +import { splitFilePathPosition } from "@t3tools/shared/fileLinks";
2 +import { isMacPlatform } from "./lib/utils";
3 +import { useAtomValue } from "@effect/atom-react";
4 +import { AuthOrchestrationOperateScope, type EnvironmentId } from "@t3tools/contracts";
5 +import type { DragEvent } from "react";
6 +
7 +import { hostEnvironmentIdAtom } from "./state/primaryEnvironment";
8 +import { useEnvironmentScope } from "./state/session";
9 +import { toastManager } from "./components/ui/toast";
10 +
11 +export function useNativeFileDrag(
12 + environmentId: EnvironmentId | null | undefined,
13 + resolvePath: () => string | Promise<string>,
14 +) {
15 + const hostEnvironmentId = useAtomValue(hostEnvironmentIdAtom);
16 + const canOperate = useEnvironmentScope(environmentId ?? null, AuthOrchestrationOperateScope);
17 + const enabled =
18 + typeof window.desktopBridge?.startFileDrag === "function" &&
19 + isMacPlatform(navigator.platform) &&
20 + environmentId != null &&
21 + environmentId === hostEnvironmentId &&
22 + canOperate;
23 + return {
24 + draggable: enabled || undefined,
25 + onDragStart: (event: DragEvent<HTMLElement>) => {
26 + if (!enabled) return;
27 + // Electron replaces the HTML drag with a native file drag.
28 + event.preventDefault();
29 + event.stopPropagation();
30 + let released = false;
31 + window.addEventListener("pointerup", () => (released = true), { once: true });
32 + void (async () => {
33 + const { path } = splitFilePathPosition(await resolvePath());
34 + if (released) return;
35 + const started = await window.desktopBridge?.startFileDrag({ environmentId, path });
36 + if (!started) throw new Error("The file is unavailable on this Mac.");
37 + })().catch((error: unknown) => {
38 + if (released) return;
39 + toastManager.add({
40 + type: "error",
41 + title: "Unable to drag file",
42 + description: error instanceof Error ? error.message : "An error occurred.",
43 + });
44 + });
45 + },
46 + };
47 +}
packages/contracts/src/ipc.ts
+1
@@ -1151,6 +1151,7 @@ export interface DesktopBridge {
1151 ) => Promise<T | null>;
1152 receiveProviderAuthCallback: (authorizationUrl: string) => Promise<string>;
1153 cancelProviderAuthCallback: (authorizationUrl: string) => Promise<void>;
1154 + startFileDrag: (input: { environmentId: EnvironmentId; path: string }) => Promise<boolean>;
1155 openExternal: (url: string) => Promise<boolean>;
1156 /** Reads http(s) media in the main process, outside renderer CORS and CSP; null on network failure. */
1157 readMedia: (url: string) => Promise<DesktopMediaRead | null>;