TypeScript
fix(desktop): accept existing Hyprland SnapShot shortcuts (#10658)
Akshar Patelfb331610a8d6
diff --git a/apps/desktop/src/snapShot/CaptureShortcutConfig.test.ts b/apps/desktop/src/snapShot/CaptureShortcutConfig.test.tsindex aa7d6cce511..ea6e176b820 100644--- a/apps/desktop/src/snapShot/CaptureShortcutConfig.test.ts+++ b/apps/desktop/src/snapShot/CaptureShortcutConfig.test.ts@@ -156,6 +156,87 @@ it("leaves already configured files unchanged, without creating a backup", asyncexpect(await setup.apply(preview.id, "niri")).toEqual({ backupPath: null, warning: null });expect(await NodeFSP.readdir(directory)).toEqual(["config.kdl"]);});+it("accepts an unchanged capture shortcut reported as a Lua callback", async () => {+ const hypr = {+ ...target(),+ desktop: "hyprland" as const,+ path: NodePath.join(directory, "bindings.lua"),+ };+ const before = `${captureConfigBinding("hyprland-lua", appId, "Ctrl+Shift+2")}\n`;+ await NodeFSP.writeFile(hypr.path, before);+ tools.hyprlandBindings.mockResolvedValue([+ { modmask: 5, key: "2", dispatcher: "__lua", arg: "92" },+ ]);++ const preview = await setup.preview(hypr, install);+ expect(preview.after).toBe(before);+ expect(preview.shortcut).toBe("Ctrl+Shift+2");+ expect(await setup.apply(preview.id, "hyprland")).toEqual({ backupPath: null, warning: null });+ expect(await NodeFSP.readFile(hypr.path, "utf8")).toBe(before);+ expect((await NodeFSP.readdir(directory)).sort()).toEqual(["bindings.lua", "config.kdl"]);+ expect(tools.reloadHyprland).not.toHaveBeenCalled();++ const unchanged = await setup.preview(hypr, install);+ await NodeFSP.appendFile(hypr.path, "-- edited elsewhere\n");+ await expect(setup.apply(unchanged.id, "hyprland")).rejects.toThrow("changed since");+ expect(await NodeFSP.readFile(hypr.path, "utf8")).toBe(before + "-- edited elsewhere\n");+});+it.each(["preview", "apply"] as const)(+ "checks unchanged Hyprland .conf shortcuts for conflicts during %s",+ async (stage) => {+ const hypr = {+ ...target(),+ desktop: "hyprland" as const,+ path: NodePath.join(directory, "hyprland.conf"),+ };+ const before = `${captureConfigBinding("hyprland", appId, "Ctrl+Shift+2")}\n`;+ await NodeFSP.writeFile(hypr.path, before);+ const ownBinding = {+ modmask: 5,+ key: "2",+ dispatcher: "global",+ arg: `${appId}:capture-window`,+ };+ tools.hyprlandBindings.mockResolvedValue([ownBinding]);+ const unchanged = await setup.preview(hypr, install);+ expect(unchanged.after).toBe(before);+ expect(await setup.apply(unchanged.id, "hyprland")).toEqual({+ backupPath: null,+ warning: null,+ });++ const preview = stage === "apply" ? await setup.preview(hypr, install) : undefined;+ tools.hyprlandBindings.mockResolvedValue([+ ownBinding,+ { modmask: 5, key: "2", dispatcher: "exec", arg: "kitty" },+ ]);+ await expect(+ preview ? setup.apply(preview.id, "hyprland") : setup.preview(hypr, install),+ ).rejects.toThrow("already used");+ expect(await NodeFSP.readFile(hypr.path, "utf8")).toBe(before);+ expect((await NodeFSP.readdir(directory)).sort()).toEqual(["config.kdl", "hyprland.conf"]);+ expect(tools.reloadHyprland).not.toHaveBeenCalled();+ },+);+it("rejects changing an existing capture shortcut to keys used by another Lua callback", async () => {+ const hypr = {+ ...target(),+ desktop: "hyprland" as const,+ path: NodePath.join(directory, "bindings.lua"),+ };+ const before = `${captureConfigBinding("hyprland-lua", appId, "Ctrl+Alt+Y")}\n`;+ await NodeFSP.writeFile(hypr.path, before);+ const request = { ...install, shortcut: "Ctrl+Shift+2" };+ const occupied = [{ modmask: 5, key: "2", dispatcher: "__lua", arg: "93" }];+ tools.hyprlandBindings.mockResolvedValue(occupied);+ await expect(setup.preview(hypr, request)).rejects.toThrow("already used");++ tools.hyprlandBindings.mockResolvedValue([]);+ const preview = await setup.preview(hypr, request);+ tools.hyprlandBindings.mockResolvedValue(occupied);+ await expect(setup.apply(preview.id, "hyprland")).rejects.toThrow("already used");+ expect(await NodeFSP.readFile(hypr.path, "utf8")).toBe(before);+});it("detects conflicts in included Niri configs without changing either file", async () => {await NodeFSP.appendFile(path, 'include "keys.kdl"\n');await NodeFSP.writeFile(NodePath.join(directory, "keys.kdl"), "binds { Shift+Ctrl+2 { quit; } }");@@ -178,7 +259,7 @@ it("rejects a conflict from another Hyprland config or dynamic Lua bind", async};await NodeFSP.writeFile(hypr.path, "-- user bindings\n");tools.hyprlandBindings.mockResolvedValue([- { modmask: 5, key: "2", dispatcher: "exec", arg: "kitty" },+ { modmask: 5, key: "2", dispatcher: "__lua", arg: "92" },]);await expect(setup.preview(hypr, install)).rejects.toThrow("already used");expect(await NodeFSP.readFile(hypr.path, "utf8")).toBe("-- user bindings\n");diff --git a/apps/desktop/src/snapShot/CaptureShortcutConfig.ts b/apps/desktop/src/snapShot/CaptureShortcutConfig.tsindex 8b86b416cdb..abf85da4f10 100644--- a/apps/desktop/src/snapShot/CaptureShortcutConfig.ts+++ b/apps/desktop/src/snapShot/CaptureShortcutConfig.ts@@ -95,7 +95,13 @@ const defaultTools = {/** The renderer can apply only the exact proposal retained by the desktop process. */export class CaptureShortcutConfig {private pending:- | { preview: DesktopCaptureConfigPreview; target: Target; files: Snapshot[]; missing: string[] }+ | {+ preview: DesktopCaptureConfigPreview;+ target: Target;+ files: Snapshot[];+ missing: string[];+ checkHyprlandKeys: boolean;+ }| undefined;private applying = false;private readonly home: string;@@ -153,6 +159,10 @@ export class CaptureShortcutConfig {request.operation,request.shortcut,);+ // Lua bindings appear as opaque callbacks, including our own unchanged shortcut.+ const checkHyprlandKeys =+ request.operation === "install" &&+ (format === "hyprland" || (format === "hyprland-lua" && edit.after !== root.text));const files = [root];const missing: string[] = [];if (target.desktop === "niri") {@@ -195,7 +205,7 @@ export class CaptureShortcutConfig {}};await visit(root, 0);- } else if (request.operation === "install") {+ } else if (checkHyprlandKeys) {await this.checkHyprlandKeys(target.appId, edit.shortcut);}const preview = {@@ -207,7 +217,7 @@ export class CaptureShortcutConfig {shortcut: edit.shortcut,operation: request.operation,};- this.pending = { preview, target, files, missing };+ this.pending = { preview, target, files, missing, checkHyprlandKeys };return preview;}@@ -255,7 +265,7 @@ export class CaptureShortcutConfig {}};await unchanged();- if (desktop === "hyprland" && preview.operation === "install")+ if (pending.checkHyprlandKeys)await this.checkHyprlandKeys(pending.target.appId, preview.shortcut);if (preview.before === preview.after) return { backupPath: null, warning: null };await NodeFSP.writeFile(temporary, preview.after, {