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(capture): name shortcut steps with the held keys on both apps, as Ctrl+K

Luis Ortiz8afc2021f424
diff --git a/apps/desktop/scripts/check-pipeline.ts b/apps/desktop/scripts/check-pipeline.ts
index 1d74664..e7e34bf 100644
--- a/apps/desktop/scripts/check-pipeline.ts
+++ b/apps/desktop/scripts/check-pipeline.ts
@@ -1,4 +1,5 @@
import { randomUUID } from 'node:crypto';
+import { keyCombo } from '@mimik/core/capture/key-combo';
import { readFileSync } from 'node:fs';
import { join } from 'node:path';
import { CaptureState } from '@mimik/core/capture/machine';
@@ -9,7 +10,6 @@ import type { KeyAction } from '../src/main/capture/input-hook';
import {
type CaptureRequest,
typedTextFor,
- comboLabel,
DesktopRecorder,
isRepeatKey,
isBoundShortcut,
@@ -356,8 +356,8 @@ app.whenReady().then(async () => {
name: 'a shortcut is its own step',
ok:
shortcut?.action === (MAC ? 'keydown:βŒƒS' : 'keydown:Ctrl+S') &&
- comboLabel(pressed(31, { ctrl: true, shift: true }), 'S', false) === 'Ctrl+Shift+S' &&
- comboLabel(pressed(31, { meta: true, shift: true, alt: true }), 'S', true) === 'βŒ₯β‡§βŒ˜S' &&
+ keyCombo(pressed(31, { ctrl: true, shift: true }), 'S', false) === 'Ctrl+Shift+S' &&
+ keyCombo(pressed(31, { meta: true, shift: true, alt: true }), 'S', true) === 'βŒ₯β‡§βŒ˜S' &&
unnamed === null &&
isRepeatKey({ keycode: 28, at: 1_000 }, 28, 1_400) &&
!isRepeatKey({ keycode: 28, at: 1_000 }, 28, 1_600) &&
diff --git a/apps/desktop/src/main/__tests__/recorder-rules.test.ts b/apps/desktop/src/main/__tests__/recorder-rules.test.ts
index 500a219..40a0399 100644
--- a/apps/desktop/src/main/__tests__/recorder-rules.test.ts
+++ b/apps/desktop/src/main/__tests__/recorder-rules.test.ts
@@ -1,3 +1,4 @@
+import { keyCombo } from '@mimik/core/capture/key-combo';
import { describe, expect, it, vi } from 'vitest';
const DISPLAY = { x: 0, y: 0, width: 1920, height: 1080 };
@@ -19,7 +20,6 @@ import type { ScreenElement } from '../capture/element';
import type { KeyAction } from '../capture/input-hook';
import {
clickAction,
- comboLabel,
frameFor,
isBoundShortcut,
isRepeatClick,
@@ -143,8 +143,8 @@ describe('keys', () => {
});
it('names a shortcut the way each platform writes it, and collapses auto-repeat', () => {
- expect(comboLabel(key(31, { ctrl: true, shift: true }), 'S', false)).toBe('Ctrl+Shift+S');
- expect(comboLabel(key(31, { meta: true, shift: true, alt: true }), 'S', true)).toBe('βŒ₯β‡§βŒ˜S');
+ expect(keyCombo(key(31, { ctrl: true, shift: true }), 'S', false)).toBe('Ctrl+Shift+S');
+ expect(keyCombo(key(31, { meta: true, shift: true, alt: true }), 'S', true)).toBe('βŒ₯β‡§βŒ˜S');
expect(isRepeatKey({ keycode: 28, at: 1_000 }, 28, 1_400)).toBe(true);
expect(isRepeatKey({ keycode: 28, at: 1_000 }, 28, 1_600)).toBe(false);
expect(isRepeatKey({ keycode: 28, at: 1_000 }, 15, 1_100)).toBe(false);
diff --git a/apps/desktop/src/main/capture/recorder.ts b/apps/desktop/src/main/capture/recorder.ts
index fbfd751..5a295d3 100644
--- a/apps/desktop/src/main/capture/recorder.ts
+++ b/apps/desktop/src/main/capture/recorder.ts
@@ -1,10 +1,10 @@
import { randomUUID } from 'node:crypto';
+import { keyCombo } from '@mimik/core/capture/key-combo';
import type { CaptureImage } from '@mimik/core/capture/sink';
import type { StepAction } from '@mimik/core/capture/step-action';
import type { ElementMeta } from '@mimik/core/guides/types';
import type { Rect } from '@mimik/core/rect';
import { clipboard, screen } from 'electron';
-import { shortcutLabel } from '../../renderer/lib/shortcut-label';
import { cursorPoint } from './displays';
import {
clearDeadKey,
@@ -146,15 +146,6 @@ export function isRepeatKey(previous: { keycode: number; at: number } | null, ke
return previous !== null && previous.keycode === keycode && at - previous.at <= REPEAT_CLICK_MS;
}
-export function comboLabel(action: KeyAction, key: string, mac = process.platform === 'darwin'): string {
- const held: string[] = [];
- if (action.meta) held.push(mac ? 'Command' : 'Meta');
- if (action.ctrl) held.push('Ctrl');
- if (action.alt) held.push('Alt');
- if (action.shift) held.push('Shift');
- return mac ? shortcutLabel([...held, key].join('+'), true) : [...held, key].join('+');
-}
-
export function clickAction(button: number): StepAction {
return button === 2 ? 'auxclick' : 'click';
}
@@ -429,7 +420,12 @@ export class DesktopRecorder {
if (Object.values(shortcuts).some((accelerator) => isBoundShortcut(accelerator, action, key))) return;
const field = await this.focused();
const where = centreOf(field) ?? cursorPoint();
- await this.write(`keydown:${comboLabel(action, key)}`, where, Promise.resolve(field), this.shoot(where));
+ await this.write(
+ `keydown:${keyCombo(action, key, process.platform === 'darwin')}`,
+ where,
+ Promise.resolve(field),
+ this.shoot(where),
+ );
}
private finalizeInput(readNow = false): void {
diff --git a/apps/desktop/src/main/index.ts b/apps/desktop/src/main/index.ts
index b0ad34c..f53dddb 100644
--- a/apps/desktop/src/main/index.ts
+++ b/apps/desktop/src/main/index.ts
@@ -5,10 +5,10 @@ import type { CaptureInsert } from '@mimik/core/capture/capture-insert';
import { captureStateUpdate } from '@mimik/core/capture/capture-state-update';
import { isLive } from '@mimik/core/capture/is-live';
import { CaptureState, captureMachine } from '@mimik/core/capture/machine';
+import { shortcutLabel } from '@mimik/core/capture/shortcut-label';
import { NARRATION_SETTLE_MS } from '@mimik/core/capture/voice/narration-settle-ms';
import { app, BrowserWindow, dialog, ipcMain, Menu, nativeImage, protocol, screen, shell, Tray } from 'electron';
import { createActor } from 'xstate';
-import { shortcutLabel } from '../renderer/lib/shortcut-label';
import { registerAiFetch } from './ai-fetch';
import { APP_ICON_SCHEME, registerAppIconProtocol } from './app-icon';
import { ask } from './ask';
diff --git a/apps/desktop/src/renderer/settings/ShortcutRecorder.tsx b/apps/desktop/src/renderer/settings/ShortcutRecorder.tsx
index 5e3a042..531b7e4 100644
--- a/apps/desktop/src/renderer/settings/ShortcutRecorder.tsx
+++ b/apps/desktop/src/renderer/settings/ShortcutRecorder.tsx
@@ -1,8 +1,8 @@
+import { shortcutLabel } from '@mimik/core/capture/shortcut-label';
import { i18n } from '@mimik/core/env';
import { Button } from '@mimik/ui';
import { X } from 'lucide-react';
import { useState } from 'react';
-import { shortcutLabel } from '../lib/shortcut-label';
import { accelerator } from './lib/accelerator';
const MAC = navigator.userAgent.includes('Mac');
diff --git a/apps/extension/src/capture/events/handlers.ts b/apps/extension/src/capture/events/handlers.ts
index e841134..d496582 100644
--- a/apps/extension/src/capture/events/handlers.ts
+++ b/apps/extension/src/capture/events/handlers.ts
@@ -14,6 +14,7 @@ import {
} from '@/core/capture/dom/element-utils';
import { locateFrame, placeInTab } from '@/core/capture/dom/frame-placement';
import { isReplayedClick, replayClick, replayInit, shouldInterceptClick } from '@/core/capture/events/click-intercept';
+import { keyCombo } from '@/core/capture/key-combo';
import type { CaptureSink } from '@/core/capture/sink';
import type { StepAction } from '@/core/capture/step-action';
import { DEFAULT_TARGET_COLOR } from '@/core/screenshot/types';
@@ -23,6 +24,7 @@ import { InputSession } from './input-session';
import { isRecordableKey } from './is-recordable-key';
const REPEAT_CLICK_MS = 300;
+const MAC = /Mac/i.test(navigator.platform);
const DRAG_MIN_PX = 30;
const INTERCEPT_DELAY_MS = 100;
const PAINT_FRAMES = 3;
@@ -247,7 +249,8 @@ class CaptureController {
if (isSensitiveField(target) || isTextField(target)) return;
if (!this.recordKeys || !isRecordableKey(ke)) return;
- this.enqueue(this.capture(`keydown:${ke.key}`, target));
+ const held = { ctrl: ke.ctrlKey, alt: ke.altKey, shift: ke.shiftKey, meta: ke.metaKey };
+ this.enqueue(this.capture(`keydown:${keyCombo(held, ke.key, MAC)}`, target));
}
private onInput(e: Event) {
diff --git a/packages/core/src/capture/__tests__/key-combo.test.ts b/packages/core/src/capture/__tests__/key-combo.test.ts
new file mode 100644
index 0000000..90ca617
--- /dev/null
+++ b/packages/core/src/capture/__tests__/key-combo.test.ts
@@ -0,0 +1,20 @@
+import { describe, expect, it } from 'vitest';
+import { keyCombo } from '../key-combo';
+
+describe('keyCombo', () => {
+ it('names the held keys and the key, so Ctrl+K never reads as k', () => {
+ expect(keyCombo({ ctrl: true }, 'k', false)).toBe('Ctrl+K');
+ expect(keyCombo({ ctrl: true, shift: true }, 'S', false)).toBe('Ctrl+Shift+S');
+ expect(keyCombo({ alt: true }, 'F4', false)).toBe('Alt+F4');
+ });
+
+ it('keeps a named key as it is when nothing is held', () => {
+ expect(keyCombo({}, 'Enter', false)).toBe('Enter');
+ expect(keyCombo({}, 'ArrowDown', false)).toBe('ArrowDown');
+ });
+
+ it('writes a Mac shortcut with the Mac symbols', () => {
+ expect(keyCombo({ meta: true }, 'k', true)).toBe('⌘K');
+ expect(keyCombo({ meta: true, shift: true, alt: true }, 'S', true)).toBe('βŒ₯β‡§βŒ˜S');
+ });
+});
diff --git a/packages/core/src/capture/key-combo.ts b/packages/core/src/capture/key-combo.ts
new file mode 100644
index 0000000..1c8d5eb
--- /dev/null
+++ b/packages/core/src/capture/key-combo.ts
@@ -0,0 +1,20 @@
+import { shortcutLabel } from './shortcut-label';
+
+export interface HeldKeys {
+ ctrl?: boolean;
+ alt?: boolean;
+ shift?: boolean;
+ meta?: boolean;
+}
+
+export function keyCombo(held: HeldKeys, key: string, mac: boolean): string {
+ const shown = key.length === 1 ? key.toUpperCase() : key;
+ const parts = [
+ ...(held.meta ? [mac ? 'Command' : 'Meta'] : []),
+ ...(held.ctrl ? ['Ctrl'] : []),
+ ...(held.alt ? ['Alt'] : []),
+ ...(held.shift ? ['Shift'] : []),
+ shown,
+ ].join('+');
+ return mac ? shortcutLabel(parts, true) : parts;
+}
diff --git a/apps/desktop/src/renderer/lib/shortcut-label.ts b/packages/core/src/capture/shortcut-label.ts
similarity index 100%
rename from apps/desktop/src/renderer/lib/shortcut-label.ts
rename to packages/core/src/capture/shortcut-label.ts