microsoft / vscode

Visual Studio Code
https://code.visualstudio.com
MIT License
162.69k stars 28.69k forks source link

Copy/paste from custom titlebar doesn't work in markdown previews #105603

Open clandrew opened 4 years ago

clandrew commented 4 years ago

Issue Type: Bug

  1. Open a markdown file for editing
  2. Open the preview view
  3. Select some text in the preview
  4. Choose context menu entry "Copy"
  5. Navigate to external program
  6. Paste

Expected: text from VSCode appears Actual: Only a blank space is pasted

VS Code version: Code 1.47.3 (91899dcef7b8110878ea59626991a18c8a6a1b3e, 2020-07-23T13:12:49.994Z) OS version: Windows_NT x64 10.0.20185

clandrew commented 4 years ago

Issue persists after update to July 2020 (version 1.48)

clandrew commented 4 years ago

Looked at it more, I think I understand the issue now. There's a problem with how it treats window focus. If the selected text is blue (using default coloring scheme), I can copy it using Ctrl+C . However, if the selected text is grey (using default coloring scheme), I can not copy it.

Both are used as selection colors, but "Blue" is more focused than "Grey"

The big plot twist: Clicking anywhere in the context menu changes your selection from "Blue" to "Grey"

clandrew commented 4 years ago

VSCode2

Showing some copying behavior

mjbvz commented 3 years ago

Only happens to me when using the custom title bar (which is the default on Windows)

turara commented 3 years ago

This seems correct behavior when doubleClickToSwitchToEditor setting is on.

スクリーンショット 2020-10-01 23 38 44
  1. Double clicking a word will move focus to source markdown file.
  2. Copy command copies a whole line in an editor when nothing is selected. (Is this correct behavior?)

A related bug

The cursor jumps into wrong line in the source markdown file when you click a word at the end of the long long sentence. For example, double clicking the last word of the sentence in the gif animation clandrew posted, will move the cursor to the line 4 of the markdown file.

This is because getEditorLineNumberForPageOffset function calculates line number by interior division of clicked position using previous/next code-line bounds.

https://github.com/microsoft/vscode/blob/ade814447c31d10643eb61ed81ad5f84148cc84e/extensions/markdown-language-features/preview-src/index.ts#L146

I think this bug can be solved by simply calculating the nearest line of clicked position with function like

export function getNearestEditorLineNumberOfPageOffset(offset: number): number {
    const { previous, next } = getLineElementsAtPageOffset(offset);
    if (!next) {
        return previous.line;
    }

    const previousBounds = getElementBounds(previous);
    const previousBottom = previousBounds.top + previousBounds.height + window.scrollY;
    const nextTop = getElementBounds(next).top + window.scrollY;

    if ((offset - previousBottom) <= (nextTop - offset)) {
        return previous.line;
    } else {
        return next.line;
    }
}

Is it OK to make a PR to fix this?

clandrew commented 3 years ago

@turara I have that setting turned on but still repro the problem (selecting text in Preview then clicking Edit->Copy causes only a blank line to be copied)

turara commented 3 years ago

@clandrew Hmm. On my macOS, clicking Edit menu dose not steal focus 🤔

I think if you have that setting turned off, at least the following problem is solved.

Select a word by doubleclicking then Ctrl+C == Just the word is shown as selected (in grey), the whole paragraph is copied

clandrew commented 3 years ago

@turara Ok yeah that's interesting if true, could help narrow things down. I have half a mind to factor the paragraph copying into a separate bug. In the beginning it seemed related to the Edit->Copy menu item problem but now I'm beginning to think it is not.

winstonhenke commented 3 years ago

Also seeing this issue.

MacOS: 11.1 VS Code: 1.52.1

clandrew commented 3 years ago

Still broken on 1.56.2

I renamed this bug to better reflect what was going on. The work-around suggested by turara concerns a different problem I mentioned in passing which is unrelated to the bug.

iamCristYe commented 3 years ago

still broken (1.58.2) Any plan to fix this?