Open History to see the last commit
1import * as HostProcess from "@t3tools/shared/HostProcess";
2import * as Context from "effect/Context";
3import * as Effect from "effect/Effect";
4import * as Layer from "effect/Layer";
5import * as ChildProcess from "effect/process/ChildProcess";
6import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
7
8export class DesktopPrimaryPaste extends Context.Service<
9 DesktopPrimaryPaste,
10 {
11 readonly enabled: boolean;
12 }
13>()("@t3tools/desktop/app/DesktopPrimaryPaste") {}
14
15// Chrome applies gtk-enable-primary-paste from chrome/, which Electron does not include.
16export const readGtkPrimaryPasteEnabled = Effect.gen(function* () {
17 const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
18 const output = yield* spawner.string(
19 ChildProcess.make(
20 "gsettings",
21 ["get", "org.gnome.desktop.interface", "gtk-enable-primary-paste"],
22 { stdin: "ignore", stderr: "ignore" },
23 ),
24 );
25 return output.trim() !== "false";
26}).pipe(
27 Effect.timeout("2 seconds"),
28 Effect.orElseSucceed(() => true),
29);
30
31const make = Effect.gen(function* () {
32 const platform = yield* HostProcess.Platform;
33 // Resolved before any window exists so the first preload never sees a provisional value.
34 const enabled =
35 platform === "darwin" || platform === "win32" ? true : yield* readGtkPrimaryPasteEnabled;
36 return DesktopPrimaryPaste.of({ enabled });
37});
38
39export const layer = Layer.effect(DesktopPrimaryPaste, make);