Skip to content

Commit fdd3227

Browse files
committed
wpf: allow OSC 52 to write the clipboard (#18905)
We never hooked up this callback! This allows a CLI application to emit text directly to the clipboard. (cherry picked from commit c64a9d2) Service-Card-Id: PVTI_lADOAF3p4s4AmhmQzgaWn6g Service-Version: 1.22
1 parent 8d5ebd5 commit fdd3227

File tree

3 files changed

+10
-4
lines changed

3 files changed

+10
-4
lines changed

.github/actions/spelling/allow/apis.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,4 +285,5 @@ xtree
285285
xutility
286286
YIcon
287287
YMax
288+
zstring
288289
zwstring

src/cascadia/TerminalControl/HwndTerminal.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,7 @@ HRESULT HwndTerminal::Initialize()
303303

304304
_terminal->Create({ 80, 25 }, 9001, *_renderer);
305305
_terminal->SetWriteInputCallback([=](std::wstring_view input) noexcept { _WriteTextToConnection(input); });
306+
_terminal->SetCopyToClipboardCallback([=](wil::zwstring_view text) noexcept { _CopyTextToSystemClipboard(text, {}, {}); });
306307
_renderer->EnablePainting();
307308

308309
_multiClickTime = std::chrono::milliseconds{ GetDoubleClickTime() };
@@ -321,6 +322,10 @@ try
321322
_renderer.reset();
322323
_renderEngine.reset();
323324

325+
// These two callbacks have a dangling reference to `this`; let's just clear them
326+
_terminal->SetWriteInputCallback(nullptr);
327+
_terminal->SetCopyToClipboardCallback(nullptr);
328+
324329
if (auto localHwnd{ _hwnd.release() })
325330
{
326331
// If we're being called through WM_DESTROY, we won't get here (hwnd is already released)
@@ -1043,7 +1048,7 @@ void __stdcall TerminalKillFocus(void* terminal)
10431048
// - text - selected text in plain-text format
10441049
// - htmlData - selected text in HTML format
10451050
// - rtfData - selected text in RTF format
1046-
HRESULT HwndTerminal::_CopyTextToSystemClipboard(const std::wstring& text, const std::string& htmlData, const std::string& rtfData) const
1051+
HRESULT HwndTerminal::_CopyTextToSystemClipboard(wil::zwstring_view text, wil::zstring_view htmlData, wil::zstring_view rtfData) const
10471052
try
10481053
{
10491054
RETURN_HR_IF_NULL(E_NOT_VALID_STATE, _terminal);
@@ -1099,7 +1104,7 @@ CATCH_RETURN()
10991104
// Arguments:
11001105
// - stringToCopy - The string to copy
11011106
// - lpszFormat - the name of the format
1102-
HRESULT HwndTerminal::_CopyToSystemClipboard(const std::string& stringToCopy, LPCWSTR lpszFormat) const
1107+
HRESULT HwndTerminal::_CopyToSystemClipboard(wil::zstring_view stringToCopy, LPCWSTR lpszFormat) const
11031108
{
11041109
const auto cbData = stringToCopy.size() + 1; // +1 for '\0'
11051110
if (cbData)

src/cascadia/TerminalControl/HwndTerminal.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,8 @@ struct HwndTerminal : ::Microsoft::Console::Types::IControlAccessibilityInfo
145145

146146
void _UpdateFont(int newDpi);
147147
void _WriteTextToConnection(const std::wstring_view text) noexcept;
148-
HRESULT _CopyTextToSystemClipboard(const std::wstring& text, const std::string& htmlData, const std::string& rtfData) const;
149-
HRESULT _CopyToSystemClipboard(const std::string& stringToCopy, LPCWSTR lpszFormat) const;
148+
HRESULT _CopyTextToSystemClipboard(wil::zwstring_view text, wil::zstring_view htmlData, wil::zstring_view rtfData) const;
149+
HRESULT _CopyToSystemClipboard(wil::zstring_view stringToCopy, LPCWSTR lpszFormat) const;
150150
void _PasteTextFromClipboard() noexcept;
151151

152152
void _FocusTSF() noexcept;

0 commit comments

Comments
 (0)