@devries-cameron20 / t3code-1 / commits / 68f1bc58d5
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

fix(desktop): SnapShot shortcut rebinds after the desktop portal restarts (#35048)

Julius Marminge committed Nov 12, 2026 at 02:31 UTC 68f1bc58d584f9d44cd3c3c0a378f416e5284b6a
2 files changed +83 -20
apps/desktop/src/snapShot/PortalCaptureShortcut.test.ts
+28 -4
@@ -30,6 +30,7 @@ class FakeBus extends NodeEvents.EventEmitter {
30 requestPath = "";
31 boundId = "";
32 foreignHandle = false;
33 + owner = ":1.2";
34
35 send(message: Message) {
36 this.sends.push(message);
@@ -39,7 +40,7 @@ class FakeBus extends NodeEvents.EventEmitter {
40 member: string,
41 path: string,
42 body: unknown[],
42 - sender = ":1.2",
43 + sender = this.owner,
44 signature = "",
45 ) {
46 this.emit(
@@ -71,7 +72,7 @@ class FakeBus extends NodeEvents.EventEmitter {
72 ]),
73 });
74 }
74 - activate(id = this.boundId, session = this.session, sender = ":1.2") {
75 + activate(id = this.boundId, session = this.session, sender = this.owner) {
76 this.signal(portal, "Activated", root, [session, id, 1, {}], sender, "osta{sv}");
77 }
78 async call(message: Message) {
@@ -87,7 +88,7 @@ class FakeBus extends NodeEvents.EventEmitter {
88 if (this.registryError) throw this.registryError;
89 return reply();
90 }
90 - if (message.member === "GetNameOwner") return reply([":1.2"]);
91 + if (message.member === "GetNameOwner") return reply([this.owner]);
92 if (message.member === "Get") return reply([new Variant("u", this.version)]);
93 if (message.member === "AddMatch" || message.member === "ConfigureShortcuts") return reply();
94 const options = message.body.at(-1) as Record<string, Variant<string>>;
@@ -338,7 +339,30 @@ it("handles a disappearing portal without keeping a false registered state", asy
339 );
340 expect(client.state.shortcutRegistered).toBe(false);
341 expect(client.state.shortcutMessage).toContain("restarted");
341 - expect(bus.disconnect).toHaveBeenCalledOnce();
342 + expect(client.hasSession).toBe(false);
343 + expect(bus.disconnect).not.toHaveBeenCalled();
344 + await vi.waitFor(() => expect(client.state.shortcutRegistered).toBe(true));
345 + expect(bus.calls.filter((call) => call.member === "CreateSession")).toHaveLength(2);
346 +});
347 +
348 +it("binds a new session when the portal restarts", async () => {
349 + const { bus, client, capture } = start(new FakeBus(), chord, true);
350 + await client.ready;
351 + const ownerChanged = (from: string, to: string) =>
352 + bus.signal(
353 + "org.freedesktop.DBus",
354 + "NameOwnerChanged",
355 + "/org/freedesktop/DBus",
356 + ["org.freedesktop.portal.Desktop", from, to],
357 + "org.freedesktop.DBus",
358 + );
359 + ownerChanged(":1.2", "");
360 + bus.owner = ":1.3";
361 + ownerChanged("", ":1.3");
362 + await vi.waitFor(() => expect(client.state.shortcutActionRegistered).toBe(true));
363 + expect(bus.calls.filter((call) => call.member === "CreateSession")).toHaveLength(2);
364 + bus.activate();
365 + expect(capture).toHaveBeenCalledOnce();
366 });
367
368 it("accepts old portals without Registry but does not ignore permission denial", async () => {
apps/desktop/src/snapShot/PortalCaptureShortcut.ts
+55 -16
@@ -67,6 +67,7 @@ export class PortalCaptureShortcut {
67 };
68 readonly ready: Promise<void>;
69 private closed = false;
70 + private connection = 0;
71 private owner = "";
72 private namespace = "";
73 private session = "";
@@ -74,8 +75,10 @@ export class PortalCaptureShortcut {
75 private version = 0;
76 private pending: { path: string; resolve: (body: unknown) => void } | undefined;
77 private responses = new Map<string, unknown>();
77 - private readonly stopped: Promise<never>;
78 + private stopped!: Promise<never>;
79 private rejectStopped!: (reason: Error) => void;
80 + private readonly appId: string;
81 + private readonly trigger: string;
82 private readonly onCapture: () => void;
83 private readonly onStateChanged: () => void;
84 private readonly bus: MessageBus;
@@ -89,6 +92,8 @@ export class PortalCaptureShortcut {
92 bus: MessageBus = sessionBus(),
93 managedByHyprland = false,
94 ) {
95 + this.appId = appId;
96 + this.trigger = portalShortcutTrigger(shortcut);
97 this.onCapture = onCapture;
98 this.onStateChanged = onStateChanged;
99 this.bus = bus;
@@ -99,13 +104,41 @@ export class PortalCaptureShortcut {
104 shortcutPending: true,
105 shortcutMessage: "Connecting to Hyprland shortcuts…",
106 };
107 + this.resetStopped();
108 + bus.on("error", this.failed);
109 + bus.on("message", this.message);
110 + this.ready = this.connect(true);
111 + }
112 +
113 + private resetStopped() {
114 this.stopped = new Promise((_, reject) => {
115 this.rejectStopped = reject;
116 });
117 void this.stopped.catch(() => undefined);
106 - bus.on("error", this.failed);
107 - bus.on("message", this.message);
108 - this.ready = this.initialize(appId, shortcut).catch(this.failed);
118 + }
119 +
120 + private connect(first = false) {
121 + const connection = ++this.connection;
122 + return this.initialize(first).catch((error: unknown) => {
123 + if (connection === this.connection) this.failed(error);
124 + });
125 + }
126 +
127 + private portalGone() {
128 + this.rejectStopped(new Error("The desktop shortcut service restarted."));
129 + this.resetStopped();
130 + this.owner = "";
131 + this.session = "";
132 + this.pending = undefined;
133 + this.responses.clear();
134 + this.update({
135 + shortcutRegistered: false,
136 + shortcutActionRegistered: false,
137 + shortcutPending: true,
138 + shortcutMessage: "The desktop shortcut service restarted. Reconnecting…",
139 + });
140 + // Register is sent to the well-known name, so it D-Bus-activates a portal that is not running.
141 + void this.connect();
142 }
143
144 close = () => {
@@ -209,10 +242,9 @@ export class PortalCaptureShortcut {
242 message.sender === "org.freedesktop.DBus" &&
243 message.interface === DBUS &&
244 message.member === "NameOwnerChanged" &&
212 - message.body[0] === PORTAL &&
213 - message.body[1] === this.owner
245 + message.body[0] === PORTAL
246 ) {
215 - this.failed(new Error("The desktop shortcut service restarted. Retry the shortcut request."));
247 + if (this.owner && message.body[1] === this.owner) this.portalGone();
248 return;
249 }
250 if (!this.owner || message.sender !== this.owner) return;
@@ -270,7 +302,8 @@ export class PortalCaptureShortcut {
302 const response = new Promise<unknown>((done) => {
303 resolve = done;
304 });
273 - this.pending = { path: expectedPath, resolve };
305 + const pending = { path: expectedPath, resolve };
306 + this.pending = pending;
307 this.responses.clear();
308 let completed = false;
309 try {
@@ -284,7 +317,7 @@ export class PortalCaptureShortcut {
317 });
318 const handle = string(reply.body[0]);
319 if (!handle.startsWith(this.namespace)) throw new Error("Invalid shortcut request handle.");
287 - this.pending.path = handle;
320 + pending.path = handle;
321 if (this.responses.has(handle)) resolve(this.responses.get(handle));
322 const [status, results] = decodeResponse(
323 await this.wait(response, member === "BindShortcuts" ? 120_000 : 5_000),
@@ -306,9 +339,11 @@ export class PortalCaptureShortcut {
339 }
340 return results;
341 } finally {
309 - if (!completed && !this.closed && this.pending) this.closeObject(this.pending.path, REQUEST);
310 - this.pending = undefined;
311 - this.responses.clear();
342 + if (this.pending === pending) {
343 + if (!completed && !this.closed) this.closeObject(pending.path, REQUEST);
344 + this.pending = undefined;
345 + this.responses.clear();
346 + }
347 }
348 }
349
@@ -338,8 +373,8 @@ export class PortalCaptureShortcut {
373 });
374 }
375
341 - private async initialize(appId: string, shortcut: SnapShotKeyChord) {
342 - const trigger = portalShortcutTrigger(shortcut);
376 + private async initialize(first: boolean) {
377 + const trigger = this.trigger;
378 if (!process.env.FLATPAK_ID && !process.env.SNAP) {
379 await this.call({
380 destination: PORTAL,
@@ -347,7 +382,7 @@ export class PortalCaptureShortcut {
382 interface: "org.freedesktop.host.portal.Registry",
383 member: "Register",
384 signature: "sa{sv}",
350 - body: [appId, {}],
385 + body: [this.appId, {}],
386 }).catch((error: unknown) => {
387 if (
388 !(
@@ -381,7 +416,11 @@ export class PortalCaptureShortcut {
416 this.version = decodeVersion(version.body[0]).value;
417 for (const rule of [
418 `type='signal',sender='${this.owner}',path_namespace='${PATH}'`,
384 - `type='signal',sender='org.freedesktop.DBus',interface='${DBUS}',member='NameOwnerChanged',arg0='${PORTAL}'`,
419 + ...(first
420 + ? [
421 + `type='signal',sender='org.freedesktop.DBus',interface='${DBUS}',member='NameOwnerChanged',arg0='${PORTAL}'`,
422 + ]
423 + : []),
424 ])
425 await this.call({
426 destination: DBUS,