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.
This commit is contained in:
Dustin L. Howett 2025-05-13 16:18:05 -07:00 committed by GitHub
parent 7359df0382
commit c64a9d2a32
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 10 additions and 4 deletions

View File

@ -290,4 +290,5 @@ xtree
xutility
YIcon
YMax
zstring
zwstring

View File

@ -303,6 +303,7 @@ HRESULT HwndTerminal::Initialize()
_terminal->Create({ 80, 25 }, 9001, *_renderer);
_terminal->SetWriteInputCallback([=](std::wstring_view input) noexcept { _WriteTextToConnection(input); });
_terminal->SetCopyToClipboardCallback([=](wil::zwstring_view text) noexcept { _CopyTextToSystemClipboard(text, {}, {}); });
_renderer->EnablePainting();
_multiClickTime = std::chrono::milliseconds{ GetDoubleClickTime() };
@ -321,6 +322,10 @@ try
_renderer.reset();
_renderEngine.reset();
// These two callbacks have a dangling reference to `this`; let's just clear them
_terminal->SetWriteInputCallback(nullptr);
_terminal->SetCopyToClipboardCallback(nullptr);
if (auto localHwnd{ _hwnd.release() })
{
// 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)
// - text - selected text in plain-text format
// - htmlData - selected text in HTML format
// - rtfData - selected text in RTF format
HRESULT HwndTerminal::_CopyTextToSystemClipboard(const std::wstring& text, const std::string& htmlData, const std::string& rtfData) const
HRESULT HwndTerminal::_CopyTextToSystemClipboard(wil::zwstring_view text, wil::zstring_view htmlData, wil::zstring_view rtfData) const
try
{
RETURN_HR_IF_NULL(E_NOT_VALID_STATE, _terminal);
@ -1099,7 +1104,7 @@ CATCH_RETURN()
// Arguments:
// - stringToCopy - The string to copy
// - lpszFormat - the name of the format
HRESULT HwndTerminal::_CopyToSystemClipboard(const std::string& stringToCopy, LPCWSTR lpszFormat) const
HRESULT HwndTerminal::_CopyToSystemClipboard(wil::zstring_view stringToCopy, LPCWSTR lpszFormat) const
{
const auto cbData = stringToCopy.size() + 1; // +1 for '\0'
if (cbData)

View File

@ -145,8 +145,8 @@ private:
void _UpdateFont(int newDpi);
void _WriteTextToConnection(const std::wstring_view text) noexcept;
HRESULT _CopyTextToSystemClipboard(const std::wstring& text, const std::string& htmlData, const std::string& rtfData) const;
HRESULT _CopyToSystemClipboard(const std::string& stringToCopy, LPCWSTR lpszFormat) const;
HRESULT _CopyTextToSystemClipboard(wil::zwstring_view text, wil::zstring_view htmlData, wil::zstring_view rtfData) const;
HRESULT _CopyToSystemClipboard(wil::zstring_view stringToCopy, LPCWSTR lpszFormat) const;
void _PasteTextFromClipboard() noexcept;
void _FocusTSF() noexcept;