Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

fileDrag.ts

37 lines1.7 KB

1import * as Effect from "effect/Effect";
2import * as FileSystem from "effect/FileSystem";
3import * as Option from "effect/Option";
4import * as Schema from "effect/Schema";
5import * as Electron from "electron";
6​
7import * as DesktopEnvironment from "../../app/DesktopEnvironment.ts";
8import * as ElectronWindow from "../../electron/ElectronWindow.ts";
9import * as IpcChannels from "../channels.ts";
10import { makeIpcMethod } from "../DesktopIpc.ts";
11import { getHostEnvironmentId } from "./localEnvironment.ts";
12​
13export 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.platform !== "win32") return false;
20 if (!environment.path.isAbsolute(input.path)) return false;
21 const hostEnvironmentId = yield* getHostEnvironmentId.handler(undefined);
22 if (hostEnvironmentId === null || input.environmentId !== hostEnvironmentId) return false;
23 const window = yield* (yield* ElectronWindow.ElectronWindow).main;
24 if (Option.isNone(window) || event?.sender.id !== window.value.webContents.id) return false;
25 const fs = yield* FileSystem.FileSystem;
26 if (!(yield* fs.exists(input.path))) return false;
27 return yield* Effect.promise(async () => {
28 try {
29 const icon = await Electron.app.getFileIcon(input.path, { size: "normal" });
30 window.value.webContents.startDrag({ file: input.path, icon });
31 return true;
32 } catch {
33 return false;
34 }
35 });
36 }),
37});