Skip to content

Commit 94696b0

Browse files
authored
fix(desktop,web): improve in-app browser shortcuts and URL behavior (pingdotgg#4703)
1 parent fff6a5b commit 94696b0

15 files changed

Lines changed: 347 additions & 133 deletions

‎apps/desktop/src/ipc/methods/preview.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import {
1616
DesktopPreviewSetColorSchemeInputSchema,
1717
DesktopPreviewTabInputSchema,
1818
DesktopPreviewWebviewConfigSchema,
19-
PreviewAnnotationPayloadSchema,
19+
PreviewAnnotationSubmissionResultSchema,
2020
PreviewAutomationSnapshot,
2121
PreviewAutomationStatus,
2222
} from "@t3tools/contracts";
@@ -227,7 +227,7 @@ export const setAnnotationTheme = DesktopIpc.makeIpcMethod({
227227
export const pickElement = DesktopIpc.makeIpcMethod({
228228
channel: IpcChannels.PREVIEW_PICK_ELEMENT_CHANNEL,
229229
payload: DesktopPreviewTabInputSchema,
230-
result: Schema.NullOr(PreviewAnnotationPayloadSchema),
230+
result: Schema.NullOr(PreviewAnnotationSubmissionResultSchema),
231231
handler: Effect.fn("desktop.ipc.preview.pickElement")(function* ({ tabId }) {
232232
const manager = yield* PreviewManager.PreviewManager;
233233
return yield* manager.pickElement(tabId);
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { describe, expect, it } from "vite-plus/test";
2+
3+
import { resolveAnnotationSubmission } from "./AnnotationKeyboard.ts";
4+
5+
const keyboardEvent = (
6+
overrides: Partial<Parameters<typeof resolveAnnotationSubmission>[0]> = {},
7+
) => ({
8+
key: "Enter",
9+
metaKey: false,
10+
ctrlKey: false,
11+
shiftKey: false,
12+
isComposing: false,
13+
...overrides,
14+
});
15+
16+
describe("resolveAnnotationSubmission", () => {
17+
it("attaches on Enter and sends on Cmd/Ctrl+Enter", () => {
18+
expect(resolveAnnotationSubmission(keyboardEvent())).toBe("attach");
19+
expect(resolveAnnotationSubmission(keyboardEvent({ metaKey: true }))).toBe("send");
20+
expect(resolveAnnotationSubmission(keyboardEvent({ ctrlKey: true }))).toBe("send");
21+
});
22+
23+
it("leaves Shift+Enter and composition events available for editing", () => {
24+
expect(resolveAnnotationSubmission(keyboardEvent({ shiftKey: true }))).toBeNull();
25+
expect(resolveAnnotationSubmission(keyboardEvent({ isComposing: true }))).toBeNull();
26+
expect(resolveAnnotationSubmission(keyboardEvent({ key: " " }))).toBeNull();
27+
});
28+
});
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import type { PreviewAnnotationSubmission } from "@t3tools/contracts";
2+
3+
interface AnnotationKeyboardEvent {
4+
readonly key: string;
5+
readonly metaKey: boolean;
6+
readonly ctrlKey: boolean;
7+
readonly shiftKey: boolean;
8+
readonly isComposing: boolean;
9+
}
10+
11+
export function resolveAnnotationSubmission(
12+
event: AnnotationKeyboardEvent,
13+
): PreviewAnnotationSubmission | null {
14+
if (event.key !== "Enter" || event.shiftKey || event.isComposing) return null;
15+
return event.metaKey || event.ctrlKey ? "send" : "attach";
16+
}

‎apps/desktop/src/preview/Manager.test.ts‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,28 @@ describe("fitPictureInPictureContentSize", () => {
3636
});
3737
});
3838

39+
describe("isPreviewRefreshShortcut", () => {
40+
const input = (overrides: Partial<Electron.Input> = {}) =>
41+
({
42+
type: "keyDown",
43+
key: "r",
44+
meta: true,
45+
control: false,
46+
shift: false,
47+
alt: false,
48+
...overrides,
49+
}) as Electron.Input;
50+
51+
it("recognizes the platform refresh chord without matching modified variants", () => {
52+
expect(PreviewManager.isPreviewRefreshShortcut(input())).toBe(true);
53+
expect(PreviewManager.isPreviewRefreshShortcut(input({ meta: false, control: true }))).toBe(
54+
true,
55+
);
56+
expect(PreviewManager.isPreviewRefreshShortcut(input({ shift: true }))).toBe(false);
57+
expect(PreviewManager.isPreviewRefreshShortcut(input({ type: "keyUp" }))).toBe(false);
58+
});
59+
});
60+
3961
const {
4062
browserWindowConstructor,
4163
createFromPath,

‎apps/desktop/src/preview/Manager.ts‎

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import type {
1111
DesktopPreviewPointerEvent,
1212
PreviewAnnotationPayload,
1313
PreviewAnnotationRect,
14+
PreviewAnnotationSubmissionResult,
1415
DesktopPreviewRecordingArtifact,
1516
DesktopPreviewRecordingFrame,
1617
DesktopPreviewScreenshotArtifact,
@@ -406,6 +407,13 @@ const APP_FORWARDED_SHORTCUTS: ReadonlyArray<{
406407
{ key: "w", meta: true, shift: false, control: false },
407408
]);
408409

410+
export const isPreviewRefreshShortcut = (input: Electron.Input): boolean =>
411+
input.type === "keyDown" &&
412+
input.key.toLowerCase() === "r" &&
413+
(input.meta || input.control) &&
414+
!input.shift &&
415+
!input.alt;
416+
409417
const isPreviewInputSignal = (value: unknown): value is PreviewInputSignal => {
410418
if (typeof value !== "object" || value === null || !("kind" in value)) return false;
411419
if (value.kind === "pointer") {
@@ -1365,6 +1373,15 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
13651373
});
13661374
});
13671375
const beforeInput = (event: Electron.Event, input: Electron.Input): void => {
1376+
if (isPreviewRefreshShortcut(input)) {
1377+
event.preventDefault();
1378+
runFork(
1379+
attempt({ operation: "shortcut.refresh", tabId, webContentsId: wc.id }, () =>
1380+
wc.reload(),
1381+
).pipe(Effect.ignore),
1382+
);
1383+
return;
1384+
}
13681385
runFork(forwardShortcut(event, input));
13691386
};
13701387
yield* Scope.addFinalizer(
@@ -1792,7 +1809,7 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
17921809
const wc = yield* requireWebContents(tabId);
17931810
yield* cancelPickElement(tabId);
17941811
const annotationTheme = yield* Ref.get(annotationThemeRef);
1795-
return yield* Effect.callback<PreviewAnnotationPayload | null, PreviewManagerError>(
1812+
return yield* Effect.callback<PreviewAnnotationSubmissionResult | null, PreviewManagerError>(
17961813
(resume) => {
17971814
const cleanup = Effect.fn("PreviewManager.cleanupPickElement")(function* () {
17981815
yield* attempt({ operation: "pickElement.cleanup", tabId, webContentsId: wc.id }, () => {
@@ -1807,14 +1824,14 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
18071824
);
18081825
});
18091826
const settlePick = Effect.fn("PreviewManager.settlePickElement")(function* (
1810-
payload: PreviewAnnotationPayload | null,
1827+
payload: PreviewAnnotationSubmissionResult | null,
18111828
) {
18121829
const active = (yield* Ref.get(pickSessionsRef)).get(tabId);
18131830
if (!active || active.cancel !== cancel) return;
18141831
yield* cleanup();
18151832
resume(Effect.succeed(payload));
18161833
});
1817-
const settle = (payload: PreviewAnnotationPayload | null) => {
1834+
const settle = (payload: PreviewAnnotationSubmissionResult | null) => {
18181835
runFork(settlePick(payload));
18191836
};
18201837
const cancelPickSession = Effect.fn("PreviewManager.cancelPickSession")(function* () {
@@ -1844,11 +1861,13 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
18441861
return;
18451862
}
18461863
const cropRect = normalizeCaptureRect(args[1]);
1864+
const submission = args[2] === "send" ? "send" : "attach";
18471865
runFork(
18481866
captureAnnotationScreenshot(tabId, wc, cropRect).pipe(
18491867
Effect.matchEffect({
1850-
onFailure: () => Effect.sync(() => settle(payload)),
1851-
onSuccess: (screenshot) => Effect.sync(() => settle({ ...payload, screenshot })),
1868+
onFailure: () => Effect.sync(() => settle({ annotation: payload, submission })),
1869+
onSuccess: (screenshot) =>
1870+
Effect.sync(() => settle({ annotation: { ...payload, screenshot }, submission })),
18521871
}),
18531872
Effect.ensuring(
18541873
attempt(
@@ -3586,7 +3605,7 @@ export class PreviewManager extends Context.Service<
35863605
) => Effect.Effect<void, PreviewManagerError>;
35873606
readonly pickElement: (
35883607
tabId: string,
3589-
) => Effect.Effect<PreviewAnnotationPayload | null, PreviewManagerError>;
3608+
) => Effect.Effect<PreviewAnnotationSubmissionResult | null, PreviewManagerError>;
35903609
readonly cancelPickElement: (tabId: string) => Effect.Effect<void, PreviewManagerError>;
35913610
readonly captureScreenshot: (
35923611
tabId: string,

‎apps/desktop/src/preview/PickPreload.ts‎

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@ import type {
1111
PreviewAnnotationRegionTarget,
1212
PreviewAnnotationStrokeTarget,
1313
PreviewAnnotationStyleChange,
14+
PreviewAnnotationSubmission,
1415
} from "@t3tools/contracts";
1516

17+
import { resolveAnnotationSubmission } from "./AnnotationKeyboard.ts";
1618
import { previewAnnotationStyles } from "./AnnotationStyles.generated.ts";
1719
import {
1820
ANNOTATION_CAPTURED_CHANNEL,
@@ -426,7 +428,7 @@ function startAnnotation(): void {
426428
"hidden h-8 w-6 shrink-0 cursor-grab select-none border-0 bg-transparent p-0 font-sans text-lg font-bold leading-5 text-muted-foreground";
427429
composerRow.appendChild(dragHandle);
428430

429-
const submit = createButton("Attach", "Attach annotation and screenshot");
431+
const submit = createButton("Attach", "Attach annotation and screenshot (Enter)");
430432
submit.className +=
431433
" h-8 shrink-0 border-primary bg-primary px-3 text-primary-foreground shadow-sm hover:bg-primary/90";
432434
composerRow.appendChild(submit);
@@ -1182,7 +1184,7 @@ function startAnnotation(): void {
11821184
refreshToolButtons();
11831185
};
11841186

1185-
submit.addEventListener("click", () => {
1187+
const submitAnnotation = (submission: PreviewAnnotationSubmission): void => {
11861188
if (pendingCapture || (selected.size === 0 && regions.length === 0 && strokes.length === 0))
11871189
return;
11881190
pendingCapture = true;
@@ -1223,13 +1225,18 @@ function startAnnotation(): void {
12231225
...regions.map((region) => region.rect),
12241226
...strokes.map((stroke) => stroke.bounds),
12251227
]);
1226-
ipcRenderer.send(ELEMENT_PICKED_CHANNEL, annotation, screenshotRect);
1228+
ipcRenderer.send(ELEMENT_PICKED_CHANNEL, annotation, screenshotRect, submission);
12271229
});
1228-
});
1229-
comment.addEventListener("keydown", (event) => {
1230-
if (event.key !== "Enter" || !(event.metaKey || event.ctrlKey)) return;
1230+
};
1231+
submit.addEventListener("click", () => submitAnnotation("attach"));
1232+
root.addEventListener("keydown", (event) => {
1233+
const submission = event.target === comment ? resolveAnnotationSubmission(event) : null;
1234+
// Keep this in the bubble phase so editor inputs receive the event before
1235+
// it is isolated from listeners installed by the inspected page.
1236+
event.stopImmediatePropagation();
1237+
if (!submission) return;
12311238
event.preventDefault();
1232-
submit.click();
1239+
submitAnnotation(submission);
12331240
});
12341241

12351242
window.addEventListener("pointermove", onPointerMove, { capture: true, passive: false });

‎apps/web/src/components/ChatView.tsx‎

Lines changed: 55 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
type ProjectScript,
99
type ProjectId,
1010
type ProviderApprovalDecision,
11+
type PreviewAnnotationPayload,
1112
ProviderInstanceId,
1213
type ServerProvider,
1314
type ResolvedKeybindingsConfig,
@@ -4561,35 +4562,80 @@ function ChatViewContent(props: ChatViewProps) {
45614562
],
45624563
);
45634564

4564-
const onSend = async (e?: { preventDefault: () => void }) => {
4565+
const onSend = async (
4566+
e?: { preventDefault: () => void },
4567+
directAnnotation?: {
4568+
annotation: PreviewAnnotationPayload;
4569+
image: ComposerImageAttachment | null;
4570+
},
4571+
) => {
45654572
e?.preventDefault();
4573+
const notifyDirectAnnotationAttached = () => {
4574+
if (!directAnnotation) return;
4575+
toastManager.add(
4576+
stackedThreadToast({
4577+
type: "info",
4578+
title: "Annotation attached to draft",
4579+
description: "Sending is unavailable right now. Finish the current action, then send.",
4580+
}),
4581+
);
4582+
};
45664583
if (
45674584
!activeThread ||
45684585
isSendBusy ||
45694586
isConnecting ||
45704587
threadDetailLoading ||
45714588
activeEnvironmentUnavailable ||
45724589
sendInFlightRef.current
4573-
)
4590+
) {
4591+
notifyDirectAnnotationAttached();
45744592
return;
4593+
}
45754594
if (activePendingProgress) {
4595+
if (directAnnotation) {
4596+
notifyDirectAnnotationAttached();
4597+
return;
4598+
}
45764599
onAdvanceActivePendingUserInput();
45774600
return;
45784601
}
45794602
const sendCtx = composerRef.current?.getSendContext();
4580-
if (!sendCtx?.providerAvailable) return;
4603+
if (!sendCtx?.providerAvailable) {
4604+
notifyDirectAnnotationAttached();
4605+
return;
4606+
}
45814607
const {
4582-
images: composerImages,
4608+
images: sendContextImages,
45834609
terminalContexts: composerTerminalContexts,
45844610
elementContexts: composerElementContexts,
4585-
previewAnnotations: composerPreviewAnnotations,
4611+
previewAnnotations: sendContextPreviewAnnotations,
45864612
reviewComments: composerReviewComments,
45874613
selectedProvider: ctxSelectedProvider,
45884614
selectedModel: ctxSelectedModel,
45894615
selectedProviderModels: ctxSelectedProviderModels,
45904616
selectedPromptEffort: ctxSelectedPromptEffort,
45914617
selectedModelSelection: ctxSelectedModelSelection,
45924618
} = sendCtx;
4619+
const composerImages =
4620+
directAnnotation?.image &&
4621+
!sendContextImages.some((image) => image.id === directAnnotation.image?.id)
4622+
? [...sendContextImages, directAnnotation.image]
4623+
: sendContextImages;
4624+
const composerPreviewAnnotations =
4625+
directAnnotation &&
4626+
!sendContextPreviewAnnotations.some(
4627+
(annotation) => annotation.id === directAnnotation.annotation.id,
4628+
)
4629+
? [
4630+
...sendContextPreviewAnnotations,
4631+
{
4632+
...directAnnotation.annotation,
4633+
screenshot: directAnnotation.annotation.screenshot
4634+
? { ...directAnnotation.annotation.screenshot, dataUrl: "" }
4635+
: null,
4636+
},
4637+
]
4638+
: sendContextPreviewAnnotations;
45934639
const promptForSend = promptRef.current;
45944640
const {
45954641
trimmedPrompt: trimmed,
@@ -4605,7 +4651,7 @@ function ChatViewContent(props: ChatViewProps) {
46054651
composerPreviewAnnotations.length +
46064652
composerReviewComments.length,
46074653
});
4608-
if (showPlanFollowUpPrompt && activeProposedPlan) {
4654+
if (!directAnnotation && showPlanFollowUpPrompt && activeProposedPlan) {
46094655
const followUp = resolvePlanFollowUpSubmission({
46104656
draftText: trimmed,
46114657
planMarkdown: activeProposedPlan.planMarkdown,
@@ -5674,6 +5720,9 @@ function ChatViewContent(props: ChatViewProps) {
56745720
tabId={activeRightPanelSurface.resourceId}
56755721
configuredUrls={configuredPreviewUrls}
56765722
visible
5723+
onSendAnnotation={(annotation, image) => {
5724+
void onSend(undefined, { annotation, image });
5725+
}}
56775726
/>
56785727
</Suspense>
56795728
) : activeRightPanelSurface?.kind === "terminal" ? (
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { renderToStaticMarkup } from "react-dom/server";
2+
import { describe, expect, it, vi } from "vite-plus/test";
3+
4+
import { PreviewChromeRow } from "./PreviewChromeRow";
5+
6+
describe("PreviewChromeRow", () => {
7+
it("shows the complete URL while the address bar is not focused", () => {
8+
const markup = renderToStaticMarkup(
9+
<PreviewChromeRow
10+
url="https://example.com/dashboard?mode=edit&tab=1#notes"
11+
loading={false}
12+
loadProgress={0}
13+
canGoBack={false}
14+
canGoForward={false}
15+
refreshDisabled={false}
16+
onBack={vi.fn()}
17+
onForward={vi.fn()}
18+
onRefresh={vi.fn()}
19+
onSubmit={vi.fn()}
20+
/>,
21+
);
22+
23+
expect(markup).toContain('value="https://example.com/dashboard?mode=edit&amp;tab=1#notes"');
24+
});
25+
});

0 commit comments

Comments
 (0)