Skip to content

Commit 77c885e

Browse files
Rhinomcdclaude
andauthored
fix(desktop): SnapShots keep working on Hyprland after an app update (#28240)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent dc102e5 commit 77c885e

6 files changed

Lines changed: 57 additions & 10 deletions

File tree

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,7 @@ import * as DesktopClientSettings from "../settings/DesktopClientSettings.ts";
427427
import * as DesktopWindow from "../window/DesktopWindow.ts";
428428
import * as DesktopSnapShot from "./DesktopSnapShot.ts";
429429
import * as SnapShotAccessibility from "./SnapShotAccessibility.ts";
430+
import { CaptureSetupRequiredError } from "./linuxCaptureSession.ts";
430431

431432
const accessibilityApp = {
432433
byPid: accessibilityByPidMock,
@@ -1875,6 +1876,22 @@ it.effect("capture fails closed while snapshots are disabled", () => {
18751876
).pipe(Effect.provide(layerTest("win32")));
18761877
});
18771878

1879+
it.effect("points capture setup failures at SnapShot settings", () => {
1880+
vi.stubEnv("XDG_SESSION_TYPE", "wayland");
1881+
linuxCaptureMock.mockRejectedValueOnce(new CaptureSetupRequiredError("Install the helper."));
1882+
return Effect.scoped(
1883+
Effect.gen(function* () {
1884+
const service = yield* DesktopSnapShot.make;
1885+
yield* service.configure(enabledSettings());
1886+
const failure = yield* Effect.flip(service.capture);
1887+
assert.equal(failure.operation, "setup-required");
1888+
}),
1889+
).pipe(
1890+
Effect.provide(layerTest("linux", { makeDirectory: () => Effect.void })),
1891+
Effect.ensuring(Effect.sync(() => vi.unstubAllEnvs())),
1892+
);
1893+
});
1894+
18781895
it.effect(
18791896
"keeps saved preferences but rechecks access and shortcut delivery after changing desktops",
18801897
() => {

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ import { niriSocketPath } from "./NiriSnapShot.ts";
5050
import { CaptureShortcutConfig, niriCaptureConfigPath } from "./CaptureShortcutConfig.ts";
5151
import type { PortalCaptureShortcut } from "./PortalCaptureShortcut.ts";
5252
import {
53+
CaptureSetupRequiredError,
5354
isGnomeCaptureSession,
5455
niriCaptureBinding,
5556
portalShortcutTrigger,
@@ -128,6 +129,7 @@ const DesktopSnapShotOperation = Schema.Literals([
128129
"disabled",
129130
"no-window-selected",
130131
"window-unavailable",
132+
"setup-required",
131133
"capture",
132134
]);
133135

@@ -155,6 +157,8 @@ export class DesktopSnapShotError extends Schema.TaggedError<DesktopSnapShotErro
155157
return "No window was selected.";
156158
case "window-unavailable":
157159
return "The active window is not available for capture.";
160+
case "setup-required":
161+
return "Finish SnapShot setup in Settings → SnapShots.";
158162
case "capture":
159163
return "Could not capture the active window.";
160164
}
@@ -164,9 +168,9 @@ export class DesktopSnapShotError extends Schema.TaggedError<DesktopSnapShotErro
164168
const isDesktopSnapShotError = Schema.is(DesktopSnapShotError);
165169

166170
function captureFailure(cause: unknown, captureId?: string): DesktopSnapShotError {
167-
return isDesktopSnapShotError(cause)
168-
? cause
169-
: new DesktopSnapShotError({ operation: "capture", captureId, cause });
171+
if (isDesktopSnapShotError(cause)) return cause;
172+
const operation = cause instanceof CaptureSetupRequiredError ? "setup-required" : "capture";
173+
return new DesktopSnapShotError({ operation, captureId, cause });
170174
}
171175

172176
export class DesktopSnapShot extends Context.Service<

‎apps/desktop/src/snapShot/HyprlandSnapShot.test.ts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
hyprlandCaptureExecutable,
1919
hyprlandCaptureShortcut,
2020
} from "./HyprlandSnapShot.ts";
21+
import { CaptureSetupRequiredError } from "./linuxCaptureSession.ts";
2122

2223
let directory: string;
2324
let paths: { bundle: string; dataHome: string };
@@ -86,6 +87,20 @@ it("updates explicitly and removes only its helper", async () => {
8687
expect((await setup.state()).status).toBe("not-installed");
8788
expect(await NodeFSP.readFile(unrelated, "utf8")).toBe("keep");
8889
});
90+
it("updates a helper left stale by an app update before capturing", async () => {
91+
await setup.perform("install-hyprland-helper");
92+
await NodeFSP.writeFile(paths.bundle, "update");
93+
const result = await captureHyprlandWindow(paths);
94+
expect(result.png).toEqual(png);
95+
expect(await NodeFSP.readFile(hyprlandCaptureExecutable(paths), "utf8")).toBe("update");
96+
result.feedback?.close();
97+
});
98+
it("leaves first-time installation to explicit setup", async () => {
99+
await expect(captureHyprlandWindow(paths)).rejects.toBeInstanceOf(CaptureSetupRequiredError);
100+
await expect(NodeFSP.stat(hyprlandCaptureExecutable(paths))).rejects.toMatchObject({
101+
code: "ENOENT",
102+
});
103+
});
89104
it("does not overwrite a linked executable", async () => {
90105
const executable = hyprlandCaptureExecutable(paths);
91106
await NodeFSP.mkdir(NodePath.dirname(executable), { recursive: true });

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import * as NodeURL from "node:url";
77
import * as Schema from "effect/Schema";
88
import type { DesktopCaptureHelperState } from "@t3tools/contracts";
99
import type { LinuxWindowSnapshot } from "./LinuxSnapShot.ts";
10-
import { readPortalPng } from "./linuxCaptureSession.ts";
10+
import { CaptureSetupRequiredError, readPortalPng } from "./linuxCaptureSession.ts";
1111
import { startNativeCaptureFeedback } from "./NativeCaptureFeedback.ts";
1212
import { HYPRLAND_CAPTURE_ACTION } from "./linuxCaptureSession.ts";
1313
export { isHyprlandCaptureSession } from "./linuxCaptureSession.ts";
@@ -167,9 +167,15 @@ export async function captureHyprlandWindow(
167167
paths: HyprlandCapturePaths,
168168
options?: { readonly flash: boolean; readonly animate: boolean },
169169
): Promise<LinuxWindowSnapshot> {
170-
const state = await new HyprlandCaptureSetup(paths).state();
171-
if (state.status !== "ready")
172-
throw new Error(`${state.message} Open Settings → SnapShots to continue setup.`);
170+
const setup = new HyprlandCaptureSetup(paths);
171+
let state = await setup.state();
172+
if (state.status === "update-required") {
173+
await setup.perform("install-hyprland-helper").catch((cause: unknown) => {
174+
throw new CaptureSetupRequiredError(state.message, { cause });
175+
});
176+
state = await setup.state();
177+
}
178+
if (state.status !== "ready") throw new CaptureSetupRequiredError(state.message);
173179
const executable = hyprlandCaptureExecutable(paths);
174180
const directory = await NodeFSP.mkdtemp(NodePath.join(NodeOS.tmpdir(), "t3-hyprland-capture-"));
175181
const cleanup = () => NodeFSP.rm(directory, { recursive: true, force: true });

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import type { DesktopCaptureHelperState } from "@t3tools/contracts";
1010

1111
import { escapeDesktopEntryExecArgument } from "../app/DesktopLinuxUrlHandler.ts";
1212
import type { LinuxWindowSnapshot } from "./LinuxSnapShot.ts";
13-
import { readPortalPng } from "./linuxCaptureSession.ts";
13+
import { CaptureSetupRequiredError, readPortalPng } from "./linuxCaptureSession.ts";
1414
import { startNativeCaptureFeedback } from "./NativeCaptureFeedback.ts";
1515
export { isKdeCaptureSession } from "./linuxCaptureSession.ts";
1616

@@ -215,8 +215,7 @@ export async function captureKdeWindow(
215215
options?: { readonly flash: boolean; readonly animate: boolean },
216216
): Promise<LinuxWindowSnapshot> {
217217
const state = await new KdeCaptureSetup(paths).state();
218-
if (state.status !== "ready")
219-
throw new Error(`${state.message} Open Settings → SnapShots to continue setup.`);
218+
if (state.status !== "ready") throw new CaptureSetupRequiredError(state.message);
220219
const { executable } = kdeCapturePaths(paths);
221220
const directory = await NodeFSP.mkdtemp(NodePath.join(NodeOS.tmpdir(), "t3-kde-capture-"));
222221
let retained = false;

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ import { nativeImage } from "electron";
77
// Keep dbus-next out of desktop detection and PNG reads in the main process.
88

99
export const HYPRLAND_CAPTURE_ACTION = "capture-window";
10+
11+
export class CaptureSetupRequiredError extends Error {
12+
constructor(state: string, options?: ErrorOptions) {
13+
super(`${state} Open Settings → SnapShots to continue setup.`, options);
14+
}
15+
}
1016
export const NIRI_CAPTURE_PATH = "/com/t3tools/SnapShot";
1117
export const NIRI_CAPTURE_INTERFACE = "com.t3tools.SnapShot";
1218

0 commit comments

Comments
 (0)