Skip to content

Commit ca658ba

Browse files
committed
Fix the bug
1 parent 45b3e8d commit ca658ba

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

src/cascadia/TerminalControl/ControlInteractivity.cpp

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -255,10 +255,21 @@ namespace winrt::Microsoft::Terminal::Control::implementation
255255
}
256256
else if (WI_IsFlagSet(buttonState, MouseButtonState::IsRightButtonDown))
257257
{
258-
// Try to copy the text and clear the selection
259-
const auto successfulCopy = CopySelectionToClipboard(shiftEnabled, nullptr);
258+
const auto copyOnSelect{ _core->CopyOnSelect() };
259+
bool successfulCopy = false;
260+
261+
// Don't try to copy if we're in copyOnSelect mode and have already
262+
// copied this selection. GH#14464 demonstrates a scenario where the
263+
// buffer contents might have changed since the selection was made,
264+
// and copying here would cause weirdness.
265+
if (_selectionNeedsToBeCopied || !copyOnSelect)
266+
{
267+
// Try to copy the text and clear the selection
268+
successfulCopy = CopySelectionToClipboard(shiftEnabled, nullptr);
269+
}
260270
_core->ClearSelection();
261-
if (_core->CopyOnSelect() || !successfulCopy)
271+
272+
if (copyOnSelect || !successfulCopy)
262273
{
263274
// CopyOnSelect: right click always pastes!
264275
// Otherwise: no selection --> paste

src/cascadia/UnitTests_Control/ControlInteractivityTests.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1041,6 +1041,9 @@ namespace ControlUnitTests
10411041
VERIFY_IS_TRUE(core->HasSelection());
10421042

10431043
Log::Comment(L" --- Right-click to paste --- ");
1044+
// Note from GH#14464: we don't want to copy _again_ at this point. The
1045+
// copy occured when the selection was made, we shouldn't stealth-update
1046+
// the clipboard again.
10441047
expectedCopyContents = std::nullopt;
10451048
expectedPaste = true;
10461049
interactivity->PointerPressed(rightMouseDown,
@@ -1053,6 +1056,9 @@ namespace ControlUnitTests
10531056

10541057
void ControlInteractivityTests::CopyOnSelectAltBuffer()
10551058
{
1059+
// This test was inspired by GH#14464. Ultimately, it's similar to the
1060+
// CopyOnSelectSimple, just with an alt buffer, and outputting text
1061+
// after the selection was made.
10561062
auto [settings, conn] = _createSettingsAndConnection();
10571063
settings->CopyOnSelect(true);
10581064
auto [core, interactivity] = _createCoreAndInteractivity(*settings, *conn);

0 commit comments

Comments
 (0)