Skip to content

armgit / mimik

Public Imported from GitHub
0

🎬 A browser extension and desktop app that turns your clicks into step-by-step guides with annotated screenshots πŸ“–

fix(desktop): stop a capture shortcut from unregistering itself mid-handler

Luis Ortize8419f326bd1
diff --git a/AGENTS.md b/AGENTS.md
index e795cda..40a37e8 100644
--- a/AGENTS.md
+++ b/AGENTS.md
@@ -708,6 +708,14 @@ another application already owns the key and throws on an accelerator Electron c
`bindShortcuts` catches both and hands back the list it could not take rather than failing the
launch over a key clash.
+**Rebinding must never happen inside a shortcut's own handler.** Unregistering the accelerator that
+is currently firing hangs the main process on Windows β€” the app goes to "not responding" with no
+error anywhere. Two things keep that from happening: `bindShortcuts` returns immediately when the
+wanted set matches what is already bound, which covers pause and resume since both count as
+recording, and `applyShortcuts` defers the work with `setImmediate` so it is never on the handler's
+stack whatever the set turns out to be. Pausing from the card worked throughout, because that path
+reaches the rebind through IPC rather than from inside a global shortcut.
+
Start/stop goes straight from hidden to recording rather than arming first, because a shortcut whose
job is to start recording should not need a second press. The stored region is used as it stands,
which is what makes that possible in `region` mode.
diff --git a/apps/desktop/scripts/check-overlay.ts b/apps/desktop/scripts/check-overlay.ts
index 62f73bc..21eea99 100644
--- a/apps/desktop/scripts/check-overlay.ts
+++ b/apps/desktop/scripts/check-overlay.ts
@@ -2,7 +2,7 @@ import { app, BrowserWindow, globalShortcut, screen, webContents } from 'electro
import { clampToDisplays, defaultRegion, loadRegion, type Region, saveRegion } from '../src/main/capture/region';
import type { CaptureMode } from '../src/main/capture/settings';
import { CaptureOverlay } from '../src/main/overlay';
-import { bindShortcuts, shortcutMap, unbindShortcuts } from '../src/main/shortcuts';
+import { bindShortcuts, sameShortcuts, shortcutMap, unbindShortcuts } from '../src/main/shortcuts';
interface CheckResult {
name: string;
@@ -238,6 +238,22 @@ app.whenReady().then(async () => {
'start/stop is always live, pause and capture only while recording, all released on unbind',
);
+ bindShortcuts(shortcutMap(keys, true), () => {});
+ let rebound = false;
+ bindShortcuts(shortcutMap(keys, true), () => {
+ rebound = true;
+ });
+ const stillLive = Object.values(keys).every((key) => globalShortcut.isRegistered(key));
+ unbindShortcuts();
+ check(
+ 'rebinding an unchanged set touches nothing',
+ stillLive &&
+ !rebound &&
+ sameShortcuts(shortcutMap(keys, true), shortcutMap(keys, true)) &&
+ !sameShortcuts(shortcutMap(keys, false), shortcutMap(keys, true)),
+ 'the same map is a no-op, so a shortcut never unregisters itself from inside its own handler',
+ );
+
const clash = bindShortcuts(shortcutMap({ ...keys, startStop: 'NotAKey+@@' }, false), () => {});
unbindShortcuts();
check(
diff --git a/apps/desktop/src/main/index.ts b/apps/desktop/src/main/index.ts
index bdb9aac..42c1214 100644
--- a/apps/desktop/src/main/index.ts
+++ b/apps/desktop/src/main/index.ts
@@ -115,9 +115,11 @@ function broadcastOverlay(command: OverlayCommand, id: string | null): void {
}
function applyShortcuts(): void {
- if (!overlay) return;
- const recording = overlay.state === 'recording' || overlay.state === 'paused';
- bindShortcuts(shortcutMap((captureSettings ?? loadSettings()).shortcuts, recording), onShortcut);
+ setImmediate(() => {
+ if (!overlay) return;
+ const recording = overlay.state === 'recording' || overlay.state === 'paused';
+ bindShortcuts(shortcutMap((captureSettings ?? loadSettings()).shortcuts, recording), onShortcut);
+ });
}
function onShortcut(name: ShortcutName): void {
diff --git a/apps/desktop/src/main/shortcuts.ts b/apps/desktop/src/main/shortcuts.ts
index 2c6fbef..b63664b 100644
--- a/apps/desktop/src/main/shortcuts.ts
+++ b/apps/desktop/src/main/shortcuts.ts
@@ -12,6 +12,14 @@ export function shortcutMap(shortcuts: CaptureShortcuts, recording: boolean): Re
}
let bound: string[] = [];
+let current: Record<ShortcutName, string | null> | null = null;
+
+export function sameShortcuts(
+ a: Record<ShortcutName, string | null> | null,
+ b: Record<ShortcutName, string | null>,
+): boolean {
+ return a !== null && a.startStop === b.startStop && a.pauseResume === b.pauseResume && a.capture === b.capture;
+}
export function unbindShortcuts(): void {
for (const accelerator of bound) {
@@ -20,12 +28,14 @@ export function unbindShortcuts(): void {
} catch {}
}
bound = [];
+ current = null;
}
export function bindShortcuts(
wanted: Record<ShortcutName, string | null>,
run: (name: ShortcutName) => void,
): string[] {
+ if (sameShortcuts(current, wanted)) return [];
unbindShortcuts();
const refused: string[] = [];
for (const [name, accelerator] of Object.entries(wanted) as [ShortcutName, string | null][]) {
@@ -37,5 +47,6 @@ export function bindShortcuts(
refused.push(accelerator);
}
}
+ current = { ...wanted };
return refused;
}