Skip to content

Commit 0d9ea0c

Browse files
committed
Merge remote-tracking branch 'origin/main' into forgejo-required-check-contexts
2 parents cfa53f9 + 01d2aa8 commit 0d9ea0c

120 files changed

Lines changed: 6994 additions & 519 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/snapShot/DesktopSnapShot.test.ts‎

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,7 @@ const layerTest = (
498498
DesktopEnvironment.DesktopEnvironment,
499499
DesktopEnvironment.DesktopEnvironment.of({
500500
platform,
501+
displayName: "T3 Code (Dev)",
501502
stateDir: "/state",
502503
linuxDesktopEntryName: "com.t3tools.T3Code.desktop",
503504
appRoot: "/repo",
@@ -3961,6 +3962,10 @@ it.effect.each([false, true])(
39613962
if (fails) {
39623963
const error = yield* service.setup("test-mac-capture").pipe(Effect.flip);
39633964
assert.equal(error.reason, "setup-failed");
3965+
assert.equal(
3966+
error.message,
3967+
'Could not capture a test snapshot. Capturing the window failed. macOS grants Screen Recording and Accessibility to each app separately. In System Settings, open Privacy & Security and check that "T3 Code (Dev)" is allowed for both.',
3968+
);
39643969
} else yield* service.setup("test-mac-capture");
39653970
assert.equal(macCaptureMock.mock.calls.length, 1);
39663971
assert.equal(macCaptureMock.mock.calls[0]?.[0], active);
@@ -3985,12 +3990,88 @@ it.effect.each([false, true])(
39853990
},
39863991
);
39873992

3993+
const macTestCaptureLayer = layerTest("darwin", {
3994+
makeTempDirectoryScoped: () => Effect.succeed("/tmp/setup-test"),
3995+
readDirectory: () => Effect.succeed([]),
3996+
});
3997+
const macTestCaptureWindow = {
3998+
platform: "macos",
3999+
id: 42,
4000+
title: "Setup",
4001+
owner: { name: "T3 Code", processId: 123, path: "/Applications/T3 Code.app" },
4002+
bounds: { x: 0, y: 0, width: 800, height: 600 },
4003+
};
4004+
4005+
it.effect("names the failed macOS window lookup without repeating what it printed", () => {
4006+
const script = 'ObjC.import("CoreGraphics"); function run() { return ""; }';
4007+
const stderr = "execution error: Error: Not authorized to send Apple events. (-1743)";
4008+
activeWindowMock
4009+
.mockReset()
4010+
.mockRejectedValue(
4011+
Object.assign(
4012+
new Error(`Command failed: /usr/bin/osascript -l JavaScript -e ${script}\n${stderr}`),
4013+
{ cmd: `/usr/bin/osascript -l JavaScript -e ${script}`, code: 1, killed: false },
4014+
),
4015+
);
4016+
macCaptureMock.mockReset();
4017+
return Effect.scoped(
4018+
Effect.gen(function* () {
4019+
const service = yield* DesktopSnapShot.make;
4020+
const error = yield* service.setup("test-mac-capture").pipe(Effect.flip);
4021+
assert.equal(
4022+
error.message,
4023+
'Could not capture a test snapshot. Finding the front window failed: osascript exited with code 1. macOS grants Screen Recording and Accessibility to each app separately. In System Settings, open Privacy & Security and check that "T3 Code (Dev)" is allowed for both.',
4024+
);
4025+
assert.include(String(error.cause), stderr);
4026+
assert.equal(macCaptureMock.mock.calls.length, 0);
4027+
}),
4028+
).pipe(Effect.provide(macTestCaptureLayer));
4029+
});
4030+
4031+
it.effect("says when macOS has no window to test capture", () => {
4032+
activeWindowMock.mockReset().mockResolvedValue(undefined);
4033+
macCaptureMock.mockReset();
4034+
return Effect.scoped(
4035+
Effect.gen(function* () {
4036+
const service = yield* DesktopSnapShot.make;
4037+
const error = yield* service.setup("test-mac-capture").pipe(Effect.flip);
4038+
assert.equal(
4039+
error.message,
4040+
"Could not capture a test snapshot. No window is available to capture.",
4041+
);
4042+
assert.equal(macCaptureMock.mock.calls.length, 0);
4043+
}),
4044+
).pipe(Effect.provide(macTestCaptureLayer));
4045+
});
4046+
4047+
it.effect("says when the macOS test capture command timed out", () => {
4048+
activeWindowMock.mockReset().mockResolvedValue(macTestCaptureWindow);
4049+
macCaptureMock.mockReset().mockRejectedValue(
4050+
Object.assign(new Error("Command failed: /usr/sbin/screencapture -l 42 -o -x"), {
4051+
code: null,
4052+
killed: true,
4053+
signal: "SIGTERM",
4054+
}),
4055+
);
4056+
return Effect.scoped(
4057+
Effect.gen(function* () {
4058+
const service = yield* DesktopSnapShot.make;
4059+
const error = yield* service.setup("test-mac-capture").pipe(Effect.flip);
4060+
assert.equal(
4061+
error.message,
4062+
'Could not capture a test snapshot. Capturing the window timed out. macOS grants Screen Recording and Accessibility to each app separately. In System Settings, open Privacy & Security and check that "T3 Code (Dev)" is allowed for both.',
4063+
);
4064+
}),
4065+
).pipe(Effect.provide(macTestCaptureLayer));
4066+
});
4067+
39884068
it.effect("rejects macOS test capture on other platforms", () =>
39894069
Effect.scoped(
39904070
Effect.gen(function* () {
39914071
const service = yield* DesktopSnapShot.make;
39924072
const error = yield* service.setup("test-mac-capture").pipe(Effect.flip);
39934073
assert.equal(error.reason, "unsupported-session");
4074+
assert.equal(error.message, "Test capture requires macOS.");
39944075
}),
39954076
).pipe(Effect.provide(layerTest("win32"))),
39964077
);

‎apps/desktop/src/snapShot/DesktopSnapShot.ts‎

Lines changed: 80 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import * as FileSystem from "effect/FileSystem";
3030
import * as Layer from "effect/Layer";
3131
import * as Option from "effect/Option";
3232
import * as Path from "effect/Path";
33+
import * as Predicate from "effect/Predicate";
3334
import * as Ref from "effect/Ref";
3435
import * as Schema from "effect/Schema";
3536
import * as Semaphore from "effect/Semaphore";
@@ -210,10 +211,36 @@ export class DesktopSnapShotSetupError extends Schema.TaggedError<DesktopSnapSho
210211
Schema.Literals(["preview-config", "apply-config"]),
211212
]),
212213
reason: Schema.Literals(["unsupported-session", "setup-failed", "shortcut-permissions"]),
214+
/** The part of the macOS test capture that failed, and how. */
215+
step: Schema.optional(Schema.Literals(["find-window", "capture-window"])),
216+
status: Schema.optional(Schema.Literals(["missing", "timed-out", "exited", "failed"])),
217+
exitCode: Schema.optional(Schema.Number),
218+
/** The running app, as macOS lists it under Privacy & Security. */
219+
appName: Schema.optional(Schema.String),
213220
cause: Schema.optional(Schema.Defect()),
214221
},
215222
) {
216223
override get message(): string {
224+
if (this.action === "test-mac-capture") {
225+
if (this.reason === "unsupported-session") return "Test capture requires macOS.";
226+
const summary = "Could not capture a test snapshot.";
227+
if (this.step === undefined) return summary;
228+
if (this.status === "missing") return `${summary} No window is available to capture.`;
229+
const [task, command] =
230+
this.step === "find-window"
231+
? ["Finding the front window", "osascript"]
232+
: ["Capturing the window", "screencapture"];
233+
const failure =
234+
this.status === "timed-out"
235+
? `${task} timed out.`
236+
: this.status === "exited" && this.exitCode !== undefined
237+
? `${task} failed: ${command} exited with code ${this.exitCode}.`
238+
: `${task} failed.`;
239+
// macOS can report both permissions as allowed while they belong to another install.
240+
return this.appName === undefined
241+
? `${summary} ${failure}`
242+
: `${summary} ${failure} macOS grants Screen Recording and Accessibility to each app separately. In System Settings, open Privacy & Security and check that "${this.appName}" is allowed for both.`;
243+
}
217244
if (this.action === "preview-config" || this.action === "apply-config") {
218245
if (this.reason === "unsupported-session")
219246
return "Config setup requires a Niri or Hyprland session.";
@@ -239,6 +266,18 @@ export class DesktopSnapShotSetupError extends Schema.TaggedError<DesktopSnapSho
239266
}
240267
}
241268

269+
/**
270+
* How a macOS capture command ended, read from the child process error's own fields.
271+
* What the command printed stays in the error's `cause`.
272+
*/
273+
function macCommandFailure(cause: unknown) {
274+
const failure = Predicate.isObject(cause) ? cause : {};
275+
if ("killed" in failure && failure.killed === true) return { status: "timed-out" } as const;
276+
return "code" in failure && typeof failure.code === "number"
277+
? ({ status: "exited", exitCode: failure.code } as const)
278+
: ({ status: "failed" } as const);
279+
}
280+
242281
type SnapShotSystemAnimationSettings = Pick<
243282
ReturnType<typeof Electron.systemPreferences.getAnimationSettings>,
244283
"prefersReducedMotion" | "shouldRenderRichAnimation"
@@ -1327,23 +1366,50 @@ export const make = Effect.gen(function* () {
13271366
return yield* new DesktopSnapShotSetupError({ action, reason: "unsupported-session" });
13281367
yield* Effect.scoped(
13291368
Effect.gen(function* () {
1330-
const directory = yield* fileSystem.makeTempDirectoryScoped({
1331-
prefix: "t3-snapshot-test-",
1332-
});
1333-
yield* Effect.tryPromise(async () => {
1334-
const active = await activeWindow("darwin");
1335-
if (!active) throw new Error("No window is available to test capture.");
1336-
await captureMacWindowSnapshot(
1337-
active,
1338-
path.join(directory, "test.png"),
1339-
snapShotThumbnailSize(active),
1369+
const directory = yield* fileSystem
1370+
.makeTempDirectoryScoped({ prefix: "t3-snapshot-test-" })
1371+
.pipe(
1372+
Effect.mapError(
1373+
(cause) => new DesktopSnapShotSetupError({ action, reason: "setup-failed", cause }),
1374+
),
13401375
);
1376+
const active = yield* Effect.tryPromise({
1377+
try: () => activeWindow("darwin"),
1378+
catch: (cause) =>
1379+
new DesktopSnapShotSetupError({
1380+
action,
1381+
reason: "setup-failed",
1382+
step: "find-window",
1383+
...macCommandFailure(cause),
1384+
appName: environment.displayName,
1385+
cause,
1386+
}),
1387+
});
1388+
if (!active)
1389+
return yield* new DesktopSnapShotSetupError({
1390+
action,
1391+
reason: "setup-failed",
1392+
step: "find-window",
1393+
status: "missing",
1394+
});
1395+
yield* Effect.tryPromise({
1396+
try: () =>
1397+
captureMacWindowSnapshot(
1398+
active,
1399+
path.join(directory, "test.png"),
1400+
snapShotThumbnailSize(active),
1401+
),
1402+
catch: (cause) =>
1403+
new DesktopSnapShotSetupError({
1404+
action,
1405+
reason: "setup-failed",
1406+
step: "capture-window",
1407+
...macCommandFailure(cause),
1408+
appName: environment.displayName,
1409+
cause,
1410+
}),
13411411
});
13421412
}),
1343-
).pipe(
1344-
Effect.mapError(
1345-
(cause) => new DesktopSnapShotSetupError({ action, reason: "setup-failed", cause }),
1346-
),
13471413
);
13481414
return;
13491415
} else if (action === "install-kde-helper" || action === "remove-kde-helper") {

‎apps/desktop/src/window/DesktopApplicationMenu.test.ts‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,37 @@ describe("DesktopApplicationMenu", () => {
280280
}),
281281
);
282282

283+
it.effect("zooms from the numeric keypad without listing the commands twice", () =>
284+
Effect.gen(function* () {
285+
for (const [accelerator, expected] of [
286+
["CmdOrCtrl+numadd", "zoom-in"],
287+
["CmdOrCtrl+numsub", "zoom-out"],
288+
["CmdOrCtrl+num0", "zoom-reset"],
289+
] as const) {
290+
const selectedAction = yield* Deferred.make<string>();
291+
const applicationMenuTemplate =
292+
yield* Deferred.make<readonly Electron.MenuItemConstructorOptions[]>();
293+
294+
yield* configureMenu(selectedAction, applicationMenuTemplate);
295+
296+
const template = yield* Deferred.await(applicationMenuTemplate);
297+
const viewMenu = template.find((item) => item.label === "View");
298+
if (!viewMenu || !Array.isArray(viewMenu.submenu)) {
299+
throw new Error("Expected View menu submenu to be an array.");
300+
}
301+
const item = viewMenu.submenu.find((entry) => entry.accelerator === accelerator);
302+
assert.isDefined(item);
303+
assert.isFalse(item.visible);
304+
if (typeof item.click !== "function") {
305+
throw new Error(`Expected ${accelerator} menu item to have a click handler.`);
306+
}
307+
308+
item.click({} as Electron.MenuItem, {} as Electron.BrowserWindow, {} as KeyboardEvent);
309+
assert.equal(yield* Deferred.await(selectedAction), expected);
310+
}
311+
}),
312+
);
313+
283314
it.effect("reloads the main window even while a browser page has focus", () =>
284315
Effect.gen(function* () {
285316
const selectedAction = yield* Deferred.make<string>();

‎apps/desktop/src/window/DesktopApplicationMenu.ts‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,26 @@ export const make = Effect.gen(function* () {
264264
click: zoomClick("in"),
265265
},
266266
{ label: "Zoom Out", accelerator: "CmdOrCtrl+-", click: zoomClick("out") },
267+
// The numeric keypad sends its own key codes, so the number-row
268+
// accelerators above never match it.
269+
{
270+
label: "Actual Size",
271+
accelerator: "CmdOrCtrl+num0",
272+
visible: false,
273+
click: zoomClick("reset"),
274+
},
275+
{
276+
label: "Zoom In",
277+
accelerator: "CmdOrCtrl+numadd",
278+
visible: false,
279+
click: zoomClick("in"),
280+
},
281+
{
282+
label: "Zoom Out",
283+
accelerator: "CmdOrCtrl+numsub",
284+
visible: false,
285+
click: zoomClick("out"),
286+
},
267287
{ type: "separator" },
268288
{ role: "togglefullscreen" },
269289
],

‎apps/desktop/src/window/DesktopWindow.test.ts‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -798,6 +798,40 @@ describe("DesktopWindow", () => {
798798
}),
799799
);
800800

801+
// Ctrl+wheel bypasses the menu accelerators, so it has to take the same
802+
// zoom step, preview restore included, from the window's zoom-changed event.
803+
it.effect("zooms the app from Ctrl+wheel like the menu does", () =>
804+
Effect.gen(function* () {
805+
const fakeWindow = makeFakeBrowserWindow();
806+
const createCount = yield* Ref.make(0);
807+
const mainWindow = yield* Ref.make<Option.Option<Electron.BrowserWindow>>(Option.none());
808+
const previewZoomReapplies: number[] = [];
809+
const layer = layerTest({
810+
window: fakeWindow.window,
811+
createCount,
812+
mainWindow,
813+
previewZoomReapplies,
814+
});
815+
816+
yield* Effect.gen(function* () {
817+
const desktopWindow = yield* DesktopWindow.DesktopWindow;
818+
yield* desktopWindow.handleBackendReady(new URL("http://127.0.0.1:3773"));
819+
820+
const zoomChanged = fakeWindow.webContentsListeners.get("zoom-changed");
821+
if (!zoomChanged) return yield* Effect.die("zoom-changed listener was not registered");
822+
zoomChanged({}, "in");
823+
zoomChanged({}, "out");
824+
zoomChanged({}, "out");
825+
826+
assert.deepEqual(
827+
fakeWindow.setZoomLevel.mock.calls.map(([level]) => level),
828+
[0.5, 0, -0.5],
829+
);
830+
assert.deepEqual(previewZoomReapplies, [0.5, 0, -0.5]);
831+
}).pipe(Effect.provide(layer));
832+
}),
833+
);
834+
801835
it.effect("reloads and inspects the main window even when a popup has focus", () =>
802836
Effect.gen(function* () {
803837
const fakeWindow = makeFakeBrowserWindow();

‎apps/desktop/src/window/DesktopWindow.ts‎

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,21 @@ export const make = Effect.gen(function* () {
329329
const currentMainWindow = electronWindow.currentMainOrFirst.pipe(Effect.flatMap(withoutSplash));
330330
const focusedMainWindow = electronWindow.focusedMainOrFirst.pipe(Effect.flatMap(withoutSplash));
331331

332+
const zoomWindow = Effect.fn("desktop.window.zoomWindow")(function* (
333+
window: Electron.BrowserWindow,
334+
direction: MainWindowZoomDirection,
335+
) {
336+
const webContents = window.webContents;
337+
webContents.setZoomLevel(
338+
direction === "reset" ? 0 : webContents.getZoomLevel() + (direction === "in" ? 0.5 : -0.5),
339+
);
340+
if (environment.platform === "darwin") syncMacosWindowButtons(window);
341+
// Chromium pushes the new level down to embedded guests, which would zoom
342+
// the previewed page along with the app UI. The preview browser keeps its
343+
// own zoom, so put each guest back where the preview left it.
344+
yield* previewManager.reapplyZoom();
345+
});
346+
332347
const createWindow = Effect.fn("desktop.window.createWindow")(function* (): Effect.fn.Return<
333348
Electron.BrowserWindow,
334349
DesktopWindowError
@@ -645,6 +660,13 @@ export const make = Effect.gen(function* () {
645660
window.webContents.on("input-event", (_event, input) => {
646661
if (input.type === "gestureScrollEnd") window.webContents.send(TRACKPAD_SCROLL_END_CHANNEL);
647662
});
663+
// Ctrl+wheel never reaches the menu accelerators: Chromium reports it here
664+
// and leaves applying the zoom to the app. A preview guest under the
665+
// pointer emits this on its own webContents and keeps its own zoom.
666+
window.webContents.on("zoom-changed", (_event, direction) => {
667+
if (window.isDestroyed()) return;
668+
runFork(zoomWindow(window, direction));
669+
});
648670

649671
window.on("page-title-updated", (event) => {
650672
event.preventDefault();
@@ -976,15 +998,7 @@ export const make = Effect.gen(function* () {
976998
if (Option.isNone(window) || window.value.isDestroyed()) {
977999
return;
9781000
}
979-
const webContents = window.value.webContents;
980-
webContents.setZoomLevel(
981-
direction === "reset" ? 0 : webContents.getZoomLevel() + (direction === "in" ? 0.5 : -0.5),
982-
);
983-
if (environment.platform === "darwin") syncMacosWindowButtons(window.value);
984-
// Chromium pushes the new level down to embedded guests, which would zoom
985-
// the previewed page along with the app UI. The preview browser keeps its
986-
// own zoom, so put each guest back where the preview left it.
987-
yield* previewManager.reapplyZoom();
1001+
yield* zoomWindow(window.value, direction);
9881002
}),
9891003
runMainContentsCommand: Effect.fn("desktop.window.runMainContentsCommand")(function* (command) {
9901004
yield* Effect.annotateCurrentSpan({ command });

0 commit comments

Comments
 (0)