Skip to content

Commit 1a96406

Browse files
committed
Merge remote-tracking branch 'origin/main' into t3/fix-interruption-wrapping
2 parents abfb6f7 + 6e6b6a7 commit 1a96406

63 files changed

Lines changed: 2444 additions & 177 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎apps/desktop/src/app/DesktopApp.test.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ import * as DesktopConnectionCatalogStore from "./DesktopConnectionCatalogStore.
5959
import * as DesktopIpc from "../ipc/DesktopIpc.ts";
6060
import * as DesktopLocalEnvironmentAuth from "../backend/DesktopLocalEnvironmentAuth.ts";
6161
import * as DesktopManagedPolicy from "./DesktopManagedPolicy.ts";
62+
import * as DesktopPrimaryPaste from "./DesktopPrimaryPaste.ts";
6263
import * as DesktopSshEnvironment from "../ssh/DesktopSshEnvironment.ts";
6364
import * as DesktopSshPasswordPrompts from "../ssh/DesktopSshPasswordPrompts.ts";
6465
import * as DesktopTelemetryPublisher from "../telemetry/DesktopTelemetryPublisher.ts";
@@ -222,6 +223,7 @@ it.effect.each([false, true])(
222223
Layer.mock(DesktopIpc.DesktopIpc, {}),
223224
Layer.mock(DesktopLocalEnvironmentAuth.DesktopLocalEnvironmentAuth, {}),
224225
Layer.mock(DesktopManagedPolicy.DesktopManagedPolicy, {}),
226+
Layer.succeed(DesktopPrimaryPaste.DesktopPrimaryPaste, { enabled: true }),
225227
Layer.mock(DesktopSshEnvironment.DesktopSshEnvironment, {}),
226228
Layer.mock(DesktopSshPasswordPrompts.DesktopSshPasswordPrompts, {}),
227229
Layer.mock(DesktopTelemetryPublisher.DesktopTelemetryPublisher, {}),
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import { assert, describe, it } from "@effect/vitest";
2+
import * as Effect from "effect/Effect";
3+
import * as Fiber from "effect/Fiber";
4+
import * as PlatformError from "effect/PlatformError";
5+
import * as Sink from "effect/Sink";
6+
import * as Stream from "effect/Stream";
7+
import * as TestClock from "effect/testing/TestClock";
8+
import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
9+
10+
import { readGtkPrimaryPasteEnabled } from "./DesktopPrimaryPaste.ts";
11+
12+
const encoder = new TextEncoder();
13+
14+
const spawnerWithStdout = (stdout: Stream.Stream<Uint8Array>) =>
15+
ChildProcessSpawner.make(() =>
16+
Effect.succeed(
17+
ChildProcessSpawner.makeHandle({
18+
pid: ChildProcessSpawner.ProcessId(1),
19+
exitCode: Effect.succeed(ChildProcessSpawner.ExitCode(0)),
20+
isRunning: Effect.succeed(false),
21+
kill: () => Effect.void,
22+
unref: Effect.succeed(Effect.void),
23+
stdin: Sink.drain,
24+
stdout,
25+
stderr: Stream.empty,
26+
all: Stream.empty,
27+
getInputFd: () => Sink.drain,
28+
getOutputFd: () => Stream.empty,
29+
}),
30+
),
31+
);
32+
33+
const read = (spawner: ChildProcessSpawner.ChildProcessSpawner["Service"]) =>
34+
readGtkPrimaryPasteEnabled.pipe(
35+
Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
36+
);
37+
38+
describe("readGtkPrimaryPasteEnabled", () => {
39+
it.effect("is disabled only when gsettings prints false", () =>
40+
Effect.gen(function* () {
41+
assert.isFalse(yield* read(spawnerWithStdout(Stream.make(encoder.encode("false\n")))));
42+
assert.isTrue(yield* read(spawnerWithStdout(Stream.make(encoder.encode("true\n")))));
43+
// A missing schema prints nothing on stdout.
44+
assert.isTrue(yield* read(spawnerWithStdout(Stream.empty)));
45+
}),
46+
);
47+
48+
it.effect("stays enabled when gsettings cannot run", () =>
49+
Effect.gen(function* () {
50+
const missing = ChildProcessSpawner.make(() =>
51+
Effect.fail(
52+
PlatformError.systemError({
53+
_tag: "NotFound",
54+
module: "ChildProcess",
55+
method: "spawn",
56+
description: "gsettings not found",
57+
}),
58+
),
59+
);
60+
assert.isTrue(yield* read(missing));
61+
}),
62+
);
63+
64+
it.effect("stays enabled when gsettings hangs", () =>
65+
Effect.gen(function* () {
66+
const fiber = yield* read(spawnerWithStdout(Stream.never)).pipe(Effect.forkChild);
67+
yield* TestClock.adjust("2 seconds");
68+
assert.isTrue(yield* Fiber.join(fiber));
69+
}),
70+
);
71+
});
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import * as HostProcess from "@t3tools/shared/HostProcess";
2+
import * as Context from "effect/Context";
3+
import * as Effect from "effect/Effect";
4+
import * as Layer from "effect/Layer";
5+
import * as ChildProcess from "effect/process/ChildProcess";
6+
import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
7+
8+
export 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.
16+
export 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+
31+
const 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+
39+
export const layer = Layer.effect(DesktopPrimaryPaste, make);

‎apps/desktop/src/ipc/DesktopIpcHandlers.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import {
4040
installRelayTelemetryEnabled,
4141
getSystemLocale,
4242
getPreferredSystemLanguages,
43+
getPrimaryPasteEnabled,
4344
getWindowFullscreenState,
4445
openExternal,
4546
readMedia,
@@ -99,6 +100,7 @@ export const installDesktopIpcHandlers = Effect.fn("desktop.ipc.installHandlers"
99100
yield* installRelayTelemetryEnabled();
100101
yield* ipc.handleSync(getSystemLocale);
101102
yield* ipc.handleSync(getPreferredSystemLanguages);
103+
yield* ipc.handleSync(getPrimaryPasteEnabled);
102104
yield* ipc.handleSync(getWindowFullscreenState);
103105
yield* ipc.handleSync(getLocalEnvironmentBootstraps);
104106
yield* ipc.handleSync(getLocalEnvironmentEnabled);

‎apps/desktop/src/ipc/channels.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ export const GET_APP_BRANDING_CHANNEL = "desktop:get-app-branding";
3737
export const GET_RELAY_TELEMETRY_ENABLED_CHANNEL = "desktop:get-relay-telemetry-enabled";
3838
export const GET_SYSTEM_LOCALE_CHANNEL = "desktop:get-system-locale";
3939
export const GET_PREFERRED_SYSTEM_LANGUAGES_CHANNEL = "desktop:get-preferred-system-languages";
40+
export const GET_PRIMARY_PASTE_ENABLED_CHANNEL = "desktop:get-primary-paste-enabled";
4041
export const GET_LOCAL_ENVIRONMENT_BOOTSTRAPS_CHANNEL = "desktop:get-local-environment-bootstraps";
4142
export const GET_LOCAL_ENVIRONMENT_ENABLED_CHANNEL = "desktop:get-local-environment-enabled";
4243
export const SET_LOCAL_ENVIRONMENT_ENABLED_CHANNEL = "desktop:set-local-environment-enabled";

‎apps/desktop/src/ipc/methods/window.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import * as DesktopBackendPool from "../../backend/DesktopBackendPool.ts";
2929
import * as DesktopLocalEnvironmentAuth from "../../backend/DesktopLocalEnvironmentAuth.ts";
3030
import * as DesktopEnvironment from "../../app/DesktopEnvironment.ts";
3131
import * as DesktopManagedPolicy from "../../app/DesktopManagedPolicy.ts";
32+
import * as DesktopPrimaryPaste from "../../app/DesktopPrimaryPaste.ts";
3233
import * as DesktopAppSettings from "../../settings/DesktopAppSettings.ts";
3334
import * as DesktopWslBackend from "../../wsl/DesktopWslBackend.ts";
3435
import * as DesktopWslEnvironment from "../../wsl/DesktopWslEnvironment.ts";
@@ -106,6 +107,15 @@ export const getSystemLocale = DesktopIpc.makeSyncIpcMethod({
106107
}),
107108
});
108109

110+
export const getPrimaryPasteEnabled = DesktopIpc.makeSyncIpcMethod({
111+
channel: IpcChannels.GET_PRIMARY_PASTE_ENABLED_CHANNEL,
112+
result: Schema.Boolean,
113+
handler: Effect.fn("desktop.ipc.window.getPrimaryPasteEnabled")(function* () {
114+
const primaryPaste = yield* DesktopPrimaryPaste.DesktopPrimaryPaste;
115+
return primaryPaste.enabled;
116+
}),
117+
});
118+
109119
export const getPreferredSystemLanguages = DesktopIpc.makeSyncIpcMethod({
110120
channel: IpcChannels.GET_PREFERRED_SYSTEM_LANGUAGES_CHANNEL,
111121
result: Schema.Array(Schema.String),

‎apps/desktop/src/main.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ import * as DesktopClerk from "./app/DesktopClerk.ts";
4343
import * as DesktopCliCommand from "./app/DesktopCliCommand.ts";
4444
import * as DesktopApplicationMenu from "./window/DesktopApplicationMenu.ts";
4545
import * as DesktopAssets from "./app/DesktopAssets.ts";
46+
import * as DesktopPrimaryPaste from "./app/DesktopPrimaryPaste.ts";
4647
import * as DesktopBackendConfiguration from "./backend/DesktopBackendConfiguration.ts";
4748
import * as DesktopBackendPool from "./backend/DesktopBackendPool.ts";
4849
import * as DesktopLocalEnvironmentAuth from "./backend/DesktopLocalEnvironmentAuth.ts";
@@ -152,6 +153,7 @@ const layerDesktopFoundation = Layer.mergeAll(
152153
DesktopClientSettings.layer,
153154
DesktopConnectionCatalogStore.layer.pipe(Layer.provideMerge(DesktopSavedEnvironments.layer)),
154155
DesktopAssets.layer,
156+
DesktopPrimaryPaste.layer,
155157
DesktopObservability.layer,
156158
DesktopRendererHistory.layer,
157159
).pipe(Layer.provideMerge(layerDesktopEnvironment));

‎apps/desktop/src/preload.ts‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,23 @@ const syncHtmlFullscreen = () => {
7070
syncHtmlFullscreen();
7171
document.addEventListener("fullscreenchange", syncHtmlFullscreen);
7272

73+
let primaryPasteEnabled = true;
74+
try {
75+
primaryPasteEnabled =
76+
ipcRenderer.sendSync(IpcChannels.GET_PRIMARY_PASTE_ENABLED_CHANNEL) !== false;
77+
} catch {}
78+
79+
if (!primaryPasteEnabled) {
80+
// Blink pastes the PRIMARY selection on a middle-click mouseup unless it is default-prevented.
81+
window.addEventListener(
82+
"mouseup",
83+
(event) => {
84+
if (event.button === 1) event.preventDefault();
85+
},
86+
true,
87+
);
88+
}
89+
7390
function unwrapEnsureSshEnvironmentResult(result: unknown) {
7491
if (
7592
typeof result === "object" &&
@@ -116,6 +133,7 @@ contextBridge.exposeInMainWorld("desktopBridge", {
116133
return () => ipcRenderer.removeListener(IpcChannels.TRACKPAD_SCROLL_END_CHANNEL, handler);
117134
},
118135
revealWindow: () => ipcRenderer.invoke(IpcChannels.REVEAL_WINDOW_CHANNEL),
136+
getPrimaryPasteEnabled: () => primaryPasteEnabled,
119137
getSystemLocale: () => {
120138
const result = ipcRenderer.sendSync(IpcChannels.GET_SYSTEM_LOCALE_CHANNEL);
121139
return typeof result === "string" ? result : null;
Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,24 @@
11
import { ipcRenderer } from "electron";
22

3-
import { PASSKEY_BRIDGE_ARGUMENT } from "./preview/GuestProtocol.ts";
3+
import {
4+
PASSKEY_BRIDGE_ARGUMENT,
5+
PRIMARY_PASTE_DISABLED_ARGUMENT,
6+
} from "./preview/GuestProtocol.ts";
47
import { installPasskeyBridge } from "./preview/PasskeyBridge.ts";
58
import "./preview/PickPreload.ts";
69

710
// Electron hands webPreferences.additionalArguments to sandboxed preloads in process.argv.
811
if (process.argv.includes(PASSKEY_BRIDGE_ARGUMENT)) {
912
installPasskeyBridge((channel, publicKey) => ipcRenderer.invoke(channel, publicKey));
1013
}
14+
15+
if (process.argv.includes(PRIMARY_PASTE_DISABLED_ARGUMENT)) {
16+
// Same guard as the app preload: Blink pastes PRIMARY on an uncancelled middle mouseup.
17+
window.addEventListener(
18+
"mouseup",
19+
(event) => {
20+
if (event.button === 1) event.preventDefault();
21+
},
22+
true,
23+
);
24+
}

‎apps/desktop/src/preview/GuestProtocol.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@ export const PASSKEY_GET_CHANNEL = "preview:passkey-get";
1717
export const PASSKEY_BRIDGE_ARGUMENT = "--t3code-preview-passkey-bridge";
1818
export const ANNOTATION_VOICE_EVENT_CHANNEL = "preview:annotation-voice-event";
1919
export const ANNOTATION_VOICE_STATE_CHANNEL = "preview:annotation-voice-state";
20+
export const PRIMARY_PASTE_DISABLED_ARGUMENT = "--t3code-preview-primary-paste-disabled";

0 commit comments

Comments
 (0)