|
13 | 13 | #include "srvinit.h" |
14 | 14 |
|
15 | 15 | #include "../interactivity/inc/ServiceLocator.hpp" |
| 16 | +#include "../interactivity/win32/CustomWindowMessages.h" |
16 | 17 | #include "../types/inc/convert.hpp" |
17 | 18 |
|
18 | 19 | using Microsoft::Console::Interactivity::ServiceLocator; |
@@ -179,6 +180,32 @@ void CONSOLE_INFORMATION::SetBracketedPasteMode(const bool enabled) noexcept |
179 | 180 | _bracketedPasteMode = enabled; |
180 | 181 | } |
181 | 182 |
|
| 183 | +void CONSOLE_INFORMATION::CopyTextToClipboard(const std::wstring_view text) |
| 184 | +{ |
| 185 | + const auto window = ServiceLocator::LocateConsoleWindow(); |
| 186 | + if (window) |
| 187 | + { |
| 188 | + // The clipboard can only be updated from the main GUI thread, so we |
| 189 | + // need to post a message to trigger the actual copy operation. But if |
| 190 | + // the pending clipboard content is already set, a message would have |
| 191 | + // already been posted, so there's no need to post another one. |
| 192 | + const auto clipboardMessageSent = _pendingClipboardText.has_value(); |
| 193 | + _pendingClipboardText = text; |
| 194 | + if (!clipboardMessageSent) |
| 195 | + { |
| 196 | + PostMessageW(window->GetWindowHandle(), CM_UPDATE_CLIPBOARD, 0, 0); |
| 197 | + } |
| 198 | + } |
| 199 | +} |
| 200 | + |
| 201 | +std::optional<std::wstring> CONSOLE_INFORMATION::UsePendingClipboardText() |
| 202 | +{ |
| 203 | + // Once the pending text has been used, we clear the variable to let the |
| 204 | + // CopyTextToClipboard method know that the last CM_UPDATE_CLIPBOARD message |
| 205 | + // has been processed, and future updates will require another message. |
| 206 | + return std::exchange(_pendingClipboardText, {}); |
| 207 | +} |
| 208 | + |
182 | 209 | // Method Description: |
183 | 210 | // - Return the active screen buffer of the console. |
184 | 211 | // Arguments: |
|
0 commit comments