Skip to content

Commit c932183

Browse files
Merge branch 'main' into fix/sidebar-unsettle-tooltip
2 parents f60ede1 + 837f6b8 commit c932183

5 files changed

Lines changed: 133 additions & 7 deletions

File tree

‎apps/web/src/components/chat/ChatHeader.tsx‎

Lines changed: 66 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,15 @@ import { ChevronDownIcon } from "lucide-react";
1515
import {
1616
memo,
1717
useCallback,
18+
useEffect,
1819
useMemo,
1920
useRef,
2021
useState,
2122
type KeyboardEvent as ReactKeyboardEvent,
2223
type MouseEvent as ReactMouseEvent,
2324
} from "react";
2425
import GitActionsControl from "../GitActionsControl";
26+
import { isTrailingDoubleClick } from "../Sidebar.logic";
2527
import { type DraftId } from "~/composerDraftStore";
2628
import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip";
2729
import { toastManager } from "../ui/toast";
@@ -88,6 +90,14 @@ export function resolveRenameCommit(input: {
8890
return { action: "commit", title: trimmed };
8991
}
9092

93+
// How long a click on the thread title waits before opening the action menu,
94+
// so a double-click-to-rename can cancel it first. Only the native desktop
95+
// menu needs this: it swallows input while open, so the wait must cover the
96+
// OS double-click interval. The browser fallback menu keeps seeing DOM
97+
// events (the second click dismisses it and dblclick still fires), so it
98+
// opens immediately.
99+
const TITLE_MENU_OPEN_DELAY_MS = 500;
100+
91101
export function shouldShowOpenInPicker(input: {
92102
readonly activeProjectName: string | undefined;
93103
readonly activeThreadEnvironmentId: EnvironmentId;
@@ -188,28 +198,77 @@ export const ChatHeader = memo(function ChatHeader({
188198
},
189199
[activeThreadEnvironmentId, activeThreadId, activeThreadTitle, updateThreadMetadata],
190200
);
191-
const { openMenu } = useThreadActionMenu({
201+
const { openMenu, closeMenu } = useThreadActionMenu({
192202
threadRef: isServerThread ? activeThreadRef : null,
193203
projectCwd: activeProjectCwd,
194204
changeRequest,
195205
onStartRename: startRename,
196206
});
197207
const titleButtonRef = useRef<HTMLButtonElement | null>(null);
198-
const openMenuFromTitle = useCallback(() => {
208+
const titleMenuTimerRef = useRef<number | null>(null);
209+
const cancelPendingTitleMenu = useCallback(() => {
210+
if (titleMenuTimerRef.current === null) return;
211+
clearTimeout(titleMenuTimerRef.current);
212+
titleMenuTimerRef.current = null;
213+
}, []);
214+
// Drop a pending menu-open when the thread changes or the header unmounts,
215+
// so it can never fire for a thread the user already left.
216+
useEffect(
217+
() => () => {
218+
cancelPendingTitleMenu();
219+
},
220+
[activeThreadId, cancelPendingTitleMenu],
221+
);
222+
const openTitleMenuNow = useCallback(() => {
223+
cancelPendingTitleMenu();
199224
const rect = titleButtonRef.current?.getBoundingClientRect();
200225
if (!rect) return;
201226
openMenu({ x: rect.left, y: rect.bottom + 4 });
202-
}, [openMenu]);
227+
}, [cancelPendingTitleMenu, openMenu]);
228+
const openMenuFromTitle = useCallback(
229+
(event: ReactMouseEvent<HTMLButtonElement>) => {
230+
// The trailing click of a double-click belongs to rename, not the menu.
231+
if (isTrailingDoubleClick(event.detail)) return;
232+
// Keyboard activation and the explicit chevron affordance can never be
233+
// the first half of a double-click, so they open without waiting.
234+
const clickedChevron =
235+
(event.target as HTMLElement).closest("[data-thread-title-chevron]") !== null;
236+
if (event.detail === 0 || clickedChevron || window.desktopBridge === undefined) {
237+
openTitleMenuNow();
238+
return;
239+
}
240+
// Stay pending long enough for dblclick to cancel the open before the
241+
// native menu appears and swallows the second click.
242+
cancelPendingTitleMenu();
243+
titleMenuTimerRef.current = window.setTimeout(() => {
244+
titleMenuTimerRef.current = null;
245+
openTitleMenuNow();
246+
}, TITLE_MENU_OPEN_DELAY_MS);
247+
},
248+
[cancelPendingTitleMenu, openTitleMenuNow],
249+
);
250+
const handleTitleDoubleClick = useCallback(
251+
(event: ReactMouseEvent) => {
252+
if (event.metaKey || event.ctrlKey || event.shiftKey || event.altKey) return;
253+
// The chevron is the explicit menu affordance; only the title text renames.
254+
if ((event.target as HTMLElement).closest("[data-thread-title-chevron]") !== null) return;
255+
cancelPendingTitleMenu();
256+
closeMenu();
257+
startRename();
258+
},
259+
[cancelPendingTitleMenu, closeMenu, startRename],
260+
);
203261
const handleHeaderContextMenu = useCallback(
204262
(event: ReactMouseEvent) => {
205263
if (!isServerThread || renamingTitle !== null) return;
206264
// The right-side controls (git, scripts, open-in) keep their own
207265
// behavior; only the breadcrumb area opens the thread menu.
208266
if ((event.target as HTMLElement).closest("[data-chat-header-actions]")) return;
267+
cancelPendingTitleMenu();
209268
event.preventDefault();
210269
openMenu({ x: event.clientX, y: event.clientY });
211270
},
212-
[isServerThread, openMenu, renamingTitle],
271+
[cancelPendingTitleMenu, isServerThread, openMenu, renamingTitle],
213272
);
214273
const handleRenameKeyDown = useCallback(
215274
(event: ReactKeyboardEvent<HTMLInputElement>) => {
@@ -285,13 +344,16 @@ export const ChatHeader = memo(function ChatHeader({
285344
aria-label={`Thread actions for ${activeThreadTitle}`}
286345
aria-haspopup="menu"
287346
onClick={openMenuFromTitle}
347+
onDoubleClick={handleTitleDoubleClick}
348+
onBlur={cancelPendingTitleMenu}
288349
className="group/thread-title inline-flex min-w-0 max-w-full cursor-pointer items-center gap-1 rounded-sm text-left focus-visible:outline-hidden focus-visible:ring-2 focus-visible:ring-ring"
289350
/>
290351
}
291352
>
292353
<h2 className="min-w-0 truncate">{activeThreadTitle}</h2>
293354
<ChevronDownIcon
294355
aria-hidden
356+
data-thread-title-chevron
295357
className="size-3.5 shrink-0 text-muted-foreground opacity-0 transition-opacity group-hover/thread-title:opacity-100 group-focus-visible/thread-title:opacity-100"
296358
/>
297359
</TooltipTrigger>

‎apps/web/src/components/chat/MessagesTimeline.tsx‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {
2222
use,
2323
useCallback,
2424
useEffect,
25+
useLayoutEffect,
2526
useMemo,
2627
useRef,
2728
useState,
@@ -68,6 +69,7 @@ import { buildExpandedImagePreview, ExpandedImagePreview } from "./ExpandedImage
6869
import { ProposedPlanCard } from "./ProposedPlanCard";
6970
import { ChangedFilesCard } from "./ChangedFilesTree";
7071
import { shouldAutoExpandChangedFiles } from "./changedFilesPresentation";
72+
import { keepTimelineEndVisibleAfterOverlayGrowth } from "./timelineScrollAnchoring";
7173
import { MessageCopyButton } from "./MessageCopyButton";
7274
import {
7375
computeStableMessagesTimelineRows,
@@ -286,6 +288,17 @@ export const MessagesTimeline = memo(function MessagesTimeline({
286288
const disclosureAnchorKeyRef = useRef<string | null>(null);
287289
const disclosureSettleFrameRef = useRef<number | null>(null);
288290
const disclosureSettleSecondFrameRef = useRef<number | null>(null);
291+
const previousContentInsetEndAdjustmentRef = useRef(contentInsetEndAdjustment);
292+
293+
useLayoutEffect(() => {
294+
keepTimelineEndVisibleAfterOverlayGrowth({
295+
timeline: listRef.current,
296+
previousOverlayHeight: previousContentInsetEndAdjustmentRef.current,
297+
overlayHeight: contentInsetEndAdjustment,
298+
followingEnd: liveFollowEnabled && anchorMessageId === null,
299+
});
300+
previousContentInsetEndAdjustmentRef.current = contentInsetEndAdjustment;
301+
}, [anchorMessageId, contentInsetEndAdjustment, listRef, liveFollowEnabled]);
289302

290303
useEffect(() => {
291304
return () => {

‎apps/web/src/components/chat/timelineScrollAnchoring.test.tsx‎

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1-
import { describe, expect, it } from "vite-plus/test";
2-
import { getAnchoredTurnMetrics, getRowBottom } from "./timelineScrollAnchoring";
1+
import { describe, expect, it, vi } from "vite-plus/test";
2+
import {
3+
getAnchoredTurnMetrics,
4+
getRowBottom,
5+
keepTimelineEndVisibleAfterOverlayGrowth,
6+
} from "./timelineScrollAnchoring";
37

48
function buildState({
59
positions,
@@ -22,6 +26,33 @@ function buildState({
2226
}
2327

2428
describe("timeline scroll anchoring", () => {
29+
it("keeps the live edge visible when the composer overlay grows", () => {
30+
const scrollToEnd = vi.fn();
31+
32+
keepTimelineEndVisibleAfterOverlayGrowth({
33+
timeline: { scrollToEnd },
34+
previousOverlayHeight: 120,
35+
overlayHeight: 180,
36+
followingEnd: true,
37+
});
38+
39+
expect(scrollToEnd).toHaveBeenCalledOnce();
40+
expect(scrollToEnd).toHaveBeenCalledWith({ animated: false });
41+
});
42+
43+
it("leaves the scroll position alone while the user reads history", () => {
44+
const scrollToEnd = vi.fn();
45+
46+
keepTimelineEndVisibleAfterOverlayGrowth({
47+
timeline: { scrollToEnd },
48+
previousOverlayHeight: 120,
49+
overlayHeight: 180,
50+
followingEnd: false,
51+
});
52+
53+
expect(scrollToEnd).not.toHaveBeenCalled();
54+
});
55+
2556
it("measures row bottoms from LegendList row position and size", () => {
2657
const state = buildState({
2758
positions: [0, 120],

‎apps/web/src/components/chat/timelineScrollAnchoring.ts‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,22 @@ export interface AnchoredTurnMetrics {
1919
readonly scrollDeltaToRevealEnd: number;
2020
}
2121

22+
export function keepTimelineEndVisibleAfterOverlayGrowth({
23+
timeline,
24+
previousOverlayHeight,
25+
overlayHeight,
26+
followingEnd,
27+
}: {
28+
readonly timeline: { scrollToEnd: (options: { animated: boolean }) => unknown } | null;
29+
readonly previousOverlayHeight: number;
30+
readonly overlayHeight: number;
31+
readonly followingEnd: boolean;
32+
}): void {
33+
if (timeline && followingEnd && overlayHeight > previousOverlayHeight) {
34+
void timeline.scrollToEnd({ animated: false });
35+
}
36+
}
37+
2238
export function getRowBottom(state: TimelineListMeasurementState, index: number): number | null {
2339
const top = state.positionAtIndex(index);
2440
const height = state.sizeAtIndex(index);

‎apps/web/src/hooks/useThreadActionMenu.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,5 +335,9 @@ export function useThreadActionMenu(input: {
335335
],
336336
);
337337

338-
return { openMenu };
338+
const closeMenu = useCallback(() => {
339+
void readLocalApi()?.contextMenu.close();
340+
}, []);
341+
342+
return { openMenu, closeMenu };
339343
}

0 commit comments

Comments
 (0)