Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
15 commits
Select commit Hold shift + click to select a range
5c99107
feat(ui): word-wise drag after double-click, line-wise after triple-c…
qwen-code-dev-bot Aug 8, 2026
01404d5
Merge branch 'main' into feat/word-line-drag-selection
qwen-code-dev-bot Aug 8, 2026
99af051
fix(ui): copy single-cell word/line selections on release
qwen-code-dev-bot Aug 8, 2026
24ae25c
fix(ui): apply release cell to word/line drags, keep multi-click chain
qwen-code-dev-bot Aug 8, 2026
797f345
Merge branch 'main' into feat/word-line-drag-selection
qwen-code-dev-bot Aug 8, 2026
a6c516e
refactor(ui): dedupe word/line drag state and span dispatch
qwen-code-dev-bot Aug 8, 2026
7cf9520
refactor(ui): dedupe collapsed-range policy and pin multi-click tests
qwen-code-dev-bot Aug 8, 2026
b5cbd2f
test(ui): pin bare-click highlight and multi-row line-drag copy (#8739)
qwen-code-dev-bot Aug 9, 2026
4fd9868
Merge branch 'main' into feat/word-line-drag-selection
qwen-code-dev-bot Aug 9, 2026
a03e911
Merge branch 'main' into feat/word-line-drag-selection
qwen-code-dev-bot Aug 10, 2026
de9c6a1
fix(ui): restore press-time copy for multi-click selection (#8739)
qwen-code-dev-bot Aug 10, 2026
133e7ff
Merge branch 'feat/word-line-drag-selection' of https://github.com/Qw…
qwen-code-dev-bot Aug 10, 2026
b32dbbe
Merge branch 'main' into feat/word-line-drag-selection
qwen-code-dev-bot Aug 11, 2026
d5999ed
Merge branch 'main' into feat/word-line-drag-selection
qwen-code-dev-bot Aug 11, 2026
aaaae0d
Merge branch 'main' into feat/word-line-drag-selection
qwen-code-dev-bot Aug 11, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 0 additions & 11 deletions packages/cli/src/ui/selection/selection-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,6 @@ export class SelectionState {
}
}

/** Select a resolved word/line span from a multi-click (not a drag). */
selectSpan(
span: { sx: number; sy: number; ex: number; ey: number },
mode: SelectionMode,
): void {
this.anchor = { x: span.sx, y: span.sy };
this.focus = { x: span.ex, y: span.ey };
this.dragging = false;
this.mode = mode;
}

finish(): void {
this.dragging = false;
}
Expand Down
167 changes: 167 additions & 0 deletions packages/cli/src/ui/selection/use-text-selection.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,173 @@ describe('TextSelectionController', () => {
expect(copyToClipboard).toHaveBeenLastCalledWith('hello');
});

it('extends a double-click word selection word-wise on drag', () => {
frame = makeFrame('foo bar baz');
viewportRect = { x: 0, y: 0, width: 11, height: 1 };
const nowSpy = vi.spyOn(Date, 'now').mockReturnValue(1000);
const handler = mount();
handler(makeEvent('left-press', 2)); // first click on "foo"
handler(makeEvent('left-press', 2)); // double-click -> selects "foo"
handler(makeEvent('move', 10)); // drag to "baz"
handler(makeEvent('left-release', 10));
nowSpy.mockRestore();

expect(setSelection).toHaveBeenLastCalledWith({
sx: 0,
sy: 0,
ex: 10,
ey: 0,
});
expect(copyToClipboard).toHaveBeenCalledWith('foo bar baz');
expect(copyToClipboard).toHaveBeenCalledTimes(1);
});

it('extends a triple-click line selection line-wise on drag', () => {
frame = makeTwoLineFrame('hello', 'world!');
viewportRect = { x: 0, y: 0, width: 6, height: 2 };
const nowSpy = vi.spyOn(Date, 'now').mockReturnValue(1000);
const handler = mount();
handler(makeEvent('left-press', 2, 1));
handler(makeEvent('left-press', 2, 1));
handler(makeEvent('left-press', 2, 1)); // triple-click -> line 0
handler(makeEvent('move', 3, 2)); // drag into the middle of line 1
handler(makeEvent('left-release', 3, 2));
nowSpy.mockRestore();

expect(setSelection).toHaveBeenLastCalledWith({
sx: 0,
sy: 0,
ex: 5,
ey: 1,
});
expect(copyToClipboard).toHaveBeenCalledWith('hello\nworld!');
expect(copyToClipboard).toHaveBeenCalledTimes(1);
});

it('copies a single-character word on a no-drag double-click', () => {
frame = makeFrame('a b');
viewportRect = { x: 0, y: 0, width: 3, height: 1 };
const nowSpy = vi.spyOn(Date, 'now').mockReturnValue(1000);
const handler = mount();
handler(makeEvent('left-press', 1));
handler(makeEvent('left-release', 1));
handler(makeEvent('left-press', 1)); // double-click -> selects "a"
handler(makeEvent('left-release', 1));
nowSpy.mockRestore();

expect(setSelection).toHaveBeenLastCalledWith({
sx: 0,
sy: 0,
ex: 0,
ey: 0,
});
expect(copyToClipboard).toHaveBeenCalledWith('a');
expect(copyToClipboard).toHaveBeenCalledTimes(1);
});

it('extends a word drag to the release cell when no move event is emitted', () => {
frame = makeFrame('foo bar baz');
viewportRect = { x: 0, y: 0, width: 11, height: 1 };
const nowSpy = vi.spyOn(Date, 'now').mockReturnValue(1000);
const handler = mount();
handler(makeEvent('left-press', 2));
handler(makeEvent('left-press', 2)); // double-click -> selects "foo"
handler(makeEvent('left-release', 10)); // release over "baz" with no move
nowSpy.mockRestore();

expect(setSelection).toHaveBeenLastCalledWith({
sx: 0,
sy: 0,
ex: 10,
ey: 0,
});
expect(copyToClipboard).toHaveBeenCalledWith('foo bar baz');
});

it('extends a double-click word selection backward when dragging left', () => {
frame = makeFrame('foo bar baz');
viewportRect = { x: 0, y: 0, width: 11, height: 1 };
const nowSpy = vi.spyOn(Date, 'now').mockReturnValue(1000);
const handler = mount();
handler(makeEvent('left-press', 9)); // first click on "baz"
handler(makeEvent('left-press', 9)); // double-click -> selects "baz"
handler(makeEvent('move', 1)); // drag back onto "foo"
handler(makeEvent('left-release', 1));
nowSpy.mockRestore();

expect(setSelection).toHaveBeenLastCalledWith({
sx: 0,
sy: 0,
ex: 10,
ey: 0,
});
expect(copyToClipboard).toHaveBeenCalledWith('foo bar baz');
});

it('extends a triple-click line selection backward when dragging up', () => {
frame = makeTwoLineFrame('hello', 'world!');
viewportRect = { x: 0, y: 0, width: 6, height: 2 };
const nowSpy = vi.spyOn(Date, 'now').mockReturnValue(1000);
const handler = mount();
handler(makeEvent('left-press', 2, 2));
handler(makeEvent('left-press', 2, 2));
handler(makeEvent('left-press', 2, 2)); // triple-click -> line 1
handler(makeEvent('move', 2, 1)); // drag up onto line 0
handler(makeEvent('left-release', 2, 1));
nowSpy.mockRestore();

expect(setSelection).toHaveBeenLastCalledWith({
sx: 0,
sy: 0,
ex: 5,
ey: 1,
});
expect(copyToClipboard).toHaveBeenCalledWith('hello\nworld!');
});

it('falls back to the cursor cell when a word drag lands on whitespace', () => {
frame = makeFrame('foo bar baz');
viewportRect = { x: 0, y: 0, width: 11, height: 1 };
const nowSpy = vi.spyOn(Date, 'now').mockReturnValue(1000);
const handler = mount();
handler(makeEvent('left-press', 2));
handler(makeEvent('left-press', 2)); // double-click -> selects "foo"
handler(makeEvent('move', 4)); // drag onto the gap after "foo"
handler(makeEvent('left-release', 4));
nowSpy.mockRestore();

expect(setSelection).toHaveBeenLastCalledWith({
sx: 0,
sy: 0,
ex: 3,
ey: 0,
});
expect(copyToClipboard).toHaveBeenCalledWith('foo ');
});

it('keeps the triple-click chain across drift during a held double-click', () => {
frame = makeFrame('foo bar baz');
viewportRect = { x: 0, y: 0, width: 11, height: 1 };
const nowSpy = vi.spyOn(Date, 'now').mockReturnValue(1000);
const handler = mount();
handler(makeEvent('left-press', 2));
handler(makeEvent('left-release', 2));
handler(makeEvent('left-press', 2)); // double-click -> selects "foo"
handler(makeEvent('move', 4)); // drift off the word while held
handler(makeEvent('left-release', 4));
handler(makeEvent('left-press', 2)); // third click -> selects the line
handler(makeEvent('left-release', 2));
nowSpy.mockRestore();

expect(setSelection).toHaveBeenLastCalledWith({
sx: 0,
sy: 0,
ex: 10,
ey: 0,
});
expect(copyToClipboard).toHaveBeenLastCalledWith('foo bar baz');
});

it('snaps a wide-character spacer to the leading cell', () => {
frame = makeWideFrame();
const handler = mount();
Expand Down
80 changes: 73 additions & 7 deletions packages/cli/src/ui/selection/use-text-selection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ export function TextSelectionController(
const baselineFrameRef = useRef<ReadonlyFrame | null>(null);
const baselineViewportRectRef = useRef<ViewportRect | null>(null);
const lastClickRef = useRef<ClickRecord | null>(null);
const spanDragRef = useRef<{
mode: 'word' | 'line';
anchorSpan: { sx: number; sy: number; ex: number; ey: number };
} | null>(null);
const bufferRef = useRef<ScreenBuffer | undefined>(undefined);
const propsRef = useRef(props);
propsRef.current = props;
Expand All @@ -113,6 +117,7 @@ export function TextSelectionController(

const clearSelection = useCallback(() => {
const selection = selectionRef.current;
spanDragRef.current = null;
if (selection.isEmpty) {
return;
}
Expand Down Expand Up @@ -183,6 +188,47 @@ export function TextSelectionController(
[getBuffer, stdout],
);

// Extend an active word/line drag so the range spans from the original
// multi-click span to the word/line boundary at the current point (issue
// #8738). Falls back to a single cell when the cursor is over whitespace.
const extendSpanDrag = useCallback(
(point: { x: number; y: number }) => {
const spanDrag = spanDragRef.current;
if (!spanDrag) return;
const selection = selectionRef.current;
const frame = getBuffer()?.frame ?? null;
const current = (spanDrag.mode === 'word'
? wordSpanAt(frame, point.x, point.y)
: lineSpanAt(frame, point.y)) ?? {
sx: point.x,
sy: point.y,
ex: point.x,
ey: point.y,
};
const a = spanDrag.anchorSpan;
const cursorAfter =
point.y > a.ey || (point.y === a.ey && point.x >= a.ex);
selection.anchor = cursorAfter
? { x: a.sx, y: a.sy }
: { x: a.ex, y: a.ey };
selection.focus = cursorAfter
? { x: current.ex, y: current.ey }
: { x: current.sx, y: current.sy };
},
[getBuffer],
);

const extendActiveDrag = useCallback(
(point: { x: number; y: number }) => {
if (spanDragRef.current) {
extendSpanDrag(point);
} else {
selectionRef.current.extend(point);
}
},
[extendSpanDrag],
);

const handleMouse = useCallback(
(event: MouseEvent) => {
const selection = selectionRef.current;
Expand Down Expand Up @@ -220,19 +266,27 @@ export function TextSelectionController(

if (count >= 2) {
const frame = getBuffer()?.frame ?? null;
const mode = count === 2 ? 'word' : 'line';
const span =
count === 2
? wordSpanAt(frame, point.x, point.y)
: lineSpanAt(frame, point.y);
if (span) {
selection.selectSpan(span, count === 2 ? 'word' : 'line');
// Enter a drag-capable word/line selection so a held double/triple
// click can extend by word/line on move (issue #8738). Copy happens
// on release, matching char drags.
selection.start({ x: span.sx, y: span.sy }, mode);
selection.extend({ x: span.ex, y: span.ey });
spanDragRef.current = { mode, anchorSpan: span };
dragScrollTopRef.current =
propsRef.current.getScrollState().scrollTop;
recordBaseline();
applyHighlight();
copySelection();
return;
Comment on lines 283 to 286

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Declined, for the same reasons recorded on rc:3740996254: copy-on-release is this PR's documented design and makes interrupted word/line drags behave exactly like interrupted char drags (cleared, no copy) — the established B1 semantic for all drags. Restoring press-time copy conflicts with this round's rc:3741007226 finding, which pins release-only copy, and would write the clipboard even for gestures that never complete. The A/B-verified difference versus the merge base is real but is the documented, intentional convergence of multi-click and drag semantics; the behavior is now pinned by toHaveBeenCalledTimes(1) assertions. If a maintainer prefers the base-tree guarantee for multi-clicks interrupted mid-hold, it is a one-line change (copySelection() in the count >= 2 press branch) — flagging for maintainer preference.

中文说明

已拒绝,理由与 rc:3740996254 上的记录相同:仅在释放时复制是本 PR 明确记载的设计,它使被打断的词/行拖动与被打断的字符拖动行为完全一致(清除选区、不复制)——这是 B1 范围内所有拖动的既定语义。恢复按下时复制与本轮 rc:3741007226 意见冲突(该意见要求钉住"仅释放时复制"),且会导致从未完成的手势也写入剪贴板。A/B 验证出的与 merge base 的差异确实存在,但那是多点点击与拖动语义有意收敛的、文档中记载的结果;该行为现已由 toHaveBeenCalledTimes(1) 断言钉住。如果维护者更希望保留 base 分支上"按住期间被打断的多点点击也保证复制"的语义,只需一行改动(在 count >= 2 按下分支加回 copySelection())——已标注供维护者定夺。

}
}

spanDragRef.current = null;
selection.start(point);
dragScrollTopRef.current = propsRef.current.getScrollState().scrollTop;
recordBaseline();
Expand All @@ -244,7 +298,11 @@ export function TextSelectionController(
if (!selection.dragging) {
return;
}
lastClickRef.current = null;
// A held multi-click keeps its click record so pointer drift cannot
// break a triple-click; char drags still break the chain.
if (!spanDragRef.current) {
lastClickRef.current = null;
}
// A scroll under the drag invalidates coordinates in B1.
if (
propsRef.current.getScrollState().scrollTop !==
Expand All @@ -257,22 +315,29 @@ export function TextSelectionController(
if (!mapped) {
return;
}
selection.extend(clampToViewport(mapped.point, mapped.rect));
extendActiveDrag(clampToViewport(mapped.point, mapped.rect));
applyHighlight();
return;
}

if (event.name === 'left-release') {
// Word/line click-selects are not drags; leave them intact.
if (!selection.dragging) {
return;
}
const mapped = mapEvent(event);
if (mapped) {
selection.extend(clampToViewport(mapped.point, mapped.rect));
extendActiveDrag(clampToViewport(mapped.point, mapped.rect));
}
// Clear the span drag only after extending, so the release cell still
// applies to a word/line drag when no move covered it.
spanDragRef.current = null;
selection.finish();
if (selection.isCollapsed || selection.isEmpty) {
// A collapsed range is a real single-cell span in word/line mode,
// but only a bare click in char mode.
if (
selection.isEmpty ||
(selection.isCollapsed && selection.mode === 'char')
) {
clearSelection();
return;
}
Expand All @@ -288,6 +353,7 @@ export function TextSelectionController(
recordBaseline,
mapEvent,
getBuffer,
extendActiveDrag,
],
);

Expand Down
Loading