Skip to content

Commit fb33161

Browse files
authored
fix(desktop): accept existing Hyprland SnapShot shortcuts (pingdotgg#10658)
1 parent bafadf3 commit fb33161

2 files changed

Lines changed: 96 additions & 5 deletions

File tree

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

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,87 @@ it("leaves already configured files unchanged, without creating a backup", async
156156
expect(await setup.apply(preview.id, "niri")).toEqual({ backupPath: null, warning: null });
157157
expect(await NodeFSP.readdir(directory)).toEqual(["config.kdl"]);
158158
});
159+
it("accepts an unchanged capture shortcut reported as a Lua callback", async () => {
160+
const hypr = {
161+
...target(),
162+
desktop: "hyprland" as const,
163+
path: NodePath.join(directory, "bindings.lua"),
164+
};
165+
const before = `${captureConfigBinding("hyprland-lua", appId, "Ctrl+Shift+2")}\n`;
166+
await NodeFSP.writeFile(hypr.path, before);
167+
tools.hyprlandBindings.mockResolvedValue([
168+
{ modmask: 5, key: "2", dispatcher: "__lua", arg: "92" },
169+
]);
170+
171+
const preview = await setup.preview(hypr, install);
172+
expect(preview.after).toBe(before);
173+
expect(preview.shortcut).toBe("Ctrl+Shift+2");
174+
expect(await setup.apply(preview.id, "hyprland")).toEqual({ backupPath: null, warning: null });
175+
expect(await NodeFSP.readFile(hypr.path, "utf8")).toBe(before);
176+
expect((await NodeFSP.readdir(directory)).sort()).toEqual(["bindings.lua", "config.kdl"]);
177+
expect(tools.reloadHyprland).not.toHaveBeenCalled();
178+
179+
const unchanged = await setup.preview(hypr, install);
180+
await NodeFSP.appendFile(hypr.path, "-- edited elsewhere\n");
181+
await expect(setup.apply(unchanged.id, "hyprland")).rejects.toThrow("changed since");
182+
expect(await NodeFSP.readFile(hypr.path, "utf8")).toBe(before + "-- edited elsewhere\n");
183+
});
184+
it.each(["preview", "apply"] as const)(
185+
"checks unchanged Hyprland .conf shortcuts for conflicts during %s",
186+
async (stage) => {
187+
const hypr = {
188+
...target(),
189+
desktop: "hyprland" as const,
190+
path: NodePath.join(directory, "hyprland.conf"),
191+
};
192+
const before = `${captureConfigBinding("hyprland", appId, "Ctrl+Shift+2")}\n`;
193+
await NodeFSP.writeFile(hypr.path, before);
194+
const ownBinding = {
195+
modmask: 5,
196+
key: "2",
197+
dispatcher: "global",
198+
arg: `${appId}:capture-window`,
199+
};
200+
tools.hyprlandBindings.mockResolvedValue([ownBinding]);
201+
const unchanged = await setup.preview(hypr, install);
202+
expect(unchanged.after).toBe(before);
203+
expect(await setup.apply(unchanged.id, "hyprland")).toEqual({
204+
backupPath: null,
205+
warning: null,
206+
});
207+
208+
const preview = stage === "apply" ? await setup.preview(hypr, install) : undefined;
209+
tools.hyprlandBindings.mockResolvedValue([
210+
ownBinding,
211+
{ modmask: 5, key: "2", dispatcher: "exec", arg: "kitty" },
212+
]);
213+
await expect(
214+
preview ? setup.apply(preview.id, "hyprland") : setup.preview(hypr, install),
215+
).rejects.toThrow("already used");
216+
expect(await NodeFSP.readFile(hypr.path, "utf8")).toBe(before);
217+
expect((await NodeFSP.readdir(directory)).sort()).toEqual(["config.kdl", "hyprland.conf"]);
218+
expect(tools.reloadHyprland).not.toHaveBeenCalled();
219+
},
220+
);
221+
it("rejects changing an existing capture shortcut to keys used by another Lua callback", async () => {
222+
const hypr = {
223+
...target(),
224+
desktop: "hyprland" as const,
225+
path: NodePath.join(directory, "bindings.lua"),
226+
};
227+
const before = `${captureConfigBinding("hyprland-lua", appId, "Ctrl+Alt+Y")}\n`;
228+
await NodeFSP.writeFile(hypr.path, before);
229+
const request = { ...install, shortcut: "Ctrl+Shift+2" };
230+
const occupied = [{ modmask: 5, key: "2", dispatcher: "__lua", arg: "93" }];
231+
tools.hyprlandBindings.mockResolvedValue(occupied);
232+
await expect(setup.preview(hypr, request)).rejects.toThrow("already used");
233+
234+
tools.hyprlandBindings.mockResolvedValue([]);
235+
const preview = await setup.preview(hypr, request);
236+
tools.hyprlandBindings.mockResolvedValue(occupied);
237+
await expect(setup.apply(preview.id, "hyprland")).rejects.toThrow("already used");
238+
expect(await NodeFSP.readFile(hypr.path, "utf8")).toBe(before);
239+
});
159240
it("detects conflicts in included Niri configs without changing either file", async () => {
160241
await NodeFSP.appendFile(path, 'include "keys.kdl"\n');
161242
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
178259
};
179260
await NodeFSP.writeFile(hypr.path, "-- user bindings\n");
180261
tools.hyprlandBindings.mockResolvedValue([
181-
{ modmask: 5, key: "2", dispatcher: "exec", arg: "kitty" },
262+
{ modmask: 5, key: "2", dispatcher: "__lua", arg: "92" },
182263
]);
183264
await expect(setup.preview(hypr, install)).rejects.toThrow("already used");
184265
expect(await NodeFSP.readFile(hypr.path, "utf8")).toBe("-- user bindings\n");

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

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,13 @@ const defaultTools = {
9595
/** The renderer can apply only the exact proposal retained by the desktop process. */
9696
export class CaptureShortcutConfig {
9797
private pending:
98-
| { preview: DesktopCaptureConfigPreview; target: Target; files: Snapshot[]; missing: string[] }
98+
| {
99+
preview: DesktopCaptureConfigPreview;
100+
target: Target;
101+
files: Snapshot[];
102+
missing: string[];
103+
checkHyprlandKeys: boolean;
104+
}
99105
| undefined;
100106
private applying = false;
101107
private readonly home: string;
@@ -153,6 +159,10 @@ export class CaptureShortcutConfig {
153159
request.operation,
154160
request.shortcut,
155161
);
162+
// Lua bindings appear as opaque callbacks, including our own unchanged shortcut.
163+
const checkHyprlandKeys =
164+
request.operation === "install" &&
165+
(format === "hyprland" || (format === "hyprland-lua" && edit.after !== root.text));
156166
const files = [root];
157167
const missing: string[] = [];
158168
if (target.desktop === "niri") {
@@ -195,7 +205,7 @@ export class CaptureShortcutConfig {
195205
}
196206
};
197207
await visit(root, 0);
198-
} else if (request.operation === "install") {
208+
} else if (checkHyprlandKeys) {
199209
await this.checkHyprlandKeys(target.appId, edit.shortcut);
200210
}
201211
const preview = {
@@ -207,7 +217,7 @@ export class CaptureShortcutConfig {
207217
shortcut: edit.shortcut,
208218
operation: request.operation,
209219
};
210-
this.pending = { preview, target, files, missing };
220+
this.pending = { preview, target, files, missing, checkHyprlandKeys };
211221
return preview;
212222
}
213223

@@ -255,7 +265,7 @@ export class CaptureShortcutConfig {
255265
}
256266
};
257267
await unchanged();
258-
if (desktop === "hyprland" && preview.operation === "install")
268+
if (pending.checkHyprlandKeys)
259269
await this.checkHyprlandKeys(pending.target.appId, preview.shortcut);
260270
if (preview.before === preview.after) return { backupPath: null, warning: null };
261271
await NodeFSP.writeFile(temporary, preview.after, {

0 commit comments

Comments
 (0)