diff --git a/src/cascadia/TerminalCore/Terminal.cpp b/src/cascadia/TerminalCore/Terminal.cpp index 55ab3cf5834..d03bcbb3abb 100644 --- a/src/cascadia/TerminalCore/Terminal.cpp +++ b/src/cascadia/TerminalCore/Terminal.cpp @@ -431,7 +431,21 @@ void Terminal::Write(std::wstring_view stringView) { auto lock = LockForWriting(); + auto& cursor = _buffer->GetCursor(); + const til::point cursorPosBefore{ cursor.GetPosition() }; + _stateMachine->ProcessString(stringView); + + const til::point cursorPosAfter{ cursor.GetPosition() }; + + // Firing the CursorPositionChanged event is very expensive so we try not to + // do that when the cursor does not need to be redrawn. We don't do this + // inside _AdjustCursorPosition, only once we're done writing the whole run + // of output. + if (cursorPosBefore != cursorPosAfter) + { + _NotifyTerminalCursorPositionChanged(); + } } void Terminal::WritePastedText(std::wstring_view stringView) @@ -989,12 +1003,6 @@ void Terminal::_WriteBuffer(const std::wstring_view& stringView) } cursor.EndDeferDrawing(); - - // Firing the CursorPositionChanged event is very expensive so we try not to - // do that when the cursor does not need to be redrawn. We don't do this - // inside _AdjustCursorPosition, only once we're done writing the whole run - // of output. - _NotifyTerminalCursorPositionChanged(); } void Terminal::_AdjustCursorPosition(const COORD proposedPosition) diff --git a/src/cascadia/TerminalCore/TerminalApi.cpp b/src/cascadia/TerminalCore/TerminalApi.cpp index 4a107d3e9d0..e2e5eb35253 100644 --- a/src/cascadia/TerminalCore/TerminalApi.cpp +++ b/src/cascadia/TerminalCore/TerminalApi.cpp @@ -92,10 +92,6 @@ try } _AdjustCursorPosition(cursorPos); - // Send an updated cursor position event. See GH#12210 for why this is done - // here instead of in _AdjustCursorPosition - _NotifyTerminalCursorPositionChanged(); - return true; } CATCH_RETURN_FALSE() diff --git a/src/cascadia/UnitTests_TerminalCore/TerminalBufferTests.cpp b/src/cascadia/UnitTests_TerminalCore/TerminalBufferTests.cpp index 906c83945b1..a5cbeea6c6f 100644 --- a/src/cascadia/UnitTests_TerminalCore/TerminalBufferTests.cpp +++ b/src/cascadia/UnitTests_TerminalCore/TerminalBufferTests.cpp @@ -610,23 +610,24 @@ void TerminalBufferTests::TestCursorNotifications() callbackWasCalled = true; expectedCallbacks--; VERIFY_IS_GREATER_THAN_OR_EQUAL(expectedCallbacks, 0); - // VERIFY_IS_TRUE(expectedCallbacks >= 0); }; term->_pfnCursorPositionChanged = cb; + // The exact number of callbacks here is fungible, if need be. + expectedCallbacks = 1; callbackWasCalled = false; term->Write(L"Foo"); VERIFY_ARE_EQUAL(0, expectedCallbacks); VERIFY_IS_TRUE(callbackWasCalled); - expectedCallbacks = 3; // one for foo, one for the newline, one for bar + expectedCallbacks = 1; callbackWasCalled = false; term->Write(L"Foo\r\nBar"); VERIFY_ARE_EQUAL(0, expectedCallbacks); VERIFY_IS_TRUE(callbackWasCalled); - expectedCallbacks = 6; + expectedCallbacks = 2; // One for each Write callbackWasCalled = false; term->Write(L"Foo\r\nBar"); term->Write(L"Foo\r\nBar");